From 5bfc2eb4ec64c4a219e7e98619a69234c2e194b7 Mon Sep 17 00:00:00 2001 From: Jules Gagnon-Marchand Date: Thu, 24 May 2018 16:52:43 -0400 Subject: [PATCH] Added Macos compatibilty --- powerline_shell/segments/battery.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/powerline_shell/segments/battery.py b/powerline_shell/segments/battery.py index 24c5f1f..e5cc157 100644 --- a/powerline_shell/segments/battery.py +++ b/powerline_shell/segments/battery.py @@ -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 "