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