From 5d709bded501b0688d3b798f121f0bbabbd46846 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Thu, 26 Mar 2020 00:53:04 +0530 Subject: [PATCH] Remove i32 conversion from fzf helper --- src/util.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/util.rs b/src/util.rs index c223a96..53d016b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -45,15 +45,8 @@ pub fn fzf_helper(now: Epoch, mut dirs: Vec) -> Result> { for dir in dirs.iter() { // ensure that frecency fits in 4 characters - let frecency = if dir.rank > 9999.0 { - 9999 - } else if dir.rank > 0.0 { - dir.rank as i32 - } else { - 0 - }; - - writeln!(fzf_stdin, "{:>4} {}", frecency, dir.path) + let frecency = clamp(dir.rank, 0.0, 9999.0); + writeln!(fzf_stdin, "{:>4.0} {}", frecency, dir.path) .with_context(|| anyhow!("could not write into fzf stdin"))?; } @@ -89,3 +82,18 @@ pub fn fzf_helper(now: Epoch, mut dirs: Vec) -> Result> { _ => bail!("fzf returned an unknown error"), } } + +// FIXME: replace with f64::clamp once it is stable +#[must_use = "method returns a new number and does not mutate the original value"] +#[inline] +pub fn clamp(val: f64, min: f64, max: f64) -> f64 { + assert!(min <= max); + let mut x = val; + if x < min { + x = min; + } + if x > max { + x = max; + } + x +}