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
This commit is contained in:
Denis Isidoro 2020-08-14 13:53:23 -03:00 committed by GitHub
parent e11c48ec03
commit 657992b173
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 4 deletions

2
Cargo.lock generated
View file

@ -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)",

View file

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

View file

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

View file

@ -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(),
)
})

View file

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

View file

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