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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# -*- python -*-
from plugins.powersupply import PowerSupply
from common import *
class MyBattery(Entry):
COMPACT = 0
NORMAL = 1
DETAILED = 2
def __init__(self, *args, show_all = False, show_battery = 0, details = 1, batteries = None, **kwargs):
self.show_all = show_all
self.show_battery = show_battery
self.supply_filter = batteries
self.details = details
self.supplies = None
self.batteries = None
self.reload()
Entry.__init__(self, *args, **kwargs)
def reload(self):
name = None
newsupplies = {}
newbatteries = []
if self.batteries is not None:
if isinstance(self.batteries, int):
name = self.batteries[self.show_battery]
else:
name = self.show_battery
for supply in PowerSupply.supplies():
if self.supply_filter is not None:
if supply not in self.supply_filter:
continue
supply = PowerSupply(supply)
if supply.type != 'Battery':
continue
newsupplies[supply.name] = supply
newbatteries.append(supply.name)
if self.supply_filter is not None:
newbatteries = [b for b in self.supply_filter if b in newsupplies]
if name is None:
index = 0
elif isinstance(name, int):
index = name % len(newbatteries) if len(newbatteries) > 0 else 0
elif name in self.batteries:
index = self.batteries.index(index)
else:
index = 0
self.supplies, self.batteries, self.show_battery = newsupplies, newbatteries, index
def action(self, col, button, x, y):
if button == LEFT_BUTTON:
self.show_all = not self.show_all
self.invalidate()
elif button == MIDDLE_BUTTON:
self.details += 1
self.details %= 3
self.invalidate()
elif button == RIGHT_BUTTON:
self.reload()
self.invalidate()
elif button == SCROLL_UP:
if len(self.batteries) > 0:
self.show_battery += 1
self.show_battery %= len(self.batteries)
self.invalidate()
elif button == SCROLL_DOWN:
if len(self.batteries) > 0:
self.show_battery -= 1
if self.show_battery < 0:
self.show_battery += len(self.batteries)
self.invalidate()
def colourise(self, text):
try:
value = text.replace(',', '.')
value = float(value) if '.' in value else int(value)
colour = '39'
if value < 80: colour = '32'
if value < 50: colour = '33'
if value < 15: colour = '31'
if value < 5: colour = '7;31'
return '\033[%sm%s\033[0m' % (colour, text)
except:
return text
def battery(self, bat):
bat = self.supplies[bat]
name = bat.name[:1].upper() + bat.name[1:].lower()
if bat.is_online():
try:
charge = (bat.get_charge(), bat.get_charge_full(), bat.charge_full_design)
current = bat.get_current()
status = bat.get_status().lower()
left = None
if charge[0] == charge[1]:
capacity = '100'
else:
capacity = '%.1f' % (charge[0] / charge[1] * 100)
capacity = self.colourise(capacity)
if current > 0:
if status == 'charging':
left = (charge[1] - charge[0]) / current
elif status == 'discharging':
left = charge[0] / current
if left is not None:
hours = int(left)
minutes = int((left - hours) * 60 + 0.5)
if minutes >= 60:
minutes -= 60
hours += 1
left = '%ih%02im until %s' % (hours, minutes, status.replace('ing', 'ed'))
if self.details == MyBattery.COMPACT:
if status == 'full':
return 'full'
elif status == 'charging':
status = '+'
elif status == 'discharging':
status = '-'
else:
status = '?'
if left is None:
return '%s%%%s' % (capacity, status)
else:
return '%s%%%s %s' % (capacity, status, left)
elif self.details == MyBattery.NORMAL:
if left is None:
return '%s: %s%% %s' % (name, capacity, status)
else:
return '%s: %s%% %s %s' % (name, capacity, status, left)
else:
if charge[1] == charge[2]:
health = '100'
else:
health = '%.1f' % (charge[1] / charge[2] * 100)
health = self.colourise(health)
volt = bat.get_voltage() / bat.voltage_min_design
if volt < 1:
volt = '\033[31m%.1f\033[0m' % volt
elif volt < 1.05:
volt = '\033[33m%.1f\033[0m' % volt
else:
volt = '%.1f' % volt
if left is None:
return '%s: %s%% %s, %s%% healthy, %sx voltage' % (name, capacity, status, health, volt)
else:
return '%s: %s%% %s %s, %s%% healthy, %sx voltage' % (name, capacity, status, left, health, volt)
except Exception as e:
return (repr(e))
return ('%s: ???' % name) if self.details > 0 else '???'
else:
return ('%s: offline' % name) if self.details > 0 else 'offline'
def function(self):
if len(self.batteries) == 0:
return 'no batteries available'
if self.show_all:
return SEPARATOR.join(self.battery(bat) for bat in self.batteries)
else:
return self.battery(self.batteries[self.show_battery])
|