diff --git a/src/info/author.rs b/src/info/author.rs index 14e1ec21a..35f3cf0b9 100644 --- a/src/info/author.rs +++ b/src/info/author.rs @@ -1,7 +1,7 @@ use super::git::metrics::GitMetrics; use crate::{ cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, + info::utils::{format_number, info_field::InfoField}, }; use serde::Serialize; use std::fmt::Write; diff --git a/src/info/churn.rs b/src/info/churn.rs index 90adf90d3..0f9547889 100644 --- a/src/info/churn.rs +++ b/src/info/churn.rs @@ -1,5 +1,5 @@ use super::{git::metrics::GitMetrics, utils::info_field::InfoField}; -use crate::{cli::NumberSeparator, info::format_number}; +use crate::{cli::NumberSeparator, info::utils::format_number}; use serde::Serialize; use std::fmt::Write; diff --git a/src/info/commits.rs b/src/info/commits.rs index 13eda2b3d..ccfbb80ef 100644 --- a/src/info/commits.rs +++ b/src/info/commits.rs @@ -1,7 +1,7 @@ use super::git::metrics::GitMetrics; use crate::{ cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, + info::utils::{format_number, info_field::InfoField}, }; use serde::Serialize; diff --git a/src/info/contributors.rs b/src/info/contributors.rs index 6addc2afd..252a127fc 100644 --- a/src/info/contributors.rs +++ b/src/info/contributors.rs @@ -1,8 +1,5 @@ -use super::git::metrics::GitMetrics; -use crate::{ - cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, -}; +use super::{git::metrics::GitMetrics, utils::format_number}; +use crate::{cli::NumberSeparator, info::utils::info_field::InfoField}; use serde::Serialize; #[derive(Serialize)] diff --git a/src/info/dependencies.rs b/src/info/dependencies.rs index 6fd3d9332..a009f6e93 100644 --- a/src/info/dependencies.rs +++ b/src/info/dependencies.rs @@ -1,6 +1,6 @@ use crate::{ cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, + info::utils::{format_number, info_field::InfoField}, }; use onefetch_manifest::Manifest; use serde::Serialize; diff --git a/src/info/loc.rs b/src/info/loc.rs index e6d713b52..2ded0e3b5 100644 --- a/src/info/loc.rs +++ b/src/info/loc.rs @@ -2,10 +2,9 @@ use crate::info::langs::get_total_loc; use crate::info::langs::language::Language; use serde::Serialize; -use crate::{ - cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, -}; +use crate::{cli::NumberSeparator, info::utils::info_field::InfoField}; + +use super::utils::format_number; #[derive(Serialize)] #[serde(rename_all = "camelCase")] diff --git a/src/info/mod.rs b/src/info/mod.rs index 22b27cae7..71a7160f7 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -27,9 +27,8 @@ use crate::ui::text_colors::TextColors; use anyhow::{Context, Result}; use gix::sec::trust::Mapping; use gix::Repository; -use num_format::ToFormattedString; use onefetch_manifest::Manifest; -use owo_colors::{DynColors, OwoColorize, Style}; +use owo_colors::{DynColors, OwoColorize}; use serde::Serialize; use std::path::Path; @@ -409,14 +408,6 @@ impl InfoBuilder { } } -fn get_style(is_bold: bool, color: DynColors) -> Style { - let mut style = Style::new().color(color); - if is_bold { - style = style.bold(); - } - style -} - fn get_manifest(repo_path: &Path) -> Result> { let manifests = onefetch_manifest::get_manifests(repo_path)?; @@ -433,51 +424,3 @@ pub fn get_work_dir(repo: &gix::Repository) -> Result { .context("please run onefetch inside of a non-bare git repository")? .to_owned()) } - -fn format_number( - number: &T, - number_separator: NumberSeparator, -) -> String { - number.to_formatted_string(&number_separator.get_format()) -} - -#[cfg(test)] -mod tests { - use super::*; - use owo_colors::AnsiColors; - - #[test] - fn test_get_style() { - let style = get_style(true, DynColors::Ansi(AnsiColors::Cyan)); - assert_eq!( - style, - Style::new().color(DynColors::Ansi(AnsiColors::Cyan)).bold() - ); - } - - #[test] - fn test_get_style_no_bold() { - let style = get_style(false, DynColors::Ansi(AnsiColors::Cyan)); - assert_eq!(style, Style::new().color(DynColors::Ansi(AnsiColors::Cyan))); - } - - #[test] - fn test_format_number() { - assert_eq!( - &format_number(&1_000_000, NumberSeparator::Comma), - "1,000,000" - ); - assert_eq!( - &format_number(&1_000_000, NumberSeparator::Space), - "1\u{202f}000\u{202f}000" - ); - assert_eq!( - &format_number(&1_000_000, NumberSeparator::Underscore), - "1_000_000" - ); - assert_eq!( - &format_number(&1_000_000, NumberSeparator::Plain), - "1000000" - ); - } -} diff --git a/src/info/project.rs b/src/info/project.rs index fb4be3e84..65e0823d5 100644 --- a/src/info/project.rs +++ b/src/info/project.rs @@ -1,13 +1,12 @@ -use crate::{ - cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, -}; +use crate::{cli::NumberSeparator, info::utils::info_field::InfoField}; use anyhow::Result; use gix::{bstr::ByteSlice, Repository}; use onefetch_manifest::Manifest; use serde::Serialize; use std::ffi::OsStr; +use super::utils::format_number; + #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct ProjectInfo { diff --git a/src/info/size.rs b/src/info/size.rs index 5f3be9a72..185f5d940 100644 --- a/src/info/size.rs +++ b/src/info/size.rs @@ -1,6 +1,6 @@ use crate::{ cli::NumberSeparator, - info::{format_number, utils::info_field::InfoField}, + info::utils::{format_number, info_field::InfoField}, }; use byte_unit::Byte; use gix::Repository; diff --git a/src/info/title.rs b/src/info/title.rs index c7d4ce599..a99b53d91 100644 --- a/src/info/title.rs +++ b/src/info/title.rs @@ -1,5 +1,4 @@ -use super::get_style; -use crate::cli; +use crate::{cli, info::utils::get_style}; use gix::Repository; use owo_colors::{DynColors, OwoColorize}; use serde::Serialize; diff --git a/src/info/utils/info_field.rs b/src/info/utils/info_field.rs index 8d10bc558..166c90d57 100644 --- a/src/info/utils/info_field.rs +++ b/src/info/utils/info_field.rs @@ -1,5 +1,4 @@ -use super::super::get_style; -use crate::ui::text_colors::TextColors; +use crate::{info::utils::get_style, ui::text_colors::TextColors}; use owo_colors::OwoColorize; use std::fmt; diff --git a/src/info/utils/mod.rs b/src/info/utils/mod.rs index 1e58c95de..bc83cb76c 100644 --- a/src/info/utils/mod.rs +++ b/src/info/utils/mod.rs @@ -1,9 +1,13 @@ use std::time::{Duration, SystemTime}; use gix::date::Time; +use num_format::ToFormattedString; +use owo_colors::{DynColors, Style}; use time::{format_description::well_known::Rfc3339, OffsetDateTime}; use time_humanize::HumanTime; +use crate::cli::NumberSeparator; + pub mod info_field; pub fn format_time(time: Time, iso_time: bool) -> String { @@ -38,8 +42,26 @@ fn to_human_time(time: Time) -> String { ht.to_string() } +pub fn format_number( + number: &T, + number_separator: NumberSeparator, +) -> String { + number.to_formatted_string(&number_separator.get_format()) +} + +pub fn get_style(is_bold: bool, color: DynColors) -> Style { + let mut style = Style::new().color(color); + if is_bold { + style = style.bold(); + } + style +} + #[cfg(test)] mod tests { + use owo_colors::AnsiColors; + use rstest::rstest; + use super::*; use std::time::{Duration, SystemTime}; @@ -107,4 +129,32 @@ mod tests { let result = to_human_time(time); assert_eq!(result, ""); } + + #[rstest] + #[case(1_000_000, NumberSeparator::Comma, "1,000,000")] + #[case(1_000_000, NumberSeparator::Space, "1\u{202f}000\u{202f}000")] + #[case(1_000_000, NumberSeparator::Underscore, "1_000_000")] + #[case(1_000_000, NumberSeparator::Plain, "1000000")] + fn test_format_number( + #[case] number: usize, + #[case] number_separator: NumberSeparator, + #[case] expected: &str, + ) { + assert_eq!(&format_number(&number, number_separator), expected); + } + + #[test] + fn test_get_style() { + let style = get_style(true, DynColors::Ansi(AnsiColors::Cyan)); + assert_eq!( + style, + Style::new().color(DynColors::Ansi(AnsiColors::Cyan)).bold() + ); + } + + #[test] + fn test_get_style_no_bold() { + let style = get_style(false, DynColors::Ansi(AnsiColors::Cyan)); + assert_eq!(style, Style::new().color(DynColors::Ansi(AnsiColors::Cyan))); + } }