vicious/helpers.lua

111 lines
2.4 KiB
Lua

---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2009, Adrian C. <anrxc@sysphere.org>
-- * (c) 2009, Rémy C. <shikamaru@mandriva.org>
-- * (c) 2009, Benedikt Sauer <filmor@gmail.com>
-- * (c) 2008, Lucas de Vries <lucas@glacicle.com>
---------------------------------------------------
-- {{{ Grab environment
local pairs = pairs
local io = { open = io.open }
local setmetatable = setmetatable
local string = {
sub = string.sub,
gsub = string.gsub
}
-- }}}
-- Helpers: provides helper functions for vicious widgets
module("vicious.helpers")
-- {{{ Variable definitions
local scroller = {}
-- }}}
-- {{{ Helper functions
-- {{{ Expose path as a Lua table
function pathtotable(path)
return setmetatable({},
{ __index = function(_, name)
local f = io.open(path .. '/' .. name)
if f then
local s = f:read("*all")
f:close()
return s
end
end
})
end
-- }}}
-- {{{ Format a string with args
function format(format, args)
for var, val in pairs(args) do
format = string.gsub(format, "$" .. var, val)
end
return format
end
-- }}}
-- {{{ Escape a string
function escape(text)
local xml_entities = {
["\""] = "&quot;",
["&"] = "&amp;",
["'"] = "&apos;",
["<"] = "&lt;",
[">"] = "&gt;"
}
return text and text:gsub("[\"&'<>]", xml_entities)
end
-- }}}
-- {{{ Truncate a string
function truncate(text, maxlen)
local txtlen = text:len()
if txtlen > maxlen then
text = string.sub(text, 1, maxlen - 3) .. "..."
end
return text
end
-- }}}
-- {{{ Scroll through a string
function scroll(text, maxlen, widget)
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 = string.sub(text, state.i, state.i + maxlen) .. "..."
state.i = state.i + 3
if maxlen + state.i >= txtlen then
state.d = false
end
else
text = "..." .. string.sub(text, state.i, state.i + maxlen)
state.i = state.i - 3
if state.i <= 1 then
state.d = true
end
end
end
return text
end
-- }}}
-- }}}