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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# -*- python -*-
from plugins.mem import Memory
from plugins.swaps import Swaps
from common import *
class MyMemory(Entry):
def __init__(self, *args, **kwargs):
self.coloured = tuple(self.colourise(i) for i in range(101))
self.show_labels = True
self.show_value = -1
self.show_default = False
self.show_swaps = False
self.show_swap = 0
self.labels = list(sorted(Memory().keys))
swaps = Swaps()
self.prio = swaps.header_map['Priority']
self.used = swaps.header_map['Used']
self.size = swaps.header_map['Size']
self.swap = swaps.header_map['Filename']
Entry.__init__(self, *args, **kwargs)
def action(self, col, button, x, y):
if button == LEFT_BUTTON:
if self.show_value >= 0:
self.show_default = not self.show_default
self.invalidate()
elif button == MIDDLE_BUTTON:
self.show_labels = not self.show_labels
self.invalidate()
elif button == RIGHT_BUTTON:
self.show_swaps = not self.show_swaps
self.invalidate()
elif button == SCROLL_UP:
if self.show_swaps:
self.show_swap += 1
self.invalidate()
else:
n = self.show_value + 1
if n < len(self.labels):
self.show_value = n
self.invalidate()
elif button == SCROLL_DOWN:
if self.show_swaps:
n = self.show_swap
if n > 0:
self.show_swap = n - 1
self.invalidate()
else:
self.show_value -= 1
if self.show_value < -1:
self.show_value = -1
self.invalidate()
def u(self, value):
units = 'KMGTPE'
unit = 0
while unit + 1 < len(units) and value >= 1024:
unit += 1
value /= 1024
return '%.1f%s' % (value, units[unit])
def colourise(self, value):
if value >= 100:
return '\033[31m100\033[0m'
colour = '39'
if value > 30: colour = '32'
if value > 50: colour = '33'
if value > 80: colour = '31'
return '\033[%sm%i\033[0m%%' % (colour, value)
def function(self):
if self.show_swaps:
swaps = Swaps()
while True:
if len(swaps.swaps) == 0:
return 'no swaps available'
try:
swap = swaps.swaps[self.show_swap]
used, size = int(swap[self.used]), int(swap[self.size])
swap, prio = swap[self.swap], int(swap[self.prio])
return '%s: %s (%sB/%sB, prio %i)' % (swap, self.colourise(used * 100 / size),
self.u(used), self.u(size), prio)
except:
self.show_swap -= 1
if self.show_swap < 0:
self.show_swaps = False
return self.function()
else:
memory = Memory()
if not self.show_default and self.show_value > 0:
label = self.labels[self.show_value]
try:
value = memory[label]
unit = 0
units = ['K', 'M', 'G', 'T', 'P', 'E']
while (unit + 1 < len(units)) and (value >= 1024):
value /= 1024
unit += 1
return '%s: %.1f%sB' % (label, value, units[unit])
except:
self.show_value = -1
if memory.mem_total == 0:
mem = '---'
shm = '---'
else:
mem = self.coloured[limited(memory.mem_used * 100 / memory.mem_total)]
shm = self.coloured[limited(memory.shmem * 100 / memory.mem_total)]
if memory.swap_total == 0:
swp = 'n/a'
else:
swp = self.coloured[limited(memory.swap_used * 100 / memory.swap_total)]
if self.show_labels:
mem = 'Mem: %s%sSwp: %s%sShm: %s' % (mem, SEPARATOR, swp, SEPARATOR, shm)
else:
mem = 'Mem: %s %s %s' % (mem, swp, shm)
return mem
|