From 83fd3ec978c1bdbe7d2936118336a4fcd9461a22 Mon Sep 17 00:00:00 2001 From: Ottatop Date: Thu, 20 Jul 2023 11:56:45 -0500 Subject: [PATCH] Remove repetition --- api/lua/LICENSE | 2 +- api/lua/example_config.lua | 26 ---- api/lua/test_config.lua | 249 +++++++++++++++++++++++++++++++++++++ api/lua/window.lua | 39 ++---- 4 files changed, 262 insertions(+), 54 deletions(-) create mode 100644 api/lua/test_config.lua diff --git a/api/lua/LICENSE b/api/lua/LICENSE index 50f7b4c..3da0834 100644 --- a/api/lua/LICENSE +++ b/api/lua/LICENSE @@ -2,7 +2,7 @@ MIT License Copyright (c) 2023 Ottatop -This license applies to the example_config.lua file only. +This license applies to the *_config.lua files. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/api/lua/example_config.lua b/api/lua/example_config.lua index b6352af..ccc709b 100644 --- a/api/lua/example_config.lua +++ b/api/lua/example_config.lua @@ -64,32 +64,6 @@ require("pinnacle").setup(function(pinnacle) process.spawn("nautilus") end) - -- Just testing stuff - input.keybind({ mod_key }, keys.h, function() - local wins = window.get_all() - for _, win in pairs(wins) do - print("loc: " .. (win:loc() and win:loc().x or "nil") .. ", " .. (win:loc() and win:loc().y or "nil")) - print("size: " .. (win:size() and win:size().w or "nil") .. ", " .. (win:size() and win:size().h or "nil")) - print("class: " .. (win:class() or "nil")) - print("title: " .. (win:title() or "nil")) - print("float: " .. tostring(win:floating())) - end - - local op = output.get_focused() --[[@as Output]] - print("res: " .. (op:res() and (op:res().w .. ", " .. op:res().h) or "nil")) - print("loc: " .. (op:loc() and (op:loc().x .. ", " .. op:loc().y) or "nil")) - print("rr: " .. (op:refresh_rate() or "nil")) - print("make: " .. (op:make() or "nil")) - print("model: " .. (op:model() or "nil")) - print("focused: " .. (tostring(op:focused()))) - - -- local tags = tag.get_on_output(output.get_focused()) - -- for _, tg in pairs(tags) do - -- print(tg:name()) - -- print(tg:output() and tg:output().name or "nil output") - -- end - end) - -- Tags --------------------------------------------------------------------------- output.connect_for_all(function(op) diff --git a/api/lua/test_config.lua b/api/lua/test_config.lua new file mode 100644 index 0000000..2920958 --- /dev/null +++ b/api/lua/test_config.lua @@ -0,0 +1,249 @@ +-- SPDX-License-Identifier: MIT + +-- Just like in Awesome, if you want access to Luarocks packages, this needs to be called. +-- NOTE: The loader doesn't load from the local Luarocks directory (probably in ~/.luarocks), +-- | so if you have any rocks installed with --local, +-- | you may need to add those paths to package.path and package.cpath. +-- Alternatively, you can add +-- eval $(luarocks path --bin) +-- to your shell's startup script to permanently have access to Luarocks in all your Lua files. +pcall(require, "luarocks.loader") + +-- Neovim users be like: +require("pinnacle").setup(function(pinnacle) + local input = pinnacle.input -- Key and mouse binds + local window = pinnacle.window -- Window management + local process = pinnacle.process -- Process spawning + local tag = pinnacle.tag -- Tag management + local output = pinnacle.output -- Output management + + -- Every key supported by xkbcommon. + -- Support for just putting in a string of a key is intended. + local keys = input.keys + + ---@type Modifier + local mod_key = "Ctrl" -- This is set to `Ctrl` instead of `Super` to not conflict with your WM/DE keybinds + -- ^ Add type annotations for that sweet, sweet autocomplete + + local terminal = "alacritty" + + -- Keybinds ---------------------------------------------------------------------- + + input.keybind({ mod_key, "Alt" }, keys.q, pinnacle.quit) + + input.keybind({ mod_key, "Alt" }, keys.c, function() + -- The commented out line may crash the config process if you have no windows open. + -- There is no nil warning here due to limitations in Lua LS type checking, so check for nil as shown below. + -- window.get_focused():close() + local win = window.get_focused() + if win ~= nil then + win:close() + end + end) + + input.keybind({ mod_key, "Alt" }, keys.space, function() + local win = window.get_focused() + if win ~= nil then + win:toggle_floating() + end + end) + + input.keybind({ mod_key }, keys.Return, function() + process.spawn(terminal, function(stdout, stderr, exit_code, exit_msg) + -- do something with the output here + end) + end) + + input.keybind({ mod_key }, keys.l, function() + process.spawn("kitty") + end) + input.keybind({ mod_key }, keys.k, function() + process.spawn("foot") + end) + input.keybind({ mod_key }, keys.j, function() + process.spawn("nautilus") + end) + + -- Just testing stuff + input.keybind({ mod_key }, keys.h, function() + local wins = window.get_all() + for _, win in pairs(wins) do + print("loc: " .. (win:loc() and win:loc().x or "nil") .. ", " .. (win:loc() and win:loc().y or "nil")) + print("size: " .. (win:size() and win:size().w or "nil") .. ", " .. (win:size() and win:size().h or "nil")) + print("class: " .. (win:class() or "nil")) + print("title: " .. (win:title() or "nil")) + print("float: " .. tostring(win:floating())) + end + + print("----------------------") + + local op = output.get_focused() --[[@as Output]] + print("res: " .. (op:res() and (op:res().w .. ", " .. op:res().h) or "nil")) + print("loc: " .. (op:loc() and (op:loc().x .. ", " .. op:loc().y) or "nil")) + print("rr: " .. (op:refresh_rate() or "nil")) + print("make: " .. (op:make() or "nil")) + print("model: " .. (op:model() or "nil")) + print("focused: " .. (tostring(op:focused()))) + + print("----------------------") + + local wins = window.get_by_class("Alacritty") + for _, win in pairs(wins) do + print("loc: " .. (win:loc() and win:loc().x or "nil") .. ", " .. (win:loc() and win:loc().y or "nil")) + print("size: " .. (win:size() and win:size().w or "nil") .. ", " .. (win:size() and win:size().h or "nil")) + print("class: " .. (win:class() or "nil")) + print("title: " .. (win:title() or "nil")) + print("float: " .. tostring(win:floating())) + end + + print("----------------------") + + local wins = window.get_by_title("~/p/pinnacle") + for _, win in pairs(wins) do + print("loc: " .. (win:loc() and win:loc().x or "nil") .. ", " .. (win:loc() and win:loc().y or "nil")) + print("size: " .. (win:size() and win:size().w or "nil") .. ", " .. (win:size() and win:size().h or "nil")) + print("class: " .. (win:class() or "nil")) + print("title: " .. (win:title() or "nil")) + print("float: " .. tostring(win:floating())) + end + + print("----------------------") + + -- local tags = tag.get_on_output(output.get_focused()) + -- for _, tg in pairs(tags) do + -- print(tg:name()) + -- print(tg:output() and tg:output().name or "nil output") + -- end + end) + + -- Tags --------------------------------------------------------------------------- + + output.connect_for_all(function(op) + op:add_tags("1", "2", "3", "4", "5") + -- Same as tag.add(op, "1", "2", "3", "4", "5") + tag.toggle("1", op) + end) + + ---@type Layout[] + local layouts = { + "MasterStack", + "Dwindle", + "Spiral", + "CornerTopLeft", + "CornerTopRight", + "CornerBottomLeft", + "CornerBottomRight", + } + local indices = {} + + -- Layout cycling + -- Yes, this is overly complicated and yes, I'll cook up a way to make it less so. + input.keybind({ mod_key }, keys.space, function() + local tags = output.get_focused():tags() + for _, tg in pairs(tags) do + if tg:active() then + local name = tg:name() + if name == nil then + return + end + tg:set_layout(layouts[indices[name] or 1]) + if indices[name] == nil then + indices[name] = 2 + else + if indices[name] + 1 > #layouts then + indices[name] = 1 + else + indices[name] = indices[name] + 1 + end + end + break + end + end + end) + input.keybind({ mod_key, "Shift" }, keys.space, function() + local tags = output.get_focused():tags() + for _, tg in pairs(tags) do + if tg:active() then + local name = tg:name() + if name == nil then + return + end + tg:set_layout(layouts[indices[name] or #layouts]) + if indices[name] == nil then + indices[name] = #layouts - 1 + else + if indices[name] - 1 < 1 then + indices[name] = #layouts + else + indices[name] = indices[name] - 1 + end + end + break + end + end + end) + + input.keybind({ mod_key }, keys.KEY_1, function() + tag.switch_to("1") + end) + input.keybind({ mod_key }, keys.KEY_2, function() + tag.switch_to("2") + end) + input.keybind({ mod_key }, keys.KEY_3, function() + tag.switch_to("3") + end) + input.keybind({ mod_key }, keys.KEY_4, function() + tag.switch_to("4") + end) + input.keybind({ mod_key }, keys.KEY_5, function() + tag.switch_to("5") + end) + + input.keybind({ mod_key, "Shift" }, keys.KEY_1, function() + tag.toggle("1") + end) + input.keybind({ mod_key, "Shift" }, keys.KEY_2, function() + tag.toggle("2") + end) + input.keybind({ mod_key, "Shift" }, keys.KEY_3, function() + tag.toggle("3") + end) + input.keybind({ mod_key, "Shift" }, keys.KEY_4, function() + tag.toggle("4") + end) + input.keybind({ mod_key, "Shift" }, keys.KEY_5, function() + tag.toggle("5") + end) + + input.keybind({ mod_key, "Alt" }, keys.KEY_1, function() + window.get_focused():move_to_tag("1") + end) + input.keybind({ mod_key, "Alt" }, keys.KEY_2, function() + window.get_focused():move_to_tag("2") + end) + input.keybind({ mod_key, "Alt" }, keys.KEY_3, function() + window.get_focused():move_to_tag("3") + end) + input.keybind({ mod_key, "Alt" }, keys.KEY_4, function() + window.get_focused():move_to_tag("4") + end) + input.keybind({ mod_key, "Alt" }, keys.KEY_5, function() + window.get_focused():move_to_tag("5") + end) + + input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_1, function() + window.get_focused():toggle_tag("1") + end) + input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_2, function() + window.get_focused():toggle_tag("2") + end) + input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_3, function() + window.get_focused():toggle_tag("3") + end) + input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_4, function() + window.get_focused():toggle_tag("4") + end) + input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_5, function() + window.get_focused():toggle_tag("5") + end) +end) diff --git a/api/lua/window.lua b/api/lua/window.lua index 6c1ba46..092a74f 100644 --- a/api/lua/window.lua +++ b/api/lua/window.lua @@ -259,57 +259,42 @@ local window = {} ---@param class string The class. For example, Alacritty's class is "Alacritty". ---@return Window[] function window.get_by_class(class) - SendRequest("GetWindows") - - local response = ReadMsg() - - local window_ids = response.RequestResponse.response.Windows.window_ids + local windows = window.get_all() ---@type Window[] - local windows = {} - for _, window_id in pairs(window_ids) do - local w = new_window({ id = window_id }) + local windows_ret = {} + for _, w in pairs(windows) do if w:class() == class then - table.insert(windows, w) + table.insert(windows_ret, w) end end - return windows + return windows_ret end ---Get all windows with the specified title. ---@param title string The title. ---@return Window[] function window.get_by_title(title) - SendRequest("GetWindows") - - local response = ReadMsg() - - local window_ids = response.RequestResponse.response.Windows.window_ids + local windows = window.get_all() ---@type Window[] - local windows = {} - for _, window_id in pairs(window_ids) do - local w = new_window({ id = window_id }) + local windows_ret = {} + for _, w in pairs(windows) do if w:title() == title then - table.insert(windows, w) + table.insert(windows_ret, w) end end - return windows + return windows_ret end ---Get the currently focused window. ---@return Window|nil function window.get_focused() - SendRequest("GetWindows") + local windows = window.get_all() - local response = ReadMsg() - - local window_ids = response.RequestResponse.response.Windows.window_ids - - for _, window_id in pairs(window_ids) do - local w = new_window({ id = window_id }) + for _, w in pairs(windows) do if w:focused() then return w end