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.
This commit is contained in:
parent
2d0cbf562e
commit
3c76e0ddd2
17
helpers.lua
17
helpers.lua
|
@ -5,7 +5,10 @@
|
||||||
|
|
||||||
-- {{{ Grab environment
|
-- {{{ Grab environment
|
||||||
local pairs = pairs
|
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)
|
return text and text:gsub("[\"&'<>]", xml_entities)
|
||||||
end
|
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
|
||||||
|
-- }}}
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
4
mbox.lua
4
mbox.lua
|
@ -36,9 +36,7 @@ function worker(format, mbox)
|
||||||
subject = helpers.escape(subject)
|
subject = helpers.escape(subject)
|
||||||
|
|
||||||
-- Don't abuse the wibox, truncate
|
-- Don't abuse the wibox, truncate
|
||||||
if subject:len() > 22 then
|
subject = helpers.truncate(subject, 22)
|
||||||
subject = subject:sub(1, 19) .. "..."
|
|
||||||
end
|
|
||||||
|
|
||||||
return {subject}
|
return {subject}
|
||||||
end
|
end
|
||||||
|
|
16
mpd.lua
16
mpd.lua
|
@ -34,12 +34,22 @@ function worker(format)
|
||||||
nowplaying = helpers.escape(np)
|
nowplaying = helpers.escape(np)
|
||||||
|
|
||||||
-- Don't abuse the wibox, truncate
|
-- Don't abuse the wibox, truncate
|
||||||
if nowplaying:len() > 30 then
|
nowplaying = helpers.truncate(nowplaying, 30)
|
||||||
nowplaying = nowplaying:sub(1, 27) .. "..."
|
|
||||||
end
|
|
||||||
|
|
||||||
return {nowplaying}
|
return {nowplaying}
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
|
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue