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.
This commit is contained in:
parent
0ab8311b02
commit
ad14818d4e
19
helpers.lua
19
helpers.lua
|
@ -12,8 +12,7 @@ local pairs = pairs
|
||||||
local io = { open = io.open }
|
local io = { open = io.open }
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local string = {
|
local string = {
|
||||||
sub = string.sub,
|
upper = string.upper,
|
||||||
gsub = string.gsub,
|
|
||||||
format = string.format
|
format = string.format
|
||||||
}
|
}
|
||||||
-- }}}
|
-- }}}
|
||||||
|
@ -46,7 +45,7 @@ end
|
||||||
-- {{{ Format a string with args
|
-- {{{ Format a string with args
|
||||||
function format(format, args)
|
function format(format, args)
|
||||||
for var, val in pairs(args) do
|
for var, val in pairs(args) do
|
||||||
format = string.gsub(format, "$" .. var, val)
|
format = format:gsub("$" .. var, val)
|
||||||
end
|
end
|
||||||
|
|
||||||
return format
|
return format
|
||||||
|
@ -77,12 +76,20 @@ function escape(text)
|
||||||
end
|
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
|
-- {{{ Truncate a string
|
||||||
function truncate(text, maxlen)
|
function truncate(text, maxlen)
|
||||||
local txtlen = text:len()
|
local txtlen = text:len()
|
||||||
|
|
||||||
if txtlen > maxlen then
|
if txtlen > maxlen then
|
||||||
text = string.sub(text, 1, maxlen - 3) .. "..."
|
text = text:sub(1, maxlen - 3) .. "..."
|
||||||
end
|
end
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
@ -100,14 +107,14 @@ function scroll(text, maxlen, widget)
|
||||||
|
|
||||||
if txtlen > maxlen then
|
if txtlen > maxlen then
|
||||||
if state.d 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
|
state.i = state.i + 3
|
||||||
|
|
||||||
if maxlen + state.i >= txtlen then
|
if maxlen + state.i >= txtlen then
|
||||||
state.d = false
|
state.d = false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
text = "..." .. string.sub(text, state.i, state.i + maxlen)
|
text = "..." .. text:sub(state.i, state.i + maxlen)
|
||||||
state.i = state.i - 3
|
state.i = state.i - 3
|
||||||
|
|
||||||
if state.i <= 1 then
|
if state.i <= 1 then
|
||||||
|
|
Loading…
Reference in New Issue