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