#!/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