Skip to content

Commit

Permalink
Make preview window show mapped values
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 4, 2021
1 parent 4c297c7 commit 1948789
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion 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 "[[ $1 == t* ]] && echo 1 || echo 0"
$ mapped: echo 'false true' | tr ' ' '\n' --- --map "grep -q t && echo 1 || echo 0"
```

The supported parameters are:
Expand Down
13 changes: 1 addition & 12 deletions src/display/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()),
);
}

Expand Down
12 changes: 9 additions & 3 deletions src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cheats/more_cases.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand Down

0 comments on commit 1948789

Please sign in to comment.