added dynamic color support for cpu_usage (#376)

* added dynamic color support for cpu_usage

* some minor fixes to pass build tests

* added new PyPI requirement to documentation

* removed explicit setting self.color in CpuUsage

as a response to @facetoe removed explicitly setting self.color and instead
set the class wide variable color to #FFFFFF to not have the output been shown
in black.
This commit is contained in:
Björn Guth 2018-12-20 15:26:35 +01:00 committed by chestm007
parent 42d9955a9b
commit c9c83bfce4

View File

@ -1,7 +1,9 @@
from collections import defaultdict
from string import Formatter
import re
from i3pystatus import IntervalModule
from i3pystatus.core.color import ColorRangeModule
try:
from natsort import natsorted as sorted
@ -9,12 +11,13 @@ except ImportError:
pass
class CpuUsage(IntervalModule):
class CpuUsage(IntervalModule, ColorRangeModule):
"""
Shows CPU usage.
The first output will be inacurate.
Linux only
Requires the PyPI package 'colour'.
.. rubric:: Available formatters
@ -28,14 +31,19 @@ class CpuUsage(IntervalModule):
format_all = "{core}:{usage:02}%"
exclude_average = False
interval = 1
color = None
color = '#FFFFFF'
dynamic_color = False
upper_limit = 100
settings = (
("format", "format string."),
("format_all", ("format string used for {usage_all} per core. "
"Available formaters are {core} and {usage}. ")),
("exclude_average", ("If True usage average of all cores will "
"not be in format_all.")),
("color", "HTML color code #RRGGBB")
("color", "HTML color code #RRGGBB"),
("dynamic_color", "Set color dynamically based on CPU usage. Note: this overrides color_up"),
("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"),
("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'")
)
def init(self):
@ -43,6 +51,17 @@ class CpuUsage(IntervalModule):
self.prev_busy = defaultdict(int)
self.formatter = Formatter()
self.key = re.findall('usage_cpu\d+', self.format)
if len(self.key) == 1:
self.key = self.key[0]
else:
self.key = 'usage_cpu'
if not self.dynamic_color:
self.start_color = self.color
self.end_color = self.color
self.colors = self.get_hex_color_range(self.start_color, self.end_color, int(self.upper_limit))
def get_cpu_timings(self):
"""
reads and parses /proc/stat
@ -118,8 +137,10 @@ class CpuUsage(IntervalModule):
usage = self.get_usage()
usage['usage_all'] = self.gen_format_all(usage)
color = self.get_gradient(usage[self.key], self.colors, int(self.upper_limit))
self.data = usage
self.output = {
"full_text": self.format.format_map(usage),
"color": self.color
"color": color
}