From 6a0578d2c3e0b434a55acc3c21c4dc931791b846 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 23 Jul 2017 18:45:07 +0200 Subject: [PATCH 1/2] [modules/battery] Incorporate @yvesh's estimated time remaining Move changes proposed by @yvesh to show the remaining battery time into the battery module. see #146 --- bumblebee/modules/battery.py | 30 ++++++++++++++++++++++++++++++ themes/icons/awesome-fonts.json | 4 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index d6bd1d2..d3c2383 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 = [] @@ -28,6 +33,7 @@ class Module(bumblebee.engine.Module): self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ] if len(self._batteries) == 0: self._batteries = [ "/sys/class/power_supply/BAT0" ] + self._estimate = "n/a" self.update(widgets) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-power-statistics") @@ -44,6 +50,24 @@ class Module(bumblebee.engine.Module): for widget in new_widgets: widgets.append(widget) self._widgets = widgets + if bumblebee.util.asbool(self.parameter("showremaining", False)): + self._widgets.append(bumblebee.output.Widget(full_text=self.remaining)) + self._widgets[-1].set("type", "remaining") + try: + type = power.PowerManagement().get_providing_power_source_type() + estimate = power.PowerManagement().get_time_remaining_estimate() + + if type == power.POWER_TYPE_AC and estimate == -2.0: + self._estimate = "Unlimited" + elif estimate == -1.0: + self._estimate = "Unknown" + else: + self._estimate = str(round(estimate / 60, 1)) + ' h' + except Exception as e: + self._estimate = "n/a" + + def remaining(self, widget): + return str(self._estimate) def capacity(self, widget): widget.set("capacity", -1) @@ -68,6 +92,12 @@ class Module(bumblebee.engine.Module): def state(self, widget): state = [] + + if widget.get("type", "battery") == "remaining": + if self._estimate == "Unlimited": + return "unlimited" + return "estimate" + capacity = widget.get("capacity") if capacity < 0: 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": " " } From 115f03cb0f0145a4783d81f429230976d0a3e19f Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Wed, 26 Jul 2017 16:41:30 +0200 Subject: [PATCH 2/2] [module/battery] Add remaining time, if available Add remaining time directly to widget, if available, and if not on charge. see #146 --- bumblebee/modules/battery.py | 44 ++++++++++++++++------------------- bumblebee/util.py | 14 ++++++++--- tests/modules/test_battery.py | 1 + 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index d3c2383..2237b2d 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -33,7 +33,6 @@ class Module(bumblebee.engine.Module): self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ] if len(self._batteries) == 0: self._batteries = [ "/sys/class/power_supply/BAT0" ] - self._estimate = "n/a" self.update(widgets) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-power-statistics") @@ -50,24 +49,22 @@ class Module(bumblebee.engine.Module): for widget in new_widgets: widgets.append(widget) self._widgets = widgets - if bumblebee.util.asbool(self.parameter("showremaining", False)): - self._widgets.append(bumblebee.output.Widget(full_text=self.remaining)) - self._widgets[-1].set("type", "remaining") - try: - type = power.PowerManagement().get_providing_power_source_type() - estimate = power.PowerManagement().get_time_remaining_estimate() - if type == power.POWER_TYPE_AC and estimate == -2.0: - self._estimate = "Unlimited" - elif estimate == -1.0: - self._estimate = "Unknown" - else: - self._estimate = str(round(estimate / 60, 1)) + ' h' - except Exception as e: - self._estimate = "n/a" + def remaining(self): + estimate = 0.0 + try: + power_type = power.PowerManagement().get_providing_power_source_type() - def remaining(self, widget): - return str(self._estimate) + # 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) @@ -88,16 +85,15 @@ 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 = [] - - if widget.get("type", "battery") == "remaining": - if self._estimate == "Unlimited": - return "unlimited" - return "estimate" - capacity = widget.get("capacity") if capacity < 0: 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")