-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusLineCPU.py
53 lines (41 loc) · 1.49 KB
/
StatusLineCPU.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
from subprocess import check_output, Popen, PIPE
import shlex
import colorsys
from Logger import Logger
from StatusLineControl import StatusLineControl
from StatusLineBlock import StatusLineBlock
class StatusLineCPU(StatusLineControl):
def __init__(self):
StatusLineControl.__init__(self, "cpu")
self.updateInterval = 5
self.cpuUsage = StatusLineBlock(" -----% ")
self.cpuUsage.separator = False
self.scheduleUpdate(0)
@property
def blocks(self):
yield self.cpuUsage
def getCPUIdle(self):
try:
mpstat = Popen([
'mpstat', '1', '1'], stdout=PIPE)
idleStr = check_output([
'awk',
'/^Average:/ {idle=$12} END {print idle}'
], stdin=mpstat.stdout)
mpstat.stdout.close()
return float(idleStr)
except:
return None
def doOnUpdate(self):
idle = self.getCPUIdle()
if idle != None:
color = colorsys.hsv_to_rgb(1.0/360*120*idle/100, 0.5, 0.75)
color = [int(c *255) for c in color]
self.cpuUsage.border = "#{0[0]:0>2x}{0[1]:0>2x}{0[2]:0>2x}".format(color)
self.cpuUsage.full_text = " {:0>5.2f}% ".format(100 - idle)
else:
self.cpuUsage.border = "red"
self.cpuUsage.full_text = " {:s} ".format("-----%")
def doOnUpdateDone(self):
self.scheduleUpdate(self.updateInterval)