2020-10-26 17:55:04 +00:00
|
|
|
use crate::config;
|
|
|
|
|
use crate::error::SilentExit;
|
|
|
|
|
|
|
|
|
|
use anyhow::{bail, Context, Result};
|
|
|
|
|
|
|
|
|
|
use std::process::{Child, ChildStdin, Command, Stdio};
|
|
|
|
|
|
|
|
|
|
pub struct Fzf {
|
|
|
|
|
child: Child,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Fzf {
|
2021-04-28 20:01:00 +00:00
|
|
|
pub fn new(multiple: bool) -> Result<Self> {
|
2020-10-26 17:55:04 +00:00
|
|
|
let mut command = Command::new("fzf");
|
2021-04-28 20:01:00 +00:00
|
|
|
if multiple {
|
|
|
|
|
command.arg("-m");
|
|
|
|
|
}
|
2020-10-26 17:55:04 +00:00
|
|
|
command
|
|
|
|
|
.arg("-n2..")
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.stdout(Stdio::piped());
|
|
|
|
|
if let Some(fzf_opts) = config::zo_fzf_opts() {
|
|
|
|
|
command.env("FZF_DEFAULT_OPTS", fzf_opts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Fzf {
|
|
|
|
|
child: command.spawn().context("could not launch fzf")?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn stdin(&mut self) -> &mut ChildStdin {
|
2021-04-28 20:01:00 +00:00
|
|
|
// unwrap is safe here because command.stdin() has been piped
|
2020-10-26 17:55:04 +00:00
|
|
|
self.child.stdin.as_mut().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn wait_select(self) -> Result<String> {
|
|
|
|
|
let output = self
|
|
|
|
|
.child
|
|
|
|
|
.wait_with_output()
|
|
|
|
|
.context("wait failed on fzf")?;
|
|
|
|
|
|
|
|
|
|
match output.status.code() {
|
|
|
|
|
// normal exit
|
|
|
|
|
Some(0) => String::from_utf8(output.stdout).context("invalid unicode in fzf output"),
|
|
|
|
|
|
|
|
|
|
// no match
|
|
|
|
|
Some(1) => bail!("no match found"),
|
|
|
|
|
|
|
|
|
|
// error
|
|
|
|
|
Some(2) => bail!("fzf returned an error"),
|
|
|
|
|
|
|
|
|
|
// terminated by a signal
|
|
|
|
|
Some(code @ 130) => bail!(SilentExit { code }),
|
|
|
|
|
Some(128..=254) | None => bail!("fzf was terminated"),
|
|
|
|
|
|
|
|
|
|
// unknown
|
|
|
|
|
_ => bail!("fzf returned an unknown error"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|