b-ryan.powerline-shell/powerline_shell/segments/battery.py
Buck Ryan 3c8172577d update battery segment to work with latest powerline shell
plus refactor a bit and try to find the battery
directory properly
2017-09-30 17:39:28 -04:00

32 lines
1.2 KiB
Python

from ..utils import BasicSegment, warn
import os
LOW_BATTERY_THRESHOLD = 20
# See discussion in https://github.com/banga/powerline-shell/pull/204 regarding
# the directory where battery info is saved
DIR_OPTIONS = ["/sys/class/power_supply/BAT0",
"/sys/class/power_supply/BAT1"]
class Segment(BasicSegment):
def add_to_powerline(self):
if os.path.exists("/sys/class/power_supply/BAT0"):
dir_ = "/sys/class/power_supply/BAT0"
elif os.path.exists("/sys/class/power_supply/BAT1"):
dir_ = "/sys/class/power_supply/BAT1"
else:
warn("battery directory could not be found")
return
with open(os.path.join(dir_, "capacity")) as f:
cap = f.read().strip()
with open(os.path.join(dir_, "status")) as f:
status = f.read().strip()
pwr = u" \u26A1 " if status == "Charging" else u" "
if int(cap) < LOW_BATTERY_THRESHOLD:
bg = self.powerline.theme.BATTERY_LOW_BG
fg = self.powerline.theme.BATTERY_LOW_FG
else:
bg = self.powerline.theme.BATTERY_NORMAL_BG
fg = self.powerline.theme.BATTERY_NORMAL_FG
self.powerline.append(" " + cap + "%" + pwr, fg, bg)