]> git.madduck.net Git - etc/awesome.git/blob - helpers.lua.orig

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:

merging with upstream
[etc/awesome.git] / helpers.lua.orig
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 <<<<<<< HEAD
14                  lines = io.lines }
15 =======
16                  lines = io.lines,
17                  popen = io.popen }
18 >>>>>>> upstream/master
19 local rawget = rawget
20
21 -- Lain helper functions for internal use
22 -- lain.helpers
23 local helpers = {}
24
25 helpers.lain_dir    = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
26 helpers.icons_dir   = helpers.lain_dir .. 'icons/'
27 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
28
29 -- {{{ Modules loader
30
31 function helpers.wrequire(table, key)
32     local module = rawget(table, key)
33     return module or require(table._NAME .. '.' .. key)
34 end
35
36 -- }}}
37
38 -- {{{ File operations
39
40 -- see if the file exists and is readable
41 function helpers.file_exists(file)
42   local f = io.open(file)
43   if f then
44       local s = f:read()
45       f:close()
46       f = s
47   end
48   return f ~= nil
49 end
50
51 -- get all lines from a file, returns an empty
52 -- list/table if the file does not exist
53 function helpers.lines_from(file)
54   if not helpers.file_exists(file) then return {} end
55   lines = {}
56   for line in io.lines(file) do
57     lines[#lines + 1] = line
58   end
59   return lines
60 end
61
62 -- get first line of a file, return nil if
63 -- the file does not exist
64 function helpers.first_line(file)
65     return helpers.lines_from(file)[1]
66 end
67
68 -- get first non empty line from a file,
69 -- returns nil otherwise
70 function helpers.first_nonempty_line(file)
71   for k,v in pairs(helpers.lines_from(file)) do
72     if #v then return v end
73   end
74   return nil
75 end
76
77 -- }}}
78
79 -- {{{ Timer maker
80
81 helpers.timer_table = {}
82
83 function helpers.newtimer(name, timeout, fun, nostart)
84     helpers.timer_table[name] = capi.timer({ timeout = timeout })
85     helpers.timer_table[name]:connect_signal("timeout", fun)
86     helpers.timer_table[name]:start()
87     if not nostart then
88         helpers.timer_table[name]:emit_signal("timeout")
89     end
90 end
91
92 -- }}}
93
94 <<<<<<< HEAD
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 >>>>>>> upstream/master
109 -- {{{ A map utility
110
111 helpers.map_table = {}
112
113 function helpers.set_map(element, value)
114     helpers.map_table[element] = value
115 end
116
117 function helpers.get_map(element)
118     return helpers.map_table[element]
119 end
120
121 -- }}}
122
123 return helpers