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, unpack = table.unpack }
16 local unpack = unpack or table.unpack -- lua 5.1 retro-compatibility
18 -- Lain helper functions for internal use
22 helpers.lain_dir = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
23 helpers.icons_dir = helpers.lain_dir .. 'icons/'
24 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
28 function helpers.wrequire(table, key)
29 local module = rawget(table, key)
30 return module or require(table._NAME .. '.' .. key)
35 -- {{{ File operations
37 -- check if the file exists and is readable
38 function helpers.file_exists(path)
39 local file = io.open(path, "rb")
40 if file then file:close() end
44 -- get a table with all lines from a file
45 function helpers.lines_from(path)
47 for line in io.lines(path) do
48 lines[#lines + 1] = line
53 -- get a table with all lines from a file matching regexp
54 function helpers.lines_match(regexp, path)
56 for line in io.lines(path) do
57 if string.match(line, regexp) then
58 lines[#lines + 1] = line
64 -- get first line of a file
65 function helpers.first_line(path)
66 local file, first = io.open(path, "rb"), nil
68 first = file:read("*l")
74 -- get first non empty line from a file
75 function helpers.first_nonempty_line(path)
76 for line in io.lines(path) do
77 if #line then return line end
86 helpers.timer_table = {}
88 function helpers.newtimer(name, timeout, fun, nostart, stoppable)
89 if not name or #name == 0 then return end
90 name = (stoppable and name) or timeout
91 if not helpers.timer_table[name] then
92 helpers.timer_table[name] = timer({ timeout = timeout })
93 helpers.timer_table[name]:start()
95 helpers.timer_table[name]:connect_signal("timeout", fun)
97 helpers.timer_table[name]:emit_signal("timeout")
99 return stoppable and helpers.timer_table[name]
104 -- {{{ Pipe operations
106 -- run a command and execute a function on its output (asynchronous pipe)
107 -- @param cmd the input command
108 -- @param callback function to execute on cmd output
110 function helpers.async(cmd, callback)
111 return spawn.easy_async(cmd,
112 function (stdout, stderr, reason, exit_code)
113 callback(stdout, exit_code)
117 -- like above, but call spawn.easy_async with a shell
118 function helpers.async_with_shell(cmd, callback)
119 return spawn.easy_async_with_shell(cmd,
120 function (stdout, stderr, reason, exit_code)
121 callback(stdout, exit_code)
125 -- run a command and execute a function on its output line by line
126 function helpers.line_callback(cmd, callback)
127 return spawn.with_line_callback(cmd, {
128 stdout = function (line)
138 helpers.map_table = {}
140 function helpers.set_map(element, value)
141 helpers.map_table[element] = value
144 function helpers.get_map(element)
145 return helpers.map_table[element]
152 -- check if an element exist on a table
153 function helpers.element_in_table(element, tbl)
154 for _, i in pairs(tbl) do
162 -- iterate over table of records sorted by keys
163 function helpers.spairs(t)
166 for k in pairs(t) do keys[#keys+1] = k end
170 -- return the iterator function
175 return keys[i], t[keys[i]]
180 -- create the partition of singletons of a given set
181 -- example: the trivial partition set of {a, b, c}, is {{a}, {b}, {c}}
182 function helpers.trivial_partition_set(set)
184 for _,e in pairs(set) do
190 -- creates the powerset of a given set
191 function helpers.powerset(s)
192 if not s then return {} end
196 t[#t+1] = {s[i],unpack(t[j])}