wezterm: Add event handling

This commit is contained in:
Marty Oehme 2022-11-25 16:21:20 +01:00
parent ddfc334ae8
commit ef80482807
Signed by: Marty
GPG key ID: 73BA40D5AFAF49C9
4 changed files with 180 additions and 65 deletions

View file

@ -0,0 +1,63 @@
local wezterm = require 'wezterm'
local io = require 'io'
local os = require 'os'
local act = wezterm.action
local function setup()
local function isViProcess(pane)
local proc = pane:get_foreground_process_name()
if (proc:find('vim') or proc:find('nvim')) then return true end
return false
end
local function conditionalActivatePane(window, pane, pane_direction,
vim_direction)
if (isViProcess(pane)) then
window:perform_action(act.Multiple {
act.SendKey { key = 'w', mods = 'CTRL' },
act.SendKey { key = vim_direction }
}, pane)
else
window:perform_action(act.ActivatePaneDirection(pane_direction),
pane)
end
end
wezterm.on('ActivatePaneDirection-right', function(window, pane)
conditionalActivatePane(window, pane, 'Right', 'l')
end)
wezterm.on('ActivatePaneDirection-left', function(window, pane)
conditionalActivatePane(window, pane, 'Left', 'h')
end)
wezterm.on('ActivatePaneDirection-up', function(window, pane)
conditionalActivatePane(window, pane, 'Up', 'k')
end)
wezterm.on('ActivatePaneDirection-down', function(window, pane)
conditionalActivatePane(window, pane, 'Down', 'j')
end)
-- Retrieve the current scrollback text and send to editor
wezterm.on('edit-scrollback', function(window, pane)
local viewport_text = pane:get_lines_as_text(10000)
-- Create a temporary file to pass to vim
local name = os.tmpname()
local f = io.open(name, 'w+')
if f == nil then return false end
f:write(viewport_text)
f:flush()
f:close()
-- Open a new window running vim and tell it to open the file
window:perform_action(act.SpawnCommandInNewTab {
args = { (os.getenv('EDITOR') or 'vi'), name }
}, pane)
-- Wait time for vim to read the file before we remove it.
wezterm.sleep_ms(1000)
os.remove(name)
end)
end
return { setup = setup }

View file

