Skip to content

Commit

Permalink
Make preview var content streamable
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 5, 2021
1 parent f966e6b commit deca1f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 62 deletions.
42 changes: 0 additions & 42 deletions src/common/clipboard.rs

This file was deleted.

27 changes: 15 additions & 12 deletions src/display/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,19 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
let inactive_color = color::Fg(*COMMENT_COLOR);

let mut colored_snippet = String::from(snippet);
let mut variables = String::from("");
let mut visited_vars: HashSet<&str> = HashSet::new();

let mut variables = String::from("");

println!(
"{comment_color}{comment} {tag_color}{tags}{reset}",
comment = comment,
tags = format!("[{}]", tags),
comment_color = color::Fg(*COMMENT_COLOR),
tag_color = color::Fg(*TAG_COLOR),
reset = reset,
);

let bracketed_current_variable = format!("<{}>", variable);

let bracketed_variables: Vec<&str> = {
Expand Down Expand Up @@ -118,20 +128,13 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
color = variable_color,
variable = variable_name,
reset = reset,
value = &finder::process(value, column, delimiter.as_deref(), map.clone()),
value = &finder::process(value, column, delimiter.as_deref(), map.clone())
.expect("Unable to process value"),
);
}

println!(
"{comment_color}{comment} {tag_color}{tags}{reset} \n{snippet}\n{variables}",
comment = comment,
tags = format!("[{}]", tags),
snippet = display::fix_newlines(&colored_snippet),
comment_color = color::Fg(*COMMENT_COLOR),
tag_color = color::Fg(*TAG_COLOR),
variables = variables,
reset = reset
);
println!("{snippet}", snippet = display::fix_newlines(&colored_snippet));
println!("{variables}", variables = variables);
}

fn limit_str(text: &str, length: usize) -> String {
Expand Down
19 changes: 11 additions & 8 deletions src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ pub trait Finder {
}

// TODO: extract
// TODO: make it return Result
fn apply_map(text: String, map_fn: Option<String>) -> String {
fn apply_map(text: String, map_fn: Option<String>) -> Result<String, Error> {
if let Some(m) = map_fn {
let cmd = format!(
r#"
Expand All @@ -47,11 +46,11 @@ echo "$_navi_input" | _navi_map_fn"#,
.arg(cmd.as_str())
.stderr(Stdio::inherit())
.output()
.expect("Failed to execute map function");
.context("Failed to execute map function")?;

String::from_utf8(output.stdout).expect("Invalid utf8 output for map function")
String::from_utf8(output.stdout).context("Invalid utf8 output for map function")
} else {
text
Ok(text)
}
}

Expand All @@ -77,7 +76,12 @@ fn get_column(text: String, column: Option<u8>, delimiter: Option<&str>) -> Stri
}

// TODO: extract
pub fn process(text: String, column: Option<u8>, delimiter: Option<&str>, map_fn: Option<String>) -> String {
pub fn process(
text: String,
column: Option<u8>,
delimiter: Option<&str>,
map_fn: Option<String>,
) -> Result<String, Error> {
apply_map(get_column(text, column, delimiter), map_fn)
}

Expand Down Expand Up @@ -132,8 +136,7 @@ fn parse(out: Output, opts: Opts) -> Result<String, Error> {
};

let output = parse_output_single(text, opts.suggestion_type)?;
let output = process(output, opts.column, opts.delimiter.as_deref(), opts.map);
Ok(output)
process(output, opts.column, opts.delimiter.as_deref(), opts.map)
}

impl Finder for FinderChoice {
Expand Down

0 comments on commit deca1f5

Please # to comment.