diff options
author | Bill <bill.risher99@gmail.com> | 2021-08-07 12:22:00 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-07 19:22:00 +0200 |
commit | b2e2330cb05b64a87107c615d8c57fcf0b14ecf3 (patch) | |
tree | 56ad4cf4a407cf0f37edae39232132d7a148e957 | |
parent | b7b7df98854b2d4ff9c5f2fdddc796449cdab908 (diff) |
feat(git_status): added symbol for local repos up-to-date with remote. (#2945)
* git_status: added symbol for when local branch is up-to-date with upstream
* updated docs
* removed unused variable, moved location of config comment
* changed uptodate default to empty string, simplified and made safer
* added uptodate default line back into docstring
* fixed linting and formatting errors
* refactored uptodate to up_to_date, removed redundant else statement
-rw-r--r-- | docs/config/README.md | 26 | ||||
-rw-r--r-- | src/configs/git_status.rs | 2 | ||||
-rw-r--r-- | src/modules/git_status.rs | 35 |
3 files changed, 45 insertions, 18 deletions
diff --git a/docs/config/README.md b/docs/config/README.md index f32c0b231..3fca0de57 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 9def0ca58..213ad2149 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 41f8db8e7..3f5ca02c8 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<Module<'a>> { 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<Module<'a>> { } 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<usize>, Option<usize>)> { self.get_repo_status().map(|data| (data.ahead, data.behind)) } @@ -217,8 +219,8 @@ fn get_stashed_count(context: &Context) -> Option<usize> { #[derive(Default, Debug, Copy, Clone)] struct RepoStatus { - ahead: usize, - behind: usize, + ahead: Option<usize>, + behind: Option<usize>, 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::<usize>().unwrap(); - self.behind = caps.get(2).unwrap().as_str().parse::<usize>().unwrap(); + self.ahead = caps.get(1).unwrap().as_str().parse::<usize>().ok(); + self.behind = caps.get(2).unwrap().as_str().parse::<usize>().ok(); } } } @@ -303,6 +305,10 @@ fn format_count(format_str: &str, config_path: &str, count: usize) -> Option<Vec }) } +fn format_symbol(format_str: &str, config_path: &str) -> Option<Vec<Segment>> { + format_text(format_str, config_path, |_variable| None) +} + #[cfg(test)] mod tests { use ansi_term::{ANSIStrings, Color}; @@ -451,6 +457,23 @@ mod tests { } #[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)?; |