aboutsummaryrefslogtreecommitdiff
path: root/helpers.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-06 03:11:54 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-06 03:11:54 +0100
commitad14818d4ec680bbe4f0736bf9d653d4230dd585 (patch)
tree5ce34102a086d99641ff67eee4fd4ad6df3b82bf /helpers.lua
parent0ab8311b02d54e22b80ced1bc1d0dde26490ba32 (diff)
downloadvicious-legacy-ad14818d4ec680bbe4f0736bf9d653d4230dd585.tar.xz
helpers: import capitalize
This helper will capitalize the first letter of every word in a given string. It'll be useful for some widget string which look out of place otherwise, like "rain, snow" (<- where did this come from?). But it can also be useful for people that like to use this format, camel case or simillar.
Diffstat (limited to 'helpers.lua')
-rw-r--r--helpers.lua19
1 files changed, 13 insertions, 6 deletions
diff --git a/helpers.lua b/helpers.lua
index ccbfe7a..8b6541d 100644
--- a/helpers.lua
+++ b/helpers.lua
@@ -12,8 +12,7 @@ local pairs = pairs
local io = { open = io.open }
local setmetatable = setmetatable
local string = {
- sub = string.sub,
- gsub = string.gsub,
+ upper = string.upper,
format = string.format
}
-- }}}
@@ -46,7 +45,7 @@ end
-- {{{ Format a string with args
function format(format, args)
for var, val in pairs(args) do
- format = string.gsub(format, "$" .. var, val)
+ format = format:gsub("$" .. var, val)
end
return format
@@ -77,12 +76,20 @@ function escape(text)
end
-- }}}
+-- {{{ Capitalize a string
+function capitalize(text)
+ return text and text:gsub("([%w])([%w]*)", function(c, s)
+ return string.upper(c) .. s
+ end)
+end
+-- }}}
+
-- {{{ Truncate a string
function truncate(text, maxlen)
local txtlen = text:len()
if txtlen > maxlen then
- text = string.sub(text, 1, maxlen - 3) .. "..."
+ text = text:sub(1, maxlen - 3) .. "..."
end
return text
@@ -100,14 +107,14 @@ function scroll(text, maxlen, widget)
if txtlen > maxlen then
if state.d then
- text = string.sub(text, state.i, state.i + maxlen) .. "..."
+ text = text:sub(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)
+ text = "..." .. text:sub(state.i, state.i + maxlen)
state.i = state.i - 3
if state.i <= 1 then