From 715d3d59e486805189b24ed23f05b2267bce749d Mon Sep 17 00:00:00 2001 From: Daniel Fitzpatrick Date: Wed, 8 Sep 2021 16:51:44 -0500 Subject: [PATCH] Add elvish widget --- docs/installation.md | 3 +++ shell/navi.plugin.elv | 31 +++++++++++++++++++++++++++++++ src/config/cli.rs | 3 ++- src/handler/shell.rs | 1 + src/shell.rs | 1 + tests/run | 1 + 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 shell/navi.plugin.elv diff --git a/docs/installation.md b/docs/installation.md index 738f589..5972be0 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -85,6 +85,9 @@ eval "$(navi widget zsh)" # fish navi widget fish | source + +# elvish +eval (navi widget elvish | slurp) ``` By default, `Ctrl+G` is assigned to launching **navi**. diff --git a/shell/navi.plugin.elv b/shell/navi.plugin.elv new file mode 100644 index 0000000..f1aa49c --- /dev/null +++ b/shell/navi.plugin.elv @@ -0,0 +1,31 @@ +use str + +fn call-navi []{ + if (eq $edit:current-command '') { + answer = (navi --print) + edit:replace-input $answer + } elif (not (str:contains-any $edit:current-command '|')) { + answer = (navi --print --query $edit:current-command) + if (not-eq $answer '') { + edit:replace-input $answer + } + } else { + cmds = [(str:split '|' $edit:current-command)] + qty = (- (count $cmds) 1) + query = (all $cmds | drop $qty) + cmds = [(all $cmds | take $qty)] + answer = '' + if (eq $query '') { + answer = (navi --print) + } else { + answer = (navi --print --query $query) + } + + if (not-eq $answer '') { + cmds = [$@cmds $answer] + edit:replace-input (str:join '| ' $cmds) + } + } +} + +edit:insert:binding[Alt-h] = []{ call-navi >/dev/tty 2>&1 } \ No newline at end of file diff --git a/src/config/cli.rs b/src/config/cli.rs index d32da1b..038108f 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -9,7 +9,7 @@ use clap::{crate_version, AppSettings, Clap}; use std::str::FromStr; const FINDER_POSSIBLE_VALUES: &[&str] = &["fzf", "skim"]; -const WIDGET_POSSIBLE_VALUES: &[&str] = &["bash", "zsh", "fish"]; +const WIDGET_POSSIBLE_VALUES: &[&str] = &["bash", "zsh", "fish", "elvish"]; const FUNC_POSSIBLE_VALUES: &[&str] = &["url::open", "welcome", "widget::last_command", "map::expand"]; const INFO_POSSIBLE_VALUES: &[&str] = &["cheats-example", "cheats-path", "config-path", "config-example"]; @@ -21,6 +21,7 @@ impl FromStr for Shell { "bash" => Ok(Shell::Bash), "zsh" => Ok(Shell::Zsh), "fish" => Ok(Shell::Fish), + "elvish" => Ok(Shell::Elvish), _ => Err("no match"), } } diff --git a/src/handler/shell.rs b/src/handler/shell.rs index a7b03ab..fc23eee 100644 --- a/src/handler/shell.rs +++ b/src/handler/shell.rs @@ -6,6 +6,7 @@ pub fn main(shell: &Shell) -> Result<()> { Shell::Bash => include_str!("../../shell/navi.plugin.bash"), Shell::Zsh => include_str!("../../shell/navi.plugin.zsh"), Shell::Fish => include_str!("../../shell/navi.plugin.fish"), + Shell::Elvish => include_str!("../../shell/navi.plugin.elv"), }; println!("{}", content); diff --git a/src/shell.rs b/src/shell.rs index dca6809..b894d51 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -12,6 +12,7 @@ pub enum Shell { Bash, Zsh, Fish, + Elvish, } #[derive(Error, Debug)] diff --git a/tests/run b/tests/run index 7f49e2f..269eddd 100755 --- a/tests/run +++ b/tests/run @@ -158,6 +158,7 @@ test::set_suite "widget" test::run "bash" _navi_widget "bash" test::run "zsh" _navi_widget "zsh" test::run "zsh" _navi_widget "fish" +test::run "elvish" _navi_widget "elvish" test::set_suite "3rd party" test::run "tldr" _navi_tldr