From f3e9ea7c86e8a159f6651d171fde5716e97a67ba Mon Sep 17 00:00:00 2001 From: Samuel Clements Date: Tue, 28 Feb 2012 16:17:39 +0000 Subject: [PATCH] Added (hopeful) MacOS support --- themes/twain/charge.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/themes/twain/charge.py b/themes/twain/charge.py index 7941c4e6..5c492f12 100755 --- a/themes/twain/charge.py +++ b/themes/twain/charge.py @@ -13,17 +13,20 @@ filled_char, empty_char = u'▪', u'▫' """ Read battery charge """ -def read_battery_percent (): - """ Reads the battery charge as an integer between 0 and 100, - using 'acpi -b' """ +def read_battery_percent (sp, regex): import subprocess, re - process = subprocess.Popen(["acpi", "-b"], stdout=subprocess.PIPE) + process = subprocess.Popen(sp, stdout=subprocess.PIPE) output = process.communicate()[0].strip() - return int(re.search("^Battery 0: .+, ([\d]+)\%",output).group(1)) + return int(re.search(regex,output).group(1)) -""" Output """ +if sys.platform.startswith('darwin'): + """ Reads the battery charge as an integer between 0 and 100, using 'pmset -g batt' """ + charge = read_battery_percent(["pmset", "-g", "batt"], "^.+([\d]+)\%") +else: + """ Reads the battery charge as an integer between 0 and 100, using 'acpi -b' """ + charge = read_battery_percent(["acpi", "-b"], "^Battery 0: .+, ([\d]+)\%") -charge = read_battery_percent() +""" Output """ if charge < hide_when_charge_is_above: filled = int(charge / output_length)