[module/battery] Add remaining time, if available

Add remaining time directly to widget, if available, and if not on
charge.

see #146
This commit is contained in:
Tobias Witek 2017-07-26 16:41:30 +02:00
parent 6a0578d2c3
commit 115f03cb0f
3 changed files with 32 additions and 27 deletions

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