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

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