Created
December 25, 2018 00:31
-
-
Save P403n1x87/2b7edd6e19bcd47a496ac2f3c5ef46eb to your computer and use it in GitHub Desktop.
File System 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 FileSystem(Canvas): | |
SIZE = (256, 512) | |
CORE_POLYGON = AttrDict({"height": 30, "length": 20}) | |
@staticmethod | |
def build(x = 0, y = 0, gravity = CanvasGravity.NORTH_WEST): | |
return FileSystem(x, y, *FileSystem.SIZE, gravity=gravity, interval=5000) | |
def on_button_pressed(self, button, *args): | |
self.dispose() | |
def draw_circle(c, n, x, y): | |
thickness = 6 | |
radius = 20 | |
c.save() | |
c.translate(x, y) | |
c.set_font_size(12) | |
c.write_text(0, 0, "{}%".format(int(n)), align=TextAlign.CENTER_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 * n / 100) | |
c.stroke() | |
c.restore() | |
def draw_memory_usage(c, x, y): | |
size = FileSystem.CORE_POLYGON.height | |
length = FileSystem.CORE_POLYGON.length | |
radius = 42 | |
thickness = 8 | |
def draw_filesystems(c): | |
radius = 42 | |
thickness = 12 | |
c.save() | |
parts = psutil.disk_partitions(False) | |
y = 0 | |
offset = 112 | |
for part in [part for part in parts if part.fstype != "squashfs"]: | |
mem = psutil.disk_usage(part.mountpoint) | |
if mem.total < 2**30: | |
continue | |
c.set_source_rgb(1, 1, 1) | |
c.draw_circle(mem.percent, 24, offset + y) | |
c.set_font_size(16) | |
c.write_text(56, offset-10 + y, part.device) | |
c.set_font_size(12) | |
c.set_source_rgb(.8, .8, .8) | |
c.write_text(56, offset+6 + y, "FREE {}/{}G".format( | |
round(mem.free / 2**30), | |
round(mem.total / 2**30) | |
)) | |
c.write_text(56, offset+18 + y, "MNT {}".format( | |
part.mountpoint | |
)) | |
y += 56 | |
c.restore() | |
def on_draw(self, c): | |
c.select_font_face(*Fonts.LAKSAMAN_NORMAL) | |
c.set_font_size(36) | |
c.set_source_rgb(1, 1, 1) | |
w, h = FileSystem.SIZE | |
y_poly = (FileSystem.CORE_POLYGON.height + FileSystem.CORE_POLYGON.length) | |
c.write_text(0, y_poly, "File System", align = TextAlign.TOP_LEFT) | |
c.draw_memory_usage(w - y_poly, y_poly) | |
c.draw_filesystems() | |
if __name__ == "__main__": | |
FileSystem.build(360, 48, 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