forked from timo/dwmstatusline
/
dwmstat.py
executable file
·170 lines (140 loc) · 4.95 KB
/
dwmstat.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
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
164
165
166
167
168
169
170
#!/usr/bin/python
# coding: utf-8
from subprocess import Popen, call, PIPE
from random import choice
import utils
from utils import animate, wait, PREVIOUS
import time
# install a few status functions
# Status functions call one of these helper functions and yield the result:
#
# next_function Resume with the next (random) status function.
# animate Use a transition function to show the next string.
# wait Wait some time before displaying the next string.
# uptime/load
# mpd status
# free hard drive storage
@utils.status_func
def memory_free():
"""Display the used RAM and Swap space."""
yield animate(2, "Used RAM")
# Parse the output of the "free" command. :(
free_output = Popen(["free"], stdout=PIPE).communicate()[0]
# get the interesting lines
interesting = free_output.split("\n")[1:]
total_ram = float(interesting[0].split()[1])
used_ram = int(interesting[1].split()[2]) / total_ram
total_swap = float(interesting[2].split()[1])
used_swap = int(interesting[2].split()[2]) / total_swap
yield animate(10, utils.pretty_progressbar(used_ram, 80))
yield animate(2, "Used Swap Space")
yield animate(10, utils.pretty_progressbar(used_swap, 80))
@utils.register_if_installed("mpc")
def mpd_np():
"""Display the mpd status via mpc."""
try:
status_output = Popen(["mpc", "status"], stdout=PIPE).communicate()[0]
except OSError:
yield wait(5, "mpc not installed :(")
return
result = ""
lines = status_output.split("\n")
if len(lines) < 3: # stopped
yield wait(10, "MPD stopped")
else:
result += "MPD " + lines[1].split()[0] + " " + lines[0].strip()
yield animate(10, result)
yield animate(10, lines[1].split()[2])
@utils.register_if_installed("acpi")
def battery():
"""Display the battery status via acpi."""
try:
status_output = Popen(["acpi"], stdout=PIPE).communicate()[0]
except OSError:
yield wait(5, "battery status could not be determined.")
return
yield animate(10, status_output.split("\n")[0])
@utils.status_func
def wallclock():
yield wait(5, "Wallclock")
for i in range(10):
yield wait(1, time.strftime("%H:%M:%S"))
time.sleep(1)
class NewsbeuterUnread(utils.DelayedUpdateDisplay):
delay = 600
def call(self):
unread_text = Popen(["newsbeuter", "-x", "print-unread"], stdout=PIPE).communicate()[0]
if unread_text.startswith("Error:"): return
unread_items = int(unread_text.split()[0])
if unread_items > 0:
yield animate(10, "Newsbeuter: %d unread news." % (unread_items,))
else:
yield animate(10, "Newsbeuter: No unread news.")
def update(self):
Popen(["newsbeuter", "-x", "reload"], stdout=PIPE)
self.last_update = time.time()
def checkInstall(self):
return call(["which", "newsbeuter"], stdout=PIPE) == 0
NewsbeuterUnread()
@utils.status_func
def load_and_uptime():
"""Display the output of uptime."""
yield wait(10, Popen(["uptime"], stdout=PIPE).communicate()[0])
# install a few transition functions
# These work in the same way as the status functions
@utils.transition_func
def shoot(prev, new):
"""Animate a transition with a flying star."""
d = 0.05
icon = "*"
length = max(len(prev), len(new))
for pos in range(0, length, 2):
padding = max(length - pos - 1 - len(prev), 0)
old = prev[-(length - pos - padding):]
yield wait(d, new[:pos] + ' ' * padding + old)
yield wait(1, new.lstrip())
def startup_animation():
"""Animate the startup of the script."""
yield wait(1, "DWM Status Bar Animator")
yield animate(1, "a silly script by timonator")
def run_animation(iter_, previous):
"""Run an animation."""
yield previous
yield iter_.send(previous)
for value in iter_:
yield value
def run_statuses(startfunc):
"""The 'main loop' of dwmstat."""
iters = [startfunc()]
previous = ''
while True:
while iters:
iter_ = iters.pop()
for value in iter_:
if isinstance(value, basestring):
utils.dwm_set_status(value)
previous = value
elif value is PREVIOUS:
iters.append(run_animation(iter_, previous))
break
else:
iters.append(iter_)
iters.append(value)
break
iters.append(choice(utils.statuses)())
if __name__ == "__main__":
try:
run_statuses(startup_animation)
except KeyboardInterrupt:
# say goodbye
utils.dwm_set_status("Goodbye.")
time.sleep(5)
utils.dwm_set_status("")
except Exception, e:
utils.dwm_set_status("dwmstat crashed: %r" % e)
time.sleep(5)
utils.dwm_set_status("")
raise
time.sleep(5)
# make sure to exit cleanly
utils.dwm_set_status("")