]> git.rmz.io Git - dotfiles.git/blob - awesome/utils.lua
Backup UH
[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.run_or_kill(prg, cprop, screen)
17 if not prg then
18 do return nil end
19 end
20
21 if not cprop then
22 cprop = { }
23 end
24
25 if not cprop.class then
26 cprop.class = prg
27 end
28
29 if not cprop.instance then
30 cprop.instance = ""
31 end
32
33 cprop.class = string.lower(cprop.class)
34 cprop.instance = string.lower(cprop.instance)
35
36 for k, c in pairs(client.get()) do
37 -- apparently some steam games don't have a client class
38 if c.class == nil then
39 return
40 end
41 local class=string.lower(c.class)
42 local instance=string.lower(c.instance)
43 if string.match(class, cprop.class) and string.match(instance, cprop.instance) then
44 for i, v in ipairs(c:tags()) do
45 c:kill()
46 return
47 end
48 end
49 end
50 awful.spawn.with_shell(prg, screen)
51 end
52
53 -- http://awesome.naquadah.org/wiki/Autostart#Simple_way
54 function utils.run_once(prg,arg_string,pname,screen)
55 if not prg then
56 do return nil end
57 end
58
59 if not pname then
60 pname = prg
61 end
62
63 if not arg_string then
64 awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")",screen)
65 else
66 awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. " ".. arg_string .."' || (" .. prg .. " " .. arg_string .. ")",screen)
67 end
68 end
69
70 function utils.get_default_sink()
71 local f = io.popen('ponymix defaults --short')
72 line = f:read('*l')
73 f:close()
74 return string.match(line, "^sink%s*%d*%s*(.-)%s")
75 end
76
77 function utils.joinTables(t1, t2)
78 for k,v in ipairs(t2) do table.insert(t1, v) end return t1
79 end
80
81 function utils.rules.append(rules)
82 awful.rules.rules = awful.util.table.join(awful.rules.rules, rules)
83 end
84
85 function utils.globalkeys.append(keys)
86 globalkeys = awful.util.table.join(globalkeys, keys)
87 end
88
89 function utils.has_battery()
90 return utils.is_dir("/sys/class/power_supply/BAT0")
91 end
92
93 return utils