diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 70684e6..c222811 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -6,36 +6,10 @@ require "config.neovide" vim.keymap.set("t", "", "", { silent = true, desc = "Exit Terminal Mode" }) -if vim.env.ZELLIJ ~= nil then - vim.keymap.set("n", "z", function() - local cwd = vim.fn.expand "%:p:h" - vim.system { - "zellij", - "run", - "--close-on-exit", - "--cwd", - cwd, - "--in-place", - "--", - "lazygit", - } - end, { desc = "Open Lazygit (Zellij)" }) -elseif vim.fn.executable "hyprctl" == 1 and vim.fn.executable "footclient" == 1 then - vim.keymap.set("n", "z", function() - local cwd = vim.fn.expand "%:p:h" - vim.system { - "hyprctl", - "dispatch", - "exec", - "--", - "foot", - "--app-id=lazygit", - "--title=lazygit", - "--working-directory=" .. cwd, - "--", - "lazygit", - } - end, { desc = "Open Lazygit (Foot)" }) +if vim.fn.executable "lazygit" == 1 then + require "config.lazygit" end -vim.keymap.set("n", "", [[silent !footclient --no-wait]], { desc = "Open New terminal" }) +if vim.fn.executable "hyprctl" == 1 and vim.fn.executable "footclient" == 1 then + require "config.terminal" +end diff --git a/lua/config/lazygit.lua b/lua/config/lazygit.lua new file mode 100644 index 0000000..1b3de59 --- /dev/null +++ b/lua/config/lazygit.lua @@ -0,0 +1,31 @@ +if vim.env.ZELLIJ ~= nil and not vim.g.neovide then + -- currently running inside zellij session and not inside neovide + vim.keymap.set("n", "z", function() + local cwd = vim.fn.expand "%:p:h" + vim.system { + "zellij", + "run", + "--close-on-exit", + "--cwd", + cwd, + "--in-place", + "--", + "lazygit", + } + end, { desc = "Open Lazygit (Zellij)" }) +elseif vim.fn.executable "hyprctl" == 1 and vim.fn.executable "foot" == 1 then + vim.keymap.set("n", "z", function() + vim.system { + "hyprctl", + "dispatch", + "exec", + "--", + "foot", + "--app-id=lazygit", + "--title=lazygit", + "--working-directory=" .. vim.fn.getcwd(), + "--", + "lazygit", + } + end, { desc = "Open Lazygit (Foot)" }) +end diff --git a/lua/config/terminal.lua b/lua/config/terminal.lua new file mode 100644 index 0000000..5903015 --- /dev/null +++ b/lua/config/terminal.lua @@ -0,0 +1,44 @@ +local session_id = math.random(1, 100000) +local app_id = ([[neovim-terminal-%d]]):format(session_id) +local class = ([[class:%s]]):format(app_id) + +vim.keymap.set("n", "", function() + vim.system({ "hyprctl", "clients", "-j" }, { text = true }, function(output) + for _, client in ipairs(vim.json.decode(output.stdout)) do + if client.class == app_id then + -- focus instead of open new + vim.system { "hyprctl", "dispatch", "focuswindow", class } + return + end + end + vim.schedule(function() + vim.system({ + "hyprctl", + "dispatch", + "exec", + "--", + "footclient", + "--working-directory", + vim.fn.getcwd(), + "--app-id", + app_id, + "--title=neovim-terminal", + }, {}, function() + vim.defer_fn(function() + vim.system { + "hyprctl", + "dispatch", + "resizewindowpixel", + ([[exact 80%% 100%%,%s]]):format(class), + } + end, 50) + end) + vim.api.nvim_create_autocmd("VimLeave", { + pattern = "*", + callback = function() + vim.system { "hyprctl", "dispatch", "closewindow", class } + end, + }) + end) + end) +end, { desc = "Open New terminal" })