Skip to content

Commit

Permalink
move get_style and format_number in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Nov 25, 2023
1 parent ba08584 commit 7dd9711
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/info/author.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/info/churn.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/info/commits.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
7 changes: 2 additions & 5 deletions src/info/contributors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/info/dependencies.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/info/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
59 changes: 1 addition & 58 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Option<Manifest>> {
let manifests = onefetch_manifest::get_manifests(repo_path)?;

Expand All @@ -433,51 +424,3 @@ pub fn get_work_dir(repo: &gix::Repository) -> Result<std::path::PathBuf> {
.context("please run onefetch inside of a non-bare git repository")?
.to_owned())
}

fn format_number<T: ToFormattedString + std::fmt::Display>(
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"
);
}
}
7 changes: 3 additions & 4 deletions src/info/project.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/info/size.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/info/title.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/info/utils/info_field.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
50 changes: 50 additions & 0 deletions src/info/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -38,8 +42,26 @@ fn to_human_time(time: Time) -> String {
ht.to_string()
}

pub fn format_number<T: ToFormattedString + std::fmt::Display>(
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};

Expand Down Expand Up @@ -107,4 +129,32 @@ mod tests {
let result = to_human_time(time);
assert_eq!(result, "<before UNIX epoch>");
}

#[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)));
}
}

0 comments on commit 7dd9711

Please sign in to comment.