Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mux): Add Wezterm plugin for easier setup on Wezterm (experimental) #144

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,30 @@ bind-key -T copy-mode-vi 'C-\' select-pane -l
> you can check how to obtain a nightly build by [following the instructions here](https://wezfurlong.org/wezterm/installation.html).

First, ensure that the `wezterm` CLI is on your `$PATH`, as the CLI is used by the integration.
Then, add the following snippet to your `~/.config/wezterm/wezterm.lua`:

Then, if you're on Wezterm nightly, you can use Wezterm's [experimental plugin loader](https://github.com/wez/wezterm/commit/e4ae8a844d8feaa43e1de34c5cc8b4f07ce525dd):

```lua
local wezterm = require('wezterm')
local smart_splits = wezterm.plugin.require('https://github.com/mrjones2014/smart-splits.nvim')
local config = wezterm.config_builder()
-- you can put the rest of your Wezterm config here
smart_splits.apply_to_config(config, {
-- the default config is here, if you'd like to use the default keys,
-- you can omit this configuration table parameter and just use
-- smart_splits.apply_to_config(config)

-- directional keys to use in order of: left, down, up, right
direction_keys = { 'h', 'j', 'k', 'l' },
-- modifier keys to combine with direction_keys
modifiers = {
move = 'CTRL', -- modifier to use for pane movement, e.g. CTRL+h to move left
resize = 'META', -- modifier to use for pane resize, e.g. META+h to resize to the left
}
})
```

Otherwise, add the following snippet to your `~/.config/wezterm/wezterm.lua`:

```lua
local w = require('wezterm')
Expand All @@ -372,11 +395,6 @@ local function is_vim(pane)
end

local direction_keys = {
Left = 'h',
Down = 'j',
Up = 'k',
Right = 'l',
-- reverse lookup
h = 'Left',
j = 'Down',
k = 'Up',
Expand Down
98 changes: 98 additions & 0 deletions plugin/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---@class SmartSplitsWeztermModifiers
---@field move string
---@field resize string

---@class SmartSplitsWeztermConfig
---@field direction_keys string[] Keys to use for movements, not including the modifier key (such as alt or ctrl), in order of left, down, up, right
---@field modifiers SmartSplitsWeztermModifiers Modifier keys to use for movement and resize actions, these should be Wezterm's modifier key strings such as 'META', 'CTRL', etc.

if vim ~= nil then
return -- this is a Wezterm plugin, not part of the Neovim plugin
end

local wezterm = require('wezterm')

local function is_vim(pane)
-- this is set by the Neovim plugin on launch, and unset on ExitPre in Neovim
return pane:get_user_vars().IS_NVIM == 'true'
end

---@type SmartSplitsWeztermConfig
local _smart_splits_wezterm_config = {
direction_keys = { 'h', 'j', 'k', 'l' },
modifiers = {
move = 'CTRL',
resize = 'META',
},
}

local direction_keys = {
h = 'Left',
j = 'Down',
k = 'Up',
l = 'Right',
}

local function split_nav(resize_or_move, key)
local modifier = resize_or_move == 'resize' and _smart_splits_wezterm_config.modifiers.resize
or _smart_splits_wezterm_config.modifiers.move
return {
key = key,
mods = modifier,
action = wezterm.action_callback(function(win, pane)
if is_vim(pane) then
-- pass the keys through to vim/nvim
win:perform_action({
SendKey = {
key = key,
mods = modifier,
},
}, pane)
else
if resize_or_move == 'resize' then
win:perform_action({ AdjustPaneSize = { direction_keys[key], 3 } }, pane)
else
win:perform_action({ ActivatePaneDirection = direction_keys[key] }, pane)
end
end
end),
}
end

---Apply plugin to Wezterm config.
---@param config_builder table
---@param plugin_config SmartSplitsWeztermConfig|nil
---@return table config_builder the updated config
local function apply_to_config(config_builder, plugin_config)
-- apply plugin config
if plugin_config then
_smart_splits_wezterm_config.direction_keys = plugin_config.direction_keys
or _smart_splits_wezterm_config.direction_keys
if plugin_config.modifiers then
_smart_splits_wezterm_config.modifiers.move = plugin_config.modifiers.move
or _smart_splits_wezterm_config.modifiers.move
_smart_splits_wezterm_config.modifiers.resize = plugin_config.modifiers.resize
or _smart_splits_wezterm_config.modifiers.resize
end
end

local keymaps = {}
for _, key in ipairs(_smart_splits_wezterm_config.direction_keys) do
table.insert(keymaps, split_nav('move', key))
table.insert(keymaps, split_nav('resize', key))
end

if config_builder.keys == nil then
config_builder.keys = keymaps
else
for _, keymap in ipairs(keymaps) do
table.insert(config_builder.keys, keymap)
end
end
return config_builder
end

return {
apply_to_config = apply_to_config,
is_vim = is_vim,
}
Loading