Merge branch 'pr146-battery-remaining'

This commit is contained in:
Tobias Witek 2017-07-29 14:25:45 +02:00
commit f74648dc45
4 changed files with 42 additions and 5 deletions

View file

@ -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 = []

View file

@ -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

View file

@ -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")

View file

@ -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": " " }