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 -- check if the file exists and is readable
37 function helpers.file_exists(path)
38 local file = io.open(path, "rb")
39 if file then file:close() end
43 -- get a table with all lines from a file
44 function helpers.lines_from(path)
46 for line in io.lines(path) do
47 lines[#lines + 1] = line
52 -- get a table with all lines from a file matching regexp
53 function helpers.lines_match(regexp, path)
55 for line in io.lines(path) do
56 if string.match(line, regexp) then
57 lines[#lines + 1] = line
63 -- get first line of a file
64 function helpers.first_line(path)
65 local file, first = io.open(path, "rb"), nil
67 first = file:read("*l")
73 -- get first non empty line from a file
74 function helpers.first_nonempty_line(path)
75 for line in io.lines(path) do
76 if #line then return line end
85 helpers.timer_table = {}
87 function helpers.newtimer(name, timeout, fun, nostart, stoppable)
88 if not name or #name == 0 then return end
89 name = (stoppable and name) or timeout
90 if not helpers.timer_table[name] then
91 helpers.timer_table[name] = timer({ timeout = timeout })
92 helpers.timer_table[name]:start()
94 helpers.timer_table[name]:connect_signal("timeout", fun)
96 helpers.timer_table[name]:emit_signal("timeout")
98 return stoppable and helpers.timer_table[name]
103 -- {{{ Pipe operations
105 -- run a command and execute a function on its output (asynchronous pipe)
106 -- @param cmd the input command
107 -- @param callback function to execute on cmd output
109 function helpers.async(cmd, callback)
110 return spawn.easy_async(cmd,
111 function (stdout, stderr, reason, exit_code)
112 callback(stdout, exit_code)
116 -- like above, but call spawn.easy_async with a shell
117 function helpers.async_with_shell(cmd, callback)
118 return spawn.easy_async_with_shell(cmd,
119 function (stdout, stderr, reason, exit_code)
120 callback(stdout, exit_code)
124 -- run a command and execute a function on its output line by line
125 function helpers.line_callback(cmd, callback)
126 return spawn.with_line_callback(cmd, {
127 stdout = function (line)
137 helpers.map_table = {}
139 function helpers.set_map(element, value)
140 helpers.map_table[element] = value
143 function helpers.get_map(element)
144 return helpers.map_table[element]
151 -- check if an element exist on a table
152 function helpers.element_in_table(element, tbl)
153 for _, i in pairs(tbl) do
161 -- iterate over table of records sorted by keys
162 function helpers.spairs(t)
165 for k in pairs(t) do keys[#keys+1] = k end
169 -- return the iterator function
174 return keys[i], t[keys[i]]
179 -- create the partition of singletons of a given set
180 -- example: the trivial partition set of {a, b, c}, is {{a}, {b}, {c}}
181 function helpers.trivial_partition_set(set)
183 for _,e in pairs(set) do
189 -- creates the powerset of a given set
190 function helpers.powerset(s)
191 if not s then return {} end
195 t[#t+1] = {s[i],unpack(t[j])}