1
0
mirror of https://github.com/dcarrillo/dotfiles.git synced 2024-12-23 17:16:47 +00:00
dotfiles/.config/polybar/scripts/cpu_usage.py

44 lines
1.5 KiB
Python
Raw Normal View History

2019-07-18 16:45:46 +00:00
#!/usr/bin/env python3
import os
import subprocess
colors = {
'us': '#43A047', # us, user : time running un-niced user processes
'sy': '#87CEFA', # sy, system : time running kernel processes
'ni': '#FFFFFF', # ni, nice : time running niced user processes
'wa': '#E53935', # wa, IO-wait : time waiting for I/O completion
'hi': '#66CDAA', # hi : time spent servicing hardware interrupts
'si': '#7FFFD4', # si : time spent servicing software interrupts
'st': '#7B2B4E', # st : time stolen from this vm by the hypervisor
'id': '#555555'
}
def show_cpu_usage():
new_env = dict(os.environ)
new_env['LANG'] = 'en_US.UTF-8' # ensure that the decimal separator is a point
top = subprocess.run(["top", "-bn1"], capture_output=True, env=new_env)
raw_cpu_usages = top.stdout.decode("utf-8").split('\n')[2]
cpu_usage = str.replace(raw_cpu_usages, '%Cpu(s):', '')
bar = ''
global_count = 0
for usage in cpu_usage.split(','):
value, key = str.lstrip(usage).split(' ')
count = round(float(value) / 10)
if count > 0 and key != 'id':
global_count += count
bar += '%{F' + colors[key] + '}' + '_' * count + '%{F-}'
padding = ''
if global_count < 10:
padding = '%{F' + colors['id'] + '}' + '_' * (10 - global_count) + '%{F-}'
print("%{A1:$TERMINAL_CMD 'top -o %CPU' &:}%{T5}" + bar + padding + "%{T-}%{A-}")
if __name__ == "__main__":
show_cpu_usage()