diff options
| -rw-r--r-- | py3status/modules/battery_level.py | 62 |
1 files changed, 44 insertions, 18 deletions
diff --git a/py3status/modules/battery_level.py b/py3status/modules/battery_level.py index 938a7f1..5ca9ea8 100644 --- a/py3status/modules/battery_level.py +++ b/py3status/modules/battery_level.py @@ -5,7 +5,7 @@ Module for displaying information about battery. Requires: - the 'acpi' command line -@author shadowprince +@author shadowprince and AdamBSteele @license Eclipse Public License """ @@ -13,11 +13,13 @@ from __future__ import division # python2 compatibility from time import time import math -import re import subprocess BLOCKS = ["_", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] CHARGING_CHARACTER = "⚡" +EMPTY_BLOCK_CHARGING = '|' +EMPTY_BLOCK_DISCHARGING = '⍀' +FULL_BLOCK = '█' class Py3status: @@ -38,39 +40,55 @@ class Py3status: format = "Battery: {}" hide_when_full = False mode = "bar" + notification = False def battery_level(self, i3s_output_list, i3s_config): response = {} - acpi = subprocess.check_output(["acpi"]).decode('utf-8') - proc = int(re.search(r"(\d+)%", acpi).group(1)) + # Example acpi raw output: "Battery 0: Discharging, 43%, 00:59:20 remaining" + acpi_raw = subprocess.check_output(["acpi"], stderr=subprocess.STDOUT) + acpi_clean = acpi_raw.translate(None, ',') - charging = bool(re.match(r".*Charging.*", acpi)) - full = bool( - re.match(r".*Unknown.*", acpi) - ) or bool( - re.match(r".*Full.*", acpi) - ) + # Example list: ['Battery', '0:', 'Discharging', '43%', '00:59:20', 'remaining'] + acpi_list = acpi_clean.split(' ') + + charging = True if acpi_list[2] == "Charging" else False + percent_charged = int(acpi_list[3].translate(None, '%')) + + self.time_remaining = ' '.join(acpi_list[4:]) + battery_full = False if self.mode == "bar": - character = BLOCKS[int(math.ceil(proc/100*(len(BLOCKS) - 1)))] + if charging: + full_text = CHARGING_CHARACTER + else: + full_text = BLOCKS[int(math.ceil(percent_charged/100*(len(BLOCKS) - 1)))] + elif self.mode == "ascii_bar": + full_part = FULL_BLOCK * int(percent_charged/10) + if charging: + empty_part = EMPTY_BLOCK_CHARGING * (10 - int(percent_charged/10)) + else: + empty_part = EMPTY_BLOCK_DISCHARGING * (10 - int(percent_charged/10)) + full_text = full_part + empty_part else: - character = self.format.format(str(proc) + "%") + full_text = self.format.format(str(percent_charged) + "%") + + response['full_text'] = full_text - if proc < 30: + if percent_charged < 30: response['color'] = ( self.color_degraded if self.color_degraded else i3s_config['color_degraded'] ) - if proc < 10: + if percent_charged < 10: response['color'] = ( self.color_bad if self.color_bad else i3s_config['color_bad'] ) - if full: + if battery_full: response['color'] = ( self.color_good if self.color_good @@ -79,10 +97,18 @@ class Py3status: response['full_text'] = "" if self.hide_when_full else BLOCKS[-1] elif charging: response['color'] = self.color_charging - response['full_text'] = CHARGING_CHARACTER - else: - response['full_text'] = character response['cached_until'] = time() + self.cache_timeout return response + + def on_click(self, i3s_output_list, i3s_config, event): + """ + Display a notification with the remaining charge time. + """ + if self.notification and self.time_remaining: + subprocess.call( + ['notify-send', '{}'.format(self.time_remaining), '-t', '4000'], + stdout=open('/dev/null', 'w'), + stderr=open('/dev/null', 'w') + ) |
