Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Launch correct fzf binary on Windows
Browse files Browse the repository at this point in the history
ajeetdsouza committed Dec 24, 2021
1 parent 339a789 commit b2f049d
Showing 6 changed files with 62 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Parse error on Cygwin/MSYS due to CRLF line endings.
- Fzf: handle spaces correctly in preview window.
- Bash: avoid initializing completions on older versions.
- Fzf: avoid launching binary from current directory on Windows.

## [0.7.9] - 2021-11-02

33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -27,12 +27,14 @@ glob = "0.3.0"
ordered-float = "2.0.0"
serde = { version = "1.0.116", features = ["derive"] }
tempfile = "3.1.0"
thiserror = "1.0.30"

[target.'cfg(windows)'.dependencies]
rand = { version = "0.8.4", features = [
"getrandom",
"small_rng",
], default-features = false }
which = "4.2.2"

[build-dependencies]
clap = { version = "=3.0.0-rc.8", features = ["derive"] }
12 changes: 6 additions & 6 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -120,6 +120,12 @@ impl<'file> Database<'file> {
}
}

#[cfg(unix)]
fn persist<P: AsRef<Path>>(file: NamedTempFile, path: P) -> Result<(), PersistError> {
file.persist(path)?;
Ok(())
}

#[cfg(windows)]
fn persist<P: AsRef<Path>>(mut file: NamedTempFile, path: P) -> Result<(), PersistError> {
use std::thread;
@@ -152,12 +158,6 @@ fn persist<P: AsRef<Path>>(mut file: NamedTempFile, path: P) -> Result<(), Persi
Ok(())
}

#[cfg(unix)]
fn persist<P: AsRef<Path>>(file: NamedTempFile, path: P) -> Result<(), PersistError> {
file.persist(path)?;
Ok(())
}

pub struct DatabaseFile {
buffer: Vec<u8>,
data_dir: PathBuf,
15 changes: 7 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use std::fmt::{self, Display, Formatter};
use std::io;

use anyhow::{bail, Context, Result};
use thiserror::Error;

#[derive(Debug, Error)]
#[error("could not find fzf, is it installed?")]
pub struct FzfNotFound;

/// Custom error type for early exit.
#[derive(Debug)]
#[derive(Debug, Error)]
#[error("")]
pub struct SilentExit {
pub code: i32,
}

impl Display for SilentExit {
fn fmt(&self, _: &mut Formatter) -> fmt::Result {
Ok(())
}
}

pub trait BrokenPipeHandler {
fn pipe_exit(self, device: &str) -> Result<()>;
}
18 changes: 13 additions & 5 deletions src/fzf.rs
Original file line number Diff line number Diff line change
@@ -5,15 +5,25 @@ use std::process::{Child, ChildStdin, Command, Stdio};
use anyhow::{bail, Context, Result};

use crate::config;
use crate::error::SilentExit;
use crate::error::{FzfNotFound, SilentExit};

pub struct Fzf {
child: Child,
}

impl Fzf {
pub fn new(multiple: bool) -> Result<Self> {
let mut command = Command::new("fzf");
// On Windows:
// - Pass in an absolute path, otherwise it will prioritize an `fzf.exe`
// from the current directory.
// - Use the `.exe` extension, otherwise Windows might launch a binary
// with an extension like `.bat` or `.cmd`.
#[cfg(unix)]
let bin_path = "fzf";
#[cfg(windows)]
let bin_path = which::which("fzf.exe").or(Err(FzfNotFound))?;

let mut command = Command::new(bin_path);
if multiple {
command.arg("-m");
}
@@ -37,9 +47,7 @@ impl Fzf {

let child = match command.spawn() {
Ok(child) => child,
Err(e) if e.kind() == io::ErrorKind::NotFound => {
bail!("could not find fzf, is it installed?")
}
Err(e) if e.kind() == io::ErrorKind::NotFound => bail!(FzfNotFound),
Err(e) => Err(e).context("could not launch fzf")?,
};

0 comments on commit b2f049d

Please sign in to comment.