From 499e5f73d00d4cee79ddfb16223b7ae655444cc2 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 24 Mar 2023 21:02:04 -0700 Subject: [PATCH] palette: show the label from the command in the palette Given an assignment like this: ``` { key = "b", mods = "ALT", action = wezterm.action.SplitPane { direction = 'Right', command = { label = 'Bash Right', args = {'/usr/bin/bash' } } } } ``` we should show the label from the command in the palette. That's what this commit enables. If there is no label, but the arguments are set, then the arguments will be shown instead. refs: #3252 --- config/src/keyassignment.rs | 10 ++++++++++ docs/changelog.md | 2 ++ wezterm-gui/src/commands.rs | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 89008d1a0..863441f65 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -224,6 +224,16 @@ impl std::fmt::Display for SpawnCommand { } impl SpawnCommand { + pub fn label_for_palette(&self) -> Option { + if let Some(label) = &self.label { + Some(label.to_string()) + } else if let Some(args) = &self.args { + Some(shlex::join(args.iter().map(|s| s.as_str()))) + } else { + None + } + } + pub fn from_command_builder(cmd: &CommandBuilder) -> anyhow::Result { let mut args = vec![]; let mut set_environment_variables = HashMap::new(); diff --git a/docs/changelog.md b/docs/changelog.md index 394994d83..f4552e379 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -63,6 +63,8 @@ As features stabilize some brief notes about them will accumulate here. in the GUI when explicitly using multiplexing domains. #2863 * macOS: update entitlements so that macOS will prompt the user when they spawn an app that wants to use the microphone and other resources. #3359 +* Command palette didn't show command label or arguments for entries constructed + from your key assignments, making it difficult to distinguish them from each other. #3252 ### 20230320-124340-559cb7b0 diff --git a/wezterm-gui/src/commands.rs b/wezterm-gui/src/commands.rs index b35465059..906dd685c 100644 --- a/wezterm-gui/src/commands.rs +++ b/wezterm-gui/src/commands.rs @@ -581,6 +581,25 @@ fn english_ordinal(n: isize) -> String { } } +fn spawn_command_from_action(action: &KeyAssignment) -> Option<&SpawnCommand> { + match action { + SplitPane(config::keyassignment::SplitPane { command, .. }) => Some(command), + SplitHorizontal(command) + | SplitVertical(command) + | SpawnCommandInNewWindow(command) + | SpawnCommandInNewTab(command) => Some(command), + _ => None, + } +} + +fn label_string(action: &KeyAssignment, candidate: String) -> String { + if let Some(label) = spawn_command_from_action(action).and_then(|cmd| cmd.label_for_palette()) { + label + } else { + candidate + } +} + /// Describes a key assignment action; returns a bunch /// of metadata that is useful in the command palette/menubar context. /// This function will be called for the result of compute_default_actions(), @@ -823,7 +842,7 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option CommandDef { - brief: format!("Spawn a new Tab with {cmd:?}").into(), + brief: label_string(action, format!("Spawn a new Tab with {cmd:?}").to_string()).into(), doc: format!("Spawn a new Tab with {cmd:?}").into(), keys: vec![], args: &[], @@ -831,7 +850,11 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option CommandDef { - brief: format!("Spawn a new Window with {cmd:?}").into(), + brief: label_string( + action, + format!("Spawn a new Window with {cmd:?}").to_string(), + ) + .into(), doc: format!("Spawn a new Window with {cmd:?}").into(), keys: vec![], args: &[], @@ -1296,7 +1319,7 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option CommandDef { - brief: "Split Vertically (Top/Bottom)".into(), + brief: label_string(action, "Split Vertically (Top/Bottom)".to_string()).into(), doc: "Split the current pane vertically into two panes, by spawning \ the default program into the bottom half" .into(), @@ -1314,7 +1337,7 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option CommandDef { - brief: "Split Horizontally (Left/Right)".into(), + brief: label_string(action, "Split Horizontally (Left/Right)".to_string()).into(), doc: "Split the current pane horizontally into two panes, by spawning \ the default program into the right hand side" .into(), @@ -1329,7 +1352,7 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option CommandDef { - brief: "Split Horizontally (Left/Right)".into(), + brief: label_string(action, "Split Horizontally (Left/Right)".to_string()).into(), doc: "Split the current pane horizontally into two panes, by spawning \ the default program into the right hand side" .into(), @@ -1339,7 +1362,7 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option CommandDef { - brief: "Split Vertically (Top/Bottom)".into(), + brief: label_string(action, "Split Vertically (Top/Bottom)".to_string()).into(), doc: "Split the current pane veritically into two panes, by spawning \ the default program into the bottom" .into(), @@ -1827,7 +1850,7 @@ pub fn derive_command_from_key_assignment(action: &KeyAssignment) -> Option { let direction = split.direction; CommandDef { - brief: format!("Split the current pane {direction:?}").into(), + brief: label_string(action, format!("Split the current pane {direction:?}")).into(), doc: format!("Split the current pane {direction:?}").into(), keys: vec![], args: &[ArgType::ActivePane],