From 6b132967419cc497b59617925af73ff0b896076f Mon Sep 17 00:00:00 2001 From: filip Date: Fri, 27 Aug 2021 16:38:46 +0200 Subject: [PATCH] feat(git_metrics): Git metrics show only nonzero diffs (#2887) * implement only_nonzero_diffs configuration option * update documetation --- docs/config/README.md | 13 +++---- src/configs/git_metrics.rs | 4 ++- src/modules/git_metrics.rs | 74 ++++++++++++++++++++++++++++++-------- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index b5ed73ec..ca4f1fb2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1272,12 +1272,13 @@ To enable it, set `disabled` to `false` in your configuration file. ### Options -| Option | Default | Description | -| ------------------------- | -------------------------------------------------------------------- | ---------------------------------------| -| `added_style` | `"bold green"` | The style for the added count. | -| `deleted_style` | `"bold red"` | The style for the deleted count. | -| `format` | `'[+$added]($added_style) [-$deleted]($deleted_style) '` | The format for the module. | -| `disabled` | `true` | Disables the `git_metrics` module. | +| Option | Default | Description | +| ------------------------- | -------------------------------------------------------------------- | --------------------------------------- | +| `added_style` | `"bold green"` | The style for the added count. | +| `deleted_style` | `"bold red"` | The style for the deleted count. | +| `only_nonzero_diffs` | `true` | Render status only for changed items. | +| `format` | `'([+$added]($added_style) )([-$deleted]($deleted_style) )'` | The format for the module. | +| `disabled` | `true` | Disables the `git_metrics` module. | ### Variables diff --git a/src/configs/git_metrics.rs b/src/configs/git_metrics.rs index f3e52e20..ed3e499a 100644 --- a/src/configs/git_metrics.rs +++ b/src/configs/git_metrics.rs @@ -7,6 +7,7 @@ use starship_module_config_derive::ModuleConfig; pub struct GitMetricsConfig<'a> { pub added_style: &'a str, pub deleted_style: &'a str, + pub only_nonzero_diffs: bool, pub format: &'a str, pub disabled: bool, } @@ -16,7 +17,8 @@ impl<'a> Default for GitMetricsConfig<'a> { GitMetricsConfig { added_style: "bold green", deleted_style: "bold red", - format: "[+$added]($added_style) [-$deleted]($deleted_style) ", + only_nonzero_diffs: true, + format: "([+$added]($added_style) )([-$deleted]($deleted_style) )", disabled: true, } } diff --git a/src/modules/git_metrics.rs b/src/modules/git_metrics.rs index 6d41e014..ec331b38 100644 --- a/src/modules/git_metrics.rs +++ b/src/modules/git_metrics.rs @@ -2,8 +2,8 @@ use regex::Regex; use std::ffi::OsStr; use crate::{ - config::RootModuleConfig, configs::git_metrics::GitMetricsConfig, formatter::StringFormatter, - module::Module, + config::RootModuleConfig, configs::git_metrics::GitMetricsConfig, + formatter::string_formatter::StringFormatterError, formatter::StringFormatter, module::Module, }; use super::Context; @@ -46,8 +46,8 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => None, }) .map(|variable| match variable { - "added" => Some(Ok(stats.added)), - "deleted" => Some(Ok(stats.deleted)), + "added" => GitDiff::get_variable(config.only_nonzero_diffs, stats.added), + "deleted" => GitDiff::get_variable(config.only_nonzero_diffs, stats.deleted), _ => None, }) .parse(None) @@ -90,6 +90,19 @@ impl<'a> GitDiff<'a> { deleted: GitDiff::get_matched_str(diff, &deleted_re), } } + + pub fn get_variable( + only_nonzero_diffs: bool, + changed: &str, + ) -> Option> { + match only_nonzero_diffs { + true => match changed { + "0" => None, + _ => Some(Ok(changed)), + }, + false => Some(Ok(changed)), + } + } } #[cfg(test)] @@ -130,11 +143,7 @@ mod tests { let actual = render_metrics(path); - let expected = Some(format!( - "{} {} ", - Color::Green.bold().paint("+1"), - Color::Red.bold().paint("-0") - )); + let expected = Some(format!("{} ", Color::Green.bold().paint("+1"),)); assert_eq!(expected, actual); repo_dir.close() @@ -150,11 +159,7 @@ mod tests { let actual = render_metrics(path); - let expected = Some(format!( - "{} {} ", - Color::Green.bold().paint("+0"), - Color::Red.bold().paint("-1") - )); + let expected = Some(format!("{} ", Color::Red.bold().paint("-1"))); assert_eq!(expected, actual); repo_dir.close() @@ -180,6 +185,47 @@ mod tests { repo_dir.close() } + #[test] + fn shows_nothing_if_no_changes() -> io::Result<()> { + let repo_dir = create_repo_with_commit()?; + let path = repo_dir.path(); + + let actual = render_metrics(path); + + let expected = None; + assert_eq!(expected, actual); + repo_dir.close() + } + + #[test] + fn shows_all_if_only_nonzero_diffs_is_false() -> io::Result<()> { + let repo_dir = create_repo_with_commit()?; + let path = repo_dir.path(); + + let the_file = path.join("the_file"); + let mut the_file = OpenOptions::new().append(true).open(&the_file)?; + writeln!(the_file, "Added line")?; + the_file.sync_all()?; + + let actual = ModuleRenderer::new("git_metrics") + .config(toml::toml! { + [git_metrics] + disabled = false + only_nonzero_diffs = false + }) + .path(path) + .collect(); + + let expected = Some(format!( + "{} {} ", + Color::Green.bold().paint("+1"), + Color::Red.bold().paint("-0") + )); + + assert_eq!(expected, actual); + repo_dir.close() + } + fn render_metrics(path: &Path) -> Option { ModuleRenderer::new("git_metrics") .config(toml::toml! {