aboutsummaryrefslogtreecommitdiff
path: root/helpers.lua
diff options
context:
space:
mode:
Diffstat (limited to 'helpers.lua')
-rw-r--r--helpers.lua46
1 files changed, 42 insertions, 4 deletions
diff --git a/helpers.lua b/helpers.lua
index 73b5268..3ccebd0 100644
--- a/helpers.lua
+++ b/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
@@ -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
-- }}}
+-- }}}