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

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