This commit is contained in:
Raphael Eidus 2022-09-27 18:57:15 +09:00 committed by GitHub
commit 0b312f853f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
from ..utils import BasicSegment, warn
import os
from sys import platform
MACOS = platform == "darwin"
class Segment(BasicSegment):
def add_to_powerline(self):
@ -10,14 +11,32 @@ class Segment(BasicSegment):
dir_ = "/sys/class/power_supply/BAT0"
elif os.path.exists("/sys/class/power_supply/BAT1"):
dir_ = "/sys/class/power_supply/BAT1"
elif MACOS:
pass
else:
warn("battery directory could not be found")
return
if MACOS:
# This way the non-macos people don't have to pay for the imports
from subprocess import check_output
import re
source = check_output(["pmset", "-g", "batt"]).strip().decode()
# capacity
cap = int(re.findall(r"[0-9]{1,3}%", source)[0][:-1])
status_source = source[source.find(";") + 2: source.rfind(";")]
with open(os.path.join(dir_, "capacity")) as f:
cap = int(f.read().strip())
with open(os.path.join(dir_, "status")) as f:
status = f.read().strip()
if status_source == "charged":
status = "Full"
elif status_source == "charging":
status = "Charging"
else:
status = "Discharging"
else:
with open(os.path.join(dir_, "capacity")) as f:
cap = int(f.read().strip())
with open(os.path.join(dir_, "status")) as f:
status = f.read().strip()
if status == "Full":
if self.powerline.segment_conf("battery", "always_show_percentage", False):
pwr_fmt = u" {cap:d}% \U0001F50C "