From 8308de4e6cb44a84bd326cccd74a1017648aaabd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 25 May 2025 19:14:21 +0530 Subject: modified dwm luastatus scripts --- .config/luastatus-scripts-dwm/alsa-gauge.lua | 18 +++++++ .config/luastatus-scripts-dwm/alsa.lua | 11 ++++ .config/luastatus-scripts-dwm/backlight.lua | 2 +- .config/luastatus-scripts-dwm/battery.lua | 2 +- .config/luastatus-scripts-dwm/bluetooth.lua | 5 +- .config/luastatus-scripts-dwm/fs.lua | 14 ++++++ .config/luastatus-scripts-dwm/ip.lua | 20 ++++++++ .config/luastatus-scripts-dwm/mem-usage.lua | 2 +- .config/luastatus-scripts-dwm/mpd.lua | 32 ++++++++++++ .config/luastatus-scripts-dwm/network.lua | 72 +++++++++++++++++++++++++++ .config/luastatus-scripts-dwm/pulse-gauge.lua | 18 +++++++ .config/luastatus-scripts-dwm/pulse.lua | 10 ++++ .config/luastatus-scripts-dwm/tor.lua | 13 +++++ .config/luastatus-scripts-dwm/weather.lua | 67 +++++++++++++++++++++++++ .config/luastatus-scripts-dwm/wireless.lua | 56 --------------------- .config/luastatus-scripts-dwm/xkb.lua | 17 +++++++ 16 files changed, 298 insertions(+), 61 deletions(-) create mode 100644 .config/luastatus-scripts-dwm/alsa-gauge.lua create mode 100644 .config/luastatus-scripts-dwm/alsa.lua create mode 100644 .config/luastatus-scripts-dwm/fs.lua create mode 100644 .config/luastatus-scripts-dwm/ip.lua create mode 100644 .config/luastatus-scripts-dwm/mpd.lua create mode 100644 .config/luastatus-scripts-dwm/network.lua create mode 100644 .config/luastatus-scripts-dwm/pulse-gauge.lua create mode 100644 .config/luastatus-scripts-dwm/pulse.lua create mode 100644 .config/luastatus-scripts-dwm/tor.lua create mode 100644 .config/luastatus-scripts-dwm/weather.lua delete mode 100644 .config/luastatus-scripts-dwm/wireless.lua create mode 100644 .config/luastatus-scripts-dwm/xkb.lua (limited to '.config/luastatus-scripts-dwm') diff --git a/.config/luastatus-scripts-dwm/alsa-gauge.lua b/.config/luastatus-scripts-dwm/alsa-gauge.lua new file mode 100644 index 0000000..966e625 --- /dev/null +++ b/.config/luastatus-scripts-dwm/alsa-gauge.lua @@ -0,0 +1,18 @@ +local GAUGE_NCHARS = 10 + +local function mk_gauge(level, full, empty) + local nfull = math.floor(level * GAUGE_NCHARS + 0.5) + return full:rep(nfull) .. empty:rep(GAUGE_NCHARS - nfull) +end + +widget = { + plugin = 'alsa', + cb = function(t) + local level = (t.vol.cur - t.vol.min) / (t.vol.max - t.vol.min) + if t.mute then + return mk_gauge(level, '×', '—') + else + return mk_gauge(level, '●', '○') + end + end, +} diff --git a/.config/luastatus-scripts-dwm/alsa.lua b/.config/luastatus-scripts-dwm/alsa.lua new file mode 100644 index 0000000..054a806 --- /dev/null +++ b/.config/luastatus-scripts-dwm/alsa.lua @@ -0,0 +1,11 @@ +widget = { + plugin = 'alsa', + cb = function(t) + if t.mute then + return '[mute]' + else + local percent = (t.vol.cur - t.vol.min) / (t.vol.max - t.vol.min) * 100 + return string.format('[%3d%%]', math.floor(0.5 + percent)) + end + end, +} diff --git a/.config/luastatus-scripts-dwm/backlight.lua b/.config/luastatus-scripts-dwm/backlight.lua index 3fa1818..37267ed 100644 --- a/.config/luastatus-scripts-dwm/backlight.lua +++ b/.config/luastatus-scripts-dwm/backlight.lua @@ -12,7 +12,7 @@ widget = luastatus.require_plugin('backlight-linux').widget{ end io.write(level * 100) - return string.format('%s %3.0f%%', icon, brightness) + return string.format(' %s %3.0f%%', icon, brightness) end end, } diff --git a/.config/luastatus-scripts-dwm/battery.lua b/.config/luastatus-scripts-dwm/battery.lua index 6155fd4..377a30e 100644 --- a/.config/luastatus-scripts-dwm/battery.lua +++ b/.config/luastatus-scripts-dwm/battery.lua @@ -48,6 +48,6 @@ widget = luastatus.require_plugin('battery-linux').widget{ end end - return string.format('%s %3d%%', icon, t.capacity) + return string.format('%s %3d%% ', icon, t.capacity) end, } diff --git a/.config/luastatus-scripts-dwm/bluetooth.lua b/.config/luastatus-scripts-dwm/bluetooth.lua index f4807a0..baf4b8c 100644 --- a/.config/luastatus-scripts-dwm/bluetooth.lua +++ b/.config/luastatus-scripts-dwm/bluetooth.lua @@ -99,7 +99,8 @@ function reprint_devices(power_state) if power_state then local t = {} for mac_address, device in pairs(devices) do - table.insert(t, string.format("󰂱 %s", device["alias"])) + -- table.insert(t, string.format("󰂱 %s", device["alias"])) + return "󰂱 " end if next(t) == nil then return "󰂯" @@ -172,6 +173,6 @@ widget = { end end end - return reprint_devices(power_state) + return " " .. reprint_devices(power_state) end } diff --git a/.config/luastatus-scripts-dwm/fs.lua b/.config/luastatus-scripts-dwm/fs.lua new file mode 100644 index 0000000..e780dc6 --- /dev/null +++ b/.config/luastatus-scripts-dwm/fs.lua @@ -0,0 +1,14 @@ +widget = { + plugin = 'fs', + opts = { + paths = {'/', '/home'}, + }, + cb = function(t) + local res = {} + for k, v in pairs(t) do + table.insert(res, string.format('%s %.0f%%', k, + (1 - v.avail / v.total) * 100)) + end + return res + end, +} diff --git a/.config/luastatus-scripts-dwm/ip.lua b/.config/luastatus-scripts-dwm/ip.lua new file mode 100644 index 0000000..37bc43a --- /dev/null +++ b/.config/luastatus-scripts-dwm/ip.lua @@ -0,0 +1,20 @@ +widget = { + plugin = 'network-linux', + cb = function(t) + local r = {} + for iface, params in pairs(t) do + local addr = params.ipv4 + if addr then + -- strip out "label" from the interface name + iface = iface:gsub(':.*', '') + -- strip out "zone index" from the address + addr = addr:gsub('%%.*', '') + + if iface ~= 'lo' then + r[#r + 1] = string.format('[%s: %s]', iface, addr) + end + end + end + return r + end, +} diff --git a/.config/luastatus-scripts-dwm/mem-usage.lua b/.config/luastatus-scripts-dwm/mem-usage.lua index ad69c2e..1c34219 100644 --- a/.config/luastatus-scripts-dwm/mem-usage.lua +++ b/.config/luastatus-scripts-dwm/mem-usage.lua @@ -2,6 +2,6 @@ widget = luastatus.require_plugin('mem-usage-linux').widget{ timer_opts = {period = 2}, cb = function(t) local used_kb = t.total.value - t.avail.value - return string.format(' %3.2f GiB', used_kb / 1024 / 1024) + return string.format('  %3.2f GiB ', used_kb / 1024 / 1024) end, } diff --git a/.config/luastatus-scripts-dwm/mpd.lua b/.config/luastatus-scripts-dwm/mpd.lua new file mode 100644 index 0000000..32cddc6 --- /dev/null +++ b/.config/luastatus-scripts-dwm/mpd.lua @@ -0,0 +1,32 @@ +-- you need to install 'utf8' module (e.g. with luarocks) if using Lua <=5.2. +utf8 = require 'utf8' + +titlewidth = 40 + +widget = { + plugin = 'mpd', + cb = function(t) + if t.what == 'update' then + local title + if t.song.Title then + title = t.song.Title + if t.song.Artist then + title = t.song.Artist .. ': ' .. title + end + else + title = t.song.file or '' + end + title = (utf8.len(title) <= titlewidth) + and title + or utf8.sub(title, 1, titlewidth - 1) .. '…' + + return string.format('%s %s', + ({play = '▶', pause = '‖', stop = '■'})[t.status.state], + title + ) + else + -- 'connecting' or 'error' + return t.what + end + end +} diff --git a/.config/luastatus-scripts-dwm/network.lua b/.config/luastatus-scripts-dwm/network.lua new file mode 100644 index 0000000..4ec808a --- /dev/null +++ b/.config/luastatus-scripts-dwm/network.lua @@ -0,0 +1,72 @@ +local MIN_DBM, MAX_DBM = -90, -20 +local NGAUGE = 5 +local COLOR_DIM = '#709080' + +local function round(x) + return math.floor(x + 0.5) +end + +local function make_wifi_gauge(dbm) + if dbm < MIN_DBM then dbm = MIN_DBM end + if dbm > MAX_DBM then dbm = MAX_DBM end + local nbright = round(NGAUGE * (1 - 0.7 * (MAX_DBM - dbm) / (MAX_DBM - MIN_DBM))) + + if nbright == 0 then + return "󰤭" + elseif nbright == 1 then + return "󰤯" + elseif nbright == 2 then + return "󰤟" + elseif nbright == 3 then + return "󰤢" + elseif nbright == 4 then + return "󰤥" + else + return "󰤨" + end +end + +widget = { + plugin = 'network-linux', + opts = { + wireless = true, + timeout = 10, + }, + cb = function(t) + if not t then + return nil + end + + local x = "" + local sep = " " + + if t.eno1 then + if t.eno1.ipv4 or t.eno1.ipv6 then + x = "" + end + end + + if t.enp5s0f3u1 then + if x ~= "" then + x = x .. sep + end + if t.enp5s0f3u1.ipv4 or t.enp5s0f3u1.ipv6 then + x = x .. "󱎔" + end + end + + if t.wlo1.wireless then + if x ~= "" then + x = x .. sep + end + if t.wlo1.wireless.signal_dbm then + x = x .. make_wifi_gauge(t.wlo1.wireless.signal_dbm) + else + x = x .. "󰤭" + end + end + + x = " " .. x .. " " + return x + end, +} diff --git a/.config/luastatus-scripts-dwm/pulse-gauge.lua b/.config/luastatus-scripts-dwm/pulse-gauge.lua new file mode 100644 index 0000000..dbc5c67 --- /dev/null +++ b/.config/luastatus-scripts-dwm/pulse-gauge.lua @@ -0,0 +1,18 @@ +local GAUGE_NCHARS = 10 + +local function mk_gauge(level, full, empty) + local nfull = math.floor(level * GAUGE_NCHARS + 0.5) + return full:rep(nfull) .. empty:rep(GAUGE_NCHARS - nfull) +end + +widget = { + plugin = 'pulse', + cb = function(t) + local level = t.cur / t.norm + if t.mute then + return mk_gauge(level, '×', '—') + else + return mk_gauge(level, '●', '○') + end + end, +} diff --git a/.config/luastatus-scripts-dwm/pulse.lua b/.config/luastatus-scripts-dwm/pulse.lua new file mode 100644 index 0000000..59b73e8 --- /dev/null +++ b/.config/luastatus-scripts-dwm/pulse.lua @@ -0,0 +1,10 @@ +widget = { + plugin = 'pulse', + cb = function(t) + if t.mute then + return '[mute]' + end + local percent = (t.cur / t.norm) * 100 + return string.format('[%3d%%]', math.floor(0.5 + percent)) + end, +} diff --git a/.config/luastatus-scripts-dwm/tor.lua b/.config/luastatus-scripts-dwm/tor.lua new file mode 100644 index 0000000..db97225 --- /dev/null +++ b/.config/luastatus-scripts-dwm/tor.lua @@ -0,0 +1,13 @@ +-- Trivial but somewhat useful widget showing if the Tor daemon is running. + +widget = { + plugin = 'timer', + opts = {period = 5}, + cb = function() + local f = io.open('/var/run/tor/tor.pid', 'r') + if f then + f:close() + return '[TOR]' + end + end, +} diff --git a/.config/luastatus-scripts-dwm/weather.lua b/.config/luastatus-scripts-dwm/weather.lua new file mode 100644 index 0000000..d626689 --- /dev/null +++ b/.config/luastatus-scripts-dwm/weather.lua @@ -0,0 +1,67 @@ +-- you need to install 'luasec' module (e.g. with luarocks) +-- you can look up all available flags here: https://github.com/chubin/wttr.in#one-line-output + +local https = require('ssl.https') +local ltn12 = require('ltn12') + +-- All the arguments except for 'url' may be absent or nil; default method is GET. +-- Returns: code (integer), body (string), headers (table), status (string). +function request(url, headers, method, body) + local out_body = {} + local is_ok, code_or_errmsg, out_headers, status = https.request( + { + url = url, + sink = ltn12.sink.table(out_body), + redirect = false, + cafile = '/etc/ssl/certs/ca-certificates.crt', + verify = 'peer', + method = method, + headers = headers, + }, + body) + assert(is_ok, code_or_errmsg) + return code_or_errmsg, table.concat(out_body), out_headers, status +end + +-- Arguments are the same to those of 'request'. +-- Returns: body (string), headers (table). +function request_check_code(...) + local code, body, headers, status = request(...) + assert(code == 200, string.format('HTTP %s %s', code, status)) + return body, headers +end + +function urlencode(s) + return string.gsub(s, '[^-_.~a-zA-Z0-9]', function(c) + return string.format('%%%02X', string.byte(c)) + end) +end + +local BASE_URL = 'wttr.in' +local LANG = 'en' +local LOCATION = '' + +function get_weather(format) + -- encoding is needed to allow usage of special use characters + format = urlencode(format) + local url = string.format('https://%s.%s/%s?format=%s', LANG, BASE_URL, LOCATION, format) + local is_ok, body = pcall(request_check_code, url) + if is_ok then + return body:gsub("\n", "") + else + return nil + end +end + +widget = { + plugin = 'timer', + opts = {period = 15 * 60}, + cb = function() + local text = get_weather('%l: %C %t(%f)') + if text == nil then + luastatus.plugin.push_period(60) -- retry in 60 seconds + text = '......' + end + return text + end, +} diff --git a/.config/luastatus-scripts-dwm/wireless.lua b/.config/luastatus-scripts-dwm/wireless.lua deleted file mode 100644 index 34300ad..0000000 --- a/.config/luastatus-scripts-dwm/wireless.lua +++ /dev/null @@ -1,56 +0,0 @@ -local MIN_DBM, MAX_DBM = -90, -20 -local NGAUGE = 5 -local COLOR_DIM = '#709080' - -local function round(x) - return math.floor(x + 0.5) -end - -local function make_wifi_gauge(dbm) - if dbm < MIN_DBM then dbm = MIN_DBM end - if dbm > MAX_DBM then dbm = MAX_DBM end - local nbright = round(NGAUGE * (1 - 0.7 * (MAX_DBM - dbm) / (MAX_DBM - MIN_DBM))) - - if nbright == 0 then - return "󰤭" - elseif nbright == 1 then - return "󰤯" - elseif nbright == 2 then - return "󰤟" - elseif nbright == 3 then - return "󰤢" - elseif nbright == 4 then - return "󰤥" - else - return "󰤨" - end -end - -widget = { - plugin = 'network-linux', - opts = { - wireless = true, - timeout = 10, - }, - cb = function(t) - if not t then - return nil - end - local r = {} - for iface, params in pairs(t) do - if params.wireless then - local x = "" - if params.wireless.signal_dbm then - x = x .. make_wifi_gauge(params.wireless.signal_dbm) .. " " - end - if params.wireless.ssid then - x = x .. params.wireless.ssid - end - return x - elseif iface ~= 'lo' and (params.ipv4 or params.ipv6) then - r[#r + 1] = string.format('[%s]', iface) - end - end - return r - end, -} diff --git a/.config/luastatus-scripts-dwm/xkb.lua b/.config/luastatus-scripts-dwm/xkb.lua new file mode 100644 index 0000000..358f0d7 --- /dev/null +++ b/.config/luastatus-scripts-dwm/xkb.lua @@ -0,0 +1,17 @@ +widget = { + plugin = 'xkb', + cb = function(t) + if t.name then + local base_layout = t.name:match('[^(]+') + if base_layout == 'gb' or base_layout == 'us' then + return '[En]' + elseif base_layout == 'ru' then + return '[Ru]' + else + return '[' .. base_layout:sub(1, 1):upper() .. base_layout:sub(2) .. ']' + end + else + return '[? ID ' .. t.id .. ']' + end + end, +} -- cgit v1.2.3