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
6 * (c) 2010-2012, Peter Hofmann
10 local debug = require("debug")
12 local capi = { timer = timer }
13 local io = { open = io.open,
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 -- see if the file exists
37 function helpers.file_exists(file)
38 local f = io.open(file, "rb")
39 if f then f:close() end
44 -- get all lines from a file, returns an empty
45 -- list/table if the file does not exist
46 function helpers.lines_from(file)
47 if not helpers.file_exists(file) then return {} end
49 for line in io.lines(file) do
50 lines[#lines + 1] = line
55 -- get first line of a file, return nil if
56 -- the file does not exist
57 function helpers.first_line(file)
58 return helpers.lines_from(file)[1]
61 -- get first non empty line from a file,
62 -- returns nil otherwise
63 function helpers.first_nonempty_line(file)
64 for k,v in pairs(lines_from(file)) do
65 if #v then return v end
74 helpers.timer_table = {}
76 function helpers.newtimer(name, timeout, fun, nostart)
77 helpers.timer_table[name] = capi.timer({ timeout = timeout })
78 helpers.timer_table[name]:connect_signal("timeout", fun)
79 helpers.timer_table[name]:start()
81 helpers.timer_table[name]:emit_signal("timeout")
89 helpers.map_table = {}
91 function helpers.set_map(element, value)
92 helpers.map_table[element] = value
95 function helpers.get_map(element)
96 return helpers.map_table[element]