1414"""
1515
1616import os
17+ import signal
1718import sys
1819import time
1920from pathlib import Path
3738# TODO: Load histogram
3839
3940
41+ def shutdown (signum , frame ):
42+ device .clear ()
43+ sys .exit (0 )
44+
45+
46+ signal .signal (signal .SIGTERM , shutdown )
47+ signal .signal (signal .SIGINT , shutdown )
48+
49+
4050def bytes2human (n ):
4151 """
4252 >>> bytes2human(10000)
@@ -56,23 +66,32 @@ def bytes2human(n):
5666
5767
5868def cpu_usage ():
59- # load average , uptime
69+ # cpu usage , uptime
6070 uptime = datetime .now () - datetime .fromtimestamp (psutil .boot_time ())
61- av1 , av2 , av3 = os .getloadavg ()
62- return "Ld:%.1f %.1f %.1f Up: %s" \
63- % (av1 , av2 , av3 , str (uptime ).split ('.' )[0 ])
71+ days = uptime .days
72+ hours , remainder = divmod (uptime .seconds , 3600 )
73+ minutes , _ = divmod (remainder , 60 )
74+
75+ cpu_percent = psutil .cpu_percent (interval = 1 )
76+ return "CPU: %d%% Up: %dd%dh%dm" % (cpu_percent , days , hours , minutes )
6477
6578
6679def mem_usage ():
6780 usage = psutil .virtual_memory ()
68- return "Mem: %s %.0f%%" \
69- % (bytes2human (usage .used ), 100 - usage .percent )
81+ return "RAM: %s/%s (%.0f%%)" % (
82+ bytes2human (usage .used ),
83+ bytes2human (usage .total ),
84+ usage .percent
85+ )
7086
7187
7288def disk_usage (dir ):
7389 usage = psutil .disk_usage (dir )
74- return "SD: %s %.0f%%" \
75- % (bytes2human (usage .used ), usage .percent )
90+ return "SD: %s/%s (%.0f%%)" % (
91+ bytes2human (usage .used ),
92+ bytes2human (usage .total ),
93+ usage .percent
94+ )
7695
7796
7897def network (iface ):
@@ -83,18 +102,22 @@ def network(iface):
83102
84103def stats (device ):
85104 # use custom font
86- font_path = str (Path (__file__ ).resolve ().parent .joinpath ('fonts' , 'C&C Red Alert [INET].ttf' ))
87- font2 = ImageFont .truetype (font_path , 12 )
105+ font_path = str (Path (__file__ ).resolve ().parent .joinpath ('fonts' , 'DejaVuSansMono.ttf' ))
106+ font2 = ImageFont .truetype (font_path , 10 )
107+ ascent , descent = font2 .getmetrics ()
108+ line_height = ascent + descent
88109
89110 with canvas (device ) as draw :
90- draw .text ((0 , 0 ), cpu_usage (), font = font2 , fill = "white" )
91- if device .height >= 32 :
92- draw .text ((0 , 14 ), mem_usage (), font = font2 , fill = "white" )
111+ draw .rectangle (device .bounding_box , outline = "white" , fill = None )
112+ draw .text ((2 , line_height * 0 ), cpu_usage (), font = font2 , fill = "white" )
113+ if device .height >= (line_height * 2 ):
114+ draw .text ((2 , line_height * 1 ), mem_usage (), font = font2 , fill = "white" )
93115
94- if device .height >= 64 :
95- draw .text ((0 , 26 ), disk_usage ('/' ), font = font2 , fill = "white" )
116+ if device .height >= ( line_height * 3 ) :
117+ draw .text ((2 , line_height * 2 ), disk_usage ('/' ), font = font2 , fill = "white" )
96118 try :
97- draw .text ((0 , 38 ), network ('wlan0' ), font = font2 , fill = "white" )
119+ if device .height >= (line_height * 4 ):
120+ draw .text ((2 , line_height * 3 ), network ('wlan0' ), font = font2 , fill = "white" )
98121 except KeyError :
99122 # no wifi enabled/available
100123 pass
@@ -112,3 +135,5 @@ def main():
112135 main ()
113136 except KeyboardInterrupt :
114137 pass
138+ finally :
139+ device .clear ()
0 commit comments