@ -1,7 +1,11 @@
local act = require('wezterm').action
local wezterm = require('wezterm')
local act = wezterm.action
local keys = {
{ key = 'O', mods = 'CTRL', action = act.ShowDebugOverlay },
{ key = '[', mods = 'CTRL', action = act.ScrollToPrompt(-1) },
{ key = ']', mods = 'CTRL', action = act.ScrollToPrompt(1) },
{ -- vertical pane
key = '\\',
mods = 'LEADER',
@ -10,11 +14,26 @@ local keys = {
key = '-',
mods = 'LEADER',
action = act.SplitVertical { domain = 'CurrentPaneDomain' }
}, { key = 'h', mods = 'CTRL', action = act.ActivatePaneDirection 'Left' },
{ key = 'j', mods = 'CTRL', action = act.ActivatePaneDirection 'Down' },
{ key = 'k', mods = 'CTRL', action = act.ActivatePaneDirection 'Up' },
{ key = 'l', mods = 'CTRL', action = act.ActivatePaneDirection 'Right' },
{ key = 'z', mods = 'LEADER', action = act.TogglePaneZoomState },
}, -- pane movement keys
{
key = 'h',
mods = 'CTRL',
action = act.EmitEvent 'ActivatePaneDirection-left'
},
{
key = 'j',
mods = 'CTRL',
action = act.EmitEvent 'ActivatePaneDirection-down'
},
{
key = 'k',
mods = 'CTRL',
action = act.EmitEvent 'ActivatePaneDirection-up'
}, {
key = 'l',
mods = 'CTRL',
action = act.EmitEvent 'ActivatePaneDirection-right'
}, { key = 'z', mods = 'LEADER', action = act.TogglePaneZoomState },
{ key = ' ', mods = 'LEADER', action = act.RotatePanes 'Clockwise' },
{ key = 'q', mods = 'LEADER', action = act.PaneSelect { mode = 'Activate' } },
{
@ -22,24 +41,38 @@ local keys = {
mods = 'LEADER',
action = act.PaneSelect { mode = 'SwapWithActive' }
}, { key = 'c', mods = 'LEADER', action = act.SpawnTab 'CurrentPaneDomain' },
{ key = 't', mods = 'LEADER', action = act.ShowTabNavigator },
{ key = ',', mods = 'LEADER', action = act.MoveTabRelative(-1) },
{ key = '.', mods = 'LEADER', action = act.MoveTabRelative(1) }, -- workspace selection
{
key = 's',
mods = 'LEADER',
action = act.ShowLauncherArgs { flags = 'FUZZY|WORKSPACES' }
}, { key = 't', mods = 'LEADER', action = act.ShowTabNavigator },
{ key = '[', mods = 'LEADER', action = act.ActivateCopyMode }, {
key = 'r',
mods = 'LEADER',
action = act.ActivateKeyTable {
name = 'resize_pane',
one_shot = false,
timeout_milliseconds = 2000
timeout_milliseconds = 2000,
replace_current = true
}
}, { key = 'F', mods = 'LEADER', action = act.QuickSelect }, {
key = '/',
mods = 'LEADER',
action = act.Search('CurrentSelectionOrEmptyString')
}, {
key = 'b',
key = 'f',
mods = 'LEADER',
action = act.ActivateKeyTable { name = 'scroll_mode', one_shot = false }
action = act.ActivateKeyTable {
name = 'scroll_mode',
one_shot = false,
replace_current = true
}
}, { key = 'e', mods = 'LEADER', action = act.EmitEvent 'edit-scrollback' }, {
key = 'l',
mods = 'LEADER',
action = act.EmitEvent 'ActivatePaneDirection-Right'
}
}
@ -59,6 +92,10 @@ local key_tables = {
{ key = 'l', action = act.AdjustPaneSize { 'Right', 1 } },
{ key = 'k', action = act.AdjustPaneSize { 'Up', 1 } },
{ key = 'j', action = act.AdjustPaneSize { 'Down', 1 } },
{ key = 'H', action = act.AdjustPaneSize { 'Left', 10 } },
{ key = 'L', action = act.AdjustPaneSize { 'Right', 10 } },
{ key = 'K', action = act.AdjustPaneSize { 'Up', 10 } },
{ key = 'J', action = act.AdjustPaneSize { 'Down', 10 } },
{ key = 'Escape', action = 'PopKeyTable' }
},
scroll_mode = {
@ -70,8 +107,6 @@ local key_tables = {
{ key = 'u', mods = 'CTRL', action = act.ScrollByPage(-0.5) },
{ key = 'g', mods = 'CTRL', action = act.ScrollToTop },
{ key = 'G', mods = 'CTRL', action = act.ScrollToBottom },
{ key = '[', mods = 'CTRL', action = act.ScrollToPrompt(-1) },
{ key = ']', mods = 'CTRL', action = act.ScrollToPrompt(1) },
{ key = 'Escape', action = 'PopKeyTable' }
}
}

View file

@ -0,0 +1,46 @@
local wezterm = require 'wezterm'
local function basename(s) return string.gsub(s or '', '(.*[/\\])(.*)', '%2') end
local SEPARATOR = ' | '
local function setup()
-- STATUSBAR
-- show currently active key table in lower right status bar
-- mimicing Vim modes
wezterm.on('update-status', function(window, pane)
local displayed = { left = {}, right = {} }
local keytable = window:active_key_table()
if keytable then
displayed.left[#displayed.left + 1] = 'MODE: ' .. keytable
end
local workspace = window:active_workspace()
if workspace and workspace ~= 'default' then
displayed.left[#displayed.left + 1] = 'WORKSPACE: ' .. workspace
end
local bat = ''
for _, b in ipairs(wezterm.battery_info()) do
bat = '🔋 ' .. string.format('%.0f%%', b.state_of_charge * 100) ..
' ' .. b.state
end
displayed.right[#displayed.right + 1] = bat
local currentprogram = pane:get_foreground_process_name()
displayed.right[#displayed.right + 1] = basename(currentprogram)
local statusleft = ''
for _, v in ipairs(displayed.left) do
statusleft = statusleft .. v .. SEPARATOR
end
local statusright = ''
for _, v in ipairs(displayed.right) do
statusright = statusright .. v .. SEPARATOR
end
window:set_left_status(statusleft or '')
window:set_right_status(statusright or '')
end)
end
return { setup = setup }

View file

@ -3,56 +3,26 @@ local mux = wezterm.mux
local maps = require 'maps'
-- STATUSBAR
-- show currently active key table in lower right status bar
-- mimicking Vim modes
wezterm.on('update-right-status', function(window, _)
local keytable = window:active_key_table()
if keytable then
keytable = 'MODE: ' .. keytable
else
keytable = ''
end
local workspace = window:active_workspace()
if workspace and workspace ~= 'default' then
workspace = 'WORKSPACE: ' .. workspace
else
workspace = ''
end
require 'statusbar'.setup()
require 'events'.setup()
local status = keytable .. ' ' .. workspace
window:set_left_status(status or '')
-- "Wed Mar 3 08:14"
local date = wezterm.strftime '%a %b %-d %H:%M '
local bat = ''
for _, b in ipairs(wezterm.battery_info()) do
bat =
'🔋 ' .. string.format('%.0f%%', b.state_of_charge * 100) .. ' ' ..
b.state
end
window:set_right_status(wezterm.format {{Text = bat .. ' ' .. date}})
end)
wezterm.on("set-up-dotfile-workspace", function(window, pane)
-- Set a workspace for coding on a current project
-- Top pane is for the editor, bottom pane is for the build tool
local project_dir = wezterm.home_dir .. '/projects/test/quarto/quarto-test'
local tab, build_pane, window = mux.spawn_window {
workspace = 'coding',
cwd = project_dir,
args = args
}
local editor_pane = build_pane:split{
direction = 'Top',
size = 0.6,
cwd = project_dir
}
build_pane:send_text 'quarto check'
mux.set_active_workspace 'coding'
end)
-- wezterm.on("set-up-dotfile-workspace", function(window, pane)
-- -- Set a workspace for coding on a current project
-- -- Top pane is for the editor, bottom pane is for the build tool
-- local project_dir = wezterm.home_dir .. '/projects/test/quarto/quarto-test'
-- local tab, build_pane, window = mux.spawn_window {
-- workspace = 'coding',
-- cwd = project_dir,
-- args = args
-- }
-- local editor_pane = build_pane:split{
-- direction = 'Top',
-- size = 0.6,
-- cwd = project_dir
-- }
-- build_pane:send_text 'quarto check'
-- mux.set_active_workspace 'coding'
-- end)
--
-- wezterm.on('gui-startup', function(cmd)
-- -- allow `wezterm start -- something` to affect what we spawn
@ -94,7 +64,8 @@ return {
tab_bar_at_bottom = true,
window_padding = { left = 0, right = 0, top = 0, bottom = 0 },
color_scheme = "Nord (base16)",
color_scheme = "Mexico Light (base16)",
-- default_prog = {"nu"},
scrollback_lines = 10000,