From 3c76e0ddd2083db5074e3cce644270cb84098c60 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Wed, 5 Aug 2009 22:11:11 +0200 Subject: [PATCH] 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. --- helpers.lua | 17 ++++++++++++++++- mbox.lua | 4 +--- mpd.lua | 16 +++++++++++++--- 3 files changed, 30 insertions(+), 7 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 +-- }}} -- }}} diff --git a/mbox.lua b/mbox.lua index f0a5817..d5c1b56 100644 --- a/mbox.lua +++ b/mbox.lua @@ -36,9 +36,7 @@ function worker(format, mbox) subject = helpers.escape(subject) -- Don't abuse the wibox, truncate - if subject:len() > 22 then - subject = subject:sub(1, 19) .. "..." - end + subject = helpers.truncate(subject, 22) return {subject} end diff --git a/mpd.lua b/mpd.lua index f8c0400..bb193b5 100644 --- a/mpd.lua +++ b/mpd.lua @@ -34,12 +34,22 @@ function worker(format) nowplaying = helpers.escape(np) -- Don't abuse the wibox, truncate - if nowplaying:len() > 30 then - nowplaying = nowplaying:sub(1, 27) .. "..." - end + nowplaying = helpers.truncate(nowplaying, 30) return {nowplaying} end -- }}} setmetatable(_M, { __call = function(_, ...) return worker(...) end }) + + + + + + + + + + + +