mirror of
https://github.com/b-ryan/powerline-shell.git
synced 2026-09-10 07:26:28 -04:00
32 lines
1.2 KiB
Python
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)
|