summaryrefslogtreecommitdiff
path: root/bin/statusbar
blob: 8d5dc6f980e3a0b03e5c10e40861795cbd8ceddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/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)