Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --number-separator CLI flag #892

Merged
merged 6 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 21 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ git-repository = { version = "0.29.0", default-features = false, features = [
git2 = { version = "0.15.0", default-features = false }
human-panic = "1.0.3"
image = "0.24.4"
num-format = "0.4.4"
onefetch-image = { path = "image", version = "2.13.2" }
onefetch-manifest = { path = "manifest", version = "2.13.2" }
owo-colors = "3.5.0"
Expand Down
32 changes: 31 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::builder::PossibleValuesParser;
use clap::builder::TypedValueParser as _;
use clap::{value_parser, Command, Parser, ValueHint};
use clap_complete::{generate, Generator, Shell};
use num_format::CustomFormat;
use onefetch_image::ImageProtocol;
use onefetch_manifest::ManifestType;
use regex::Regex;
Expand Down Expand Up @@ -125,7 +126,7 @@ pub struct Config {
/// '--text-colors 9 10 11 12 13 14'
#[arg(
long,
short = 't',
short,
value_name = "X",
value_parser = value_parser!(u8).range(..16),
num_args = 1..=6
Expand All @@ -134,6 +135,9 @@ pub struct Config {
/// Use ISO 8601 formatted timestamps
#[arg(long, short = 'z')]
pub iso_time: bool,
/// Which thousands SEPARATOR to use
#[arg(long, value_name = "SEPARATOR", value_enum)]
pub number_separator: Option<NumberSeparator>,
/// Show the email address of each author
#[arg(long, short = 'E')]
pub email: bool,
Expand Down Expand Up @@ -179,6 +183,7 @@ impl Default for Config {
show_logo: When::Always,
text_colors: Default::default(),
iso_time: Default::default(),
number_separator: Default::default(),
email: Default::default(),
include_hidden: Default::default(),
r#type: vec![LanguageType::Programming, LanguageType::Markup],
Expand Down Expand Up @@ -229,6 +234,31 @@ pub enum When {
Always,
}

#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
pub enum NumberSeparator {
Comma,
Space,
Underscore,
}

impl NumberSeparator {
fn separator(&self) -> &'static str {
match self {
Self::Comma => ",",
Self::Space => "\u{202f}",
Self::Underscore => "_",
}
}

pub fn get_format(&self) -> CustomFormat {
num_format::CustomFormat::builder()
.grouping(num_format::Grouping::Standard)
.separator(self.separator())
.build()
.unwrap()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
4 changes: 3 additions & 1 deletion src/info/git.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::repo::author::Author;
use crate::cli::MyRegex;
use crate::cli::{MyRegex, NumberSeparator};
use anyhow::Result;
use git::bstr::BString;
use git_repository as git;
Expand Down Expand Up @@ -35,6 +35,7 @@ impl Commits {
bot_regex_pattern: &Option<MyRegex>,
number_of_authors_to_display: usize,
show_email: bool,
number_separator: Option<&NumberSeparator>,
) -> Result<Self> {
// assure that objects we just traversed are coming from cache
// when we read the commit right after.
Expand Down Expand Up @@ -93,6 +94,7 @@ impl Commits {
author_nbr_of_commits,
total_nbr_of_commits,
show_email,
number_separator,
)
})
.take(number_of_authors_to_display)
Expand Down
53 changes: 46 additions & 7 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ use self::repo::size::SizeInfo;
use self::repo::url::UrlInfo;
use self::repo::version::VersionInfo;
use self::title::Title;
use crate::cli::{is_truecolor_terminal, Config, MyRegex, When};
use crate::cli::{is_truecolor_terminal, Config, MyRegex, NumberSeparator, When};
use crate::ui::get_ascii_colors;
use crate::ui::text_colors::TextColors;
use anyhow::{Context, Result};
use num_format::ToFormattedString;
use onefetch_manifest::Manifest;
use owo_colors::{DynColors, OwoColorize, Style};
use regex::Regex;
Expand Down Expand Up @@ -245,17 +246,23 @@ impl Info {
let description = DescriptionInfo::new(manifest.as_ref());
let pending = PendingInfo::new(&git_repo)?;
let repo_url = UrlInfo::new(&git_repo)?;
let project = ProjectInfo::new(&git_repo, &repo_url.repo_url, manifest.as_ref())?;
let project = ProjectInfo::new(
&git_repo,
&repo_url.repo_url,
manifest.as_ref(),
config.number_separator.as_ref(),
)?;
let head = HeadInfo::new(&git_repo)?;
let version = VersionInfo::new(&git_repo, manifest.as_ref())?;
let size = SizeInfo::new(&git_repo);
let size = SizeInfo::new(&git_repo, config.number_separator.as_ref());
let license = LicenseInfo::new(&repo_path, manifest.as_ref())?;
let mut commits = Commits::new(
git_repo,
config.no_merges,
&no_bots,
config.number_of_authors,
config.email,
config.number_separator.as_ref(),
)?;
let created = CreatedInfo::new(config.iso_time, &commits);
let languages = LanguagesInfo::new(
Expand All @@ -264,12 +271,17 @@ impl Info {
config.number_of_languages,
text_colors.info,
);
let dependencies = DependenciesInfo::new(manifest.as_ref());
let dependencies =
DependenciesInfo::new(manifest.as_ref(), config.number_separator.as_ref());
let authors = AuthorsInfo::new(text_colors.info, &mut commits);
let last_change = LastChangeInfo::new(config.iso_time, &commits);
let contributors = ContributorsInfo::new(&commits, config.number_of_authors);
let commits = CommitsInfo::new(&commits);
let lines_of_code = LocInfo { lines_of_code };
let contributors = ContributorsInfo::new(
&commits,
config.number_of_authors,
config.number_separator.as_ref(),
);
let commits = CommitsInfo::new(&commits, config.number_separator.as_ref());
let lines_of_code = LocInfo::new(lines_of_code, config.number_separator.as_ref());

Ok(Self {
title,
Expand Down Expand Up @@ -343,6 +355,16 @@ impl Info {
}
}

fn format_number<T: ToFormattedString + std::fmt::Display>(
number: T,
number_separator: Option<&NumberSeparator>,
) -> String {
match number_separator {
Some(v) => number.to_formatted_string(&v.get_format()),
None => number.to_string(),
}
}

fn get_style(is_bold: bool, color: DynColors) -> Style {
let mut style = Style::new().color(color);
if is_bold {
Expand Down Expand Up @@ -400,4 +422,21 @@ mod tests {
assert_eq!(style, Style::new().color(DynColors::Ansi(AnsiColors::Cyan)));
Ok(())
}

#[test]
fn test_format_number() {
assert_eq!(
&format_number(1_000_000, Some(&NumberSeparator::Comma)),
"1,000,000"
);
assert_eq!(
&format_number(1_000_000, Some(&NumberSeparator::Space)),
"1\u{202f}000\u{202f}000"
);
assert_eq!(
&format_number(1_000_000, Some(&NumberSeparator::Underscore)),
"1_000_000"
);
assert_eq!(&format_number(1_000_000, None), "1000000");
}
}
Loading