From c4009312a5181fe17d8f9e3ddd83b75408566221 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Sun, 4 Apr 2021 18:06:49 -0300 Subject: [PATCH 1/2] Apr-04 18h06: finder.rs, more_cases.cheat, cheatsheet_syntax.md --- docs/cheatsheet_syntax.md | 7 +++---- src/finder.rs | 17 +++++++++++++++-- tests/cheats/more_cases.cheat | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/docs/cheatsheet_syntax.md b/docs/cheatsheet_syntax.md index 17c26783..356cc480 100644 --- a/docs/cheatsheet_syntax.md +++ b/docs/cheatsheet_syntax.md @@ -57,7 +57,7 @@ docker rmi echo $ image_id: docker images --- --column 3 --header-lines 1 --delimiter '\s\s+' -$ mapped: echo 'false true' | tr ' ' '\n' --- --map "[[ $0 == t* ]] && echo 1 || echo 0" +$ mapped: echo 'false true' | tr ' ' '\n' --- --map "[[ $1 == t* ]] && echo 1 || echo 0" ``` The supported parameters are: @@ -135,8 +135,7 @@ true \ ```sh # This will result into: cat "file1.json" "file2.json" -jsons=($(echo "")) -cat "${jsons[@]}" +cat -$ jsons: find . -iname '*.json' -type f -print --- --multi +$ jsons: find . -iname '*.json' -type f -print --- --multi --map "awk '{ print "\""$0"\""}' | tr '\n' ' '" ``` \ No newline at end of file diff --git a/src/finder.rs b/src/finder.rs index 94c82f2d..a48e30c4 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -26,10 +26,23 @@ pub trait Finder { fn apply_map(text: String, map_fn: Option) -> String { if let Some(m) = map_fn { + let cmd = format!( + r#" +_navi_map_fn() {{ + {} +}} + +read -r -d '' navi_input <<'NAVIEOF' +{} +NAVIEOF + +echo "$navi_input" | _navi_map_fn"#, + m, text + ); + let output = Command::new("bash") .arg("-c") - .arg(m.as_str()) - .arg(text.as_str()) + .arg(cmd.as_str()) .stderr(Stdio::inherit()) .output() .expect("Failed to execute map function"); diff --git a/tests/cheats/more_cases.cheat b/tests/cheats/more_cases.cheat index b1a18728..8f0e9513 100644 --- a/tests/cheats/more_cases.cheat +++ b/tests/cheats/more_cases.cheat @@ -65,7 +65,7 @@ $ table_elem: echo -e '0 rust rust-lang.org\n1 clojure clojure.org' --- $ table_elem2: echo -e '0;rust;rust-lang.org\n1;clojure;clojure.org' --- --column 2 --delimiter ';' $ multi_col: ls -la | awk '{print $1, $9}' --- --column 2 --delimiter '\s' --multi $ langs: echo 'clojure rust javascript' | tr ' ' '\n' --- --multi -$ mapped: echo 'true false' | tr ' ' '\n' --- --map "[[ $0 == t* ]] && echo 1 || echo 0" +$ mapped: echo 'true false' | tr ' ' '\n' --- --map "[[ $1 == t* ]] && echo 1 || echo 0" $ examples: echo -e 'foo bar\nlorem ipsum\ndolor sit' --- --multi $ multiword: echo -e 'foo bar\nlorem ipsum\ndolor sit\nbaz'i $ file: ls . --- --preview 'cat {}' --preview-window 'right:50%' From 6d839cb83290b0bb6e888e2f6d35e87d63cb4061 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Sun, 4 Apr 2021 18:25:26 -0300 Subject: [PATCH 2/2] Apr-04 18h25: more_cases.cheat, finder.rs, cheatsheet_syntax.md --- docs/cheatsheet_syntax.md | 2 +- src/finder.rs | 4 ++-- tests/cheats/more_cases.cheat | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/cheatsheet_syntax.md b/docs/cheatsheet_syntax.md index 356cc480..9c3aafb9 100644 --- a/docs/cheatsheet_syntax.md +++ b/docs/cheatsheet_syntax.md @@ -137,5 +137,5 @@ true \ # This will result into: cat "file1.json" "file2.json" cat -$ jsons: find . -iname '*.json' -type f -print --- --multi --map "awk '{ print "\""$0"\""}' | tr '\n' ' '" +$ jsons: find . -iname '*.json' -type f -print --- --multi --map "sed -e 's/^.*$/\"&\"/' | tr '\n' ' ' ``` \ No newline at end of file diff --git a/src/finder.rs b/src/finder.rs index a48e30c4..777ac5ee 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -32,11 +32,11 @@ _navi_map_fn() {{ {} }} -read -r -d '' navi_input <<'NAVIEOF' +read -r -d '' _navi_input <<'NAVIEOF' {} NAVIEOF -echo "$navi_input" | _navi_map_fn"#, +echo "$_navi_input" | _navi_map_fn"#, m, text ); diff --git a/tests/cheats/more_cases.cheat b/tests/cheats/more_cases.cheat index 8f0e9513..b4a7c80a 100644 --- a/tests/cheats/more_cases.cheat +++ b/tests/cheats/more_cases.cheat @@ -53,6 +53,9 @@ echo description blank # x echo description one character +# map can be used to expand into multiple arguments +echo + # Concatenate pdf files files=($(echo "")) echo pdftk "${files[@]:-}" cat output @@ -69,6 +72,7 @@ $ mapped: echo 'true false' | tr ' ' '\n' --- --map "[[ $1 == t* ]] && echo 1 || $ examples: echo -e 'foo bar\nlorem ipsum\ndolor sit' --- --multi $ multiword: echo -e 'foo bar\nlorem ipsum\ndolor sit\nbaz'i $ file: ls . --- --preview 'cat {}' --preview-window 'right:50%' +$ phrases: echo -e "foo bar\nlorem ipsum\ndolor sit" --- --multi --map "sed -e 's/^.*$/\"&\"/' | tr '\n' ' '" # this should be displayed echo hi