Created
December 25, 2018 00:26
-
-
Save P403n1x87/b145607b79bebe3dc54a719332f1fa40 to your computer and use it in GitHub Desktop.
Memory usage Blighty widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import pi as PI | |
import psutil | |
from attrdict import AttrDict | |
from blighty import CanvasGravity, TextAlign | |
from blighty.legacy import Graph | |
from blighty.x11 import Canvas, start_event_loop | |
from fonts import Fonts | |
class Memory(Canvas): | |
SIZE = (256, 256) | |
CORE_POLYGON = AttrDict({"height": 30, "length": 20}) | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
with open('/proc/cpuinfo', 'r') as fin: | |
raw_cpuinfo = fin.read().strip() | |
self.coreinfo = [ | |
{ | |
k.strip(): v | |
for k, v in [p.split(":") for p in core.split('\n')] | |
} | |
for core in raw_cpuinfo.split('\n\n') | |
] | |
self.graph = Graph(0, 110, self.width, 40) | |
@staticmethod | |
def build(x = 0, y = 0, gravity = CanvasGravity.NORTH_WEST): | |
return Memory(x, y, *Memory.SIZE, gravity = gravity, interval = 5000) | |
def on_button_pressed(self, button, *args): | |
self.dispose() | |
def draw_polygon(c, n, x, y, size): | |
a = 2 * PI / n | |
c.save() | |
c.translate(x, y) | |
c.move_to(size, 0) | |
for i in range(n): | |
c.rotate(a) | |
c.line_to(size, 0) | |
c.stroke() | |
c.restore() | |
def draw_memory_usage(c, x, y): | |
size = Memory.CORE_POLYGON.height | |
length = Memory.CORE_POLYGON.length | |
radius = 42 | |
thickness = 8 | |
c.save() | |
c.translate(x, y) | |
c.set_source_rgb(.8, .8, .8) | |
mem = psutil.virtual_memory() | |
used = mem.used / mem.total | |
buffers = used + mem.buffers / mem.total | |
cached = buffers + mem.cached / mem.total | |
c.set_font_size(24) | |
c.write_text(0, 0, '{}%'.format(int(used * 100)), TextAlign.TOP_MIDDLE) | |
c.set_font_size(14) | |
c.write_text(0, 4, 'of {}G'.format(round(mem.total / 2**30)), TextAlign.BOTTOM_MIDDLE) | |
c.set_line_width(1) | |
c.arc(0, 0, radius, 0, 2 * PI) | |
c.stroke() | |
c.rotate(-PI/2) | |
c.set_line_width(thickness) | |
c.arc(0, 0, radius - (thickness >> 1), 0, 2 * PI * used) | |
c.stroke() | |
c.set_source_rgba(.8, .8, .8, .67) | |
c.set_line_width(thickness) | |
c.arc(0, 0, radius - (thickness >> 1), 2 * PI * used, 2 * PI * buffers) | |
c.stroke() | |
c.set_source_rgba(.8, .8, .8, .33) | |
c.set_line_width(thickness) | |
c.arc(0, 0, radius - (thickness >> 1), 2 * PI * buffers, 2 * PI * cached) | |
c.stroke() | |
c.restore() | |
def draw_processes(c): | |
ps = [ | |
p.info | |
for p in psutil.process_iter(attrs=['pid', 'name', 'memory_percent']) | |
] | |
ps = sorted(ps, key=lambda p: p["memory_percent"], reverse=True)[:5] | |
y = 120 | |
c.save() | |
c.select_font_face(*Fonts.LAKSAMAN_NORMAL) | |
c.set_font_size(12) | |
for p in ps: | |
c.write_text(48, y, str(p["pid"]), align = TextAlign.TOP_RIGHT) | |
c.write_text(52, y, p["name"][:24]) | |
c.write_text( | |
c.canvas.width - 2, y, "{:.1f}%".format(p["memory_percent"]), | |
align=TextAlign.TOP_RIGHT | |
) | |
y += 18 | |
c.restore() | |
def draw_swap(c): | |
c.save() | |
swap = psutil.swap_memory() | |
c.set_font_size(12) | |
c.write_text( | |
0, 90, | |
"SWAP {}% of {}G".format(round(swap.percent), round(swap.total / 2**30)) | |
) | |
c.restore() | |
def on_draw(self, c): | |
# c.draw_grid() | |
c.select_font_face(*Fonts.LAKSAMAN_NORMAL) | |
c.set_font_size(36) | |
c.set_source_rgb(1, 1, 1) | |
w, h = Memory.SIZE | |
y_poly = (Memory.CORE_POLYGON.height + Memory.CORE_POLYGON.length) | |
c.write_text(0, y_poly, "Memory", align = TextAlign.TOP_LEFT) | |
c.draw_memory_usage(w - y_poly, y_poly) | |
c.draw_processes() | |
c.draw_swap() | |
c.set_source_rgb(1, 1, 1) | |
self.graph.draw(c) | |
if __name__ == "__main__": | |
Memory.build(32, 300, gravity = CanvasGravity.NORTH_WEST).show() | |
start_event_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment