dotfiles/sway/status.py
Alan Orth d39c0c7892
sway/status.py: A few changes
- Switch to iwd for Wi-Fi
- Remove unused code
- Format with black
2020-12-13 17:10:25 +02:00

66 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Requires the following packages on Arch Linux:
# - ttf-twemoji (and make sure there are no other emoji fonts like noto-fonts-emoji
# - networkmanager
# - pulsemixer
from datetime import datetime
from subprocess import check_output
from sys import stdout
from psutil import sensors_battery
def write(data):
stdout.write("%s\n" % data)
stdout.flush()
def refresh():
try:
ssid = (
check_output(
"iwctl station wlan0 show | grep 'Connected network' | awk '{print $3}'",
shell=True,
)
.strip()
.decode("utf-8")
)
except Exception:
ssid = "None"
wifi_status = f"{ssid} 📶"
battery_percent = int(sensors_battery().percent)
battery_icon = "🔋"
battery_status = f"{battery_percent} {battery_icon}"
power_status = "🔌" if sensors_battery().power_plugged else ""
try:
# pulsemixer returns "10 10" so we need to split the output to get "10"
volume_level = (
check_output("pulsemixer --get-volume", shell=True).strip().decode("utf-8")
)
volume_level = int(volume_level.split(" ")[0])
if volume_level >= 70:
volume_icon = "🔊"
elif volume_level >= 30 and volume_level < 70:
volume_icon = "🔉"
elif volume_level < 30 and volume_level >= 1:
volume_icon = "🔈"
else:
volume_icon = "🔇"
except Exception:
volume_level = ""
volume_status = f"{volume_level} {volume_icon}"
date = datetime.now().strftime("%Y-%m-%d %l:%M:%S %p")
write(f"{wifi_status} {volume_status} {battery_status} {power_status} {date}")
refresh()