mirror of
https://github.com/vicious-widgets/vicious.git
synced 2026-09-10 07:06:17 -04:00
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.
130 lines
2.8 KiB
Lua
130 lines
2.8 KiB
Lua
---------------------------------------------------
|
|
-- Licensed under the GNU General Public License v2
|
|
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
|
|
-- * (c) 2009, Rémy C. <shikamaru@mandriva.org>
|
|
-- * (c) 2009, Benedikt Sauer <filmor@gmail.com>
|
|
-- * (c) 2009, Henning Glawe <glaweh@debian.org>
|
|
-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
|
|
---------------------------------------------------
|
|
|
|
-- {{{ Grab environment
|
|
local pairs = pairs
|
|
local io = { open = io.open }
|
|
local setmetatable = setmetatable
|
|
local string = {
|
|
upper = string.upper,
|
|
format = string.format
|
|
}
|
|
-- }}}
|
|
|
|
|
|
-- Helpers: provides helper functions for vicious widgets
|
|
module("vicious.helpers")
|
|
|
|
|
|
-- {{{ Variable definitions
|
|
local scroller = {}
|
|
-- }}}
|
|
|
|
-- {{{ Helper functions
|
|
-- {{{ Expose path as a Lua table
|
|
function pathtotable(path)
|
|
return setmetatable({},
|
|
{ __index = function(_, name)
|
|
local f = io.open(path .. '/' .. name)
|
|
if f then
|
|
local s = f:read("*all")
|
|
f:close()
|
|
return s
|
|
end
|
|
end
|
|
})
|
|
end
|
|
-- }}}
|
|
|
|
-- {{{ Format a string with args
|
|
function format(format, args)
|
|
for var, val in pairs(args) do
|
|
format = format:gsub("$" .. var, val)
|
|
end
|
|
|
|
return format
|
|
end
|
|
-- }}}
|
|
|
|
-- {{{ Format units to one decimal point
|
|
function uformat(array, key, value, unit)
|
|
for u, v in pairs(unit) do
|
|
array["{"..key.."_"..u.."}"] = string.format("%.1f", value/v)
|
|
end
|
|
|
|
return array
|
|
end
|
|
-- }}}
|
|
|
|
-- {{{ Escape a string
|
|
function escape(text)
|
|
local xml_entities = {
|
|
["\""] = """,
|
|
["&"] = "&",
|
|
["'"] = "'",
|
|
["<"] = "<",
|
|
[">"] = ">"
|
|
}
|
|
|
|
return text and text:gsub("[\"&'<>]", xml_entities)
|
|
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 = text:sub(1, maxlen - 3) .. "..."
|
|
end
|
|
|
|
return text
|
|
end
|
|
-- }}}
|
|
|
|
-- {{{ Scroll through a string
|
|
function scroll(text, maxlen, widget)
|
|
if not scroller[widget] then
|
|
scroller[widget] = { i = 1, d = true }
|
|
end
|
|
|
|
local txtlen = text:len()
|
|
local state = scroller[widget]
|
|
|
|
if txtlen > maxlen then
|
|
if state.d then
|
|
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 = "..." .. text:sub(state.i, state.i + maxlen)
|
|
state.i = state.i - 3
|
|
|
|
if state.i <= 1 then
|
|
state.d = true
|
|
end
|
|
end
|
|
end
|
|
|
|
return text
|
|
end
|
|
-- }}}
|
|
-- }}}
|