Skip to content

Commit

Permalink
Allow implicit variable dependencies (#379)
Browse files Browse the repository at this point in the history
* Allow implicit variable dependencies

* Aug-14 12h01: core.rs, Cargo.toml, Cargo.lock

* Aug-14 12h04: cases.cheat

* Aug-14 12h08: cases.cheat
  • Loading branch information
denisidoro authored Aug 14, 2020
1 parent e11c48e commit 657992b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "navi"
version = "2.7.2"
version = "2.8.0"
authors = ["Denis Isidoro <[email protected]>"]
edition = "2018"
description = "An interactive cheatsheet tool for the command-line"
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion src/flows/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
})
Expand Down
1 change: 1 addition & 0 deletions src/structures/cheat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion tests/no_prompt_cheats/cases.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 657992b

Please sign in to comment.