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 +}