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]]
name = "navi"
version = "2.17.2"
version = "2.18.0"
dependencies = [
"anyhow",
"clap",

View file

@ -1,6 +1,6 @@
[package]
name = "navi"
version = "2.17.2"
version = "2.18.0"
authors = ["Denis Isidoro <denis_isidoro@live.com>"]
edition = "2018"
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 variable_cmd = String::from("");
for (line_nr, line_result) in lines.enumerate() {
let line = line_result
.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);
}
// 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();
item.snippet = String::from("");
let (variable, command, opts) = parse_variable_line(&line).with_context(|| {
format!(
"Failed to parse variable line. See line number {} in cheatsheet `{}`",
line_nr + 1,
id
)
})?;
variables.insert_suggestion(&item.tags, variable, (String::from(command), opts));
variable_cmd.push_str(line.trim_end_matches('\\'));
if !line.ends_with('\\') {
let full_variable_cmd = variable_cmd.clone();
let (variable, command, opts) =
parse_variable_line(&full_variable_cmd).with_context(|| {
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
else {

View file

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