]> 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 #253 from aajjbb/fix-alsabar-typo
[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 local debug  = require("debug")
10
11 local assert = assert
12 local capi   = { timer = (type(timer) == 'table' and timer or require ("gears.timer")) }
13 local io     = { open  = io.open,
14                  lines = io.lines,
15                  popen = io.popen }
16 local rawget = rawget
17 local table  = { sort   = table.sort }
18
19 local wibox  = require("wibox")
20
21 -- Lain helper functions for internal use
22 -- lain.helpers
23 local helpers = {}
24
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/'
28
29 -- {{{ Modules loader
30
31 function helpers.wrequire(table, key)
32     local module = rawget(table, key)
33     return module or require(table._NAME .. '.' .. key)
34 end
35
36 -- }}}
37
38 -- {{{ File operations
39
40 -- see if the file exists and is readable
41 function helpers.file_exists(file)
42   local f = io.open(file)
43   if f then
44       local s = f:read()
45       f:close()
46       f = s
47   end
48   return f ~= nil
49 end
50
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
55   local lines = {}
56   for line in io.lines(file) do
57     lines[#lines + 1] = line
58   end
59   return lines
60 end
61
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)
65         local lines = {}
66         for index,line in pairs(helpers.lines_from(file)) do
67                 if string.match(line, regexp) then
68                         lines[index] = line
69                 end
70         end
71         return lines
72 end
73
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]
78 end
79
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
85   end
86   return nil
87 end
88
89 -- }}}
90
91 -- {{{ Timer maker
92
93 helpers.timer_table = {}
94
95 function helpers.newtimer(_name, timeout, fun, nostart)
96     local name = timeout
97     if not helpers.timer_table[name] then
98         helpers.timer_table[name] = capi.timer({ timeout = timeout })
99         helpers.timer_table[name]:start()
100     end
101     helpers.timer_table[name]:connect_signal("timeout", fun)
102     if not nostart then
103         helpers.timer_table[name]:emit_signal("timeout")
104     end
105 end
106
107 -- }}}
108
109 -- {{{ Pipe operations
110
111 -- read the full output of a command output
112 function helpers.read_pipe(cmd)
113    local f = assert(io.popen(cmd))
114    local output = f:read("*all")
115    f:close()
116    return output
117 end
118
119 -- return line iterator of a command output
120 function helpers.pipelines(...)
121     local f = assert(io.popen(...))
122     return function () -- iterator
123         local data = f:read()
124         if data == nil then f:close() end
125         return data
126     end
127 end
128
129 -- }}}
130
131 -- {{{ A map utility
132
133 helpers.map_table = {}
134
135 function helpers.set_map(element, value)
136     helpers.map_table[element] = value
137 end
138
139 function helpers.get_map(element)
140     return helpers.map_table[element]
141 end
142
143 -- }}}
144
145 -- {{{ Misc
146
147 -- iterate over table of records sorted by keys
148 function helpers.spairs(t)
149     -- collect the keys
150     local keys = {}
151     for k in pairs(t) do keys[#keys+1] = k end
152
153     table.sort(keys)
154
155     -- return the iterator function
156     local i = 0
157     return function()
158         i = i + 1
159         if keys[i] then
160             return keys[i], t[keys[i]]
161         end
162     end
163 end
164
165 -- create a lain textbox widget
166 function helpers.make_widget_textbox()
167     local w = wibox.widget.textbox('')
168     local t = wibox.widget.base.make_widget(w)
169     t.widget = w
170     return t
171 end
172
173 -- }}}
174
175 return helpers