1
1
mirror of https://github.com/wez/wezterm.git synced 2024-10-26 23:58:28 +03:00

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
This commit is contained in:
Wez Furlong 2023-03-24 21:02:04 -07:00
parent b113cc909e
commit 499e5f73d0
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
3 changed files with 42 additions and 7 deletions

View File

@ -224,6 +224,16 @@ impl std::fmt::Display for SpawnCommand {
}
impl SpawnCommand {
pub fn label_for_palette(&self) -> Option<String> {
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<Self> {
let mut args = vec![];
let mut set_environment_variables = HashMap::new();

View File

@ -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

View File

@ -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<Comm
icon: Some("mdi_tab_plus"),
},
SpawnCommandInNewTab(cmd) => 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<Comm
icon: Some("mdi_tab_plus"),
},
SpawnCommandInNewWindow(cmd) => 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<Comm
domain: SpawnTabDomain::CurrentPaneDomain,
..
}) => 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<Comm
domain: SpawnTabDomain::CurrentPaneDomain,
..
}) => 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<Comm
icon: Some("cod_split_horizontal"),
},
SplitHorizontal(_) => 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<Comm
icon: Some("cod_split_horizontal"),
},
SplitVertical(_) => 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<Comm
SplitPane(split) => {
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],