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")
11 local capi = { timer = (type(timer) == 'table' and timer or require ("gears.timer")) }
12 local io = { open = io.open,
16 local table = { sort = table.sort }
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 -- see if the file exists and is readable
38 function helpers.file_exists(file)
39 local f = io.open(file)
48 -- get all lines from a file, returns an empty
49 -- list/table if the file does not exist
50 function helpers.lines_from(file)
51 if not helpers.file_exists(file) then return {} end
53 for line in io.lines(file) do
54 lines[#lines + 1] = line
59 -- get first line of a file, return nil if
60 -- the file does not exist
61 function helpers.first_line(file)
62 return helpers.lines_from(file)[1]
65 -- get first non empty line from a file,
66 -- returns nil otherwise
67 function helpers.first_nonempty_line(file)
68 for k,v in pairs(helpers.lines_from(file)) do
69 if #v then return v end
78 helpers.timer_table = {}
80 function helpers.newtimer(_name, timeout, fun, nostart)
82 if not helpers.timer_table[name] then
83 helpers.timer_table[name] = capi.timer({ timeout = timeout })
85 helpers.timer_table[name]:connect_signal("timeout", fun)
86 helpers.timer_table[name]:start()
88 helpers.timer_table[name]:emit_signal("timeout")
94 -- {{{ Pipe operations
96 -- read the full output of a pipe (command)
97 function helpers.read_pipe(cmd)
98 local f = assert(io.popen(cmd))
99 local output = f:read("*all")
108 helpers.map_table = {}
110 function helpers.set_map(element, value)
111 helpers.map_table[element] = value
114 function helpers.get_map(element)
115 return helpers.map_table[element]
120 --{{{ Iterate over table of records sorted by keys
121 function helpers.spairs(t)
124 for k in pairs(t) do keys[#keys+1] = k end
128 -- return the iterator function
133 return keys[i], t[keys[i]]