dotfiles/hm/frobar/.dev/oldbar.py

200 lines
5 KiB
Python
Raw Normal View History

2018-09-05 09:07:37 +02:00
#!/usr/bin/env python3
"""
Debugging script
"""
import i3ipc
import os
import psutil
2021-06-13 11:49:21 +02:00
2018-09-05 09:07:37 +02:00
# import alsaaudio
from time import time
import subprocess
i3 = i3ipc.Connection()
2021-06-13 11:49:21 +02:00
lemonbar = subprocess.Popen(["lemonbar", "-b"], stdin=subprocess.PIPE)
2018-09-05 09:07:37 +02:00
# Utils
def upChart(p):
2021-06-13 11:49:21 +02:00
block = " ▁▂▃▄▅▆▇█"
return block[round(p * (len(block) - 1))]
2018-09-05 09:07:37 +02:00
2021-06-13 11:49:21 +02:00
def humanSizeOf(num, suffix="B"): # TODO Credit
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
2018-09-05 09:07:37 +02:00
if abs(num) < 1024.0:
return "%3.0f%2s%s" % (num, unit, suffix)
num /= 1024.0
2021-06-13 11:49:21 +02:00
return "%.0f%2s%s" % (num, "Yi", suffix)
2018-09-05 09:07:37 +02:00
# Values
2021-06-13 11:49:21 +02:00
mode = ""
2018-09-05 09:07:37 +02:00
container = i3.get_tree().find_focused()
workspaces = i3.get_workspaces()
outputs = i3.get_outputs()
2021-06-13 11:49:21 +02:00
username = os.environ["USER"]
hostname = os.environ["HOSTNAME"]
if "-" in hostname:
hostname = hostname.split("-")[-1]
2018-09-05 09:07:37 +02:00
oldNetIO = dict()
oldTime = time()
2021-06-13 11:49:21 +02:00
2018-09-05 09:07:37 +02:00
def update():
2021-06-13 11:49:21 +02:00
activeOutputs = sorted(
sorted(list(filter(lambda o: o.active, outputs)), key=lambda o: o.rect.y),
key=lambda o: o.rect.x,
)
z = ""
2018-09-05 09:07:37 +02:00
for aOutput in range(len(activeOutputs)):
output = activeOutputs[aOutput]
# Mode || Workspaces
t = []
2021-06-13 11:49:21 +02:00
if mode != "":
2018-09-05 09:07:37 +02:00
t.append(mode)
else:
2021-06-13 11:49:21 +02:00
t.append(
" ".join(
[
(w.name.upper() if w.focused else w.name)
for w in workspaces
if w.output == output.name
]
)
)
2018-09-05 09:07:37 +02:00
# Windows Title
2021-06-13 11:49:21 +02:00
# if container:
2018-09-05 09:07:37 +02:00
# t.append(container.name)
# CPU
2021-06-13 11:49:21 +02:00
t.append(
"C" + "".join([upChart(p / 100) for p in psutil.cpu_percent(percpu=True)])
)
2018-09-05 09:07:37 +02:00
# Memory
2021-06-13 11:49:21 +02:00
t.append(
"M"
+ str(round(psutil.virtual_memory().percent))
+ "% "
+ "S"
+ str(round(psutil.swap_memory().percent))
+ "%"
)
2018-09-05 09:07:37 +02:00
# Disks
d = []
for disk in psutil.disk_partitions():
2021-06-13 11:49:21 +02:00
e = ""
if disk.device.startswith("/dev/sd"):
e += "S" + disk.device[-2:].upper()
elif disk.device.startswith("/dev/mmcblk"):
e += "M" + disk.device[-3] + disk.device[-1]
2018-09-05 09:07:37 +02:00
else:
2021-06-13 11:49:21 +02:00
e += "?"
e += " "
e += str(round(psutil.disk_usage(disk.mountpoint).percent)) + "%"
2018-09-05 09:07:37 +02:00
d.append(e)
2021-06-13 11:49:21 +02:00
t.append(" ".join(d))
2018-09-05 09:07:37 +02:00
# Network
netStats = psutil.net_if_stats()
netIO = psutil.net_io_counters(pernic=True)
net = []
2021-06-13 11:49:21 +02:00
for iface in filter(lambda i: i != "lo" and netStats[i].isup, netStats.keys()):
s = ""
if iface.startswith("eth"):
s += "E"
elif iface.startswith("wlan"):
s += "W"
2018-09-05 09:07:37 +02:00
else:
2021-06-13 11:49:21 +02:00
s += "?"
2018-09-05 09:07:37 +02:00
2021-06-13 11:49:21 +02:00
s += " "
2018-09-05 09:07:37 +02:00
now = time()
global oldNetIO, oldTime
2021-06-13 11:49:21 +02:00
sent = (
(oldNetIO[iface].bytes_sent if iface in oldNetIO else 0)
- (netIO[iface].bytes_sent if iface in netIO else 0)
) / (oldTime - now)
recv = (
(oldNetIO[iface].bytes_recv if iface in oldNetIO else 0)
- (netIO[iface].bytes_recv if iface in netIO else 0)
) / (oldTime - now)
s += (
""
+ humanSizeOf(abs(recv), "B/s")
+ ""
+ humanSizeOf(abs(sent), "B/s")
)
2018-09-05 09:07:37 +02:00
oldNetIO = netIO
oldTime = now
net.append(s)
2021-06-13 11:49:21 +02:00
t.append(" ".join(net))
2018-09-05 09:07:37 +02:00
# Battery
2021-06-13 11:49:21 +02:00
if os.path.isdir("/sys/class/power_supply/BAT0"):
with open("/sys/class/power_supply/BAT0/charge_now") as f:
2018-09-05 09:07:37 +02:00
charge_now = int(f.read())
2021-06-13 11:49:21 +02:00
with open("/sys/class/power_supply/BAT0/charge_full_design") as f:
2018-09-05 09:07:37 +02:00
charge_full = int(f.read())
2021-06-13 11:49:21 +02:00
t.append("B" + str(round(100 * charge_now / charge_full)) + "%")
2018-09-05 09:07:37 +02:00
# Volume
# t.append('V ' + str(alsaaudio.Mixer('Master').getvolume()[0]) + '%')
2021-06-13 11:49:21 +02:00
t.append(username + "@" + hostname)
2018-09-05 09:07:37 +02:00
# print(' - '.join(t))
# t = [output.name]
2021-06-13 11:49:21 +02:00
z += " - ".join(t) + "%{S" + str(aOutput + 1) + "}"
# lemonbar.stdin.write(bytes(' - '.join(t), 'utf-8'))
# lemonbar.stdin.write(bytes('%{S' + str(aOutput + 1) + '}', 'utf-8'))
2018-09-05 09:07:37 +02:00
2021-06-13 11:49:21 +02:00
lemonbar.stdin.write(bytes(z + "\n", "utf-8"))
2018-09-05 09:07:37 +02:00
lemonbar.stdin.flush()
2021-06-13 11:49:21 +02:00
2018-09-05 09:07:37 +02:00
# Event listeners
def on_mode(i3, e):
global mode
2021-06-13 11:49:21 +02:00
if e.change == "default":
mode = ""
else:
2018-09-05 09:07:37 +02:00
mode = e.change
update()
2021-06-13 11:49:21 +02:00
2018-09-05 09:07:37 +02:00
i3.on("mode", on_mode)
2021-06-13 11:49:21 +02:00
# def on_window_focus(i3, e):
2018-09-05 09:07:37 +02:00
# global container
# container = e.container
# update()
#
2021-06-13 11:49:21 +02:00
# i3.on("window::focus", on_window_focus)
2018-09-05 09:07:37 +02:00
def on_workspace_focus(i3, e):
global workspaces
workspaces = i3.get_workspaces()
update()
2021-06-13 11:49:21 +02:00
2018-09-05 09:07:37 +02:00
i3.on("workspace::focus", on_workspace_focus)
# Starting
update()
i3.main()