Skip to content
numToStr edited this page Dec 12, 2022 · 2 revisions

Basic

To implement a custom multiplexer class, all we need is two methods: zoomed() and navigate(direction)

-- file: mux.lua

---@class Mux
local Mux = {}

---Check if the current mux pane is zoomed in
---@return boolean
function Mux.zoomed()
    return false
end

---Switch pane in mux
---@param direction Direction
---@return Mux
function Mux:navigate(direction)
    return self
end

return Mux

NOTE - Use Mux:zoomed() instead, if you need access to self.

Then you can provide that class directly into mux config option:

require('Navigator').setup({
    mux = require('mux')
})

Mux Detection

If we are not certain that mux is available or not, then we can add little bit of detection code and fallback gracefully using a new() method in the class itself.

-- file: mux.lua

---@class Mux
local Mux = {}

---Initialize new Mux navigator instance
---@return Mux
function Mux:new()
    assert(os.getenv('TERM_PROGRAM') == 'Mux', string.format('[Navigator] Mux is not running!'))

    local state = {}
    self.__index = self
    return setmetatable(state, self)
end

---Check if the current mux pane is zoomed in
---@return boolean
function Mux.zoomed()
    return false
end

---Switch pane in mux
---@param direction Direction
---@return Mux
function Mux:navigate(direction)
    return self
end


return Mux

NOTE - There is a high possibility, that the detection logic will be different for every mux so make sure you use optimal solution to detect your mux.

Add little bit of error handling using pcall, then profit!!

local ok_mux, mux = pcall(function ()
    return require('mux'):new()
end)

require('Navigator').setup({
    mux = ok_mux and mux or 'auto',
})

Extending from Vi

In the plugin, there is a Vi class which describes the neovim window navigation and can be used as base class for other implementations. The pro of extending is that you don't need to define redundant methods, let say if your mux doesn't support zoom detection then you don't need to define zoomed() method.

-- file: mux.lua

---@class Mux: Vi
local Mux = require('Navigator.mux.vi'):new()

---Initialize new Mux navigator instance
---@return Mux
function Mux:new()
    assert(os.getenv('TERM_PROGRAM') == 'Mux', string.format('[Navigator] Mux is not running!'))

    local state = {}
    self.__index = self
    return setmetatable(state, self)
end

---------------------------------------------------------------
-- You can omit `zoomed()` and it'll work without any error. --
---------------------------------------------------------------

---Switch pane in mux
---@param direction Direction
---@return Mux
function Mux:navigate(direction)
    return self
end

return Mux

Usage is same as previous

local ok_mux, mux = pcall(function()
    return require('mux'):new()
end)

require('Navigator').setup({
    mux = ok_mux and mux or 'auto',
})