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
10 local debug = require("debug")
11 local io = { lines = io.lines,
15 local table = { sort = table.sort }
17 local easy_async = require("awful.spawn").easy_async
18 local timer = require("gears.timer")
19 local wibox = require("wibox")
21 -- Lain helper functions for internal use
25 helpers.lain_dir = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
26 helpers.icons_dir = helpers.lain_dir .. 'icons/'
27 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
31 function helpers.wrequire(table, key)
32 local module = rawget(table, key)
33 return module or require(table._NAME .. '.' .. key)
38 -- {{{ File operations
40 -- see if the file exists and is readable
41 function helpers.file_exists(file)
42 local f = io.open(file)
51 -- get all lines from a file, returns an empty
52 -- list/table if the file does not exist
53 function helpers.lines_from(file)
54 if not helpers.file_exists(file) then return {} end
56 for line in io.lines(file) do
57 lines[#lines + 1] = line
62 -- match all lines from a file, returns an empty
63 -- list/table if the file or match does not exist
64 function helpers.lines_match(regexp, file)
66 for index,line in pairs(helpers.lines_from(file)) do
67 if string.match(line, regexp) then
74 -- get first line of a file, return nil if
75 -- the file does not exist
76 function helpers.first_line(file)
77 return helpers.lines_from(file)[1]
80 -- get first non empty line from a file,
81 -- returns nil otherwise
82 function helpers.first_nonempty_line(file)
83 for k,v in pairs(helpers.lines_from(file)) do
84 if #v then return v end
93 helpers.timer_table = {}
95 function helpers.newtimer(_name, timeout, fun, nostart)
97 if not helpers.timer_table[name] then
98 helpers.timer_table[name] = timer({ timeout = timeout })
99 helpers.timer_table[name]:start()
101 helpers.timer_table[name]:connect_signal("timeout", fun)
103 helpers.timer_table[name]:emit_signal("timeout")
109 -- {{{ Pipe operations
111 -- return the full output of an input command (synchronous pipe)
112 -- @param cmd the input command
113 -- @return command output (string)
114 function helpers.read_pipe(cmd)
115 local f = io.popen(cmd)
116 local output = f:read("*all")
121 -- run a command and execute a function on its output (asynchronous pipe)
122 -- @param cmd the input command
123 -- @param callback function to execute on cmd output
125 function helpers.async(cmd, callback)
126 return easy_async(cmd,
127 function (stdout, stderr, reason, exit_code)
136 helpers.map_table = {}
138 function helpers.set_map(element, value)
139 helpers.map_table[element] = value
142 function helpers.get_map(element)
143 return helpers.map_table[element]
150 -- check if an element exist on a table
151 function helpers.element_in_table(element, tbl)
152 for _, i in pairs(tbl) do
160 -- iterate over table of records sorted by keys
161 function helpers.spairs(t)
164 for k in pairs(t) do keys[#keys+1] = k end
168 -- return the iterator function
173 return keys[i], t[keys[i]]
178 -- create a lain textbox widget
179 function helpers.make_widget_textbox()
180 local w = wibox.widget.textbox('')
181 local t = wibox.widget.base.make_widget(w)