aboutsummaryrefslogtreecommitdiff
path: root/helpers.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2009-08-05 22:11:11 +0200
committerAdrian C. (anrxc) <anrxc@sysphere.org>2009-08-05 22:11:11 +0200
commit3c76e0ddd2083db5074e3cce644270cb84098c60 (patch)
tree923238e2ed8c422f7e7f95b2e2a2b7730ab32af4 /helpers.lua
parent2d0cbf562ed19b4d68dd4d7325d912d104ed71aa (diff)
downloadvicious-legacy-3c76e0ddd2083db5074e3cce644270cb84098c60.tar.xz
Introduced the truncate helper.
Function takes two arguments, the text to be truncated and the max lenght. Last three characters will be replaced by "...". Mbox and MPD widgets that previously did it them selves are now using this helper.
Diffstat (limited to 'helpers.lua')
-rw-r--r--helpers.lua17
1 files changed, 16 insertions, 1 deletions
diff --git a/helpers.lua b/helpers.lua
index 874de73..cc5d823 100644
--- a/helpers.lua
+++ b/helpers.lua
@@ -5,7 +5,10 @@
-- {{{ Grab environment
local pairs = pairs
-local string = { gsub = string.gsub }
+local string = {
+ sub = string.sub,
+ gsub = string.gsub
+}
-- }}}
@@ -38,4 +41,16 @@ function escape(text)
return text and text:gsub("[\"&'<>]", xml_entities)
end
-- }}}
+
+--{{{ Truncate a string
+function truncate(text, maxlen)
+ txtlen = text:len()
+
+ if txtlen > maxlen then
+ text = text:sub(1, maxlen - 3) .. "..."
+ end
+
+ return text
+end
+-- }}}
-- }}}