Make preview window show mapped values

This commit is contained in:
Denis Isidoro 2021-04-04 18:34:01 -03:00 committed by GitHub
parent 4c297c7a29
commit 194878915d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 17 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 "[[ $1 == t* ]] && echo 1 || echo 0"
$ mapped: echo 'false true' | tr ' ' '\n' --- --map "grep -q t && echo 1 || echo 0"
```
The supported parameters are:

View file

@ -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()),
);
}

View file

@ -24,6 +24,8 @@ pub trait Finder {
F: Fn(&mut process::ChildStdin, &mut Vec<String>) -> Result<Option<VariableMap>, Error>;
}
// TODO: extract
// TODO: make it return Result
fn apply_map(text: String, map_fn: Option<String>) -> 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<u8>, delimiter: Option<&str>) -> String {
fn get_column(text: String, column: Option<u8>, 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<u8>, delimiter: Option<&str>) ->
}
}
// TODO: extract
pub fn process(text: String, column: Option<u8>, delimiter: Option<&str>, map_fn: Option<String>) -> String {
apply_map(get_column(text, column, delimiter), map_fn)
}
fn parse_output_single(mut text: String, suggestion_type: SuggestionType) -> Result<String, Error> {
Ok(match suggestion_type {
SuggestionType::SingleSelection => text
@ -125,8 +132,7 @@ fn parse(out: Output, opts: Opts) -> Result<String, Error> {
};
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)
}

View file

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