]> 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 pull request #173 from guotsuan/master
[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 = (type(timer) == 'table' and timer or require ("gears.timer")) }
12 local io     = { open  = io.open,
13                  lines = io.lines,
14                  popen = io.popen }
15 local rawget = rawget
16 local table  = { sort   = table.sort }
17
18 -- Lain helper functions for internal use
19 -- lain.helpers
20 local helpers = {}
21
22 helpers.lain_dir    = debug.getinfo(1, 'S').source:match[[^@(.*/).*$]]
23 helpers.icons_dir   = helpers.lain_dir .. 'icons/'
24 helpers.scripts_dir = helpers.lain_dir .. 'scripts/'
25
26 -- {{{ Modules loader
27
28 function helpers.wrequire(table, key)
29     local module = rawget(table, key)
30     return module or require(table._NAME .. '.' .. key)
31 end
32
33 -- }}}
34
35 -- {{{ File operations
36
37 -- see if the file exists and is readable
38 function helpers.file_exists(file)
39   local f = io.open(file)
40   if f then
41       local s = f:read()
42       f:close()
43       f = s
44   end
45   return f ~= nil
46 end
47
48 -- get all lines from a file, returns an empty
49 -- list/table if the file does not exist
50 function helpers.lines_from(file)
51   if not helpers.file_exists(file) then return {} end
52   lines = {}
53   for line in io.lines(file) do
54     lines[#lines + 1] = line
55   end
56   return lines
57 end
58
59 -- get first line of a file, return nil if
60 -- the file does not exist
61 function helpers.first_line(file)
62     return helpers.lines_from(file)[1]
63 end
64
65 -- get first non empty line from a file,
66 -- returns nil otherwise
67 function helpers.first_nonempty_line(file)
68   for k,v in pairs(helpers.lines_from(file)) do
69     if #v then return v end
70   end
71   return nil
72 end
73
74 -- }}}
75
76 -- {{{ Timer maker
77
78 helpers.timer_table = {}
79
80 function helpers.newtimer(name, timeout, fun, nostart)
81     helpers.timer_table[name] = capi.timer({ timeout = timeout })
82     helpers.timer_table[name]:connect_signal("timeout", fun)
83     helpers.timer_table[name]:start()
84     if not nostart then
85         helpers.timer_table[name]:emit_signal("timeout")
86     end
87 end
88
89 -- }}}
90
91 -- {{{ Pipe operations
92
93 -- read the full output of a pipe (command)
94 function helpers.read_pipe(cmd)
95    local f = assert(io.popen(cmd))
96    local output = f:read("*all")
97    f:close()
98    return output
99 end
100
101 -- }}}
102
103 -- {{{ A map utility
104
105 helpers.map_table = {}
106
107 function helpers.set_map(element, value)
108     helpers.map_table[element] = value
109 end
110
111 function helpers.get_map(element)
112     return helpers.map_table[element]
113 end
114
115 -- }}}
116
117 --{{{ Iterate over table of records sorted by keys
118 function helpers.spairs(t)
119     -- collect the keys
120     local keys = {}
121     for k in pairs(t) do keys[#keys+1] = k end
122
123     table.sort(keys)
124
125     -- return the iterator function
126     local i = 0
127     return function()
128         i = i + 1
129         if keys[i] then
130             return keys[i], t[keys[i]]
131         end
132     end
133 end
134 --}}}
135
136 return helpers