]> 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:

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