Add support for multiline variables

Fixes #628
This commit is contained in:
Denis Isidoro 2021-10-15 13:16:10 -03:00 committed by GitHub
parent 8672c02558
commit 3b1038c884
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 11 deletions

2
Cargo.lock generated
View file

@ -325,7 +325,7 @@ dependencies = [
[[package]] [[package]]
name = "navi" name = "navi"
version = "2.17.2" version = "2.18.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "navi" name = "navi"
version = "2.17.2" version = "2.18.0"
authors = ["Denis Isidoro <denis_isidoro@live.com>"] authors = ["Denis Isidoro <denis_isidoro@live.com>"]
edition = "2018" edition = "2018"
description = "An interactive cheatsheet tool for the command-line" description = "An interactive cheatsheet tool for the command-line"

View file

@ -175,6 +175,8 @@ pub fn read_lines(
let mut should_break = false; let mut should_break = false;
let mut variable_cmd = String::from("");
for (line_nr, line_result) in lines.enumerate() { for (line_nr, line_result) in lines.enumerate() {
let line = line_result let line = line_result
.with_context(|| format!("Failed to read line number {} in cheatsheet `{}`", line_nr, id))?; .with_context(|| format!("Failed to read line number {} in cheatsheet `{}`", line_nr, id))?;
@ -212,17 +214,26 @@ pub fn read_lines(
item.comment = without_prefix(&line); item.comment = without_prefix(&line);
} }
// variable // variable
else if line.starts_with('$') && line.contains(':') { else if !variable_cmd.is_empty() || (line.starts_with('$') && line.contains(':')) {
should_break = write_cmd(&item, stdin, allowlist, denylist, visited_lines).is_err(); should_break = write_cmd(&item, stdin, allowlist, denylist, visited_lines).is_err();
item.snippet = String::from(""); item.snippet = String::from("");
let (variable, command, opts) = parse_variable_line(&line).with_context(|| {
format!( variable_cmd.push_str(line.trim_end_matches('\\'));
"Failed to parse variable line. See line number {} in cheatsheet `{}`",
line_nr + 1, if !line.ends_with('\\') {
id let full_variable_cmd = variable_cmd.clone();
) let (variable, command, opts) =
})?; parse_variable_line(&full_variable_cmd).with_context(|| {
variables.insert_suggestion(&item.tags, variable, (String::from(command), opts)); format!(
"Failed to parse variable line. See line number {} in cheatsheet `{}`",
line_nr + 1,
id
)
})?;
variable_cmd = String::from("");
variables.insert_suggestion(&item.tags, variable, (String::from(command), opts));
}
} }
// snippet // snippet
else { else {

View file

@ -58,6 +58,9 @@ echo "foo" \
&& echo "match" \ && echo "match" \
|| echo "no match" || echo "no match"
# multiline variable -> "foo bar"
echo "<multilinevar>"
$ x: echo '2' $ x: echo '2'
$ x2: echo "$((x+10))" $ x2: echo "$((x+10))"
$ y: echo 'a' $ y: echo 'a'
@ -66,8 +69,12 @@ $ language2: echo '1;clojure;clojure.org' --- --column 2 --delimiter ';'
$ multiword: echo 'foo bar' $ multiword: echo 'foo bar'
$ pictures_folder: echo "/my/pictures" $ pictures_folder: echo "/my/pictures"
$ map1: echo "foo" --- --map 'echo _$(cat)_' $ map1: echo "foo" --- --map 'echo _$(cat)_'
$ multilinevar: echo "xoo yar" \
| tr 'x' 'f' \
| tr 'y' 'b'
$ expand1: echo "foo" --- --expand $ expand1: echo "foo" --- --expand
# this should be displayed -> "hi" # this should be displayed -> "hi"
echo hi echo hi