]> 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 branch 'master' of github.com:copycat-killer/lain
[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 capi   = { timer = timer }
12 local io     = { open  = io.open,
13                  lines = io.lines,
14                  popen = io.popen }
15 local rawget = rawget
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 -- see if the file exists and is readable
37 function helpers.file_exists(file)
38   local f = io.open(file)
39   if f then
40       local s = f:read()
41       f:close()
42       f = s
43   end
44   return f ~= nil
45 end
46
47 -- get all lines from a file, returns an empty
48 -- list/table if the file does not exist
49 function helpers.lines_from(file)
50   if not helpers.file_exists(file) then return {} end
51   lines = {}
52   for line in io.lines(file) do
53     lines[#lines + 1] = line
54   end
55   return lines
56 end
57
58 -- get first line of a file, return nil if
59 -- the file does not exist
60 function helpers.first_line(file)
61     return helpers.lines_from(file)[1]
62 end
63
64 -- get first non empty line from a file,
65 -- returns nil otherwise
66 function helpers.first_nonempty_line(file)
67   for k,v in pairs(helpers.lines_from(file)) do
68     if #v then return v end
69   end
70   return nil
71 end
72
73 -- }}}
74
75 -- {{{ Timer maker
76
77 helpers.timer_table = {}
78
79 function helpers.newtimer(_name, timeout, fun, nostart)
80     local name = timeout
81
82     if not helpers.timer_table[name] then
83         helpers.timer_table[name] = capi.timer({ timeout = timeout })
84         helpers.timer_table[name]:start()
85     end
86
87     helpers.timer_table[name]:connect_signal("timeout", fun)
88
89     if not nostart then
90         helpers.timer_table[name]:emit_signal("timeout")
91     end
92 end
93
94 -- }}}
95
96 -- {{{ Pipe operations
97
98 -- read the full output of a pipe (command)
99 function helpers.read_pipe(cmd)
100    local f = assert(io.popen(cmd))
101    local output = f:read("*all")
102    f:close()
103    return output
104 end
105
106 -- }}}
107
108 -- {{{ A map utility
109
110 helpers.map_table = {}
111
112 function helpers.set_map(element, value)
113     helpers.map_table[element] = value
114 end
115
116 function helpers.get_map(element)
117     return helpers.map_table[element]
118 end
119
120 -- }}}
121
122 return helpers