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

97cb7976f208b8253a9aa5d61c90f7723389ad5f
[etc/awesome.git] / helpers.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local debug  = require("debug")
11
12 local capi   = { timer = timer }
13 local io     = { open = io.open }
14 local rawget = rawget
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 -- {{{ Read the first line of a file or return nil.
34
35 function helpers.first_line(f)
36     local fp = io.open(f)
37     if not fp
38     then
39         return nil
40     end
41
42     local content = fp:read("*l")
43     fp:close()
44     return content
45 end
46
47 -- }}}
48
49 -- {{{ Timer maker
50
51 helpers.timer_table = {}
52
53 function helpers.newtimer(name, timeout, fun, nostart)
54     helpers.timer_table[name] = capi.timer({ timeout = timeout })
55     helpers.timer_table[name]:connect_signal("timeout", fun)
56     helpers.timer_table[name]:start()
57     if not nostart then
58         helpers.timer_table[name]:emit_signal("timeout")
59     end
60 end
61
62 -- }}}
63
64 -- {{{ A map utility
65
66 helpers.map_table = {}
67
68 function helpers.set_map(element, value)
69     helpers.map_table[element] = value
70 end
71
72 function helpers.get_map(element)
73     return helpers.map_table[element]
74 end
75
76 -- }}}
77
78 return helpers