ripgrep-all/src/lib.rs

71 lines
1.9 KiB
Rust
Raw Normal View History

2019-06-06 21:43:30 +00:00
#![warn(clippy::all)]
2020-06-11 21:09:31 +00:00
#![feature(negative_impls)]
#![feature(specialization)]
2019-06-06 21:43:30 +00:00
2019-06-04 18:08:26 +00:00
pub mod adapters;
2019-06-07 19:46:03 +00:00
pub mod args;
2019-06-05 14:43:40 +00:00
mod caching_writer;
2019-06-12 10:25:02 +00:00
pub mod matching;
2020-06-11 21:09:31 +00:00
pub mod pipe;
2019-06-06 09:00:13 +00:00
pub mod preproc;
2019-06-07 17:00:24 +00:00
pub mod preproc_cache;
2020-06-11 21:09:31 +00:00
#[cfg(test)]
pub mod test_utils;
2020-06-08 21:11:43 +00:00
use anyhow::Context;
use anyhow::Result;
2019-06-05 14:43:40 +00:00
pub use caching_writer::CachingWriter;
2020-06-08 21:11:43 +00:00
use directories_next::ProjectDirs;
2020-06-09 10:47:34 +00:00
use std::time::Instant;
2020-06-08 21:11:43 +00:00
pub fn project_dirs() -> Result<ProjectDirs> {
directories_next::ProjectDirs::from("", "", "ripgrep-all")
.context("no home directory found! :(")
}
2020-06-09 10:47:34 +00:00
// no "significant digits" format specifier in rust??
// https://stackoverflow.com/questions/60497397/how-do-you-format-a-float-to-the-first-significant-decimal-and-with-specified-pr
fn meh(float: f32, precision: usize) -> usize {
// compute absolute value
let a = float.abs();
// if abs value is greater than 1, then precision becomes less than "standard"
let precision = if a >= 1. {
// reduce by number of digits, minimum 0
let n = (1. + a.log10().floor()) as usize;
if n <= precision {
precision - n
} else {
0
}
// if precision is less than 1 (but non-zero), then precision becomes greater than "standard"
} else if a > 0. {
// increase number of digits
let n = -(1. + a.log10().floor()) as usize;
precision + n
// special case for 0
} else {
0
};
precision
}
pub fn print_dur(start: Instant) -> String {
let mut dur = Instant::now().duration_since(start).as_secs_f32();
let mut suffix = "";
if dur < 0.1 {
suffix = "m";
dur *= 1000.0;
}
let precision = meh(dur, 3);
format!(
"{dur:.prec$}{suffix}s",
dur = dur,
prec = precision,
suffix = suffix
)
}
pub fn print_bytes(bytes: impl Into<f64>) -> String {
return pretty_bytes::converter::convert(bytes.into());
}