diff options
author | filip <filipbachul@gmail.com> | 2021-08-27 16:38:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-27 09:38:46 -0500 |
commit | 6b132967419cc497b59617925af73ff0b896076f (patch) | |
tree | 62954072b98a7a3d46022fdafded0e765391d568 /src | |
parent | ce168e3241a6ed20065808eb955b2882d92dacc1 (diff) |
feat(git_metrics): Git metrics show only nonzero diffs (#2887)
* implement only_nonzero_diffs configuration option
* update documetation
Diffstat (limited to 'src')
-rw-r--r-- | src/configs/git_metrics.rs | 4 | ||||
-rw-r--r-- | src/modules/git_metrics.rs | 74 |
2 files changed, 63 insertions, 15 deletions
diff --git a/src/configs/git_metrics.rs b/src/configs/git_metrics.rs index f3e52e200..ed3e499a2 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 6d41e0145..ec331b380 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<Module<'a>> { _ => 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<Result<&str, StringFormatterError>> { + 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<String> { ModuleRenderer::new("git_metrics") .config(toml::toml! { |