]> git.madduck.net Git - etc/awesome.git/blob - helpers.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #398 from BarbUk/feature/tpbat/configurable-battery-thresholds
[etc/awesome.git] / helpers.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013, Luca CPZ
5
6 --]]
7
8 local spawn      = require("awful.spawn")
9 local timer      = require("gears.timer")
10 local debug      = require("debug")
11 local io         = { lines = io.lines,
12                      open  = io.open }
13 local pairs      = pairs
14 local rawget     = rawget
15 local table      = { sort  = table.sort }
16
17 -- Lain helper functions for internal use
18 -- lain.helpers
19 local helpers = {}
20
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/'
24
25 -- {{{ Modules loader
26
27 function helpers.wrequire(table, key)
28     local module = rawget(table, key)
29     return module or require(table._NAME .. '.' .. key)
30 end
31
32 -- }}}
33
34 -- {{{ File operations
35
36 -- check if the file exists and is readable
37 function helpers.file_exists(path)
38     local file = io.open(path, "rb")
39     if file then file:close() end
40     return file ~= nil
41 end
42
43 -- get a table with all lines from a file
44 function helpers.lines_from(path)
45     local lines = {}
46     for line in io.lines(path) do
47         lines[#lines + 1] = line
48     end
49     return lines
50 end
51
52 -- get a table with all lines from a file matching regexp
53 function helpers.lines_match(regexp, path)
54     local lines = {}
55     for line in io.lines(path) do
56         if string.match(line, regexp) then
57             lines[#lines + 1] = line
58         end
59     end
60     return lines
61 end
62
63 -- get first line of a file
64 function helpers.first_line(path)
65     local file, first = io.open(path, "rb"), nil
66     if file then
67         first = file:read("*l")
68         file:close()
69     end
70     return first
71 end
72
73 -- get first non empty line from a file
74 function helpers.first_nonempty_line(path)
75     for line in io.lines(path) do
76         if #line then return line end
77     end
78     return nil
79 end
80
81 -- }}}
82
83 -- {{{ Timer maker
84
85 helpers.timer_table = {}
86
87 function helpers.newtimer(name, timeout, fun, nostart, stoppable)
88     if not name or #name == 0 then return end
89     name = (stoppable and name) or timeout
90     if not helpers.timer_table[name] then
91         helpers.timer_table[name] = timer({ timeout = timeout })
92         helpers.timer_table[name]:start()
93     end
94     helpers.timer_table[name]:connect_signal("timeout", fun)
95     if not nostart then
96         helpers.timer_table[name]:emit_signal("timeout")
97     end
98     return stoppable and helpers.timer_table[name]
99 end
100
101 -- }}}
102
103 -- {{{ Pipe operations
104
105 -- run a command and execute a function on its output (asynchronous pipe)
106 -- @param cmd the input command
107 -- @param callback function to execute on cmd output
108 -- @return cmd PID
109 function helpers.async(cmd, callback)
110     return spawn.easy_async(cmd,
111     function (stdout, stderr, reason, exit_code)
112         callback(stdout, exit_code)
113     end)
114 end
115
116 -- like above, but call spawn.easy_async with a shell
117 function helpers.async_with_shell(cmd, callback)
118     return spawn.easy_async_with_shell(cmd,
119     function (stdout, stderr, reason, exit_code)
120         callback(stdout, exit_code)
121     end)
122 end
123
124 -- run a command and execute a function on its output line by line
125 function helpers.line_callback(cmd, callback)
126     return spawn.with_line_callback(cmd, {
127         stdout = function (line)
128             callback(line)
129         end,
130     })
131 end
132
133 -- }}}
134
135 -- {{{ A map utility
136
137 helpers.map_table = {}
138
139 function helpers.set_map(element, value)
140     helpers.map_table[element] = value
141 end
142
143 function helpers.get_map(element)
144     return helpers.map_table[element]
145 end
146
147 -- }}}
148
149 -- {{{ Misc
150
151 -- check if an element exist on a table
152 function helpers.element_in_table(element, tbl)
153     for _, i in pairs(tbl) do
154         if i == element then
155             return true
156         end
157     end
158     return false
159 end
160
161 -- iterate over table of records sorted by keys
162 function helpers.spairs(t)
163     -- collect the keys
164     local keys = {}
165     for k in pairs(t) do keys[#keys+1] = k end
166
167     table.sort(keys)
168
169     -- return the iterator function
170     local i = 0
171     return function()
172         i = i + 1
173         if keys[i] then
174             return keys[i], t[keys[i]]
175         end
176     end
177 end
178
179 -- create the partition of singletons of a given set
180 -- example: the trivial partition set of {a, b, c}, is {{a}, {b}, {c}}
181 function helpers.trivial_partition_set(set)
182     local ss = {}
183     for _,e in pairs(set) do
184         ss[#ss+1] = {e}
185     end
186     return ss
187 end
188
189 -- creates the powerset of a given set
190 function helpers.powerset(s)
191     if not s then return {} end
192     local t = {{}}
193     for i = 1, #s do
194         for j = 1, #t do
195             t[#t+1] = {s[i],unpack(t[j])}
196         end
197     end
198     return t
199 end
200
201 -- }}}
202
203 return helpers