aboutsummaryrefslogtreecommitdiff
path: root/helpers.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2009-11-03 01:09:46 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2009-11-03 01:09:46 +0100
commit589feb1ce937ae166695bb2ec2b0c85398cf1b5b (patch)
tree7ce31727bfac0242e3a9791c9bc337e4c475dcd1 /helpers.lua
parent0d73f6d8ae32f1cd48ce9f089b902eb0877605e1 (diff)
downloadvicious-legacy-589feb1ce937ae166695bb2ec2b0c85398cf1b5b.tar.xz
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.
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
-- }}}
+-- }}}