diff --git a/src/common/clipboard.rs b/src/common/clipboard.rs deleted file mode 100644 index addc97b7..00000000 --- a/src/common/clipboard.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::shell::BashSpawnError; -use anyhow::Error; -use std::process::Command; - -pub fn copy(text: String) -> Result<(), Error> { - let cmd = r#" -exst() { - type "$1" &>/dev/null -} - -_copy() { - if exst pbcopy; then - pbcopy - elif exst xclip; then - xclip -selection clipboard - elif exst clip.exe; then - clip.exe - else - exit 55 - fi -}"#; - - Command::new("bash") - .arg("-c") - .arg( - format!( - r#"{} - read -r -d '' x <<'NAVIEOF' -{} -NAVIEOF - -echo -n "$x" | _copy"#, - cmd, text - ) - .as_str(), - ) - .spawn() - .map_err(|e| BashSpawnError::new(cmd, e))? - .wait()?; - - Ok(()) -} diff --git a/src/display/terminal.rs b/src/display/terminal.rs index 4168cc29..e960655f 100644 --- a/src/display/terminal.rs +++ b/src/display/terminal.rs @@ -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> = { @@ -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 { diff --git a/src/finder.rs b/src/finder.rs index 3e3eb594..1ed5c8e1 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -25,8 +25,7 @@ pub trait Finder { } // TODO: extract -// TODO: make it return Result -fn apply_map(text: String, map_fn: Option) -> String { +fn apply_map(text: String, map_fn: Option) -> Result { if let Some(m) = map_fn { let cmd = format!( r#" @@ -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) } } @@ -77,7 +76,12 @@ fn get_column(text: String, column: Option, delimiter: Option<&str>) -> Stri } // TODO: extract -pub fn process(text: String, column: Option, delimiter: Option<&str>, map_fn: Option) -> String { +pub fn process( + text: String, + column: Option, + delimiter: Option<&str>, + map_fn: Option, +) -> Result { apply_map(get_column(text, column, delimiter), map_fn) } @@ -132,8 +136,7 @@ fn parse(out: Output, opts: Opts) -> Result { }; 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 {