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

some other space fixes
[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       * (c) 2010,      Adrian C. <anrxc@sysphere.org> 
8                                                       
9 --]]
10
11 local awful  = require("awful")
12 local debug  = require("debug")
13 local pairs  = pairs
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 -- {{{
34 -- If lain.terminal is a string, e.g. "xterm", then "xterm -e " .. cmd is
35 -- run. But if lain.terminal is a function, then terminal(cmd) is run.
36
37 function helpers.run_in_terminal(cmd)
38     if type(terminal) == "function"
39     then
40         terminal(cmd)
41     elseif type(terminal) == "string"
42     then
43         awful.util.spawn(terminal .. ' -e ' .. cmd)
44     end
45 end
46
47 -- }}}
48
49 -- {{{ Format units to one decimal point
50
51 function helpers.uformat(array, key, value, unit)
52     for u, v in pairs(unit) do
53         array["{"..key.."_"..u.."}"] = string.format("%.1f", value/v)
54     end
55     return array
56 end
57
58 -- }}}
59
60 -- {{{ Read the first line of a file or return nil.
61
62 function helpers.first_line(f)
63     local fp = io.open(f)
64     if not fp
65     then
66         return nil
67     end
68
69     local content = fp:read("*l")
70     fp:close()
71     return content
72 end
73
74 -- }}}
75
76 -- {{{ A map utility
77
78 helpers.map_table = {}
79
80 function helpers.set_map(element, value)
81     helpers.map_table[element] = value
82 end
83
84 function helpers.get_map(element)
85     return helpers.map_table[element]
86 end
87
88 -- }}}
89
90 return helpers