dotfiles/dot_config/i3/scripts/executable_battery2

87 lines
2.5 KiB
Python

#!/usr/bin/env python3
#
# Copyright (C) 2016 James Murphy
# Licensed under the GPL version 2 only
#
# A battery indicator blocklet script for i3blocks
from subprocess import check_output
import re
status = check_output(['acpi'], universal_newlines=True)
if not status:
# stands for no battery found
fulltext = "<span font='FontAwesome' color='#fb4934'>\uf00d \uf240</span>".format(color)
percentleft = 100
else:
# if there is more than one battery in one laptop, the percentage left is
# available for each battery separately, although state and remaining
# time for overall block is shown in the status of the first battery
batteries = status.split("\n")
state_batteries=[]
commasplitstatus_batteries=[]
percentleft_batteries=[]
for battery in batteries:
if battery!='':
state_batteries.append(battery.split(": ")[1].split(", ")[0])
commasplitstatus = battery.split(", ")
p = int(commasplitstatus[1].rstrip("%\n"))
if p>0:
percentleft_batteries.append(p)
commasplitstatus_batteries.append(commasplitstatus)
state = state_batteries[0]
commasplitstatus = commasplitstatus_batteries[0]
if percentleft_batteries:
percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries))
else:
percentleft = 0
def color(percent):
if percent < 35:
return colors.get("low")
if percent < 50 and percent > 35:
return colors.get("mid")
if percent > 50:
return colors.get("high")
return "#FFFFFF"
B_TEMPLATE = "<span font='FontAwesome'>{}</span>"
B_FULL = B_TEMPLATE.format("\uf240")
B_MID = B_TEMPLATE.format("\uf242")
B_DISCHARING = B_TEMPLATE.format("\uf241")
B_CHARGING = B_TEMPLATE.format("\uf1e6")
B_DOWN = B_TEMPLATE.format("\uf06a")
B_UNKNOWN = B_TEMPLATE.format("\uf128")
colors = {
'low': '#d32f2f',
'mid': '#ffa000',
'high': '#d4e157'
}
display = "<span color=\"{}\">{}{}%</span>"
battery = ""
color = color(percentleft)
if(percentleft >= 99):
state = "Full"
if state == "Discharging":
battery = B_DISCHARING + " "
elif state == "Full":
battery = B_FULL + " "
elif state == "Charging":
battery = B_CHARGING + " "
elif state == "Unknown":
battery = B_UNKNOWN + " "
else:
battery = B_UNKNOWN + " " + B_MID + " "
display = display.format(color, battery, percentleft)
print(display)
if percentleft < 15:
exit(33)