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
4 * (c) 2013, Luke Bonham
8 local spawn = require("awful.spawn")
9 local timer = require("gears.timer")
10 local debug = require("debug")
11 local io = { lines = io.lines,
14 local table = { sort = table.sort }
16 -- Lain helper functions for internal use
20 helpers.lain_dir = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
21 helpers.icons_dir = helpers.lain_dir .. 'icons/'
22 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
26 function helpers.wrequire(table, key)
27 local module = rawget(table, key)
28 return module or require(table._NAME .. '.' .. key)
33 -- {{{ File operations
35 -- see if the file exists and is readable
36 function helpers.file_exists(file)
37 local f = io.open(file)
46 -- get all lines from a file, returns an empty
47 -- list/table if the file does not exist
48 function helpers.lines_from(file)
49 if not helpers.file_exists(file) then return {} end
51 for line in io.lines(file) do
52 lines[#lines + 1] = line
57 -- match all lines from a file, returns an empty
58 -- list/table if the file or match does not exist
59 function helpers.lines_match(regexp, file)
61 for index,line in pairs(helpers.lines_from(file)) do
62 if string.match(line, regexp) then
69 -- get first line of a file, return nil if
70 -- the file does not exist
71 function helpers.first_line(file)
72 return helpers.lines_from(file)[1]
75 -- get first non empty line from a file,
76 -- returns nil otherwise
77 function helpers.first_nonempty_line(file)
78 for k,v in pairs(helpers.lines_from(file)) do
79 if #v then return v end
88 helpers.timer_table = {}
90 function helpers.newtimer(name, timeout, fun, nostart, stoppable)
91 if not name or #name == 0 then return end
92 name = (stoppable and name) or timeout
93 if not helpers.timer_table[name] then
94 helpers.timer_table[name] = timer({ timeout = timeout })
95 helpers.timer_table[name]:start()
97 helpers.timer_table[name]:connect_signal("timeout", fun)
99 helpers.timer_table[name]:emit_signal("timeout")
101 return stoppable and helpers.timer_table[name]
106 -- {{{ Pipe operations
108 -- run a command and execute a function on its output (asynchronous pipe)
109 -- @param cmd the input command
110 -- @param callback function to execute on cmd output
112 function helpers.async(cmd, callback)
113 return spawn.easy_async(cmd,
114 function (stdout, stderr, reason, exit_code)
115 callback(stdout, exit_code)
119 -- like above, but call spawn.easy_async with a shell
120 function helpers.async_with_shell(cmd, callback)
121 return spawn.easy_async_with_shell(cmd,
122 function (stdout, stderr, reason, exit_code)
123 callback(stdout, exit_code)
127 -- run a command and execute a function on its output line by line
128 function helpers.line_callback(cmd, callback)
129 return spawn.with_line_callback(cmd, {
130 stdout = function (line)
140 helpers.map_table = {}
142 function helpers.set_map(element, value)
143 helpers.map_table[element] = value
146 function helpers.get_map(element)
147 return helpers.map_table[element]
154 -- check if an element exist on a table
155 function helpers.element_in_table(element, tbl)
156 for _, i in pairs(tbl) do
164 -- iterate over table of records sorted by keys
165 function helpers.spairs(t)
168 for k in pairs(t) do keys[#keys+1] = k end
172 -- return the iterator function
177 return keys[i], t[keys[i]]