mirror of
https://github.com/starship/starship.git
synced 2024-12-24 16:18:53 +01: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
|
### Options
|
||||||
|
|
||||||
| Option | Default | Description |
|
| Option | Default | Description |
|
||||||
| ------------------------- | -------------------------------------------------------------------- | ---------------------------------------|
|
| ------------------------- | -------------------------------------------------------------------- | --------------------------------------- |
|
||||||
| `added_style` | `"bold green"` | The style for the added count. |
|
| `added_style` | `"bold green"` | The style for the added count. |
|
||||||
| `deleted_style` | `"bold red"` | The style for the deleted count. |
|
| `deleted_style` | `"bold red"` | The style for the deleted count. |
|
||||||
| `format` | `'[+$added]($added_style) [-$deleted]($deleted_style) '` | The format for the module. |
|
| `only_nonzero_diffs` | `true` | Render status only for changed items. |
|
||||||
| `disabled` | `true` | Disables the `git_metrics` module. |
|
| `format` | `'([+$added]($added_style) )([-$deleted]($deleted_style) )'` | The format for the module. |
|
||||||
|
| `disabled` | `true` | Disables the `git_metrics` module. |
|
||||||
|
|
||||||
### Variables
|
### Variables
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use starship_module_config_derive::ModuleConfig;
|
|||||||
pub struct GitMetricsConfig<'a> {
|
pub struct GitMetricsConfig<'a> {
|
||||||
pub added_style: &'a str,
|
pub added_style: &'a str,
|
||||||
pub deleted_style: &'a str,
|
pub deleted_style: &'a str,
|
||||||
|
pub only_nonzero_diffs: bool,
|
||||||
pub format: &'a str,
|
pub format: &'a str,
|
||||||
pub disabled: bool,
|
pub disabled: bool,
|
||||||
}
|
}
|
||||||
@ -16,7 +17,8 @@ impl<'a> Default for GitMetricsConfig<'a> {
|
|||||||
GitMetricsConfig {
|
GitMetricsConfig {
|
||||||
added_style: "bold green",
|
added_style: "bold green",
|
||||||
deleted_style: "bold red",
|
deleted_style: "bold red",
|
||||||
format: "[+$added]($added_style) [-$deleted]($deleted_style) ",
|
only_nonzero_diffs: true,
|
||||||
|
format: "([+$added]($added_style) )([-$deleted]($deleted_style) )",
|
||||||
disabled: true,
|
disabled: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ use regex::Regex;
|
|||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::RootModuleConfig, configs::git_metrics::GitMetricsConfig, formatter::StringFormatter,
|
config::RootModuleConfig, configs::git_metrics::GitMetricsConfig,
|
||||||
module::Module,
|
formatter::string_formatter::StringFormatterError, formatter::StringFormatter, module::Module,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Context;
|
use super::Context;
|
||||||
@ -46,8 +46,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.map(|variable| match variable {
|
.map(|variable| match variable {
|
||||||
"added" => Some(Ok(stats.added)),
|
"added" => GitDiff::get_variable(config.only_nonzero_diffs, stats.added),
|
||||||
"deleted" => Some(Ok(stats.deleted)),
|
"deleted" => GitDiff::get_variable(config.only_nonzero_diffs, stats.deleted),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.parse(None)
|
.parse(None)
|
||||||
@ -90,6 +90,19 @@ impl<'a> GitDiff<'a> {
|
|||||||
deleted: GitDiff::get_matched_str(diff, &deleted_re),
|
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)]
|
#[cfg(test)]
|
||||||
@ -130,11 +143,7 @@ mod tests {
|
|||||||
|
|
||||||
let actual = render_metrics(path);
|
let actual = render_metrics(path);
|
||||||
|
|
||||||
let expected = Some(format!(
|
let expected = Some(format!("{} ", Color::Green.bold().paint("+1"),));
|
||||||
"{} {} ",
|
|
||||||
Color::Green.bold().paint("+1"),
|
|
||||||
Color::Red.bold().paint("-0")
|
|
||||||
));
|
|
||||||
|
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
repo_dir.close()
|
repo_dir.close()
|
||||||
@ -150,11 +159,7 @@ mod tests {
|
|||||||
|
|
||||||
let actual = render_metrics(path);
|
let actual = render_metrics(path);
|
||||||
|
|
||||||
let expected = Some(format!(
|
let expected = Some(format!("{} ", Color::Red.bold().paint("-1")));
|
||||||
"{} {} ",
|
|
||||||
Color::Green.bold().paint("+0"),
|
|
||||||
Color::Red.bold().paint("-1")
|
|
||||||
));
|
|
||||||
|
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
repo_dir.close()
|
repo_dir.close()
|
||||||
@ -180,6 +185,47 @@ mod tests {
|
|||||||
repo_dir.close()
|
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> {
|
fn render_metrics(path: &Path) -> Option<String> {
|
||||||
ModuleRenderer::new("git_metrics")
|
ModuleRenderer::new("git_metrics")
|
||||||
.config(toml::toml! {
|
.config(toml::toml! {
|
||||||
|
Loading…
Reference in New Issue
Block a user