use local capi instead of global

this improves performance drastically.
This commit is contained in:
BZ 2021-03-09 12:14:14 +01:00
parent e965635749
commit 5b2c6e6799
1 changed files with 88 additions and 109 deletions

197
init.lua
View File

@ -1,122 +1,101 @@
local capi = { local capi = {root = root, screen = screen, client = client, keygrabber = keygrabber}
root = root,
screen = screen,
client = client,
keygrabber = keygrabber
}
local awful = require("awful") local awful = require("awful")
local glib = require("lgi").GLib local glib = require("lgi").GLib
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
local module = {} local module = {}
local keys = { local keys = {up = {"k"}, down = {"j"}, left = {"h"}, right = {"l"}}
up = { "k" },
down = { "j" },
left = { "h" },
right = { "l" }
}
local function new(ks) local function new(ks)
keys = ks or keys keys = ks or keys
local aw = {} local aw = {}
glib.idle_add(glib.PRIORITY_DEFAULT_IDLE, function() glib.idle_add(glib.PRIORITY_DEFAULT_IDLE, function()
for k, v in pairs(keys) do for k, v in pairs(keys) do
for _, key_name in ipairs(v) do for _, key_name in ipairs(v) do
aw[#aw + 1] = awful.key( aw[#aw + 1] = awful.key({"Mod4"}, key_name, function()
{ "Mod4" }, module.focus(k)
key_name, end, {description = "focus " .. key_name .. " window", group = "client"})
function() end
module.focus(k) end
end, capi.root.keys(awful.util.table.join(capi.root.keys(), unpack(aw)))
{ end)
description = "Change focus to the " .. key_name .. " window", return module
group = "Navigator"
}
)
end
end
capi.root.keys(awful.util.table.join(capi.root.keys(), unpack(aw)))
end)
return module
end end
function module.focus(dir) function module.focus(dir)
local c = client.focus local c = capi.client.focus
local client_name = c and c.name or ""
local client_name = c and c.name or "" if string.find(client_name, "- N?VIM$") then
capi.keygrabber.stop()
if string.find(client_name, "- N?VIM$") then capi.root.fake_input("key_release", "Super_L")
keygrabber.stop() capi.root.fake_input("key_release", "Control_L")
root.fake_input("key_release", "Super_L") capi.root.fake_input("key_press", "Control_L")
root.fake_input("key_release", "Control_L") if dir == "left" then
root.fake_input("key_press", "Control_L") capi.root.fake_input("key_release", "h")
capi.root.fake_input("key_press", "h")
if dir == "left" then capi.root.fake_input("key_release", "h")
root.fake_input("key_release", "h") else
root.fake_input("key_press", "h") if dir == "right" then
root.fake_input("key_release", "h") capi.root.fake_input("key_release", "l")
else capi.root.fake_input("key_press", "l")
if dir == "right" then capi.root.fake_input("key_release", "l")
root.fake_input("key_release", "l") else
root.fake_input("key_press", "l") if dir == "up" then
root.fake_input("key_release", "l") capi.root.fake_input("key_release", "k")
else capi.root.fake_input("key_press", "k")
if dir == "up" then capi.root.fake_input("key_release", "k")
root.fake_input("key_release", "k") else
root.fake_input("key_press", "k") if dir == "down" then
root.fake_input("key_release", "k") capi.root.fake_input("key_release", "j")
else capi.root.fake_input("key_press", "j")
if dir == "down" then capi.root.fake_input("key_release", "j")
root.fake_input("key_release", "j") end
root.fake_input("key_press", "j") end
root.fake_input("key_release", "j") end
end end
end capi.root.fake_input("key_release", "Control_L")
end capi.root.fake_input("key_press", "Super_L")
end return
root.fake_input("key_release", "Control_L") else
root.fake_input("key_press", "Super_L") if string.find(client_name, "- TMUX$") then
return capi.keygrabber.stop()
else capi.root.fake_input("key_release", "Super_L")
if string.find(client_name, "- TMUX$") then capi.root.fake_input("key_press", "Control_L")
keygrabber.stop() if dir == "left" then
root.fake_input("key_release", "Super_L") capi.root.fake_input("key_release", "Left")
root.fake_input("key_press", "Control_L") capi.root.fake_input("key_press", "Left")
capi.root.fake_input("key_release", "Left")
if dir == "left" then else
root.fake_input("key_release", "Left") if dir == "right" then
root.fake_input("key_press", "Left") capi.root.fake_input("key_release", "Right")
root.fake_input("key_release", "Left") capi.root.fake_input("key_press", "Right")
else capi.root.fake_input("key_release", "Right")
if dir == "right" then else
root.fake_input("key_release", "Right") if dir == "up" then
root.fake_input("key_press", "Right") capi.root.fake_input("key_release", "Up")
root.fake_input("key_release", "Right") capi.root.fake_input("key_press", "Up")
else capi.root.fake_input("key_release", "Up")
if dir == "up" then else
root.fake_input("key_release", "Up") if dir == "down" then
root.fake_input("key_press", "Up") capi.root.fake_input("key_release", "Down")
root.fake_input("key_release", "Up") capi.root.fake_input("key_press", "Down")
else capi.root.fake_input("key_release", "Down")
if dir == "down" then end
root.fake_input("key_release", "Down") end
root.fake_input("key_press", "Down") end
root.fake_input("key_release", "Down") end
end capi.root.fake_input("key_release", "Control_L")
end capi.root.fake_input("key_press", "Super_L")
end return
end else
root.fake_input("key_release", "Control_L") awful.client.focus.global_bydirection(dir)
root.fake_input("key_press", "Super_L") end
return end
else
awful.client.focus.global_bydirection(dir)
end
end
end end
return setmetatable(module, { __call = function(_, ...) return setmetatable(module, {
return new(...) __call = function(_, ...)
end }) return new(...)
end
})