diff --git a/docs/config/README.md b/docs/config/README.md index f32c0b23..3fca0de5 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1314,6 +1314,7 @@ current directory. | `ahead` | `"⇡"` | The format of `ahead` | | `behind` | `"⇣"` | The format of `behind` | | `diverged` | `"⇕"` | The format of `diverged` | +| `up_to_date` | `""` | The format of `up_to_date` | | `untracked` | `"?"` | The format of `untracked` | | `stashed` | `"$"` | The format of `stashed` | | `modified` | `"!"` | The format of `modified` | @@ -1327,18 +1328,18 @@ current directory. The following variables can be used in `format`: -| Variable | Description | -| -------------- | --------------------------------------------------------------------------------------------- | -| `all_status` | Shortcut for`$conflicted$stashed$deleted$renamed$modified$staged$untracked` | -| `ahead_behind` | Displays `diverged` `ahead` or `behind` format string based on the current status of the repo | -| `conflicted` | Displays `conflicted` when this branch has merge conflicts. | -| `untracked` | Displays `untracked` when there are untracked files in the working directory. | -| `stashed` | Displays `stashed` when a stash exists for the local repository. | -| `modified` | Displays `modified` when there are file modifications in the working directory. | -| `staged` | Displays `staged` when a new file has been added to the staging area. | -| `renamed` | Displays `renamed` when a renamed file has been added to the staging area. | -| `deleted` | Displays `deleted` when a file's deletion has been added to the staging area. | -| style\* | Mirrors the value of option `style` | +| Variable | Description | +| -------------- | ----------------------------------------------------------------------------------------------------------- | +| `all_status` | Shortcut for`$conflicted$stashed$deleted$renamed$modified$staged$untracked` | +| `ahead_behind` | Displays `diverged`, `ahead`, `behind` or `up_to_date` format string based on the current status of the repo. | +| `conflicted` | Displays `conflicted` when this branch has merge conflicts. | +| `untracked` | Displays `untracked` when there are untracked files in the working directory. | +| `stashed` | Displays `stashed` when a stash exists for the local repository. | +| `modified` | Displays `modified` when there are file modifications in the working directory. | +| `staged` | Displays `staged` when a new file has been added to the staging area. | +| `renamed` | Displays `renamed` when a renamed file has been added to the staging area. | +| `deleted` | Displays `deleted` when a file's deletion has been added to the staging area. | +| style\* | Mirrors the value of option `style` | \*: This variable can only be used as a part of a style string @@ -1365,6 +1366,7 @@ conflicted = "🏳" ahead = "🏎💨" behind = "😰" diverged = "😵" +up_to_date = "✓" untracked = "🤷‍" stashed = "📦" modified = "📝" diff --git a/src/configs/git_status.rs b/src/configs/git_status.rs index 9def0ca5..213ad214 100644 --- a/src/configs/git_status.rs +++ b/src/configs/git_status.rs @@ -10,6 +10,7 @@ pub struct GitStatusConfig<'a> { pub stashed: &'a str, pub ahead: &'a str, pub behind: &'a str, + pub up_to_date: &'a str, pub diverged: &'a str, pub conflicted: &'a str, pub deleted: &'a str, @@ -28,6 +29,7 @@ impl<'a> Default for GitStatusConfig<'a> { stashed: "\\$", ahead: "⇡", behind: "⇣", + up_to_date: "", diverged: "⇕", conflicted: "=", deleted: "✘", diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index 41f8db8e..3f5ca02c 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -18,6 +18,7 @@ const ALL_STATUS_FORMAT: &str = "$conflicted$stashed$deleted$renamed$modified$st /// - `⇡` – This branch is ahead of the branch being tracked /// - `⇣` – This branch is behind of the branch being tracked /// - `⇕` – This branch has diverged from the branch being tracked +/// - `` – This branch is up-to-date with the branch being tracked /// - `?` — There are untracked files in the working directory /// - `$` — A stash exists for the local repository /// - `!` — There are file modifications in the working directory @@ -47,6 +48,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { format_count(config.stashed, "git_status.stashed", count) }), "ahead_behind" => info.get_ahead_behind().and_then(|(ahead, behind)| { + let (ahead, behind) = (ahead?, behind?); if ahead > 0 && behind > 0 { format_text(config.diverged, "git_status.diverged", |variable| { match variable { @@ -60,7 +62,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { } else if behind > 0 && ahead == 0 { format_count(config.behind, "git_status.behind", behind) } else { - None + format_symbol(config.up_to_date, "git_status.up_to_date") } }), "conflicted" => info.get_conflicted().and_then(|count| { @@ -120,7 +122,7 @@ impl<'a> GitStatusInfo<'a> { } } - pub fn get_ahead_behind(&self) -> Option<(usize, usize)> { + pub fn get_ahead_behind(&self) -> Option<(Option, Option)> { self.get_repo_status().map(|data| (data.ahead, data.behind)) } @@ -217,8 +219,8 @@ fn get_stashed_count(context: &Context) -> Option { #[derive(Default, Debug, Copy, Clone)] struct RepoStatus { - ahead: usize, - behind: usize, + ahead: Option, + behind: Option, conflicted: usize, deleted: usize, renamed: usize, @@ -271,8 +273,8 @@ impl RepoStatus { let re = Regex::new(r"branch\.ab \+([0-9]+) \-([0-9]+)").unwrap(); if let Some(caps) = re.captures(s) { - self.ahead = caps.get(1).unwrap().as_str().parse::().unwrap(); - self.behind = caps.get(2).unwrap().as_str().parse::().unwrap(); + self.ahead = caps.get(1).unwrap().as_str().parse::().ok(); + self.behind = caps.get(2).unwrap().as_str().parse::().ok(); } } } @@ -303,6 +305,10 @@ fn format_count(format_str: &str, config_path: &str, count: usize) -> Option Option> { + format_text(format_str, config_path, |_variable| None) +} + #[cfg(test)] mod tests { use ansi_term::{ANSIStrings, Color}; @@ -450,6 +456,23 @@ mod tests { repo_dir.close() } + #[test] + fn shows_up_to_date_with_upstream() -> io::Result<()> { + let repo_dir = fixture_repo(FixtureProvider::Git)?; + + let actual = ModuleRenderer::new("git_status") + .config(toml::toml! { + [git_status] + up_to_date="✓" + }) + .path(&repo_dir.path()) + .collect(); + let expected = format_output("✓"); + + assert_eq!(expected, actual); + repo_dir.close() + } + #[test] fn shows_conflicted() -> io::Result<()> { let repo_dir = fixture_repo(FixtureProvider::Git)?;