From d4f22fb0c567d7eaf1eb5ed15d1909a44effa539 Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 31 Oct 2022 22:05:47 +0100 Subject: [PATCH] Pluralize words correctly. --- hooked-cli/source/cli/run.rs | 8 ++++++-- hooked-cli/source/main.rs | 1 + hooked-cli/source/utilities.rs | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 hooked-cli/source/utilities.rs diff --git a/hooked-cli/source/cli/run.rs b/hooked-cli/source/cli/run.rs index 6c338ae..4350c24 100644 --- a/hooked-cli/source/cli/run.rs +++ b/hooked-cli/source/cli/run.rs @@ -10,6 +10,8 @@ use { supports_color::Stream, }; +use crate::utilities::plural; + /// The `run` subcommand. pub fn hooked_run(config: Config, hook_type: String) -> Result<()> { let (success_style, warn_style, error_style) = @@ -25,9 +27,11 @@ pub fn hooked_run(config: Config, hook_type: String) -> Result<()> { }; if hook_type == "pre-commit" { + let hook_count = config.pre_commit.len(); println!( - "Hooked: Running {} pre-commit hooks.", - config.pre_commit.len() + "Hooked: Running {} pre-commit {}.", + hook_count, + plural(hook_count, "hook", None) ); for hook in config.pre_commit { diff --git a/hooked-cli/source/main.rs b/hooked-cli/source/main.rs index af1a25a..a634862 100644 --- a/hooked-cli/source/main.rs +++ b/hooked-cli/source/main.rs @@ -27,6 +27,7 @@ pub const DEFAULT_TEMPLATE: &str = include_str!("templates/default.sh"); pub const HOOK_TYPES: [&str; 1] = ["pre-commit"]; mod cli; +mod utilities; fn main() -> Result<()> { install()?; diff --git a/hooked-cli/source/utilities.rs b/hooked-cli/source/utilities.rs new file mode 100644 index 0000000..42adfb8 --- /dev/null +++ b/hooked-cli/source/utilities.rs @@ -0,0 +1,14 @@ +//! Miscellaneous utilities. + +/// Simple function to create a pluralized string. +pub fn plural(count: usize, singular: &str, plural: Option<&str>) -> String { + if count == 0 { + return singular.to_string(); + } + + if let Some(plural) = plural { + return plural.to_string(); + } + + format!("{singular}s") +}