]> git.rmz.io Git - dotfiles.git/blob - awesome/utils.lua
nvim: add FPP copyright snippet
[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 local function terminal_cmd(prg, name)
17 local join = require("gears.table").join
18 local term_cmd = {terminal}
19 if name then
20 if terminal:match("rxvt") then
21 term_cmd = join(term_cmd, {"-name", name})
22 end
23 end
24 return join(term_cmd, {"-e", prg})
25 end
26
27 function utils.spawn_terminal(prg, cprop, cb)
28 if not cprop then
29 cprop = {}
30 end
31 local name = cprop.instance
32 awful.spawn(terminal_cmd(prg, name), cprop, cb)
33 end
34
35 function utils.spawn_terminal_once(prg, rules, matcher, unique_id, cb)
36 local name = rules.instance
37 awful.spawn.once(terminal_cmd(prg, name), rules, matcher, unique_id, cb)
38 end
39
40 function utils.run_or_kill(prg, cprop, screen)
41 if not prg then
42 do return nil end
43 end
44
45 if not cprop then
46 cprop = { }
47 end
48
49 if not cprop.class then
50 cprop.class = prg
51 end
52
53 if not cprop.instance then
54 cprop.instance = ""
55 end
56
57 cprop.class = string.lower(cprop.class)
58 cprop.instance = string.lower(cprop.instance)
59
60 for k, c in pairs(client.get()) do
61 -- apparently some steam games don't have a client class
62 if c.class == nil then
63 return
64 end
65 local class=string.lower(c.class)
66 local instance=string.lower(c.instance)
67 if string.match(class, cprop.class) and string.match(instance, cprop.instance) then
68 for i, v in ipairs(c:tags()) do
69 c:kill()
70 return
71 end
72 end
73 end
74 awful.spawn.with_shell(prg, screen)
75 end
76
77 -- http://awesome.naquadah.org/wiki/Autostart#Simple_way
78 function utils.run_once(prg,arg_string,pname,screen)
79 if not prg then
80 do return nil end
81 end
82
83 if not pname then
84 pname = prg
85 end
86
87 if not arg_string then
88 awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")",screen)
89 else
90 awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. " ".. arg_string .."' || (" .. prg .. " " .. arg_string .. ")",screen)
91 end
92 end
93
94 function utils.get_default_sink()
95 local f = io.popen('pactl get-default-sink')
96 line = f:read('*l')
97 f:close()
98 return string.match(line, "^sink%s*%d*%s*(.-)%s")
99 end
100
101 function utils.joinTables(t1, t2)
102 for k,v in ipairs(t2) do table.insert(t1, v) end return t1
103 end
104
105 function utils.rules.append(rules)
106 awful.rules.rules = gears.table.join(awful.rules.rules, rules)
107 end
108
109 function utils.globalkeys.append(keys)
110 globalkeys = gears.table.join(globalkeys, keys)
111 end
112
113 function utils.has_battery()
114 return utils.is_dir("/sys/class/power_supply/BAT0")
115 end
116
117 return utils