mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-11 07:40:57 +00:00
feat(git_metrics): Git metrics show only nonzero diffs (#2887)
* implement only_nonzero_diffs configuration option * update documetation
This commit is contained in:
parent
ce168e3241
commit
6b13296741
@ -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
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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! {
|
||||
|
Loading…
Reference in New Issue
Block a user