From 589feb1ce937ae166695bb2ec2b0c85398cf1b5b Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Tue, 3 Nov 2009 01:09:46 +0100 Subject: [PATCH] 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. --- helpers.lua | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file 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. +-- * (c) 2009, Adrian C. +-- * (c) 2009, Rémy C. -- * (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 = { ["\""] = """, @@ -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 +-- }}} +-- }}}