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

#25 fix attempt 2
[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                  lines = io.lines }
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
37 function helpers.file_exists(file)
38   local f = io.open(file)
39   if f then f:close() end
40   return f ~= nil
41 end
42
43 -- get all lines from a file, returns an empty 
44 -- list/table if the file does not exist
45 function helpers.lines_from(file)
46   if not helpers.file_exists(file) then return {} end
47   lines = {}
48   for line in io.lines(file) do 
49     lines[#lines + 1] = line
50   end
51   return lines
52 end
53
54 -- get first line of a file, return nil if
55 -- the file does not exist
56 function helpers.first_line(file)
57     return helpers.lines_from(file)[1]
58 end
59
60 -- get first non empty line from a file,
61 -- returns nil otherwise
62 function helpers.first_nonempty_line(file)
63   for k,v in pairs(helpers.lines_from(file)) do
64     if #v then return v end 
65   end
66   return nil
67 end
68
69 -- }}}
70
71 -- {{{ Timer maker
72
73 helpers.timer_table = {}
74
75 function helpers.newtimer(name, timeout, fun, nostart)
76     helpers.timer_table[name] = capi.timer({ timeout = timeout })
77     helpers.timer_table[name]:connect_signal("timeout", fun)
78     helpers.timer_table[name]:start()
79     if not nostart then
80         helpers.timer_table[name]:emit_signal("timeout")
81     end
82 end
83
84 -- }}}
85
86 -- {{{ A map utility
87
88 helpers.map_table = {}
89
90 function helpers.set_map(element, value)
91     helpers.map_table[element] = value
92 end
93
94 function helpers.get_map(element)
95     return helpers.map_table[element]
96 end
97
98 -- }}}
99
100 return helpers