summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorSam Nystrom <sam@samnystrom.dev>2023-07-10 20:15:31 -0400
committerSam Nystrom <sam@samnystrom.dev>2023-07-19 11:52:52 -0400
commit6ad29f5700ab0ab0afed346875e48e977eacae4f (patch)
treef583585e4d42ab0a893bc5478dee19fc5e3ee7a4 /bin
parent0613f29757b688b55b74027063d35dcebc23b10a (diff)
statusbar: rewrite to python
Mostly for maintainability
Diffstat (limited to 'bin')
-rw-r--r--[-rwxr-xr-x]bin/statusbar133
1 files changed, 74 insertions, 59 deletions
diff --git a/bin/statusbar b/bin/statusbar
index a0b61d5..8d5dc6f 100755..100644
--- a/bin/statusbar
+++ b/bin/statusbar
@@ -1,59 +1,74 @@
-#!/bin/sh -u
-
-volume() {
- volume=$(pactl get-sink-volume @DEFAULT_SINK@ | sed -nE 's/.*\s([0-9]+)%.*/\1/p')
- if [ "$(pactl get-sink-mute @DEFAULT_SINK@)" = "Mute: yes" ]; then
- volume_symbol="󰝟"
- elif [ "$volume" -eq 0 ]; then
- volume_symbol=""
- elif [ "$volume" -le 50 ]; then
- volume_symbol=""
- else
- volume_symbol=""
- fi
- printf '<span foreground="#ed8796">%s %d%%</span>' "$volume_symbol" "$volume"
-}
-
-network() {
- ssid=$(iwgetid -r)
- if [ -n "$ssid" ]; then
- symbol="󰖩 "
- else
- symbol="󰖪"
- fi
- printf '<span foreground="#f5a97f">%s %s</span>' "$symbol" "$ssid"
-}
-
-brightness() {
- brightness=$(brightnessctl -m | awk -F, '{ print $4 }')
- printf '<span foreground="#eed49f"> %s</span>' "$brightness"
-}
-
-battery() {
- status=$(cat /sys/class/power_supply/BAT0/status)
- capacity=$(cat /sys/class/power_supply/BAT0/capacity)
-
- idx=$(((capacity - 1) / 10 + 1))
- case "$status" in
- "Discharging")
- symbol=$(echo '<span foreground="#ed8796">󰂃</span>,󰁻,󰁼,󰁽,󰁾,󰁿,󰂀,󰂁,󰂂,󰁹' |
- awk -F, "{ print \$$idx }")
- ;;
- "Charging")
- symbol=$(echo "󰢜 󰂆 󰂇 󰂈 󰢝 󰂉 󰢞 󰂊 󰂋 󰂅" | awk "{ print \$$idx }")
- ;;
- "Full")
- symbol="󰂄"
- ;;
- esac
- printf '<span foreground="#a6da95">%s %d%%</span>' "$symbol" "$capacity"
-}
-
-clock() {
- printf '<span foreground="#7dc4e4"> %s</span>' "$(date '+%b %d %H:%M')"
-}
-
-while true; do
- somebar -c status "<span foreground=\"#cad3f5\">$(volume) | $(network) | $(brightness) | $(battery) | $(clock)</span>"
- sleep 0.2
-done
+#!/usr/bin/env python3
+
+import subprocess
+import time
+import re
+
+
+def span(color, *args):
+ return f'<span foreground="{color}">{" ".join(args)}</span>'
+
+
+def volume():
+ sink_info = subprocess.check_output(
+ ['pactl', 'get-sink-volume', '@DEFAULT_SINK@']).decode('utf-8')
+ volume = int(re.search('\s(\d+)%', sink_info).group(1))
+ mute = subprocess.check_output(
+ ['pactl', 'get-sink-mute', '@DEFAULT_SINK@']).decode('utf-8')
+
+ symbol = ''
+ if mute == 'Mute: yes':
+ symbol = '󰝟'
+ elif volume == 0:
+ symbol = ''
+ elif volume <= 50:
+ symbol = ''
+
+ return span('#ed8796', symbol, str(volume) + '%')
+
+
+def network():
+ ssid = subprocess.check_output(['iwgetid', '-r']).decode('utf-8').strip()
+ symbol = '󰖪 ' if ssid == '' else '󰖩 '
+ return span('#f5a97f', symbol, ssid)
+
+
+def brightness():
+ brightness = subprocess.check_output(['brightnessctl',
+ '-m']).decode('utf-8').split(',')[3]
+ return span('#eed49f', '', brightness)
+
+
+def battery():
+ with open('/sys/class/power_supply/BAT0/capacity', 'r') as file:
+ capacity = int(file.read())
+ with open('/sys/class/power_supply/BAT0/status', 'r') as file:
+ status = file.read().strip()
+
+ symbol = '󰁹'
+ index = (capacity - 1) // 10 + 1
+ if status == 'Discharging':
+ symbol = [
+ span('#ed8796', '󰂃'), '󰁻', '󰁼', '󰁽', '󰁾', '󰁿', '󰂀', '󰂁', '󰂂', '󰁹'
+ ][index]
+ elif status == 'Charging':
+ symbol = ['󰢜', '󰂆', '󰂇', '󰂈', '󰢝', '󰂉', '󰢞', '󰂊', '󰂋', '󰂅'][index]
+ elif status == 'Full':
+ symbol = '󰂄'
+ return span('#a6da95', symbol, str(capacity) + '%')
+
+
+def clock():
+ return span('#7dc4e4', '', time.strftime("%b %d %H:%M"))
+
+prev = ''
+while True:
+ try:
+ status = ' | '.join([volume(), network(), brightness(), battery(), clock()])
+ status = span('#cad3f5', status)
+ if status != prev:
+ subprocess.run(['somebar', '-c', 'status', status])
+ prev = status
+ except Exception as e:
+ print(e)
+ time.sleep(1 - time.time() % 1)