]> git.rmz.io Git - dotfiles.git/blob - awesome/utils.lua
awesome: use TERMINAL env as much as possible
[dotfiles.git] / awesome / utils.lua
1 local utils = { }
2 local io = { popen = io.popen }
3 local string = { match = string.match,
4 lower = string.lower}
5
6 utils.rules = { }
7 utils.globalkeys = { }
8
9 local host = io.popen("hostname")
10 utils.host = host:read("*l")
11
12 function utils.is_dir(path)
13 return os.execute(('[ -d "%s" ]'):format(path))
14 end
15
16 function utils.spawn_terminal(prg, cprop, cb)
17 local join = require("gears.table").join
18 local term_cmd = {terminal}
19 if cprop and cprop.instance then
20 if terminal:match("rxvt") then
21 term_cmd = join(term_cmd, {"-name", cprop.instance})
22 end
23 end
24 awful.spawn(join(term_cmd, {"-e", prg}), cprop, cb)
25 end
26
27 function utils.run_or_kill(prg, cprop, screen)
28 if not prg then
29 do return nil end
30 end
31
32 if not cprop then
33 cprop = { }
34 end
35
36 if not cprop.class then
37 cprop.class = prg
38 end
39
40 if not cprop.instance then
41 cprop.instance = ""
42 end
43
44 cprop.class = string.lower(cprop.class)
45 cprop.instance = string.lower(cprop.instance)
46
47 for k, c in pairs(client.get()) do
48 -- apparently some steam games don't have a client class
49 if c.class == nil then
50 return
51 end
52 local class=string.lower(c.class)
53 local instance=string.lower(c.instance)
54 if string.match(class, cprop.class) and string.match(instance, cprop.instance) then
55 for i, v in ipairs(c:tags()) do
56 c:kill()
57 return
58 end
59 end
60 end
61 awful.spawn.with_shell(prg, screen)
62 end
63
64 -- http://awesome.naquadah.org/wiki/Autostart#Simple_way
65 function utils.run_once(prg,arg_string,pname,screen)
66 if not prg then
67 do return nil end
68 end
69
70 if not pname then
71 pname = prg
72 end
73
74 if not arg_string then
75 awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")",screen)
76 else
77 awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. " ".. arg_string .."' || (" .. prg .. " " .. arg_string .. ")",screen)
78 end
79 end
80
81 function utils.get_default_sink()
82 local f = io.popen('ponymix defaults --short')
83 line = f:read('*l')
84 f:close()
85 return string.match(line, "^sink%s*%d*%s*(.-)%s")
86 end
87
88 function utils.joinTables(t1, t2)
89 for k,v in ipairs(t2) do table.insert(t1, v) end return t1
90 end
91
92 function utils.rules.append(rules)
93 awful.rules.rules = gears.table.join(awful.rules.rules, rules)
94 end
95
96 function utils.globalkeys.append(keys)
97 globalkeys = gears.table.join(globalkeys, keys)
98 end
99
100 function utils.has_battery()
101 return utils.is_dir("/sys/class/power_supply/BAT0")
102 end
103
104 return utils