From fde7b6bb787d70a13690af840ec9f2a7dfe469f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Mon, 3 Dec 2018 14:41:02 +0300 Subject: [PATCH] Added levels settings to the battery module (#654) * Added levels settings to the battery module Specify text for custom levels of the battery charge * Round up charge the same way it is displayed percentage value used to determine text from levels setting should be round up the same way as when it is displayed on the screen --- i3pystatus/battery.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/i3pystatus/battery.py b/i3pystatus/battery.py index 74374c5..16a3740 100644 --- a/i3pystatus/battery.py +++ b/i3pystatus/battery.py @@ -1,3 +1,4 @@ +import bisect import configparser import os import re @@ -188,6 +189,15 @@ class BatteryChecker(IntervalModule): ("base_path", "Override the default base path for searching for batteries"), ("battery_prefix", "Override the default battery prefix"), ("status", "A dictionary mapping ('DPL', 'DIS', 'CHR', 'FULL') to alternative names"), + ("levels", + "A dictionary mapping of charge levels to corresponding names. Let the keys be a < b < c < d. " + "Then the following intervals correspond to each value:\n" + " 0 -> status['DPL']\n" + " |0 < x <= a| -> levels[a]\n" + " |a < x <= b| -> levels[b]\n" + " |b < x <= c| -> levels[c]\n" + " |c < x <= d| -> levels[d]\n" + " |d < x <= 100| -> status['FULL']"), ("color", "The text color"), ("full_color", "The full color"), ("charging_color", "The charging color"), @@ -207,6 +217,7 @@ class BatteryChecker(IntervalModule): "DIS": "DIS", "FULL": "FULL", } + levels = None not_present_text = "Battery {battery_ident} not present" alert = False @@ -362,7 +373,14 @@ class BatteryChecker(IntervalModule): self.notification.update(title=title, body=body) - fdict["status"] = self.status[fdict["status"]] + if self.levels and fdict['status'] == 'DIS': + self.levels.setdefault(0, self.status.get('DPL', 'DPL')) + self.levels.setdefault(100, self.status.get('FULL', 'FULL')) + keys = sorted(self.levels.keys()) + index = bisect.bisect_left(keys, int(fdict['percentage'])) + fdict["status"] = self.levels[keys[index]] + else: + fdict["status"] = self.status[fdict["status"]] self.data = fdict self.output = {