Remove MODE and format the code
2 files changed, 117 insertions(+), 100 deletions(-)
@@ -4,21 +4,21 @@
-- Neovim version check
if vim.fn.has("nvim-0.9") == 1 then
health.ok("Neovim version >= 0.9")
health.ok("Neovim version >= 0.9")
else
health.error("Neovim 0.9+ is required")
health.error("Neovim 0.9+ is required")
end
-- Zellij executable check
if vim.fn.executable("zellij") == 1 then
health.ok("zellij found in PATH")
health.ok("zellij found in PATH")
else
health.error("zellij not found in PATH")
health.error("zellij not found in PATH")
end
-- check if inside zellij session
if vim.env.ZELLIJ then
health.ok("Running inside Zellij session")
health.ok("Running inside Zellij session")
else
health.warn("Not running inside Zellij (plugin will still load, but actions will fail)")
health.warn("Not running inside Zellij (plugin will still load, but actions will fail)")
end
@@ -1,138 +1,155 @@
local M = {}
-- Modes
local MODE = {
WINDOW = "window",
TAB = "tab",
local direction_translation = {
up = "k",
down = "j",
left = "h",
right = "l",
}
local direction_translation = {
up = "k",
down = "j",
left = "h",
right = "l",
}
-- Run zellij action safely
function M.zellij_action(args)
vim.fn.system("zellij action " .. args)
if vim.v.shell_error ~= 0 then
error("zellij executable not found in PATH")
end
vim.fn.system("zellij action " .. args)
if vim.v.shell_error ~= 0 then
error("zellij executable not found in PATH")
end
end
-- Try vim window move, fallback to zellij
local function navigate(direction, vim_key, fallback)
local before = vim.fn.winnr()
vim.cmd("wincmd " .. vim_key)
local after = vim.fn.winnr()
-- If vim didn't move, fallback to zellij
if before == after then
M.zellij_action(fallback .. " " .. direction)
end
local before = vim.fn.winnr()
vim.cmd("wincmd " .. vim_key)
local after = vim.fn.winnr()
-- If vim didn't move, fallback to zellij
if before == after then
M.zellij_action(fallback .. " " .. direction)
end
end
-- Window navigation
function M.move(direction)
navigate(direction, direction_translation[direction], "move-focus")
navigate(direction, direction_translation[direction], "move-focus")
end
-- Window navigation or tab switch
function M.move_or_tab(direction)
navigate(direction, direction_translation[direction], "move-focus-or-tab")
navigate(direction, direction_translation[direction], "move-focus-or-tab")
end
-- Tab actions
function M.new_tab(opts)
if type(opts) == "table" then
opts = opts.args
end
if opts ~= "" then
M.zellij_action("new-tab " .. opts)
else
M.zellij_action("new-tab")
end
if type(opts) == "table" then
opts = opts.args
end
if opts ~= "" then
M.zellij_action("new-tab " .. opts)
else
M.zellij_action("new-tab")
end
end
function M.rename_tab(name)
if type(name) == "table" then
name = name.args
end
if name == "" or name == nil then
name = vim.fn.input("Tab name: ")
end
if name == nil or name == "" then
vim.notify("No tab name provided", vim.log.levels.ERROR)
return
end
M.zellij_action("rename-tab " .. name)
if type(name) == "table" then
name = name.args
end
if name == "" or name == nil then
name = vim.fn.input("Tab name: ")
end
if name == nil or name == "" then
vim.notify("No tab name provided", vim.log.levels.ERROR)
return
end
M.zellij_action("rename-tab " .. name)
end
function M.move_tab(direction)
M.zellij_action("move-tab " .. direction)
M.zellij_action("move-tab " .. direction)
end
-- Pane actions
function M.new_pane(opts)
if type(opts) == "table" then
opts = opts.args
end
if opts ~= "" then
-- allow custom flags like -d right, -n name, etc
M.zellij_action("new-pane " .. opts)
else
M.zellij_action("new-pane")
end
if type(opts) == "table" then
opts = opts.args
end
if opts ~= "" then
-- allow custom flags like -d right, -n name, etc
M.zellij_action("new-pane " .. opts)
else
M.zellij_action("new-pane")
end
end
function M.rename_pane(name)
if type(name) == "table" then
name = name.args
end
if name == "" or name == nil then
name = vim.fn.input("Pane name: ")
end
if name == nil or name == "" then
vim.notify("No pane name provided", vim.log.levels.ERROR)
return
end
M.zellij_action("rename-pane " .. name)
if type(name) == "table" then
name = name.args
end
if name == "" or name == nil then
name = vim.fn.input("Pane name: ")
end
if name == nil or name == "" then
vim.notify("No pane name provided", vim.log.levels.ERROR)
return
end
M.zellij_action("rename-pane " .. name)
end
function M.move_pane(direction)
M.zellij_action("move-pane " .. direction)
M.zellij_action("move-pane " .. direction)
end
-- Setup user commands
function M.setup()
vim.api.nvim_create_user_command("ZellijUp", function() M.move("up") end, {})
vim.api.nvim_create_user_command("ZellijDown", function() M.move("down") end, {})
vim.api.nvim_create_user_command("ZellijLeft", function() M.move("left") end, {})
vim.api.nvim_create_user_command("ZellijRight", function() M.move("right") end, {})
vim.api.nvim_create_user_command("ZellijLeftTab", function() M.move_or_tab("left") end, {})
vim.api.nvim_create_user_command("ZellijRightTab", function() M.move_or_tab("right") end, {})
vim.api.nvim_create_user_command("ZellijNewTab", M.new_tab, { nargs = "*" })
vim.api.nvim_create_user_command("ZellijRenameTab", M.rename_tab, { nargs = "?" })
vim.api.nvim_create_user_command("ZellijMoveTabRight", function() M.move_tab("right") end, {})
vim.api.nvim_create_user_command("ZellijMoveTabLeft", function() M.move_tab("left") end, {})
vim.api.nvim_create_user_command("ZellijNewPane", M.new_pane, { nargs = "*" })
vim.api.nvim_create_user_command("ZellijRenamePane", M.rename_pane, { nargs = "?" })
vim.api.nvim_create_user_command("ZellijMovePaneUp", function() M.move_pane("up") end, {})
vim.api.nvim_create_user_command("ZellijMovePaneDown", function() M.move_pane("down") end, {})
vim.api.nvim_create_user_command("ZellijMovePaneRight", function() M.move_pane("right") end, {})
vim.api.nvim_create_user_command("ZellijMovePaneLeft", function() M.move_pane("left") end, {})
vim.api.nvim_create_user_command("ZellijUp", function()
M.move("up")
end, {})
vim.api.nvim_create_user_command("ZellijDown", function()
M.move("down")
end, {})
vim.api.nvim_create_user_command("ZellijLeft", function()
M.move("left")
end, {})
vim.api.nvim_create_user_command("ZellijRight", function()
M.move("right")
end, {})
vim.api.nvim_create_user_command("ZellijLeftTab", function()
M.move_or_tab("left")
end, {})
vim.api.nvim_create_user_command("ZellijRightTab", function()
M.move_or_tab("right")
end, {})
vim.api.nvim_create_user_command("ZellijNewTab", M.new_tab, { nargs = "*" })
vim.api.nvim_create_user_command("ZellijRenameTab", M.rename_tab, { nargs = "?" })
vim.api.nvim_create_user_command("ZellijMoveTabRight", function()
M.move_tab("right")
end, {})
vim.api.nvim_create_user_command("ZellijMoveTabLeft", function()
M.move_tab("left")
end, {})
vim.api.nvim_create_user_command("ZellijNewPane", M.new_pane, { nargs = "*" })
vim.api.nvim_create_user_command("ZellijRenamePane", M.rename_pane, { nargs = "?" })
vim.api.nvim_create_user_command("ZellijMovePaneUp", function()
M.move_pane("up")
end, {})
vim.api.nvim_create_user_command("ZellijMovePaneDown", function()
M.move_pane("down")
end, {})
vim.api.nvim_create_user_command("ZellijMovePaneRight", function()
M.move_pane("right")
end, {})
vim.api.nvim_create_user_command("ZellijMovePaneLeft", function()
M.move_pane("left")
end, {})
end
return M