]> git.rmz.io Git - dotfiles.git/blobdiff - awesome/utils.lua
lazyvim: absorb icons into rmz.util.ui
[dotfiles.git] / awesome / utils.lua
index 8bbf3400941d2cf8de6cd4c353bdef8b9c537ab7..13856335ad3c7acbc919e44b8ab88023260ebf45 100644 (file)
@@ -1,26 +1,84 @@
-local utils = { }
+local utils  = { }
+local io     = { popen = io.popen }
+local string = { match = string.match,
+                 lower = string.lower}
+
 utils.rules = { }
 utils.globalkeys = { }
 
 local host = io.popen("hostname")
 utils.host = host:read("*l")
 
 utils.rules = { }
 utils.globalkeys = { }
 
 local host = io.popen("hostname")
 utils.host = host:read("*l")
 
-function utils.run_or_kill(command)
-    -- Check throught the clients if the class match the command
-    local lower_command=string.lower(command)
+function utils.is_dir(path)
+    return os.execute(('[ -d "%s" ]'):format(path))
+end
+
+local function terminal_cmd(prg, name)
+    local join = require("gears.table").join
+    local term_cmd = {terminal}
+    if terminal:match("rxvt") then
+        if name then
+            term_cmd = join(term_cmd, {"-name", name})
+        end
+    elseif terminal:match("wezterm") then
+        term_cmd = join(term_cmd, {"start"})
+        if name then
+            term_cmd = join(term_cmd, {"--class", name})
+        end
+    else
+        term_cmd = join(term_cmd, {"-e"})
+    end
+    return join(term_cmd, {prg})
+end
+
+function utils.spawn_terminal(prg, cprop, cb)
+    if not cprop then
+        cprop = {}
+    end
+    local name = cprop.instance
+    awful.spawn(terminal_cmd(prg, name), cprop, cb)
+end
+
+function utils.spawn_terminal_once(prg, rules, matcher, unique_id, cb)
+    local name = rules.instance
+    awful.spawn.once(terminal_cmd(prg, name), rules, matcher, unique_id, cb)
+end
+
+function utils.run_or_kill(prg, cprop, screen)
+    if not prg then
+        do return nil end
+    end
+
+    if not cprop then
+        cprop = { }
+    end
+
+    if not cprop.class then
+        cprop.class = prg
+    end
+
+    if not cprop.instance then
+        cprop.instance = ""
+    end
+
+    cprop.class = string.lower(cprop.class)
+    cprop.instance = string.lower(cprop.instance)
+
     for k, c in pairs(client.get()) do
         -- apparently some steam games don't have a client class
     for k, c in pairs(client.get()) do
         -- apparently some steam games don't have a client class
-        if c.class ~= nil then
-            local class=string.lower(c.class)
-            if string.match(class, lower_command) then
-                for i, v in ipairs(c:tags()) do
-                    c:kill()
-                    return
-                end
+        if c.class == nil then
+            return
+        end
+        local class=string.lower(c.class)
+        local instance=string.lower(c.instance)
+        if string.match(class, cprop.class) and string.match(instance, cprop.instance) then
+            for i, v in ipairs(c:tags()) do
+                c:kill()
+                return
             end
         end
     end
             end
         end
     end
-    awful.util.spawn(command)
+    awful.spawn.with_shell(prg, screen)
 end
 
 -- http://awesome.naquadah.org/wiki/Autostart#Simple_way
 end
 
 -- http://awesome.naquadah.org/wiki/Autostart#Simple_way
@@ -33,24 +91,34 @@ function utils.run_once(prg,arg_string,pname,screen)
        pname = prg
     end
 
        pname = prg
     end
 
-    if not arg_string then 
-        awful.util.spawn_with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")",screen)
+    if not arg_string then
+        awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. "' || (" .. prg .. ")",screen)
     else
     else
-        awful.util.spawn_with_shell("pgrep -f -u $USER -x '" .. pname .. " ".. arg_string .."' || (" .. prg .. " " .. arg_string .. ")",screen)
+        awful.spawn.with_shell("pgrep -f -u $USER -x '" .. pname .. " ".. arg_string .."' || (" .. prg .. " " .. arg_string .. ")",screen)
     end
 end
 
     end
 end
 
+function utils.get_default_sink()
+    local f = io.popen('pactl get-default-sink')
+    line = f:read('*l')
+    f:close()
+    return string.match(line, "^sink%s*%d*%s*(.-)%s")
+end
 
 function utils.joinTables(t1, t2)
     for k,v in ipairs(t2) do table.insert(t1, v) end return t1
 end
 
 function utils.rules.append(rules)
 
 function utils.joinTables(t1, t2)
     for k,v in ipairs(t2) do table.insert(t1, v) end return t1
 end
 
 function utils.rules.append(rules)
-    awful.rules.rules = awful.util.table.join(awful.rules.rules, rules)
+    awful.rules.rules = gears.table.join(awful.rules.rules, rules)
 end
 
 function utils.globalkeys.append(keys)
 end
 
 function utils.globalkeys.append(keys)
-    globalkeys = awful.util.table.join(globalkeys, keys)
+    globalkeys = gears.table.join(globalkeys, keys)
+end
+
+function utils.has_battery()
+    return utils.is_dir("/sys/class/power_supply/BAT0")
 end
 
 return utils
 end
 
 return utils