summaryrefslogtreecommitdiff
path: root/bin/statusline
blob: 948e8cb6a1c1fe48ec754196f35a13af57cfa000 (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
75
76
77
78
79
80
81
#!/usr/bin/env lua5.4

local stdio = require('posix.stdio')
local poll = require('posix.poll')

function get_output(prog)
	local f = assert(io.popen(prog))
	local output = f:read('*a')
	f:close()
	return output
end

function volume()
	local output = get_output('wpctl get-volume @DEFAULT_AUDIO_SINK@')
	local muted = output:find('MUTED') ~= nil
	local _, _, volume = output:find('^Volume: ([0-9.]+)')
	volume = math.floor(volume * 100)

	local symbol = 'ERROR'
	if muted then
		symbol = ' '
	elseif volume == 0 then
		symbol = ''
	elseif volume <= 50 then
		symbol = ''
	else
		symbol = ' '
	end
	return string.format('%s %d%%', symbol, volume)
end

function network()
	local output = get_output('iwctl station wlan0 get-networks')
	local _, _, ssid = output:find('>.-  (.-)  ')
	return ssid and '󰖩  ' .. ssid or '󰖪 '
end

local current_brightness = 0

function brightness()
	return ' ' .. current_brightness .. '%'
end

function battery()
	local f = io.open('/sys/class/power_supply/BAT0/capacity', 'r')
	local capacity = tonumber(f:read('*a'))
	f:close()
	f = io.open('/sys/class/power_supply/BAT0/status', 'r')
	local status = f:read('*a')
	f:close()

	local symbol = 'ERROR'
	local i = capacity > 0 and (capacity - 1) // 10 or 0
	if status == 'Discharging\n' then
		symbol = ({"󰁺", "󰁻", "󰁼", "󰁽", "󰁾", "󰁿", "󰂀", "󰂁", "󰂂", "󰁹"})[i] -- 󰂃
	elseif status == 'Charging\n' then
		symbol = ({"󰢜", "󰂆", "󰂇", "󰂈", "󰢝", "󰂉", "󰢞", "󰂊", "󰂋", "󰂅"})[i]
	elseif status == 'Full\n' then
		symbol = "󰂄"
	end
	return symbol .. ' ' .. capacity .. '%'
end

function clock()
	return os.date('  %b %d %H:%M:%S')
end

local brightness_file = assert(io.popen('brightctl -l'))

while true do
	if poll.rpoll(stdio.fileno(brightness_file), 1000) > 0 then
		current_brightness = tonumber(brightness_file:read('l'))
	end
	io.write(string.format('all status %s | %s | %s | %s | %s\n',
		volume(),
		network(),
		brightness(),
		battery(),
		clock()
	))
end