From 194878915dc949df7f5e379240d1050f55dd5685 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Sun, 4 Apr 2021 18:34:01 -0300 Subject: [PATCH] Make preview window show mapped values --- docs/cheatsheet_syntax.md | 2 +- src/display/terminal.rs | 13 +------------ src/finder.rs | 12 +++++++++--- tests/cheats/more_cases.cheat | 2 +- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/docs/cheatsheet_syntax.md b/docs/cheatsheet_syntax.md index 9c3aafb9..829e0f03 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 "[[ $1 == t* ]] && echo 1 || echo 0" +$ mapped: echo 'false true' | tr ' ' '\n' --- --map "grep -q t && echo 1 || echo 0" ``` The supported parameters are: diff --git a/src/display/terminal.rs b/src/display/terminal.rs index 649b86a5..4168cc29 100644 --- a/src/display/terminal.rs +++ b/src/display/terminal.rs @@ -42,14 +42,6 @@ pub fn preview(comment: &str, tags: &str, snippet: &str) { ); } -pub fn wrapped_by_map(text: &str, map: Option<&str>) -> String { - if map.is_none() { - text.to_string() - } else { - format!("map({})", text) - } -} - fn get_env_var(name: &str) -> String { if let Ok(v) = env::var(name) { v @@ -126,10 +118,7 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) { color = variable_color, variable = variable_name, reset = reset, - value = wrapped_by_map( - &finder::get_column(value, column, delimiter.as_deref()), - map.as_deref() - ) + value = &finder::process(value, column, delimiter.as_deref(), map.clone()), ); } diff --git a/src/finder.rs b/src/finder.rs index 777ac5ee..3e3eb594 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -24,6 +24,8 @@ pub trait Finder { F: Fn(&mut process::ChildStdin, &mut Vec) -> Result, Error>; } +// TODO: extract +// TODO: make it return Result fn apply_map(text: String, map_fn: Option) -> String { if let Some(m) = map_fn { let cmd = format!( @@ -54,7 +56,7 @@ echo "$_navi_input" | _navi_map_fn"#, } // TODO: extract -pub fn get_column(text: String, column: Option, delimiter: Option<&str>) -> String { +fn get_column(text: String, column: Option, delimiter: Option<&str>) -> String { if let Some(c) = column { let mut result = String::from(""); let re = regex::Regex::new(delimiter.unwrap_or(r"\s\s+")).expect("Invalid regex"); @@ -74,6 +76,11 @@ pub fn get_column(text: String, column: Option, delimiter: Option<&str>) -> } } +// TODO: extract +pub fn process(text: String, column: Option, delimiter: Option<&str>, map_fn: Option) -> String { + apply_map(get_column(text, column, delimiter), map_fn) +} + fn parse_output_single(mut text: String, suggestion_type: SuggestionType) -> Result { Ok(match suggestion_type { SuggestionType::SingleSelection => text @@ -125,8 +132,7 @@ fn parse(out: Output, opts: Opts) -> Result { }; let output = parse_output_single(text, opts.suggestion_type)?; - let output = get_column(output, opts.column, opts.delimiter.as_deref()); - let output = apply_map(output, opts.map); + let output = process(output, opts.column, opts.delimiter.as_deref(), opts.map); Ok(output) } diff --git a/tests/cheats/more_cases.cheat b/tests/cheats/more_cases.cheat index b4a7c80a..d9f542a3 100644 --- a/tests/cheats/more_cases.cheat +++ b/tests/cheats/more_cases.cheat @@ -68,7 +68,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 "[[ $1 == t* ]] && echo 1 || echo 0" +$ mapped: echo 'true false' | tr ' ' '\n' --- --map "grep -q 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%'