Initial conky_printf commit

This commit is contained in:
Trey Blancher 2020-10-25 17:48:58 -04:00
parent 12fe560b47
commit c571a09f4b
2 changed files with 126 additions and 11 deletions

View File

@ -39,10 +39,11 @@ conky.config = {
conky.text =
[[\
RAM:${lua_parse fmt %3.0f%% ${memperc}} \
Swap:${lua_parse fmt %3.0f%% ${swapperc}} \
CPU:${lua_parse fmt %3.0f%% ${cpu cpu0}} \
RAM:${lua_parse printf %3.0f%% ${memperc}} \
Swap:${lua_parse printf %3.0f%% ${swapperc}} \
CPU:${lua_parse printf %3.0f%% ${cpu cpu0}} \
/ ${fs_used /}/${fs_size /} \
${loadavg} \
${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1} \
Net: ${lua_parse fmt2 %10s▲%10s▼ ${upspeed wlp4s0} ${downspeed wlp4s0}}
Net: ${lua_parse printf %10s▲%10s▼ ${upspeed wlp4s0} ${downspeed wlp4s0}} \
]];

View File

@ -2,13 +2,127 @@ function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function conky_fmt(format, a)
return string.format(format, tonumber(trim(conky_parse(a))))
function split_fmt(f)
if not type(f) == "string" then
io.stderr:write("ERROR! Argument '" .. format .. "' is not a string.")
return nil
end
function conky_fmt2(format, a, b)
as = conky_parse(a)
bs = conky_parse(b)
return string.format(format, as, bs)
local ret = {}
local curr = ""
local in_spec = false
for char in string.gmatch(f, ".") do
if in_spec then
if string.match(char, '[cdEefgiouXx]') then
curr = curr .. char
table.insert(ret, curr)
in_spec = false
curr = ""
elseif string.match(char, '[qs]') then
if string.match(curr, '^%%[0-9]*$') then
curr = curr .. char
table.insert(ret, curr)
in_spec = false
curr = ""
else
io.stderr:write("Invalid format: '" .. curr .. char .. "'\n")
return nil
end
elseif string.match(char, '[0-9%.]') then
curr = curr .. char
elseif string.match(char, '%%') then
if string.match(curr, '^%%') then
curr = curr .. char
table.insert(ret, curr)
in_spec = false
curr = ""
else
io.stderr:write("Invalid format: '" .. curr .. char .. "'\n")
return nil
end
else
io.stderr:write("Invalid format: '" .. curr .. char .. "'\n")
return nil
end
else
if char == '%' then
in_spec = true
if curr ~= "" then
table.insert(ret, curr)
end
curr = char
else
curr = curr .. char
end
end
end
if curr ~= "" then
table.insert(ret, curr)
end
return ret
end
function table_length(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
function count_formats(formats)
if type(formats) ~= 'table' then
io.stderr:write("ERROR! formats is not a table.\n")
return nil
end
local count = 0
for i,v in ipairs(formats) do
if string.match(v, '^%%') and not string.match(v, '%%$') then
count = count + 1
end
end
return count
end
function is_fmt_spec(str)
return string.match(str, "^%%")
end
function conky_printf(format, ...)
formats = split_fmt(format)
values = {...}
if count_formats(formats) ~= table_length(values) then
io.stderr:write("ERROR! Number of format specifiers to printf doesn't match the number of values passed.\n")
return nil
end
local parsed_values = {}
for i,v in ipairs(values) do
parsed_values[i] = conky_parse(v)
end
for i,fs in ipairs(formats) do
if is_fmt_spec(fs) then -- we have a format specifier
if string.match(fs, '[dEefgiouXx]$') then -- we have a numeric format specifier, convert value to number
parsed_values[i] = tonumber(trim((parsed_values[i])))
end
end
end
local formatted_output = ""
local idx = 1
for i,fs in ipairs(formats) do
if is_fmt_spec(fs) then
if not string.match(fs, '%%$') then
formatted_output = formatted_output .. string.format(fs, parsed_values[idx])
idx = idx + 1
else
formatted_output = formatted_output .. '%'
end
else
formatted_output = formatted_output .. fs
end
end
return formatted_output
end