helpers: import scroll from Remy Clouard

First scroll helper, that provides an alternative to truncate. Text
shifts every time the widget is updated, there is no internal timer.
This commit is contained in:
Adrian C. (anrxc) 2009-11-03 01:09:46 +01:00
parent 0d73f6d8ae
commit 589feb1ce9
1 changed files with 42 additions and 4 deletions

View File

@ -1,6 +1,7 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2009, Adrian C. <anrxc.sysphere.org>
-- * (c) 2009, Adrian C. <anrxc@sysphere.org>
-- * (c) 2009, Rémy C. <shikamaru@mandriva.org>
-- * (c) Wicked, Lucas de Vries
---------------------------------------------------
@ -17,6 +18,11 @@ local string = {
module("vicious.helpers")
-- {{{ Variable definitions
local scroller = {}
-- }}}
-- {{{ Helper functions
-- {{{ Format a string with args
function format(format, args)
for var, val in pairs(args) do
@ -27,7 +33,7 @@ function format(format, args)
end
-- }}}
--{{{ Escape a string
-- {{{ Escape a string
function escape(text)
local xml_entities = {
["\""] = "&quot;",
@ -41,14 +47,46 @@ function escape(text)
end
-- }}}
--{{{ Truncate a string
-- {{{ Truncate a string
function truncate(text, maxlen)
local txtlen = text:len()
if txtlen > maxlen then
text = text:sub(1, maxlen - 3) .. "..."
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
-- }}}
-- }}}