index : neolij.nvim
Seamless Neovim/Zellij integration with built-in navigation, pane/tab management, and Lua scripting.
about summary refs log tree commit diff

Diffstat (limited to 'lua/zellij/init.lua')

-rw-r--r-- lua/zellij/init.lua 205
 1 file changed, 111 insertions(+), 94 deletions(-)

diff --git a/lua/zellij/init.lua b/lua/zellij/init.lua
index 8e3908d..9c819e1 100644
--- a/lua/zellij/init.lua
+++ b/lua/zellij/init.lua
@@ -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