index : neolij.nvim
Seamless Neovim/Zellij integration with built-in navigation, pane/tab management, and Lua scripting.
about summary refs log tree commit diff
neolij.nvim / lua / zellij / init.lua
-rw-r--r-- blob c10684455022138890608a9126baa17d08f27d21 3684 bytes (plain)
1
local M = {}
2
3
local direction_translation = {
4
	up = "k",
5
	down = "j",
6
	left = "h",
7
	right = "l",
8
}
9
-- Run zellij action safely
10
function M.zellij_action(args)
11
	if not os.getenv("ZELLIJ") then
12
		return
13
	end
14
	vim.fn.system("zellij action " .. args)
15
	if vim.v.shell_error ~= 0 then
16
		error("zellij executable not found in PATH")
17
	end
18
end
19
20
-- Try vim window move, fallback to zellij
21
local function navigate(direction, vim_key, fallback)
22
	local before = vim.fn.winnr()
23
	vim.cmd("wincmd " .. vim_key)
24
	local after = vim.fn.winnr()
25
26
	-- If vim didn't move, fallback to zellij
27
	if before == after then
28
		M.zellij_action(fallback .. " " .. direction)
29
	end
30
end
31
32
-- Window navigation
33
function M.move(direction)
34
	navigate(direction, direction_translation[direction], "move-focus")
35
end
36
37
-- Window navigation or tab switch
38
function M.move_or_tab(direction)
39
	navigate(direction, direction_translation[direction], "move-focus-or-tab")
40
end
41
42
-- Tab actions
43
function M.new_tab(opts)
44
	if type(opts) == "table" then
45
		opts = opts.args
46
	end
47
48
	if opts ~= "" then
49
		M.zellij_action("new-tab " .. opts)
50
	else
51
		M.zellij_action("new-tab")
52
	end
53
end
54
55
function M.rename_tab(name)
56
	if type(name) == "table" then
57
		name = name.args
58
	end
59
60
	if name == "" or name == nil then
61
		name = vim.fn.input("Tab name: ")
62
	end
63
64
	if name == nil or name == "" then
65
		vim.notify("No tab name provided", vim.log.levels.ERROR)
66
		return
67
	end
68
69
	M.zellij_action("rename-tab " .. name)
70
end
71
72
function M.move_tab(direction)
73
	M.zellij_action("move-tab " .. direction)
74
end
75
76
-- Pane actions
77
function M.new_pane(opts)
78
	if type(opts) == "table" then
79
		opts = opts.args
80
	end
81
82
	if opts ~= "" then
83
		-- allow custom flags like -d right, -n name, etc
84
		M.zellij_action("new-pane " .. opts)
85
	else
86
		M.zellij_action("new-pane")
87
	end
88
end
89
90
function M.rename_pane(name)
91
	if type(name) == "table" then
92
		name = name.args
93
	end
94
95
	if name == "" or name == nil then
96
		name = vim.fn.input("Pane name: ")
97
	end
98
99
	if name == nil or name == "" then
100
		vim.notify("No pane name provided", vim.log.levels.ERROR)
101
		return
102
	end
103
104
	M.zellij_action("rename-pane " .. name)
105
end
106
107
function M.move_pane(direction)
108
	M.zellij_action("move-pane " .. direction)
109
end
110
111
-- Setup user commands
112
function M.setup()
113
	vim.api.nvim_create_user_command("ZellijUp", function()
114
		M.move("up")
115
	end, {})
116
	vim.api.nvim_create_user_command("ZellijDown", function()
117
		M.move("down")
118
	end, {})
119
	vim.api.nvim_create_user_command("ZellijLeft", function()
120
		M.move("left")
121
	end, {})
122
	vim.api.nvim_create_user_command("ZellijRight", function()
123
		M.move("right")
124
	end, {})
125
126
	vim.api.nvim_create_user_command("ZellijLeftTab", function()
127
		M.move_or_tab("left")
128
	end, {})
129
	vim.api.nvim_create_user_command("ZellijRightTab", function()
130
		M.move_or_tab("right")
131
	end, {})
132
133
	vim.api.nvim_create_user_command("ZellijNewTab", M.new_tab, { nargs = "*" })
134
	vim.api.nvim_create_user_command("ZellijRenameTab", M.rename_tab, { nargs = "?" })
135
	vim.api.nvim_create_user_command("ZellijMoveTabRight", function()
136
		M.move_tab("right")
137
	end, {})
138
	vim.api.nvim_create_user_command("ZellijMoveTabLeft", function()
139
		M.move_tab("left")
140
	end, {})
141
142
	vim.api.nvim_create_user_command("ZellijNewPane", M.new_pane, { nargs = "*" })
143
	vim.api.nvim_create_user_command("ZellijRenamePane", M.rename_pane, { nargs = "?" })
144
	vim.api.nvim_create_user_command("ZellijMovePaneUp", function()
145
		M.move_pane("up")
146
	end, {})
147
	vim.api.nvim_create_user_command("ZellijMovePaneDown", function()
148
		M.move_pane("down")
149
	end, {})
150
	vim.api.nvim_create_user_command("ZellijMovePaneRight", function()
151
		M.move_pane("right")
152
	end, {})
153
	vim.api.nvim_create_user_command("ZellijMovePaneLeft", function()
154
		M.move_pane("left")
155
	end, {})
156
end
157
158
return M