|
| 1 | +---@mod navigator.zellij Zellij navigator |
| 2 | +---@brief [[ |
| 3 | +---This module provides navigation and interaction for Zellij, and uses |navigator.vi| |
| 4 | +---as a base class. This is used automatically when zellij is detected on host system |
| 5 | +---but can also be used to manually override the mux. |
| 6 | +--- |
| 7 | +---@brief ]] |
| 8 | + |
| 9 | +---@private |
| 10 | +---@class Zellij: Vi |
| 11 | +---@field private direction table<Direction, string> |
| 12 | +---@field private execute fun(arg: string): unknown |
| 13 | +local Zellij = require('Navigator.mux.vi'):new() |
| 14 | + |
| 15 | +---Creates a new Zellij navigator instance |
| 16 | +---@return Zellij |
| 17 | +---@usage [[ |
| 18 | +---local ok, zellij = pcall(function() |
| 19 | +--- return require('Navigator.mux.zellij'):new() |
| 20 | +---end) |
| 21 | +--- |
| 22 | +---require('Navigator').setup({ |
| 23 | +--- mux = ok and zellij or 'auto' |
| 24 | +---}) |
| 25 | +---@usage ]] |
| 26 | +function Zellij:new() |
| 27 | + assert(os.getenv('ZELLIJ'), '[Navigator] Zellij is not running!') |
| 28 | + local U = require('Navigator.utils') |
| 29 | + |
| 30 | + ---@type Zellij |
| 31 | + local state = { |
| 32 | + execute = function(arg) |
| 33 | + local cmd = string.format('zellij action %s', arg) |
| 34 | + return U.execute(cmd) |
| 35 | + end, |
| 36 | + direction = { h = 'left', k = 'up', l = 'right', j = 'down' }, |
| 37 | + } |
| 38 | + self.__index = self |
| 39 | + return setmetatable(state, self) |
| 40 | +end |
| 41 | + |
| 42 | +---Switch pane in zellij |
| 43 | +---@param direction Direction See |navigator.api.Direction| |
| 44 | +---@return Zellij |
| 45 | +function Zellij:navigate(direction) |
| 46 | + self.execute(string.format('move-focus %s', self.direction[direction])) |
| 47 | + return self |
| 48 | +end |
| 49 | + |
| 50 | +return Zellij |
0 commit comments