~kris/dots

srice

ref: e9b48d06a8541f3eda5c4db90382ab3c77183afb srice/.config/conky/lean-conky/lib/tform.lua -rw-r--r-- 954 bytes
e9b48d06 — Kris Yotam xprofile: systemd-aware pipewire start + blueman-applet; sb-internet: tolerate missing /proc/net/wireless 2 months ago
                                                                                
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
-- defend Lua version incompatibility, just in case
local utils = require("utils")

local tform = {}
T_ = tform -- T_: global alias for `tform`

-- scale a number
function tform.sc(num, scale)
    scale = scale or lcc.config.scale
    return num * scale
end

-- scale then round, for where only integers are allowed
function tform.sr(num, scale)
    return math.floor(tform.sc(num, scale) + 0.5)
end

-- scale then round to a multiple of 0.5
-- might be useful for certain cases, e.g. font size
function tform.sh(num, scale)
    return math.floor(tform.sc(num * 2, scale) + 0.5) / 2.0
end

-- apply transform functions to rewrite values in a string
-- e.g. "$sc{42}" would be replaced by the value of T_.sc(42)
setmetatable(tform, { __call = function(_, s)
    local ts = s:gsub("$([%w_]+)(%b{})", function(f, args)
        return assert(utils.loadstring("return T_." .. f .. "(" .. args:sub(2, -2) .. ")"))()
    end)
    return ts
end })

return tform