1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

palette: add augment-command-palette event

This allows the user to add entries to the command palette without
having to define dummy key assignments for them.

refs: https://github.com/wez/wezterm/issues/3595
This commit is contained in:
Wez Furlong 2023-04-23 09:22:38 -07:00
parent e0a92c73a3
commit f8bafc3c67
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
4 changed files with 124 additions and 12 deletions

View File

@ -38,6 +38,9 @@ As features stabilize some brief notes about them will accumulate here.
* [window:keyboard_modifiers](config/lua/window/keyboard_modifiers.md) #3444
* [win32_system_backdrop](config/lua/config/win32_system_backdrop.md). Thanks to @kingavatar! #3528 #1614
* [wezterm cli adjust-pane-size](cli/cli/adjust-pane-size.md). Thanks to @mrjones2014! #3471 #3491
* [augment-command-palette](config/lua/window-events/augment-command-palette.md)
event for adding entries to the command palette without assigning keyboard
shortcuts. #3595
#### Fixed

View File

@ -0,0 +1,53 @@
# `augment-command-palette`
{{since('nightly')}}
This event is emitted when the [Command Palette](../keyassignment/ActivateCommandPalette.md) is shown.
It's purpose is to enable you to add additional entries to the list of commands
shown in the palette.
This hook is synchronous; calling asynchronous functions will not succeed.
The return value is a table listing the additional entries. Each element of the
returned table may have the following fields:
* `brief` - required: the brief description for the entry
* `doc` - optional: a long description that may be shown after the entry, or that
may be used in future wezterms of wezterm to provide more information about the
command.
* `action` - the action to take when the item is activated. Can be any key assignment
action.
* `icon` - optional Nerd Fonts glyph name to use for the icon for the entry. See
[wezterm.nerdfonts](../wezterm/nerdfonts.md) for a list of icon names.
## Adding a Rename Tab entry to the palette
In this example, an entry is added for renaming tabs:
```lua
local wezterm = require 'wezterm'
local act = wezterm.action
local config = wezterm.config_builder()
wezterm.on('augment-command-palette', function(window, pane)
return {
{
brief = 'Rename tab',
icon = 'mdi_rename_box',
action = act.PromptInputLine {
description = 'Enter new name for tab',
action = wezterm.action_callback(function(window, pane, line)
if line then
window:active_tab():set_title(line)
end
end),
},
},
}
end)
return config
```

View File

@ -74,7 +74,7 @@ pub struct ExpandedCommand {
pub action: KeyAssignment,
pub keys: Vec<(Modifiers, KeyCode)>,
pub menubar: &'static [&'static str],
pub icon: Option<&'static str>,
pub icon: Option<Cow<'static, str>>,
}
impl std::fmt::Debug for CommandDef {
@ -177,7 +177,7 @@ impl CommandDef {
keys,
action,
menubar: def.menubar,
icon: def.icon,
icon: def.icon.map(Cow::Borrowed),
})
}
}
@ -214,7 +214,7 @@ impl CommandDef {
keys: vec![],
action: KeyAssignment::SpawnCommandInNewTab(cmd.clone()),
menubar: &["Shell"],
icon: Some("mdi_tab_plus"),
icon: Some("mdi_tab_plus".into()),
});
}
@ -249,7 +249,7 @@ impl CommandDef {
..SpawnCommand::default()
}),
menubar: &["Shell"],
icon: Some("mdi_tab_plus"),
icon: Some("mdi_tab_plus".into()),
});
} else {
result.push(ExpandedCommand {
@ -258,7 +258,7 @@ impl CommandDef {
keys: vec![],
action: KeyAssignment::AttachDomain(name.to_string()),
menubar: &["Shell", "Attach"],
icon: Some("mdi_pipe"),
icon: Some("mdi_pipe".into()),
});
}
}
@ -280,7 +280,7 @@ impl CommandDef {
name.to_string(),
)),
menubar: &["Shell", "Detach"],
icon: Some("mdi_pipe_disconnected"),
icon: Some("mdi_pipe_disconnected".into()),
});
}
}
@ -331,7 +331,7 @@ impl CommandDef {
keys: vec![(*mods, keycode.clone())],
action: entry.action.clone(),
menubar: cmd.menubar,
icon: cmd.icon,
icon: cmd.icon.map(Cow::Borrowed),
});
}
}
@ -351,7 +351,7 @@ impl CommandDef {
keys: vec![],
action: entry.action.clone(),
menubar: cmd.menubar,
icon: cmd.icon,
icon: cmd.icon.map(Cow::Borrowed),
});
}
}

