From 657992b1737c94e8d95352673679b827c4702691 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Fri, 14 Aug 2020 13:53:23 -0300 Subject: [PATCH] Allow implicit variable dependencies (#379) * Allow implicit variable dependencies * Aug-14 12h01: core.rs, Cargo.toml, Cargo.lock * Aug-14 12h04: cases.cheat * Aug-14 12h08: cases.cheat --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 12 ++++++++++++ src/flows/core.rs | 10 +++++++++- src/structures/cheat.rs | 1 + tests/no_prompt_cheats/cases.cheat | 13 ++++++++++++- 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f3e86e4e..ca7bf1b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,7 +174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "navi" -version = "2.7.2" +version = "2.8.0" dependencies = [ "anyhow 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index e3cf8deb..c055a268 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "navi" -version = "2.7.2" +version = "2.8.0" authors = ["Denis Isidoro "] edition = "2018" description = "An interactive cheatsheet tool for the command-line" diff --git a/README.md b/README.md index 9167cfd2..3dc34f4f 100644 --- a/README.md +++ b/README.md @@ -237,10 +237,22 @@ The command for generating possible inputs can refer previous variables: # If you select "hello" for , the possible values of will be "hello foo" and "hello bar" echo +# If you want to ignore the contents of and only print +: ; echo + $ x: echo "hello hi" | tr ' ' '\n' $ y: echo "$x foo;$x bar" | tr ';' '\n' ``` +If you want to have implicit variable dependency, you can use the `` syntax inside a variable command: +```sh +# Should print /my/pictures/wallpapers +echo "" + +$ pictures_folder: echo "/my/pictures" +$ wallpaper_folder: echo "/wallpapers" +``` + ### Multiline snippets Commands may be multiline: diff --git a/src/flows/core.rs b/src/flows/core.rs index 2894cfe3..3b8055af 100644 --- a/src/flows/core.rs +++ b/src/flows/core.rs @@ -151,10 +151,18 @@ fn replace_variables_from_snippet( .get(&tags, &variable_name) .ok_or_else(|| anyhow!("No suggestions")) .and_then(|suggestion| { + let mut new_suggestion = suggestion.clone(); + new_suggestion.0 = replace_variables_from_snippet( + &new_suggestion.0, + tags, + variables.clone(), + config, + )?; + prompt_with_suggestions( variable_name, &config, - suggestion, + &new_suggestion, interpolated_snippet.clone(), ) }) diff --git a/src/structures/cheat.rs b/src/structures/cheat.rs index 778a337e..2bcdf935 100644 --- a/src/structures/cheat.rs +++ b/src/structures/cheat.rs @@ -8,6 +8,7 @@ fn gen_key(tags: &str, variable: &str) -> u64 { format!("{};{}", tags, variable).hash_line() } +#[derive(Clone)] pub struct VariableMap(HashMap); impl VariableMap { diff --git a/tests/no_prompt_cheats/cases.cheat b/tests/no_prompt_cheats/cases.cheat index 470282d4..e3f07ddb 100644 --- a/tests/no_prompt_cheats/cases.cheat +++ b/tests/no_prompt_cheats/cases.cheat @@ -28,15 +28,26 @@ echo " is cool" # multiple words -> "lorem foo bar ipsum" echo "lorem ipsum" -# variable dependency -> "2 12 a 2" +# variable dependency, full -> "2 12 a 2" echo " " +# variable dependency, we can ignore intermediate values -> "12" +: ; echo "" + +# nested used value -> "path: /my/pictures/wallpapers" +echo "path: " + +# nested unused value -> "path: /my/pictures" +echo "path: " + $ x: echo '2' $ x2: echo "$((x+10))" $ y: echo 'a' $ language: echo '0 rust rust-lang.org' --- --column 2 $ language2: echo '1;clojure;clojure.org' --- --column 2 --delimiter ';' $ multiword: echo 'foo bar' +$ pictures_folder: echo "/my/pictures" +$ wallpaper_folder: echo "/wallpapers" # this should be displayed -> "hi" echo hi \ No newline at end of file