Make preview var content streamable

This commit is contained in:
Denis Isidoro 2021-04-05 09:23:56 -03:00 committed by GitHub
parent f966e6b374
commit deca1f5426
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 62 deletions

View file

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

View file

@ -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 {

View file

@ -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#"
@ -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<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)
}
@ -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 {