-
Notifications
You must be signed in to change notification settings - Fork 0
/
ustatus.py
102 lines (75 loc) · 2.42 KB
/
ustatus.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
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
#!/usr/bin/env python
# coding: utf8
def default_format(status):
return '{}: {:3.0f}%'.format(status.name, status.query())
class Status(object):
def __init__(self, name, query, format=default_format):
self._name = name
self.query = query
self._format = format
@property
def name(self):
return self._name
def format(self):
return self._format(self)
class StatusCollection(Status):
def __init__(self, *statuses):
self.statuses = list(statuses)
@property
def name(self):
pass
def format(self):
pass
def update(self, index=None):
if not index:
for status in self.statuses:
status.update()
else:
self.statuses[index].update()
class StatusComposer(StatusCollection):
def __init__(self, *statuses, **kwargs):
super(StatusComposer, self).__init__(*statuses)
self.separator = kwargs.get('separator', ' | ')
@property
def name(self):
return 'Composer'
def format(self):
return self.separator.join(status.format() for status in self.statuses)
class StatusCycler(StatusCollection):
def __init__(self, *statuses, **kwargs):
super(StatusCycler, self).__init__(*statuses)
self.active = kwargs.get('active', len(self.statuses) - 1)
@property
def name(self):
return 'Cycler'
def format(self):
return self.statuses[self.active].format()
def update(self):
self.update(self.active)
def next(self):
self.active = (self.active + 1) % len(self.statuses)
def set_title(app, title):
app.title = title
def configure_app(*statuses, **kwargs):
import rumps
refresh = kwargs.get('refresh', 1)
composer = kwargs.get('composer', False)
sc = None
if composer:
sep = kwargs.get('separator', ' | ')
sc = StatusComposer(*statuses, separator=sep)
else:
sc = StatusCycler(*statuses)
cycle = kwargs.get('cycle', 5)
rumps.Timer(lambda _: sc.next(), cycle).start()
app = rumps.App(u"µStatus", title=sc.format())
rumps.Timer(lambda _: set_title(app, sc.format()), refresh).start()
return app
if __name__ == '__main__':
import psutil
configure_app(
Status('CPU', lambda: psutil.cpu_percent()),
Status('RAM', lambda: psutil.phymem_usage().percent),
composer=True,
separator=', '
).run()