Make --map behave as function body (#468)

This commit is contained in:
Denis Isidoro 2021-04-04 18:26:16 -03:00 committed by GitHub
parent 17cd4f52d1
commit 4c297c7a29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 7 deletions

View file

@ -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:
@ -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' ' '
```

View file

@ -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");

View file

@ -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>
@ -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