Skip to content

Instantly share code, notes, and snippets.

@sohnryang
Created December 21, 2021 22:52
Show Gist options
  • Save sohnryang/2a78b742219f5d35d9a90f76748b0c30 to your computer and use it in GitHub Desktop.
Save sohnryang/2a78b742219f5d35d9a90f76748b0c30 to your computer and use it in GitHub Desktop.
i3 statusbar using font awesome and nerd fonts
from datetime import datetime
from subprocess import check_output
from time import sleep
import json
import psutil
red = "#ed6259"
orange = "#f29b34"
green = "#8dd773"
cyan = "#5bdbcc"
def render_statusbar(
wifi_name,
upload_speed,
download_speed,
memory_used,
total_memory,
cpu_usage,
cpu_temp,
battery,
charging,
):
if charging:
battery_color = cyan
else:
if 20 <= battery:
battery_color = green
elif 10 <= battery < 20:
battery_color = orange
else:
battery_color = red
if cpu_temp < 55:
cpu_temp_color = green
elif 55 <= cpu_temp < 80:
cpu_temp_color = orange
else:
cpu_temp_color = red
if memory_used / total_memory < 0.7:
memory_color = green
elif 0.7 <= memory_used / total_memory < 0.9:
memory_color = orange
else:
memory_color = red
return [
{"full_text": "\uf1eb {}".format(wifi_name)},
{
"full_text": "\uf093 {:.2f} MiB/s \uf019 {:.2f} MiB/s".format(
upload_speed, download_speed
),
},
{
"full_text": " \uf538 {:.2f}/{:.2f} GiB ".format(memory_used, total_memory),
"color": "#000000",
"background": memory_color,
"separator_block_width": 0,
},
{
"full_text": " \uf85a {:.2f}% \uf2c7 {:.2f}°C ".format(cpu_usage, cpu_temp),
"color": "#000000",
"background": cpu_temp_color,
"separator_block_width": 0,
},
{
"full_text": " {} {:.1f}% ".format(
"\uf1e6" if charging else "\uf240", battery
),
"color": "#000000",
"background": battery_color,
},
{"full_text": datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")},
]
print('{"version": 1}')
print("[")
last_sent_counter = psutil.net_io_counters().bytes_sent
last_recv_counter = psutil.net_io_counters().bytes_recv
while True:
cpu_usage = psutil.cpu_percent()
cpu_temp = psutil.sensors_temperatures()["coretemp"][0].current
memory_used = psutil.virtual_memory().used / 1024 ** 3
total_memory = psutil.virtual_memory().total / 1024 ** 3
battery = psutil.sensors_battery().percent # type: ignore
charging = psutil.sensors_battery().power_plugged # type: ignore
upload_speed = (
(psutil.net_io_counters().bytes_sent - last_sent_counter) * 2 / 1024 ** 2
)
download_speed = (
(psutil.net_io_counters().bytes_recv - last_recv_counter) * 2 / 1024 ** 2
)
last_sent_counter = psutil.net_io_counters().bytes_sent
last_recv_counter = psutil.net_io_counters().bytes_recv
try:
wifi_name = check_output(["iwgetid", "-r"]).decode("utf-8").strip()
except:
wifi_name = None
print(
json.dumps(
render_statusbar(
wifi_name,
upload_speed,
download_speed,
memory_used,
total_memory,
cpu_usage,
cpu_temp,
battery,
charging,
)
),
end=",",
flush=True,
)
sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment