dwmb-battery2 (2748B) - raw


      1 #!/usr/bin/env python3
      2 #
      3 # Copyright (C) 2016 James Murphy
      4 # Licensed under the GPL version 2 only
      5 #
      6 # A battery indicator blocklet script for i3blocks
      7 
      8 import re
      9 from subprocess import check_output
     10 
     11 status = check_output(['acpi'], universal_newlines=True)
     12 
     13 if not status:
     14     # stands for no battery found
     15     fulltext = "\uf00d \uf240"
     16     percentleft = 100
     17 else:
     18     # if there is more than one battery in one laptop, the percentage left is
     19     # available for each battery separately, although state and remaining
     20     # time for overall block is shown in the status of the first battery
     21     batteries = status.split("\n")
     22     state_batteries=[]
     23     commasplitstatus_batteries=[]
     24     percentleft_batteries=[]
     25     time = ""
     26     for battery in batteries:
     27         if battery!='':
     28             state_batteries.append(battery.split(": ")[1].split(", ")[0])
     29             commasplitstatus = battery.split(", ")
     30             if not time:
     31                 time = commasplitstatus[-1].strip()
     32                 # check if it matches a time
     33                 time = re.match(r"(\d+):(\d+)", time)
     34                 if time:
     35                     time = ":".join(time.groups())
     36                     timeleft = " ({})".format(time)
     37                 else:
     38                     timeleft = ""
     39 
     40             p = int(commasplitstatus[1].rstrip("%\n"))
     41             if p>0:
     42                 percentleft_batteries.append(p)
     43             commasplitstatus_batteries.append(commasplitstatus)
     44     state = state_batteries[0]
     45     commasplitstatus = commasplitstatus_batteries[0]
     46     if percentleft_batteries:
     47         percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries))
     48     else:
     49         percentleft = 0
     50 
     51     # stands for charging
     52     FA_LIGHTNING = "\uf0e7"
     53 
     54     # stands for plugged in
     55     FA_PLUG = "\uf1e6"
     56 
     57     # stands for using battery
     58     FA_BATTERY_0 = "\uf244" # 0/4
     59     FA_BATTERY_1 = "\uf243" # 1/4
     60     FA_BATTERY_2 = "\uf242" # 2/4
     61     FA_BATTERY_3 = "\uf241" # 3/4
     62     FA_BATTERY_4 = "\uf240" # 4/4
     63 
     64     # stands for unknown status of battery
     65     FA_QUESTION = "\uf128"
     66 
     67 
     68     if state == "Discharging":
     69         if percentleft <= 10:
     70             fulltext = FA_BATTERY_0 + " "
     71         elif percentleft < 35:
     72             fulltext = FA_BATTERY_1 + " "
     73         elif percentleft < 65:
     74             fulltext = FA_BATTERY_2 + " "
     75         elif percentleft < 90:
     76             fulltext = FA_BATTERY_3 + " "
     77         else:
     78             fulltext = FA_BATTERY_4 + " "
     79     elif state == "Full":
     80         fulltext = FA_PLUG + " "
     81         timeleft = ""
     82     elif state == "Unknown":
     83         fulltext = FA_BATTERY_0 + " " + FA_QUESTION + " "
     84         timeleft = ""
     85     else:
     86         fulltext = FA_LIGHTNING + " "
     87 
     88     fulltext += str(percentleft) + "%" + timeleft
     89 
     90 print(fulltext)