All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 Licensed under GNU General Public License v2
8 local spawn = require("awful.spawn")
9 local timer = require("gears.timer")
10 local debug = require("debug")
11 local io = { lines = io.lines,
15 local table = { sort = table.sort }
17 -- Lain helper functions for internal use
21 helpers.lain_dir = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
22 helpers.icons_dir = helpers.lain_dir .. 'icons/'
23 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
27 function helpers.wrequire(table, key)
28 local module = rawget(table, key)
29 return module or require(table._NAME .. '.' .. key)
34 -- {{{ File operations
36 -- see if the file exists and is readable
37 function helpers.file_exists(file)
38 local f = io.open(file)
47 -- get all lines from a file, returns an empty
48 -- list/table if the file does not exist
49 function helpers.lines_from(file)
50 if not helpers.file_exists(file) then return {} end
52 for line in io.lines(file) do
53 lines[#lines + 1] = line
58 -- match all lines from a file, returns an empty
59 -- list/table if the file or match does not exist
60 function helpers.lines_match(regexp, file)
62 for index,line in pairs(helpers.lines_from(file)) do
63 if string.match(line, regexp) then
70 -- get first line of a file, return nil if
71 -- the file does not exist
72 function helpers.first_line(file)
73 return helpers.lines_from(file)[1]
76 -- get first non empty line from a file,
77 -- returns nil otherwise
78 function helpers.first_nonempty_line(file)
79 for k,v in pairs(helpers.lines_from(file)) do
80 if #v then return v end
89 helpers.timer_table = {}
91 function helpers.newtimer(name, timeout, fun, nostart, stoppable)
92 if not name or #name == 0 then return end
93 name = (stoppable and name) or timeout
94 if not helpers.timer_table[name] then
95 helpers.timer_table[name] = timer({ timeout = timeout })
96 helpers.timer_table[name]:start()
98 helpers.timer_table[name]:connect_signal("timeout", fun)
100 helpers.timer_table[name]:emit_signal("timeout")
102 return stoppable and helpers.timer_table[name]
107 -- {{{ Pipe operations
109 -- run a command and execute a function on its output (asynchronous pipe)
110 -- @param cmd the input command
111 -- @param callback function to execute on cmd output
113 function helpers.async(cmd, callback)
114 return spawn.easy_async(cmd,
115 function (stdout, stderr, reason, exit_code)
116 callback(stdout, exit_code)
120 -- like above, but call spawn.easy_async with a shell
121 function helpers.async_with_shell(cmd, callback)
122 return spawn.easy_async_with_shell(cmd,
123 function (stdout, stderr, reason, exit_code)
124 callback(stdout, exit_code)
128 -- run a command and execute a function on its output line by line
129 function helpers.line_callback(cmd, callback)
130 return spawn.with_line_callback(cmd, {
131 stdout = function (line)
141 helpers.map_table = {}
143 function helpers.set_map(element, value)
144 helpers.map_table[element] = value
147 function helpers.get_map(element)
148 return helpers.map_table[element]
155 -- check if an element exist on a table
156 function helpers.element_in_table(element, tbl)
157 for _, i in pairs(tbl) do
165 -- iterate over table of records sorted by keys
166 function helpers.spairs(t)
169 for k in pairs(t) do keys[#keys+1] = k end
173 -- return the iterator function
178 return keys[i], t[keys[i]]
183 -- create the partition of singletons of a given set
184 -- example: the trivial partition set of {a, b, c}, is {{a}, {b}, {c}}
185 function helpers.trivial_partition_set(set)
187 for _,e in pairs(set) do
193 -- creates the powerset of a given set
194 function helpers.powerset(s)
195 if not s then return {} end
199 t[#t+1] = {s[i],unpack(t[j])}