Skip to content

Commit

Permalink
Make --map behave as function body (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 4, 2021
1 parent 17cd4f5 commit 4c297c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
7 changes: 3 additions & 4 deletions docs/cheatsheet_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ docker rmi <image_id>
echo <mapped>

$ 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:
Expand Down Expand Up @@ -135,8 +135,7 @@ true \

```sh
# This will result into: cat "file1.json" "file2.json"
jsons=($(echo "<jsons>"))
cat "${jsons[@]}"
cat <jsons>

$ jsons: find . -iname '*.json' -type f -print --- --multi
$ jsons: find . -iname '*.json' -type f -print --- --multi --map "sed -e 's/^.*$/\"&\"/' | tr '\n' ' '
```
17 changes: 15 additions & 2 deletions src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,23 @@ pub trait Finder {

fn apply_map(text: String, map_fn: Option<String>) -> 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");
Expand Down
6 changes: 5 additions & 1 deletion tests/cheats/more_cases.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ echo description blank
# x
echo description one character

# map can be used to expand into multiple arguments
echo <phrases>

# Concatenate pdf files
files=($(echo "<files>"))
echo pdftk "${files[@]:-}" cat output <pdf_output>
Expand All @@ -65,10 +68,11 @@ $ 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%'
$ phrases: echo -e "foo bar\nlorem ipsum\ndolor sit" --- --multi --map "sed -e 's/^.*$/\"&\"/' | tr '\n' ' '"

# this should be displayed
echo hi
Expand Down

0 comments on commit 4c297c7

Please sign in to comment.