diff --git a/.gitmux.yml b/.gitmux.yml index 8e29d03..51e462a 100644 --- a/.gitmux.yml +++ b/.gitmux.yml @@ -85,3 +85,5 @@ tmux: swap_divergence: false # Add a space between behind & ahead upstream counts. divergence_space: false + # Show flags symbols without counts. + flags_without_count: false diff --git a/README.md b/README.md index 645dc3b..92d89f1 100644 --- a/README.md +++ b/README.md @@ -264,14 +264,15 @@ layout: [branch, "|", flags, "|", stats] This is the list of additional configuration `options`: -| Option | Description | Default | -| :----------------- | :------------------------------------------------------------------------------ | :----------------: | -| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) | -| `branch_trim` | Trim left, right or from the center of the branch (`right`, `left` or `center`) | `right` (trailing) | -| `ellipsis` | Character to show branch name has been truncated | `…` | -| `hide_clean` | Hides the clean flag entirely | `false` | -| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` | -| `divergence_space` | Add a space between behind & ahead upstream counts | `false` | +| Option | Description | Default | +| :------------------- | :------------------------------------------------------------------------------ | :----------------: | +| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) | +| `branch_trim` | Trim left, right or from the center of the branch (`right`, `left` or `center`) | `right` (trailing) | +| `ellipsis` | Character to show branch name has been truncated | `…` | +| `hide_clean` | Hides the clean flag entirely | `false` | +| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` | +| `divergence_space` | Add a space between behind & ahead upstream counts | `false` | +| `flags_without_count`| Show flags symbols without counts | `false` | ## Troubleshooting diff --git a/tmux/formater.go b/tmux/formater.go index a7a87f3..76eea97 100644 --- a/tmux/formater.go +++ b/tmux/formater.go @@ -88,12 +88,13 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error { } type options struct { - BranchMaxLen int `yaml:"branch_max_len"` - BranchTrim direction `yaml:"branch_trim"` - Ellipsis string `yaml:"ellipsis"` - HideClean bool `yaml:"hide_clean"` - DivergenceSpace bool `yaml:"divergence_space"` - SwapDivergence bool `yaml:"swap_divergence"` + BranchMaxLen int `yaml:"branch_max_len"` + BranchTrim direction `yaml:"branch_trim"` + Ellipsis string `yaml:"ellipsis"` + HideClean bool `yaml:"hide_clean"` + DivergenceSpace bool `yaml:"divergence_space"` + SwapDivergence bool `yaml:"swap_divergence"` + FlagsWithoutCount bool `yaml:"flags_without_count"` } // A Formater formats git status to a tmux style string. @@ -294,12 +295,19 @@ func (f *Formater) currentRef() string { return fmt.Sprintf("%s%s%s", f.Styles.Clear, f.Styles.Branch, branch) } +// formatFlag formats a flag with or without count based on the flags_without_count option +func (f *Formater) formatFlag(style, symbol string, count int) string { + if f.Options.FlagsWithoutCount { + return fmt.Sprintf("%s%s", style, symbol) + } + return fmt.Sprintf("%s%s%d", style, symbol, count) +} + func (f *Formater) flags() string { var flags []string if f.st.IsClean { if f.st.NumStashed != 0 { - flags = append(flags, - fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed)) + flags = append(flags, f.formatFlag(f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed)) } if !f.Options.HideClean { @@ -312,28 +320,23 @@ func (f *Formater) flags() string { } if f.st.NumStaged != 0 { - flags = append(flags, - fmt.Sprintf("%s%s%d", f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged)) + flags = append(flags, f.formatFlag(f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged)) } if f.st.NumConflicts != 0 { - flags = append(flags, - fmt.Sprintf("%s%s%d", f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts)) + flags = append(flags, f.formatFlag(f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts)) } if f.st.NumModified != 0 { - flags = append(flags, - fmt.Sprintf("%s%s%d", f.Styles.Modified, f.Symbols.Modified, f.st.NumModified)) + flags = append(flags, f.formatFlag(f.Styles.Modified, f.Symbols.Modified, f.st.NumModified)) } if f.st.NumStashed != 0 { - flags = append(flags, - fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed)) + flags = append(flags, f.formatFlag(f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed)) } if f.st.NumUntracked != 0 { - flags = append(flags, - fmt.Sprintf("%s%s%d", f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked)) + flags = append(flags, f.formatFlag(f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked)) } if len(flags) > 0 { diff --git a/tmux/formater_test.go b/tmux/formater_test.go index 70ffce7..adeb759 100644 --- a/tmux/formater_test.go +++ b/tmux/formater_test.go @@ -104,6 +104,129 @@ func TestFlags(t *testing.T) { } } +func TestFlagsWithoutCount(t *testing.T) { + tests := []struct { + name string + styles styles + symbols symbols + options options + st *gitstatus.Status + want string + }{ + { + name: "flags with counts (default)", + styles: styles{ + Clear: "StyleClear", + Modified: "StyleMod", + Stashed: "StyleStash", + Staged: "StyleStaged", + }, + symbols: symbols{ + Modified: "SymbolMod", + Stashed: "SymbolStash", + Staged: "SymbolStaged", + }, + options: options{ + FlagsWithoutCount: false, + }, + st: &gitstatus.Status{ + NumStashed: 1, + Porcelain: gitstatus.Porcelain{ + NumModified: 2, + NumStaged: 3, + }, + }, + want: "StyleClear" + "StyleStagedSymbolStaged3 StyleModSymbolMod2 StyleStashSymbolStash1", + }, + { + name: "flags without counts", + styles: styles{ + Clear: "StyleClear", + Modified: "StyleMod", + Stashed: "StyleStash", + Staged: "StyleStaged", + }, + symbols: symbols{ + Modified: "SymbolMod", + Stashed: "SymbolStash", + Staged: "SymbolStaged", + }, + options: options{ + FlagsWithoutCount: true, + }, + st: &gitstatus.Status{ + NumStashed: 1, + Porcelain: gitstatus.Porcelain{ + NumModified: 2, + NumStaged: 3, + }, + }, + want: "StyleClear" + "StyleStagedSymbolStaged StyleModSymbolMod StyleStashSymbolStash", + }, + { + name: "all flags without counts", + styles: styles{ + Clear: "StyleClear", + Conflict: "StyleConflict", + Modified: "StyleMod", + Stashed: "StyleStash", + Staged: "StyleStaged", + Untracked: "StyleUntracked", + }, + symbols: symbols{ + Conflict: "SymbolConflict", + Modified: "SymbolMod", + Stashed: "SymbolStash", + Staged: "SymbolStaged", + Untracked: "SymbolUntracked", + }, + options: options{ + FlagsWithoutCount: true, + }, + st: &gitstatus.Status{ + NumStashed: 5, + Porcelain: gitstatus.Porcelain{ + NumConflicts: 1, + NumModified: 10, + NumStaged: 3, + NumUntracked: 7, + }, + }, + want: "StyleClear" + "StyleStagedSymbolStaged StyleConflictSymbolConflict StyleModSymbolMod StyleStashSymbolStash StyleUntrackedSymbolUntracked", + }, + { + name: "clean with stash without count", + styles: styles{ + Clear: "StyleClear", + Clean: "StyleClean", + Stashed: "StyleStash", + }, + symbols: symbols{ + Clean: "SymbolClean", + Stashed: "SymbolStash", + }, + options: options{ + FlagsWithoutCount: true, + }, + st: &gitstatus.Status{ + IsClean: true, + NumStashed: 1, + }, + want: "StyleClearStyleStashSymbolStash StyleCleanSymbolClean", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &Formater{ + Config: Config{Styles: tt.styles, Symbols: tt.symbols, Options: tt.options}, + st: tt.st, + } + + compareStrings(t, tt.want, f.flags()) + }) + } +} + func TestDivergence(t *testing.T) { tests := []struct { name string