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.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
9 local debug = require("debug")
12 local capi = { timer = (type(timer) == 'table' and timer or require ("gears.timer")) }
13 local io = { open = io.open,
17 local table = { sort = table.sort }
19 -- Lain helper functions for internal use
23 helpers.lain_dir = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
24 helpers.icons_dir = helpers.lain_dir .. 'icons/'
25 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
29 function helpers.wrequire(table, key)
30 local module = rawget(table, key)
31 return module or require(table._NAME .. '.' .. key)
36 -- {{{ File operations
38 -- see if the file exists and is readable
39 function helpers.file_exists(file)
40 local f = io.open(file)
49 -- get all lines from a file, returns an empty
50 -- list/table if the file does not exist
51 function helpers.lines_from(file)
52 if not helpers.file_exists(file) then return {} end
54 for line in io.lines(file) do
55 lines[#lines + 1] = line
60 -- match all lines from a file, returns an empty
61 -- list/table if the file or match does not exist
62 function helpers.lines_match(regexp, file)
64 for index,line in pairs(helpers.lines_from(file)) do
65 if string.match(line, regexp) then
72 -- get first line of a file, return nil if
73 -- the file does not exist
74 function helpers.first_line(file)
75 return helpers.lines_from(file)[1]
78 -- get first non empty line from a file,
79 -- returns nil otherwise
80 function helpers.first_nonempty_line(file)
81 for k,v in pairs(helpers.lines_from(file)) do
82 if #v then return v end
91 helpers.timer_table = {}
93 function helpers.newtimer(_name, timeout, fun, nostart)
95 if not helpers.timer_table[name] then
96 helpers.timer_table[name] = capi.timer({ timeout = timeout })
97 helpers.timer_table[name]:start()
99 helpers.timer_table[name]:connect_signal("timeout", fun)
101 helpers.timer_table[name]:emit_signal("timeout")
107 -- {{{ Pipe operations
109 -- read the full output of a command output
110 function helpers.read_pipe(cmd)
111 local f = assert(io.popen(cmd))
112 local output = f:read("*all")
117 -- return line iterator of a command output
118 function helpers.pipelines(...)
119 local f = assert(io.popen(...))
120 return function () -- iterator
121 local data = f:read()
122 if data == nil then f:close() end
131 helpers.map_table = {}
133 function helpers.set_map(element, value)
134 helpers.map_table[element] = value
137 function helpers.get_map(element)
138 return helpers.map_table[element]
143 --{{{ Iterate over table of records sorted by keys
144 function helpers.spairs(t)
147 for k in pairs(t) do keys[#keys+1] = k end
151 -- return the iterator function
156 return keys[i], t[keys[i]]