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:
parent
0d73f6d8ae
commit
589feb1ce9
42
helpers.lua
42
helpers.lua
|
@ -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
|
||||
|
@ -46,9 +52,41 @@ 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
|
||||
-- }}}
|
||||
-- }}}
|
||||
|
|
Loading…
Reference in New Issue