add bar drawing function, and modules for showing cpu and ram usage as a bar
This commit is contained in:
parent
00c230f4f8
commit
a5b4ad0b1c
@ -362,3 +362,21 @@ def internet():
|
|||||||
return True
|
return True
|
||||||
except OSError:
|
except OSError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def make_bar(percentage):
|
||||||
|
"""
|
||||||
|
Draws a bar made of unicode box characters.
|
||||||
|
|
||||||
|
:param percentage: A value between 0 and 100
|
||||||
|
:returns: Bar as a string
|
||||||
|
"""
|
||||||
|
|
||||||
|
bars = [' ', '▏', '▎', '▍', '▌', '▋', '▋', '▊', '▊', '█']
|
||||||
|
tens = int(percentage / 10)
|
||||||
|
ones = int(percentage) - tens * 10
|
||||||
|
result = tens * '█'
|
||||||
|
if(ones >= 1):
|
||||||
|
result = result + bars[ones]
|
||||||
|
result = result + (10 - len(result)) * ' '
|
||||||
|
return result
|
||||||
|
|
||||||
|
60
i3pystatus/cpu_usage_bar.py
Normal file
60
i3pystatus/cpu_usage_bar.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
|
||||||
|
from i3pystatus import IntervalModule
|
||||||
|
from i3pystatus.core.util import make_bar
|
||||||
|
|
||||||
|
class CpuUsageBar(IntervalModule):
|
||||||
|
"""
|
||||||
|
Shows CPU usage as a bar (made with unicode box characters).
|
||||||
|
The first output will be inacurate
|
||||||
|
Linux only
|
||||||
|
|
||||||
|
Available formatters:
|
||||||
|
|
||||||
|
* {usage_bar}
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
format = "{usage_bar}"
|
||||||
|
settings = (
|
||||||
|
("format", "format string"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def init(self):
|
||||||
|
self.prev_idle = 0
|
||||||
|
self.prev_busy = 0
|
||||||
|
self.interval = 1
|
||||||
|
|
||||||
|
def get_usage(self):
|
||||||
|
"""
|
||||||
|
parses /proc/stat and calcualtes total and busy time
|
||||||
|
(more specific USER_HZ see man 5 proc for further informations )
|
||||||
|
"""
|
||||||
|
with open('/proc/stat', 'r') as file_obj:
|
||||||
|
stats = file_obj.readline().strip().split()
|
||||||
|
|
||||||
|
cpu_timings = [int(x) for x in stats[1:]]
|
||||||
|
cpu_total = sum(cpu_timings)
|
||||||
|
del cpu_timings[3:5]
|
||||||
|
cpu_busy = sum(cpu_timings)
|
||||||
|
|
||||||
|
return cpu_total, cpu_busy
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
cpu_total, cpu_busy = self.get_usage()
|
||||||
|
|
||||||
|
diff_cpu_total = cpu_total - self.prev_idle
|
||||||
|
diff_cpu_busy = cpu_busy - self.prev_busy
|
||||||
|
|
||||||
|
self.prev_idle = cpu_total
|
||||||
|
self.prev_busy = cpu_busy
|
||||||
|
|
||||||
|
cpu_busy_percentage = diff_cpu_busy / diff_cpu_total * 100
|
||||||
|
cpu_busy_bar = make_bar(cpu_busy_percentage)
|
||||||
|
|
||||||
|
self.output = {
|
||||||
|
"full_text": self.format.format(
|
||||||
|
usage_bar=cpu_busy_bar
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
49
i3pystatus/mem_bar.py
Normal file
49
i3pystatus/mem_bar.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
from i3pystatus import IntervalModule
|
||||||
|
from psutil import virtual_memory
|
||||||
|
from i3pystatus.core.util import make_bar
|
||||||
|
|
||||||
|
|
||||||
|
class MemBar(IntervalModule):
|
||||||
|
"""
|
||||||
|
Shows memory load as a bar.
|
||||||
|
|
||||||
|
Available formatters:
|
||||||
|
* {used_mem_bar}
|
||||||
|
|
||||||
|
Requires psutil (from PyPI)
|
||||||
|
"""
|
||||||
|
|
||||||
|
format = "{used_mem_bar}"
|
||||||
|
color = "#00FF00"
|
||||||
|
warn_color = "#FFFF00"
|
||||||
|
alert_color = "#FF0000"
|
||||||
|
warn_percentage = 50
|
||||||
|
alert_percentage = 80
|
||||||
|
|
||||||
|
settings = (
|
||||||
|
("format", "format string used for output."),
|
||||||
|
("warn_percentage", "minimal percentage for warn state"),
|
||||||
|
("alert_percentage", "minimal percentage for alert state"),
|
||||||
|
("color", "standard color"),
|
||||||
|
("warn_color",
|
||||||
|
"defines the color used wann warn percentage ist exceeded"),
|
||||||
|
("alert_color",
|
||||||
|
"defines the color used when alert percentage is exceeded"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
memory_usage = virtual_memory()
|
||||||
|
|
||||||
|
if memory_usage.percent >= self.alert_percentage:
|
||||||
|
color = self.alert_color
|
||||||
|
|
||||||
|
elif memory_usage.percent >= self.warn_percentage:
|
||||||
|
color = self.warn_color
|
||||||
|
else:
|
||||||
|
color = self.color
|
||||||
|
|
||||||
|
self.output = {
|
||||||
|
"full_text": self.format.format(
|
||||||
|
used_mem_bar=make_bar(memory_usage.percent)),
|
||||||
|
"color": color
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user