View File

@ -5,19 +5,23 @@ use crate::termwindow::render::corners::{
BOTTOM_LEFT_ROUNDED_CORNER, BOTTOM_RIGHT_ROUNDED_CORNER, TOP_LEFT_ROUNDED_CORNER,
TOP_RIGHT_ROUNDED_CORNER,
};
use crate::termwindow::{DimensionContext, TermWindow};
use crate::termwindow::{DimensionContext, GuiWin, TermWindow};
use crate::utilsprites::RenderMetrics;
use config::keyassignment::KeyAssignment;
use config::Dimension;
use frecency::Frecency;
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use luahelper::{from_lua_value_dynamic, impl_lua_conversion_dynamic};
use mux_lua::MuxPane;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::{Ref, RefCell};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::path::PathBuf;
use termwiz::nerdfonts::NERD_FONTS;
use wezterm_dynamic::{FromDynamic, ToDynamic};
use wezterm_term::{KeyCode, KeyModifiers, MouseEvent};
use window::color::LinearRgba;
use window::Modifiers;
@ -75,9 +79,56 @@ fn save_recent(command: &ExpandedCommand) -> anyhow::Result<()> {
Ok(())
}
fn build_commands(filter_copy_mode: bool) -> Vec<ExpandedCommand> {
#[derive(Debug, Clone, FromDynamic, ToDynamic)]
pub struct UserPaletteEntry {
pub brief: String,
pub doc: Option<String>,
pub action: KeyAssignment,
pub icon: Option<String>,
}
impl_lua_conversion_dynamic!(UserPaletteEntry);
fn build_commands(
gui_window: GuiWin,
pane: Option<MuxPane>,
filter_copy_mode: bool,
) -> Vec<ExpandedCommand> {
let mut commands = CommandDef::actions_for_palette_and_menubar(&config::configuration());
match config::run_immediate_with_lua_config(|lua| {
let mut entries: Vec<UserPaletteEntry> = vec![];
if let Some(lua) = lua {
let result = config::lua::emit_sync_callback(
&*lua,
("augment-command-palette".to_string(), (gui_window, pane)),
)?;
entries = from_lua_value_dynamic(result)?;
}
Ok(entries)
}) {
Ok(entries) => {
for entry in entries {
commands.push(ExpandedCommand {
brief: entry.brief.into(),
doc: match entry.doc {
Some(doc) => doc.into(),
None => "".into(),
},
action: entry.action,
keys: vec![],
menubar: &[],
icon: entry.icon.map(Cow::Owned),
});
}
}
Err(err) => {
log::warn!("augment-command-palette: {err:#}");
}
}
commands.retain(|cmd| {
if filter_copy_mode {
!matches!(cmd.action, KeyAssignment::CopyMode(_))
@ -175,7 +226,12 @@ impl CommandPalette {
.is_none()
})
.unwrap_or(true);
let commands = build_commands(filter_copy_mode);
let mux_pane = term_window
.get_active_pane_or_overlay()
.map(|pane| MuxPane(pane.pane_id()));
let commands = build_commands(GuiWin::new(term_window), mux_pane, filter_copy_mode);
Self {
element: RefCell::new(None),
@ -242,7 +298,7 @@ impl CommandPalette {
};
let icon = match &command.icon {
Some(nf) => NERD_FONTS.get(nf).unwrap_or_else(|| {
Some(nf) => NERD_FONTS.get(nf.as_ref()).unwrap_or_else(|| {
log::error!("nerdfont {nf} not found in NERD_FONTS");
&'?'
}),