2017-12-01 00:21:33 -06:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
|
|
|
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
|
|
|
|
-- * (c) 2009, Rémy C. <shikamaru@mandriva.org>
|
|
|
|
-- * (c) 2009, Benedikt Sauer <filmor@gmail.com>
|
|
|
|
-- * (c) 2009, Henning Glawe <glaweh@debian.org>
|
|
|
|
-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local pairs = pairs
|
|
|
|
local rawget = rawget
|
|
|
|
local require = require
|
|
|
|
local tonumber = tonumber
|
|
|
|
local io = { open = io.open }
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local getmetatable = getmetatable
|
|
|
|
local string = {
|
2017-12-09 21:23:04 -06:00
|
|
|
upper = string.upper,
|
|
|
|
format = string.format
|
2017-12-01 00:21:33 -06:00
|
|
|
}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Helpers: provides helper functions for vicious widgets
|
|
|
|
-- vicious.helpers
|
|
|
|
local helpers = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Variable definitions
|
|
|
|
local scroller = {}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Helper functions
|
|
|
|
-- {{{ Loader of vicious modules
|
|
|
|
function helpers.wrequire(table, key)
|
2017-12-09 21:23:04 -06:00
|
|
|
local module = rawget(table, key)
|
|
|
|
return module or require(table._NAME .. "." .. key)
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Expose path as a Lua table
|
|
|
|
function helpers.pathtotable(dir)
|
2017-12-09 21:23:04 -06:00
|
|
|
return setmetatable({ _path = dir },
|
|
|
|
{ __index = function(table, index)
|
|
|
|
local path = table._path .. '/' .. index
|
|
|
|
local f = io.open(path)
|
|
|
|
if f then
|
|
|
|
local s = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
if s then
|
|
|
|
return s
|
|
|
|
else
|
|
|
|
local o = { _path = path }
|
|
|
|
setmetatable(o, getmetatable(table))
|
|
|
|
return o
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
2017-12-09 21:23:04 -06:00
|
|
|
end
|
|
|
|
})
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Format a string with args
|
|
|
|
function helpers.format(format, args)
|
2017-12-09 21:23:04 -06:00
|
|
|
for var, val in pairs(args) do
|
|
|
|
format = format:gsub("$" .. (tonumber(var) and var or
|
|
|
|
var:gsub("[-+?*]", function(i) return "%"..i end)),
|
|
|
|
val)
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-09 21:23:04 -06:00
|
|
|
return format
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Format units to one decimal point
|
|
|
|
function helpers.uformat(array, key, value, unit)
|
2017-12-09 21:23:04 -06:00
|
|
|
for u, v in pairs(unit) do
|
|
|
|
array["{"..key.."_"..u.."}"] = string.format("%.1f", value/v)
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-09 21:23:04 -06:00
|
|
|
return array
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Escape a string
|
|
|
|
function helpers.escape(text)
|
2017-12-09 21:23:04 -06:00
|
|
|
local xml_entities = {
|
|
|
|
["\""] = """,
|
|
|
|
["&"] = "&",
|
|
|
|
["'"] = "'",
|
|
|
|
["<"] = "<",
|
|
|
|
[">"] = ">"
|
|
|
|
}
|
|
|
|
|
|
|
|
return text and text:gsub("[\"&'<>]", xml_entities)
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Escape a string for save usage on the command line
|
|
|
|
function helpers.shellquote(s)
|
2017-12-09 21:23:04 -06:00
|
|
|
if s == nil then return "" end
|
|
|
|
-- use single quotes, and put single quotes into double quotes
|
|
|
|
-- the string $'b is then quoted as '$'"'"'b'"'"'
|
|
|
|
return "'" .. s:gsub("'", "'\"'\"'") .. "'"
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Capitalize a string
|
|
|
|
function helpers.capitalize(text)
|
2017-12-09 21:23:04 -06:00
|
|
|
return text and text:gsub("([%w])([%w]*)", function(c, s)
|
|
|
|
return string.upper(c) .. s
|
|
|
|
end)
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Truncate a string
|
|
|
|
function helpers.truncate(text, maxlen)
|
2017-12-09 21:23:04 -06:00
|
|
|
local txtlen = text:len()
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-09 21:23:04 -06:00
|
|
|
if txtlen > maxlen then
|
|
|
|
text = text:sub(1, maxlen - 3) .. "..."
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-09 21:23:04 -06:00
|
|
|
return text
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Scroll through a string
|
|
|
|
function helpers.scroll(text, maxlen, widget)
|
2017-12-09 21:23:04 -06:00
|
|
|
if not scroller[widget] then
|
|
|
|
scroller[widget] = { i = 1, d = true }
|
|
|
|
end
|
|
|
|
|
|
|
|
local txtlen = text:len()
|
|
|
|
local state = scroller[widget]
|
|
|
|
|
|
|
|
if txtlen > maxlen then
|
|
|
|
if state.d then
|
|
|
|
text = text:sub(state.i, state.i + maxlen) .. "..."
|
|
|
|
state.i = state.i + 3
|
|
|
|
|
|
|
|
if maxlen + state.i >= txtlen then
|
|
|
|
state.d = false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
text = "..." .. text:sub(state.i, state.i + maxlen)
|
|
|
|
state.i = state.i - 3
|
|
|
|
|
|
|
|
if state.i <= 1 then
|
|
|
|
state.d = true
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
2017-12-09 21:23:04 -06:00
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-09 21:23:04 -06:00
|
|
|
return text
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
return helpers
|
|
|
|
|
|
|
|
-- }}}
|