diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index d6bd1d2..2237b2d 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -17,6 +17,11 @@ import bumblebee.output import bumblebee.engine import bumblebee.util +try: + import power +except ImportError: + pass + class Module(bumblebee.engine.Module): def __init__(self, engine, config): widgets = [] @@ -45,6 +50,22 @@ class Module(bumblebee.engine.Module): widgets.append(widget) self._widgets = widgets + def remaining(self): + estimate = 0.0 + try: + power_type = power.PowerManagement().get_providing_power_source_type() + + # do not show remaining if on AC + if power.PowerManagement().get_providing_power_source_type() == power.POWER_TYPE_AC: + return None + + estimate = power.PowerManagement().get_time_remaining_estimate() + if estimate == -1.0: + return "n/a" + except Exception: + return "n/a" + return bumblebee.util.durationfmt(estimate*60, shorten=True, suffix=True) # estimate is in minutes + def capacity(self, widget): widget.set("capacity", -1) widget.set("ac", False) @@ -64,7 +85,12 @@ class Module(bumblebee.engine.Module): widget.set("theme.minwidth", "100% ({})".format(os.path.basename(widget.name))) return "{}% ({})".format(capacity, os.path.basename(widget.name)) widget.set("theme.minwidth", "100%") - return "{}%".format(capacity) + + remaining = None + if bumblebee.util.asbool(self.parameter("showremaining", True)): + remaining = self.remaining() + + return "{}%{}".format(capacity, "" if not remaining else " ({})".format(remaining)) def state(self, widget): state = [] diff --git a/bumblebee/util.py b/bumblebee/util.py index 30719ea..11119f8 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import shlex +import datetime import logging import subprocess @@ -45,12 +46,19 @@ def bytefmt(num): num /= 1024.0 return "{:.2f}GiB".format(num*1024.0) -def durationfmt(duration): +def durationfmt(duration, shorten=False, suffix=False): + duration = int(duration) minutes, seconds = divmod(duration, 60) hours, minutes = divmod(minutes, 60) + suf = "m" res = "{:02d}:{:02d}".format(minutes, seconds) - if hours > 0: res = "{:02d}:{}".format(hours, res) + if hours > 0: + if shorten: + res = "{:02d}:{:02d}".format(hours, minutes) + else: + res = "{:02d}:{}".format(hours, res) + suf = "h" - return res + return "{}{}".format(res, suf if suffix else "") # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/test_battery.py b/tests/modules/test_battery.py index fb96093..6d0ec0a 100644 --- a/tests/modules/test_battery.py +++ b/tests/modules/test_battery.py @@ -32,6 +32,7 @@ class TestBatteryModule(unittest.TestCase): self.exists.return_value = True self.engine = mock.Mock() self.config = Config() + self.config.set("battery.showremaining", "false") self.module = Module(engine=self.engine, config={"config":self.config}) self.config.set("battery.critical", "20") diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index a50c962..9917d97 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -75,7 +75,9 @@ "discharging-25": { "prefix": "", "suffix": "" }, "discharging-50": { "prefix": "", "suffix": "" }, "discharging-80": { "prefix": "", "suffix": "" }, - "discharging-100": { "prefix": "", "suffix": "" } + "discharging-100": { "prefix": "", "suffix": "" }, + "unlimited": { "prefix": "", "suffix": "" }, + "estimate": { "prefix": "" } }, "caffeine": { "activated": {"prefix": " " }, "deactivated": { "prefix": " " }