vnstat module: replace coloring with optimized thresholds
@@ -4,7 +4,6 @@ Display vnstat statistics.
Configuration parameters:
cache_timeout: refresh interval for this module (default 180)
- coloring: see coloring rules below (default {})
format: display format for this module (default '{total}')
initial_multi: set to 1 to disable first bytes
(default 1024)
@@ -13,29 +12,33 @@ Configuration parameters:
next unit from units (default 1024)
precision: (default 1)
statistics_type: d for daily, m for monthly (default 'd')
+ thresholds: thresholds to use for color changes (default [])
unit_multi: value to divide if rate is greater than multiplier_top
(default 1024)
-Coloring rules:
- If value is more than dict key, the string will change color based on the
- specified values in the coloring section.
-
-Example:
-```
- coloring = {
- 800: "#dddd00", # over 800: yellow
- 900: "#dd0000", # over 900: red
- }
-```
-
Format placeholders:
{down} download
{total} total
{up} upload
+Color thresholds:
+ xxx: print a color based on the value of `xxx` placeholder
+
Requires:
vnstat: a console-based network traffic monitor
+Examples:
+```
+# colorize thresholds
+vnstat {
+ format = '[\?color=total {total}]'
+ thresholds = [
+ (838860800, "degraded"), # 838860800 B -> 800 MiB
+ (943718400, "bad"), # 943718400 B -> 900 MiB
+ ]
+}
+```
+
@author shadowprince
@license Eclipse Public License
@@ -55,13 +58,13 @@ class Py3status:
# available configuration parameters
cache_timeout = 180
- coloring = {}
format = "{total}"
initial_multi = 1024
left_align = 0
multiplier_top = 1024
precision = 1
statistics_type = "d"
+ thresholds = []
unit_multi = 1024
def post_config_hook(self):
@@ -82,6 +85,15 @@ class Py3status:
# third - value/1024^2, etc...
self.units = ["kb", "mb", "gb", "tb"]
+ # deprecations
+ self.coloring = getattr(self, "coloring", None)
+ if self.coloring and not self.thresholds:
+ self.thresholds = [
+ (num * 1024 ** 2, col) for num, col in self.coloring.items()
+ ]
+
+ self.thresholds_init = self.py3.get_color_names_list(self.format)
+
def _divide_and_format(self, value):
# Divide a value and return formatted string
value /= self.initial_multi
@@ -98,13 +110,12 @@ class Py3status:
stat = dict(zip(["down", "up", "total"], map(int, values)))
response = {"cached_until": self.py3.time_in(self.cache_timeout)}
- keys = list(self.coloring.keys())
- keys.sort()
- for k in keys:
- if stat["total"] < k * 1024 * 1024:
- break
- else:
- response["color"] = self.coloring[k]
+ if self.coloring:
+ response["color"] = self.py3.threshold_get_color(stat["total"])
+
+ for x in self.thresholds_init:
+ if x in stat:
+ self.py3.threshold_get_color(stat[x], x)
response["full_text"] = self.py3.safe_format(
self.format,
