/src/lib.rs

https://github.com/phiresky/ripgrep-all · Rust · 70 lines · 56 code · 6 blank · 8 comment · 7 complexity · d2a7f06bfab48a7dc79b366c770c32af MD5 · raw file

  1. #![warn(clippy::all)]
  2. #![feature(negative_impls)]
  3. #![feature(specialization)]
  4. pub mod adapters;
  5. mod caching_writer;
  6. pub mod config;
  7. pub mod matching;
  8. pub mod pipe;
  9. pub mod preproc;
  10. pub mod preproc_cache;
  11. #[cfg(test)]
  12. pub mod test_utils;
  13. use anyhow::Context;
  14. use anyhow::Result;
  15. pub use caching_writer::CachingReader;
  16. use directories_next::ProjectDirs;
  17. use std::time::Instant;
  18. pub fn project_dirs() -> Result<ProjectDirs> {
  19. directories_next::ProjectDirs::from("", "", "ripgrep-all")
  20. .context("no home directory found! :(")
  21. }
  22. // no "significant digits" format specifier in rust??
  23. // https://stackoverflow.com/questions/60497397/how-do-you-format-a-float-to-the-first-significant-decimal-and-with-specified-pr
  24. fn meh(float: f32, precision: usize) -> usize {
  25. // compute absolute value
  26. let a = float.abs();
  27. // if abs value is greater than 1, then precision becomes less than "standard"
  28. let precision = if a >= 1. {
  29. // reduce by number of digits, minimum 0
  30. let n = (1. + a.log10().floor()) as usize;
  31. if n <= precision {
  32. precision - n
  33. } else {
  34. 0
  35. }
  36. // if precision is less than 1 (but non-zero), then precision becomes greater than "standard"
  37. } else if a > 0. {
  38. // increase number of digits
  39. let n = -(1. + a.log10().floor()) as usize;
  40. precision + n
  41. // special case for 0
  42. } else {
  43. 0
  44. };
  45. precision
  46. }
  47. pub fn print_dur(start: Instant) -> String {
  48. let mut dur = Instant::now().duration_since(start).as_secs_f32();
  49. let mut suffix = "";
  50. if dur < 0.1 {
  51. suffix = "m";
  52. dur *= 1000.0;
  53. }
  54. let precision = meh(dur, 3);
  55. format!(
  56. "{dur:.prec$}{suffix}s",
  57. dur = dur,
  58. prec = precision,
  59. suffix = suffix
  60. )
  61. }
  62. pub fn print_bytes(bytes: impl Into<f64>) -> String {
  63. return pretty_bytes::converter::convert(bytes.into());
  64. }