diff --git a/Cargo.lock b/Cargo.lock index 3fa6c0b..42f5e48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,7 +325,7 @@ dependencies = [ [[package]] name = "navi" -version = "2.17.2" +version = "2.18.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 36bc44d..284e0b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "navi" -version = "2.17.2" +version = "2.18.0" authors = ["Denis Isidoro "] edition = "2018" description = "An interactive cheatsheet tool for the command-line" diff --git a/src/parser.rs b/src/parser.rs index 1aeb9ee..18d33cc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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 { diff --git a/tests/no_prompt_cheats/cases.cheat b/tests/no_prompt_cheats/cases.cheat index 8ecd64a..653ba2a 100644 --- a/tests/no_prompt_cheats/cases.cheat +++ b/tests/no_prompt_cheats/cases.cheat @@ -58,6 +58,9 @@ echo "foo" \ && echo "match" \ || echo "no match" +# multiline variable -> "foo bar" +echo "" + $ 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