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 d984ef56ffb327ca8bfae68c4f17b384d5bf3351 3409 bytes (plain)
1
local M = {}
2
3
-- Modes
4
local MODE = {
5
  WINDOW = "window",
6
  TAB = "tab",
7
}
8
9
  local direction_translation = {
10
    up = "k",
11
    down = "j",
12
    left = "h",
13
    right = "l",
14
  }
15
-- Run zellij action safely
16
function M.zellij_action(args)
17
  vim.fn.system("zellij action " .. args)
18
  if vim.v.shell_error ~= 0 then
19
    error("zellij executable not found in PATH")
20
  end
21
end
22
23
-- Try vim window move, fallback to zellij
24
local function navigate(direction, vim_key, fallback)
25
  local before = vim.fn.winnr()
26
  vim.cmd("wincmd " .. vim_key)
27
  local after = vim.fn.winnr()
28
29
  -- If vim didn't move, fallback to zellij
30
  if before == after then
31
    M.zellij_action(fallback .. " " .. direction)
32
  end
33
end
34
35
-- Window navigation
36
function M.move(direction)
37
  navigate(direction, direction_translation[direction], "move-focus")
38
end
39
40
-- Window navigation or tab switch
41
function M.move_or_tab(direction)
42
  navigate(direction, direction_translation[direction], "move-focus-or-tab")
43
end
44
45
-- Tab actions
46
function M.new_tab()
47
  M.zellij_action("new-tab")
48
end
49
50
function M.rename_tab(name)
51
  if type(name) == "table" then
52
    name = name.args
53
  end
54
55
  if name == "" or name == nil then
56
    name = vim.fn.input("Tab name: ")
57
  end
58
59
  if name == nil or name == "" then
60
    vim.notify("No tab name provided", vim.log.levels.ERROR)
61
    return
62
  end
63
64
  M.zellij_action("rename-tab " .. name)
65
end
66
67
function M.move_tab(direction)
68
  M.zellij_action("move-tab " .. direction)
69
end
70
71
-- Pane actions
72
function M.new_pane()
73
  M.zellij_action("new-pane")
74
end
75
76
function M.rename_pane(name)
77
    if type(name) == "table" then
78
    name = name.args
79
  end
80
81
  if name == "" or name == nil then
82
    name = vim.fn.input("Pane name: ")
83
  end
84
85
  if name == nil or name == "" then
86
    vim.notify("No pane name provided", vim.log.levels.ERROR)
87
    return
88
  end
89
90
  M.zellij_action("rename-pane " .. name)
91
end
92
93
function M.move_pane(direction)
94
  M.zellij_action("move-pane " .. direction)
95
end
96
97
-- Setup user commands
98
function M.setup()
99
  vim.api.nvim_create_user_command("ZellijUp", function() M.move("up") end, {})
100
  vim.api.nvim_create_user_command("ZellijDown", function() M.move("down") end, {})
101
  vim.api.nvim_create_user_command("ZellijLeft", function() M.move("left") end, {})
102
  vim.api.nvim_create_user_command("ZellijRight", function() M.move("right") end, {})
103
104
  vim.api.nvim_create_user_command("ZellijLeftTab", function() M.move_or_tab("left") end, {})
105
  vim.api.nvim_create_user_command("ZellijRightTab", function() M.move_or_tab("right") end, {})
106
107
  vim.api.nvim_create_user_command("ZellijNewTab", M.new_tab, {})
108
  vim.api.nvim_create_user_command("ZellijRenameTab", M.rename_tab, { nargs = "?" })
109
  vim.api.nvim_create_user_command("ZellijMoveTabRight", function() M.move_tab("right") end, {})
110
  vim.api.nvim_create_user_command("ZellijMoveTabLeft", function() M.move_tab("left") end, {})
111
112
  vim.api.nvim_create_user_command("ZellijNewPane", M.new_pane, {})
113
  vim.api.nvim_create_user_command("ZellijRenamePane", M.rename_pane, { nargs = "?" })
114
  vim.api.nvim_create_user_command("ZellijMovePaneUp", function() M.move_pane("up") end, {})
115
  vim.api.nvim_create_user_command("ZellijMovePaneDown", function() M.move_pane("down") end, {})
116
  vim.api.nvim_create_user_command("ZellijMovePaneRight", function() M.move_pane("right") end, {})
117
  vim.api.nvim_create_user_command("ZellijMovePaneLeft", function() M.move_pane("left") end, {})
118
119
end
120
121
return M