Created
July 16, 2015 21:52
-
-
Save la11111/7ec66e79018420271fcf to your computer and use it in GitHub Desktop.
dwstat.py - my dwm status bar script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from threading import Thread,Lock,Event,Timer | |
import os | |
import sys | |
import signal | |
import subprocess | |
import math | |
import psutil | |
import re | |
import time | |
mutex = Lock() | |
thread_data = [] | |
class UpdaterThread(Thread): | |
def __init__(self, id, sleep, callback): | |
Thread.__init__(self) | |
self.stopped = Event() | |
self.id = id | |
self.sleeptime = sleep | |
self.callback = callback | |
def stop(self): | |
self.stopped.set() | |
def run(self): | |
thread_data[self.id] = self.callback() | |
with mutex: | |
update() | |
while not self.stopped.is_set(): | |
flagged = self.stopped.wait( | |
self.sleeptime | |
) | |
if not flagged: | |
thread_data[self.id] = self.callback() | |
with mutex: | |
update() | |
def update(): | |
subprocess.call([ | |
"xsetroot", | |
"-name", | |
" ".join(thread_data) | |
]) | |
def start(conf): | |
threads = [] | |
for i in range(len(conf)): | |
thread_data.append("...") | |
t = UpdaterThread( | |
i, | |
conf[i]['sleep'], | |
conf[i]['callback'] | |
) | |
t.setDaemon(True) | |
threads.append(t) | |
for t in threads: | |
t.start() | |
try: | |
signal.pause() | |
except (KeyboardInterrupt, SystemExit): | |
print "stopping threads" | |
for t in threads: | |
t.stop() | |
for t in threads: | |
t.join() | |
print "bye!" | |
return 0 | |
#### vvv configuration vvv #### | |
def datetime(): | |
return "".join([' ',time.strftime("%m.%d.%y - %H:%M:%S"),' ']) | |
def wifi(): | |
dev = "wlan0" | |
wifi_info = open("/proc/net/wireless").read() | |
signal_strength = 0 | |
try: | |
essid = re.sub( | |
r'^.*"([^"]*).*"', | |
r'\1', | |
subprocess.check_output( | |
["iwgetid"], | |
shell=False | |
).strip()) | |
except: | |
essid = "(x)" | |
try: | |
signal_strength = int(int(re.findall(r'{}:\s*\S*\s*(\d*)'.format(dev), wifi_info)[0])/70.0*100) | |
except: | |
signal_strength = 0 | |
output = ['wi: '] | |
output.append("\x1b[38;5;246m") | |
output.append('({})'.format(essid)) | |
output.append('{}'.format(rainbow_percent(signal_strength))) | |
""" | |
output.append(r"{0: >3}% ".format(pct)) | |
if (pct < 30): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=196) | |
) | |
elif (pct < 40): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=202) | |
) | |
elif (pct < 45): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=214) | |
) | |
elif (pct < 50): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=184) | |
) | |
elif (pct < 60): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=154) | |
) | |
else: | |
output.append( | |
bargraph(pct=pct, bars=1) | |
) | |
""" | |
return "".join(output) | |
def battery(): | |
acpath = "/sys/class/power_supply/ACAD" | |
batpath = "/sys/class/power_supply/BAT1" | |
ac_online = int(open(acpath+'/online','r').read().strip()) | |
max = int(open(batpath+'/energy_full','r').read().strip()) | |
cur = int(open(batpath+'/energy_now','r').read().strip()) | |
pct = int(100*float(cur)/float(max)) | |
output = ['bat: '] | |
output.append("{}".format(rainbow_percent(pct))) | |
output.append("\x1b[38;5;246m") | |
output.append("(") | |
if ac_online: | |
if pct == 100: | |
output.append("\x1b[38;5;46m+") | |
else: | |
output.append("\x1b[38;5;32m~") | |
else: | |
output.append("\x1b[38;5;196m-") | |
output.append("\x1b[38;5;246m") | |
output.append(")") | |
""" | |
if (pct < 10): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=196) | |
) | |
elif (pct < 20): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=202) | |
) | |
elif (pct < 30): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=214) | |
) | |
elif (pct < 40): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=184) | |
) | |
elif (pct < 50): | |
output.append( | |
bargraph(pct=pct, bars=1, oncolor=154) | |
) | |
else: | |
output.append( | |
bargraph(pct=pct, bars=1) | |
) | |
""" | |
return "".join(output) | |
def cpu(): | |
cpus = psutil.cpu_percent(interval=.5, percpu=True) | |
mhz = re.search( | |
"cpu MHz\s*:\s*(\S*)", | |
open('/proc/cpuinfo','r' | |
).read()).groups()[0].split('.')[0] | |
output = ['cpu: ']; | |
output.append("\x1b[38;5;246m") | |
output.append('{}Mz'.format(mhz)) | |
output.append('(') | |
for c in cpus: | |
output.append('{}'.format(rainbow_percent(int(c), True))) | |
#output.append("{0: >3}% ".format(int(c))) | |
output.append("\x1b[38;5;246m") | |
output.append(')') | |
output.append("\x1b[0m") | |
return "".join(output) | |
def mem(): | |
minfo = psutil.virtual_memory() | |
output = ['mem:'] | |
output.append("\x1b[38;5;246m") | |
output.append("{0: >3}%".format(int(minfo.percent))) | |
# output.append("({0:.3f}GB)".format(float(minfo.total - minfo.used)/1024**3)) | |
# output.append(bargraph(pct=int(minfo.percent), bars=6)) | |
return "".join(output) | |
def vol(): | |
mixerinfo = subprocess.check_output(['amixer','get','Master'], shell=False).split('\n')[-2] | |
master_vol = int(re.search( | |
r'\[(\d*)%\]', | |
mixerinfo | |
).groups()[0]) | |
muted = re.search(r'\[(on|off)\]', mixerinfo).groups()[0] | |
output = ['vol:'] | |
output.append("\x1b[38;5;246m") | |
output.append("{}".format(rainbow_percent(master_vol, True))) | |
if muted == 'off': | |
output.append("\x1b[38;5;231m") | |
output.append("\x1b[48;5;124m") | |
output.append('M') | |
output.append("\x1b[0m") | |
return "".join(output) | |
def bargraph(cur=0,max=100,pct=0,bars=8,oncolor='46',offcolor='232'): | |
if not pct: | |
if not (cur and max): | |
pct = 0 | |
else: | |
pct = int(100 * (float(cur)/float(max))) | |
if not pct: | |
ppb = onbars = 0 | |
else: | |
ppb = float(100)/bars | |
onbars = int(math.ceil(pct/ppb)) | |
output = [] | |
output.append('\x1b[48;5;{}m'.format(oncolor)) | |
output.append(' ' * onbars) | |
output.append('\x1b[48;5;{}m'.format(offcolor)) | |
output.append(' ' * (bars - onbars)) | |
output.append('\x1b[0m') | |
return "".join(output) | |
def rainbow_percent(pct=0, invert=False): | |
""" | |
pct: 0-100 | |
invert: False=0 is red | |
True=100 is red | |
""" | |
colors = [124, 202, 220, 190, 118, 70, 75, 63, 57, 92] | |
output = [' '] | |
text = pct | |
if invert: | |
pct = 100-pct | |
if pct < 0 or pct > 100: | |
return ''.join(output) | |
elif (pct >= 0) and (pct <= 10): | |
output.append("\x1b[38;5;232m\x1b[48;5;{}m".format(colors[0])) | |
else: | |
output.append("\x1b[38;5;{}m".format(colors[(pct/10)-1])) | |
output.append(r"{0: >3}%".format(text)) | |
output.append("\x1b[0m") | |
return ''.join(output) | |
def rpct_test(): | |
output = [''] | |
for i in xrange(0,101,10): | |
output.append('{}'.format(rainbow_percent(i))) | |
return ''.join(output) | |
if __name__ == '__main__': | |
conf = [ | |
# { | |
# "sleep":30, | |
# "callback":rpct_test, | |
# | |
# }, | |
{ | |
"sleep":2, | |
"callback":cpu, | |
}, | |
{ | |
"sleep":10, | |
"callback":mem, | |
}, | |
{ | |
"sleep":3, | |
"callback":wifi, | |
}, | |
{ | |
"sleep":1, | |
"callback":vol, | |
}, | |
{ | |
"sleep":10, | |
"callback":battery, | |
}, | |
{ | |
"sleep":1, | |
"callback":datetime, | |
}, | |
] | |
sys.exit(start(conf)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment