Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make --map behave as function body #468

Merged
merged 2 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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