diff options
author | Denis Isidoro <denisidoro@users.noreply.github.com> | 2020-08-14 13:53:23 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-14 13:53:23 -0300 |
commit | 657992b1737c94e8d95352673679b827c4702691 (patch) | |
tree | ef9b769f0d68424f293f02adaba1748173720c1c | |
parent | e11c48ec03e6c0528e92988ed6813652209dc294 (diff) |
Allow implicit variable dependencies (#379)v2.8.0
* Allow implicit variable dependencies
* Aug-14 12h01: core.rs, Cargo.toml, Cargo.lock
* Aug-14 12h04: cases.cheat
* Aug-14 12h08: cases.cheat
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | src/flows/core.rs | 10 | ||||
-rw-r--r-- | src/structures/cheat.rs | 1 | ||||
-rw-r--r-- | tests/no_prompt_cheats/cases.cheat | 13 |
6 files changed, 36 insertions, 4 deletions
@@ -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)", @@ -1,6 +1,6 @@ [package] name = "navi" -version = "2.7.2" +version = "2.8.0" authors = ["Denis Isidoro <denis_isidoro@live.com>"] edition = "2018" description = "An interactive cheatsheet tool for the command-line" @@ -237,10 +237,22 @@ The command for generating possible inputs can refer previous variables: # If you select "hello" for <x>, the possible values of <y> will be "hello foo" and "hello bar" echo <x> <y> +# If you want to ignore the contents of <x> and only print <y> +: <x>; echo <y> + $ 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 `<varname>` syntax inside a variable command: +```sh +# Should print /my/pictures/wallpapers +echo "<wallpaper_folder>" + +$ pictures_folder: echo "/my/pictures" +$ wallpaper_folder: echo "<pictures_folder>/wallpapers" +``` + ### Multiline snippets Commands may be multiline: diff --git a/src/flows/core.rs b/src/flows/core.rs index 2894cfe..3b8055a 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 778a337..2bcdf93 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<u64, Suggestion>); impl VariableMap { diff --git a/tests/no_prompt_cheats/cases.cheat b/tests/no_prompt_cheats/cases.cheat index 470282d..e3f07dd 100644 --- a/tests/no_prompt_cheats/cases.cheat +++ b/tests/no_prompt_cheats/cases.cheat @@ -28,15 +28,26 @@ echo "<language2> is cool" # multiple words -> "lorem foo bar ipsum" echo "lorem <multiword> ipsum" -# variable dependency -> "2 12 a 2" +# variable dependency, full -> "2 12 a 2" echo "<x> <x2> <y> <x>" +# variable dependency, we can ignore intermediate values -> "12" +: <x>; echo "<x2>" + +# nested used value -> "path: /my/pictures/wallpapers" +echo "path: <wallpaper_folder>" + +# nested unused value -> "path: /my/pictures" +echo "path: <pictures_folder>" + $ 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 "<pictures_folder>/wallpapers" # this should be displayed -> "hi" echo hi
\ No newline at end of file |