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

readme updated
[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 local rawget = rawget
12 local io     = { open = io.open }
13
14 -- Lain helper functions for internal use
15 -- lain.helpers
16 local helpers = {}
17
18 helpers.lain_dir    = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
19 helpers.icons_dir   = helpers.lain_dir .. 'icons/'
20 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
21
22 -- {{{ Modules loader
23
24 function helpers.wrequire(table, key)
25     local module = rawget(table, key)
26     return module or require(table._NAME .. '.' .. key)
27 end
28
29 -- }}}
30
31 -- {{{ Read the first line of a file or return nil.
32
33 function helpers.first_line(f)
34     local fp = io.open(f)
35     if not fp
36     then
37         return nil
38     end
39
40     local content = fp:read("*l")
41     fp:close()
42     return content
43 end
44
45 -- }}}
46
47 -- {{{ Timer maker 
48
49 helpers.timer_table = {}
50
51 function helpers.newtimer(name, timeout, fun, nostart)
52     helpers.timer_table[name] = timer({ timeout = timeout })
53     helpers.timer_table[name]:connect_signal("timeout", fun)
54     helpers.timer_table[name]:start()
55     if not nostart then
56         helpers.timer_table[name]:emit_signal("timeout")
57     end
58 end
59
60 -- }}}
61
62 -- {{{ A map utility
63
64 helpers.map_table = {}
65
66 function helpers.set_map(element, value)
67     helpers.map_table[element] = value
68 end
69
70 function helpers.get_map(element)
71     return helpers.map_table[element]
72 end
73
74 -- }}}
75
76 return helpers