diff options
author | Aram Drevekenin <aram@poor.dev> | 2022-10-17 19:39:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-17 19:39:37 +0200 |
commit | 271abb3ea2842cadc131ae91a3cd899e320e958e (patch) | |
tree | b20d4231f6d5b46737efe6b60b4e4e0170a87640 /zellij-utils | |
parent | 4562982409cfcdfc151c5a4c2878d358dac9e76c (diff) |
feat(cli): zellij run improvements (#1804)
* feat(cli): move command to the end of the cli arguments
* feat(cli): allow naming panes from the command line
* fix(cli): adjust actions after pane rename
* feat(cli): zellij run completions for fish
* feat(cli): zellij run completions for bash and zsh
* style(fmt): rustfmt
* fix(e2e): fix run test and snapshot
* style(fmt): rustfmt
Diffstat (limited to 'zellij-utils')
12 files changed, 166 insertions, 55 deletions
diff --git a/zellij-utils/assets/completions/comp.bash b/zellij-utils/assets/completions/comp.bash new file mode 100644 index 000000000..210fd25da --- /dev/null +++ b/zellij-utils/assets/completions/comp.bash @@ -0,0 +1,4 @@ +function zp () { zellij run --name "$*" -- bash -c "$*";} # zellij pane +function zpf () { zellij run --name "$*" --floating -- bash -c "$*";} # zellij pane floating +function zo () { zellij edit "$*";} # zellij open +function zof () { zellij edit --floating "$*";} # zellij open floating diff --git a/zellij-utils/assets/completions/comp.fish b/zellij-utils/assets/completions/comp.fish index 97f1f29f9..286150b84 100644 --- a/zellij-utils/assets/completions/comp.fish +++ b/zellij-utils/assets/completions/comp.fish @@ -6,3 +6,19 @@ complete -c zellij -n "__fish_seen_subcommand_from a" -f -a "(__fish_complete_se complete -c zellij -n "__fish_seen_subcommand_from kill-session" -f -a "(__fish_complete_sessions)" -d "Session" complete -c zellij -n "__fish_seen_subcommand_from k" -f -a "(__fish_complete_sessions)" -d "Session" complete -c zellij -n "__fish_seen_subcommand_from setup" -l "generate-completion" -x -a "bash elvish fish zsh powershell" -d "Shell" +function zp + # zellij pane + command zellij run --name "$argv" -- fish -c "$argv" +end +function zpf + # zellij pane floating + command zellij run --name "$argv" --floating -- fish -c "$argv" +end +function zo + # zellij open + command zellij edit $argv +end +function zof + # zellij open floating + command zellij edit --floating $argv +end diff --git a/zellij-utils/assets/completions/comp.zsh b/zellij-utils/assets/completions/comp.zsh new file mode 100644 index 000000000..5c5c4af34 --- /dev/null +++ b/zellij-utils/assets/completions/comp.zsh @@ -0,0 +1,4 @@ +function zp () { zellij run --name "$*" -- zsh -c "$*";} # zellij pane +function zpf () { zellij run --name "$*" --floating -- zsh -c "$*";} # zellij pane floating +function zo () { zellij edit "$*";} # zellij open +function zof () { zellij edit --floating "$*";} # zellij open floating diff --git a/zellij-utils/src/cli.rs b/zellij-utils/src/cli.rs index ae0c2141c..c7c7b0dcf 100644 --- a/zellij-utils/src/cli.rs +++ b/zellij-utils/src/cli.rs @@ -127,13 +127,16 @@ pub enum Sessions { /// Run a command in a new pane #[clap(visible_alias = "r")] Run { - command: Option<String>, + #[clap(last(true), required(true))] + command: Vec<String>, #[clap(short, long, value_parser, conflicts_with("floating"))] direction: Option<Direction>, #[clap(long, value_parser)] cwd: Option<PathBuf>, #[clap(short, long, value_parser, default_value("false"), takes_value(false))] floating: bool, + #[clap(short, long, value_parser)] + name: Option<String>, }, /// Edit file with default $EDITOR / $VISUAL #[clap(visible_alias = "e")] @@ -206,12 +209,14 @@ pub enum CliAction { NewPane { #[clap(short, long, value_parser, conflicts_with("floating"))] direction: Option<Direction>, - #[clap(short, long, value_parser)] - command: Option<String>, + #[clap(last(true))] + command: Vec<String>, #[clap(long, value_parser)] cwd: Option<PathBuf>, #[clap(short, long, value_parser, default_value("false"), takes_value(false))] floating: bool, + #[clap(short, long, value_parser)] + name: Option<String>, }, /// Open the specified file in a new zellij pane with your default EDITOR Edit { diff --git a/zellij-utils/src/input/actions.rs b/zellij-utils/src/input/actions.rs index d337a75a9..2f2e39818 100644 --- a/zellij-utils/src/input/actions.rs +++ b/zellij-utils/src/input/actions.rs @@ -109,37 +109,6 @@ impl FromStr for SearchOption { } } -fn split_escaped_whitespace(s: &str) -> Vec<String> { - s.split_ascii_whitespace() - .map(|s| String::from(s)) - .fold(vec![], |mut acc, part| { - if let Some(previous_part) = acc.last_mut() { - if previous_part.ends_with('\\') { - previous_part.push(' '); - previous_part.push_str(&part); - return acc; - } - } - acc.push(part); - acc - }) -} - -fn split_command_and_args(command: String) -> (PathBuf, Vec<String>) { - let mut full_command = split_escaped_whitespace(&command); - let mut command = None; - let mut command_args = vec![]; - for part in full_command.drain(..) { - if command.is_none() { - command = Some(part); - } else { - command_args.push(part); - } - } - let command = PathBuf::from(command.unwrap()); - (command, command_args) -} - // As these actions are bound to the default config, please // do take care when refactoring - or renaming. // They might need to be adjusted in the default config @@ -198,13 +167,15 @@ pub enum Action { ToggleActiveSyncTab, /// Open a new pane in the specified direction (relative to focus). /// If no direction is specified, will try to use the biggest available space. - NewPane(Option<Direction>), + NewPane(Option<Direction>, Option<String>), // String is an optional pane name /// Open the file in a new pane using the default editor EditFile(PathBuf, Option<usize>, Option<Direction>, bool), // usize is an optional line number, bool is floating true/false /// Open a new floating pane - NewFloatingPane(Option<RunCommandAction>), + NewFloatingPane(Option<RunCommandAction>, Option<String>), // String is an optional pane name /// Open a new tiled (embedded, non-floating) pane - NewTiledPane(Option<Direction>, Option<RunCommandAction>), + NewTiledPane(Option<Direction>, Option<RunCommandAction>, Option<String>), // String is an + // optional pane + // name /// Embed focused pane in tab if floating or float focused pane if embedded TogglePaneEmbedOrFloating, /// Toggle the visibility of all floating panes (if any) in the current Tab @@ -285,9 +256,11 @@ impl Action { command, cwd, floating, - } => match command { - Some(command) => { - let (command, args) = split_command_and_args(command); + name, + } => { + if !command.is_empty() { + let mut command = command.clone(); + let (command, args) = (PathBuf::from(command.remove(0)), command); let cwd = cwd.or_else(|| std::env::current_dir().ok()); let run_command_action = RunCommandAction { command, @@ -297,21 +270,24 @@ impl Action { hold_on_close: true, }; if floating { - Ok(vec![Action::NewFloatingPane(Some(run_command_action))]) + Ok(vec![Action::NewFloatingPane( + Some(run_command_action), + name, + )]) } else { Ok(vec![Action::NewTiledPane( direction, Some(run_command_action), + name, )]) } - }, - None => { + } else { if floating { - Ok(vec![Action::NewFloatingPane(None)]) + Ok(vec![Action::NewFloatingPane(None, name)]) } else { - Ok(vec![Action::NewTiledPane(direction, None)]) + Ok(vec![Action::NewTiledPane(direction, None, name)]) } - }, + } }, CliAction::Edit { direction, diff --git a/zellij-utils/src/kdl/mod.rs b/zellij-utils/src/kdl/mod.rs index e7b7fa509..0b4110c95 100644 --- a/zellij-utils/src/kdl/mod.rs +++ b/zellij-utils/src/kdl/mod.rs @@ -430,7 +430,7 @@ impl Action { "DumpScreen" => Ok(Action::DumpScreen(string)), "NewPane" => { if string.is_empty() { - return Ok(Action::NewPane(None)); + return Ok(Action::NewPane(None, None)); } else { let direction = Direction::from_str(string.as_str()).map_err(|_| { ConfigError::new_kdl_error( @@ -439,7 +439,7 @@ impl Action { action_node.span().len(), ) })?; - Ok(Action::NewPane(Some(direction))) + Ok(Action::NewPane(Some(direction), None)) } }, "SearchToggleOption" => { diff --git a/zellij-utils/src/setup.rs b/zellij-utils/src/setup.rs index 9972915e4..c105c929e 100644 --- a/zellij-utils/src/setup.rs +++ b/zellij-utils/src/setup.rs @@ -125,6 +125,18 @@ pub const FISH_EXTRA_COMPLETION: &[u8] = include_bytes!(concat!( "assets/completions/comp.fish" )); +pub const BASH_EXTRA_COMPLETION: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/", + "assets/completions/comp.bash" +)); + +pub const ZSH_EXTRA_COMPLETION: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/", + "assets/completions/comp.zsh" +)); + pub const BASH_AUTO_START_SCRIPT: &[u8] = include_bytes!(concat!( env!("CARGO_MANIFEST_DIR"), "/", @@ -412,13 +424,17 @@ impl Setup { clap_complete::generate(shell, &mut CliArgs::command(), "zellij", &mut out); // add shell dependent extra completion match shell { - Shell::Bash => {}, + Shell::Bash => { + let _ = out.write_all(BASH_EXTRA_COMPLETION); + }, Shell::Elvish => {}, Shell::Fish => { let _ = out.write_all(FISH_EXTRA_COMPLETION); }, Shell::PowerShell => {}, - Shell::Zsh => {}, + Shell::Zsh => { + let _ = out.write_all(ZSH_EXTRA_COMPLETION); + }, _ => {}, }; } diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap index 35cd5f7e0..1231ad635 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__default_config_with_no_cli_arguments.snap @@ -1,6 +1,6 @@ --- source: zellij-utils/src/setup.rs -assertion_line: 491 +assertion_line: 503 expression: "format!(\"{:#?}\", config)" --- Config { @@ -76,6 +76,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -339,6 +340,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -498,6 +500,7 @@ Config { Some( Down, ), + None, ), SwitchToMode( Normal, @@ -552,6 +555,7 @@ Config { ): [ NewPane( None, + None, ), SwitchToMode( Normal, @@ -569,6 +573,7 @@ Config { Some( Right, ), + None, ), SwitchToMode( Normal, @@ -668,6 +673,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1019,6 +1025,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1280,6 +1287,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1481,6 +1489,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1761,6 +1770,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1962,6 +1972,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2160,6 +2171,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2363,6 +2375,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2624,6 +2637,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2819,6 +2833,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2978,6 +2993,7 @@ Config { Some( Down, ), + None, ), SwitchToMode( Normal, @@ -2990,6 +3006,7 @@ Config { Some( Right, ), + None, ), SwitchToMode( Normal, @@ -3164,6 +3181,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap index 07c91c5aa..a111b68b0 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_env_vars_override_config_env_vars.snap @@ -1,6 +1,6 @@ --- source: zellij-utils/src/setup.rs -assertion_line: 541 +assertion_line: 561 expression: "format!(\"{:#?}\", config)" --- Config { @@ -76,6 +76,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -339,6 +340,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -498,6 +500,7 @@ Config { Some( Down, ), + None, ), SwitchToMode( Normal, @@ -552,6 +555,7 @@ Config { ): [ NewPane( None, + None, ), SwitchToMode( Normal, @@ -569,6 +573,7 @@ Config { Some( Right, ), + None, ), SwitchToMode( Normal, @@ -668,6 +673,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1019,6 +1025,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1280,6 +1287,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1481,6 +1489,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1761,6 +1770,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1962,6 +1972,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2160,6 +2171,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2363,6 +2375,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2624,6 +2637,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2819,6 +2833,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -2978,6 +2993,7 @@ Config { Some( Down, ), + None, ), SwitchToMode( Normal, @@ -2990,6 +3006,7 @@ Config { Some( Right, ), + None, ), SwitchToMode( Normal, @@ -3164,6 +3181,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( diff --git a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_plugins_override_config_plugins.snap b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_plugins_override_config_plugins.snap index 5a71f06ae..dc39c74b7 100644 --- a/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_plugins_override_config_plugins.snap +++ b/zellij-utils/src/snapshots/zellij_utils__setup__setup_test__layout_plugins_override_config_plugins.snap @@ -1,6 +1,6 @@ --- source: zellij-utils/src/setup.rs -assertion_line: 557 +assertion_line: 589 expression: "format!(\"{:#?}\", config)" --- Config { @@ -76,6 +76,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -339,6 +340,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -498,6 +500,7 @@ Config { Some( Down, ), + None, ), SwitchToMode( Normal, @@ -552,6 +555,7 @@ Config { ): [ NewPane( None, + None, ), SwitchToMode( Normal, @@ -569,6 +573,7 @@ Config { Some( Right, ), + None, ), SwitchToMode( Normal, @@ -668,6 +673,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1019,6 +1025,7 @@ Config { ): [ NewPane( None, + None, ), ], Alt( @@ -1280,6 +1287,7 @@ Config { ): [ NewPane( None, + None, |