nixos/home/wezterm/modules/navigator.lua
2024-11-30 13:53:43 -08:00

53 lines
1.7 KiB
Lua

local wezterm = require("wezterm")
local M = {}
-- https://github.com/lrvdijk/dotfiles/blob/master/wezterm/wezterm.lua
-- Integration with neovim panes
function M.isViProcess(pane)
-- get_foreground_process_name On Linux, macOS and Windows,
-- the process can be queried to determine this path. Other operating systems
-- (notably, FreeBSD and other unix systems) are not currently supported
-- return pane:get_foreground_process_name():find('n?vim') ~= nil
-- Use get_title as it works for multiplexed sessions too
return pane:get_title():find("n?vim") ~= nil
end
function M.conditionalActivatePane(window, pane, pane_direction, vim_direction)
local vim_pane_changed = false
if M.isViProcess(pane) then
local before = pane:get_cursor_position()
window:perform_action(
-- This should match the keybinds you set in Neovim.
wezterm.action.SendKey({ key = vim_direction, mods = "CTRL" }),
pane
)
wezterm.sleep_ms(50)
local after = pane:get_cursor_position()
if before.x ~= after.x and before.y ~= after.y then
vim_pane_changed = true
end
end
if not vim_pane_changed then
window:perform_action(wezterm.action.ActivatePaneDirection(pane_direction), pane)
end
end
function M.apply_to_config(config)
wezterm.on("ActivatePaneDirection-right", function(window, pane)
M.conditionalActivatePane(window, pane, "Right", "l")
end)
wezterm.on("ActivatePaneDirection-left", function(window, pane)
M.conditionalActivatePane(window, pane, "Left", "h")
end)
wezterm.on("ActivatePaneDirection-up", function(window, pane)
M.conditionalActivatePane(window, pane, "Up", "k")
end)
wezterm.on("ActivatePaneDirection-down", function(window, pane)
M.conditionalActivatePane(window, pane, "Down", "j")
end)
end
return M