Skip to content

Commit 5538825

Browse files
Copilotarl
andauthored
Add flags_without_count option to show git status icons without counts (#129)
* Initial plan * Implement flags_without_count feature Co-authored-by: arl <[email protected]> * Update README.md to document flags_without_count option Co-authored-by: arl <[email protected]> * Fix README.md table alignment for flags_without_count option Co-authored-by: arl <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: arl <[email protected]>
1 parent 1094f6d commit 5538825

File tree

4 files changed

+155
-26
lines changed

4 files changed

+155
-26
lines changed

.gitmux.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ tmux:
8585
swap_divergence: false
8686
# Add a space between behind & ahead upstream counts.
8787
divergence_space: false
88+
# Show flags symbols without counts.
89+
flags_without_count: false

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,15 @@ layout: [branch, "|", flags, "|", stats]
264264

265265
This is the list of additional configuration `options`:
266266

267-
| Option | Description | Default |
268-
| :----------------- | :------------------------------------------------------------------------------ | :----------------: |
269-
| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) |
270-
| `branch_trim` | Trim left, right or from the center of the branch (`right`, `left` or `center`) | `right` (trailing) |
271-
| `ellipsis` | Character to show branch name has been truncated | `…` |
272-
| `hide_clean` | Hides the clean flag entirely | `false` |
273-
| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` |
274-
| `divergence_space` | Add a space between behind & ahead upstream counts | `false` |
267+
| Option | Description | Default |
268+
| :------------------- | :------------------------------------------------------------------------------ | :----------------: |
269+
| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) |
270+
| `branch_trim` | Trim left, right or from the center of the branch (`right`, `left` or `center`) | `right` (trailing) |
271+
| `ellipsis` | Character to show branch name has been truncated | `…` |
272+
| `hide_clean` | Hides the clean flag entirely | `false` |
273+
| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` |
274+
| `divergence_space` | Add a space between behind & ahead upstream counts | `false` |
275+
| `flags_without_count`| Show flags symbols without counts | `false` |
275276

276277
## Troubleshooting
277278

tmux/formater.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error {
8888
}
8989

9090
type options struct {
91-
BranchMaxLen int `yaml:"branch_max_len"`
92-
BranchTrim direction `yaml:"branch_trim"`
93-
Ellipsis string `yaml:"ellipsis"`
94-
HideClean bool `yaml:"hide_clean"`
95-
DivergenceSpace bool `yaml:"divergence_space"`
96-
SwapDivergence bool `yaml:"swap_divergence"`
91+
BranchMaxLen int `yaml:"branch_max_len"`
92+
BranchTrim direction `yaml:"branch_trim"`
93+
Ellipsis string `yaml:"ellipsis"`
94+
HideClean bool `yaml:"hide_clean"`
95+
DivergenceSpace bool `yaml:"divergence_space"`
96+
SwapDivergence bool `yaml:"swap_divergence"`
97+
FlagsWithoutCount bool `yaml:"flags_without_count"`
9798
}
9899

99100
// A Formater formats git status to a tmux style string.
@@ -294,12 +295,19 @@ func (f *Formater) currentRef() string {
294295
return fmt.Sprintf("%s%s%s", f.Styles.Clear, f.Styles.Branch, branch)
295296
}
296297

298+
// formatFlag formats a flag with or without count based on the flags_without_count option
299+
func (f *Formater) formatFlag(style, symbol string, count int) string {
300+
if f.Options.FlagsWithoutCount {
301+
return fmt.Sprintf("%s%s", style, symbol)
302+
}
303+
return fmt.Sprintf("%s%s%d", style, symbol, count)
304+
}
305+
297306
func (f *Formater) flags() string {
298307
var flags []string
299308
if f.st.IsClean {
300309
if f.st.NumStashed != 0 {
301-
flags = append(flags,
302-
fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
310+
flags = append(flags, f.formatFlag(f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
303311
}
304312

305313
if !f.Options.HideClean {
@@ -312,28 +320,23 @@ func (f *Formater) flags() string {
312320
}
313321

314322
if f.st.NumStaged != 0 {
315-
flags = append(flags,
316-
fmt.Sprintf("%s%s%d", f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged))
323+
flags = append(flags, f.formatFlag(f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged))
317324
}
318325

319326
if f.st.NumConflicts != 0 {
320-
flags = append(flags,
321-
fmt.Sprintf("%s%s%d", f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts))
327+
flags = append(flags, f.formatFlag(f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts))
322328
}
323329

324330
if f.st.NumModified != 0 {
325-
flags = append(flags,
326-
fmt.Sprintf("%s%s%d", f.Styles.Modified, f.Symbols.Modified, f.st.NumModified))
331+
flags = append(flags, f.formatFlag(f.Styles.Modified, f.Symbols.Modified, f.st.NumModified))
327332
}
328333

329334
if f.st.NumStashed != 0 {
330-
flags = append(flags,
331-
fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
335+
flags = append(flags, f.formatFlag(f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
332336
}
333337

334338
if f.st.NumUntracked != 0 {
335-
flags = append(flags,
336-
fmt.Sprintf("%s%s%d", f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked))
339+
flags = append(flags, f.formatFlag(f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked))
337340
}
338341

339342
if len(flags) > 0 {

tmux/formater_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,129 @@ func TestFlags(t *testing.T) {
104104
}
105105
}
106106

107+
func TestFlagsWithoutCount(t *testing.T) {
108+
tests := []struct {
109+
name string
110+
styles styles
111+
symbols symbols
112+
options options
113+
st *gitstatus.Status
114+
want string
115+
}{
116+
{
117+
name: "flags with counts (default)",
118+
styles: styles{
119+
Clear: "StyleClear",
120+
Modified: "StyleMod",
121+
Stashed: "StyleStash",
122+
Staged: "StyleStaged",
123+
},
124+
symbols: symbols{
125+
Modified: "SymbolMod",
126+
Stashed: "SymbolStash",
127+
Staged: "SymbolStaged",
128+
},
129+
options: options{
130+
FlagsWithoutCount: false,
131+
},
132+
st: &gitstatus.Status{
133+
NumStashed: 1,
134+
Porcelain: gitstatus.Porcelain{
135+
NumModified: 2,
136+
NumStaged: 3,
137+
},
138+
},
139+
want: "StyleClear" + "StyleStagedSymbolStaged3 StyleModSymbolMod2 StyleStashSymbolStash1",
140+
},
141+
{
142+
name: "flags without counts",
143+
styles: styles{
144+
Clear: "StyleClear",
145+
Modified: "StyleMod",
146+
Stashed: "StyleStash",
147+
Staged: "StyleStaged",
148+
},
149+
symbols: symbols{
150+
Modified: "SymbolMod",
151+
Stashed: "SymbolStash",
152+
Staged: "SymbolStaged",
153+
},
154+
options: options{
155+
FlagsWithoutCount: true,
156+
},
157+
st: &gitstatus.Status{
158+
NumStashed: 1,
159+
Porcelain: gitstatus.Porcelain{
160+
NumModified: 2,
161+
NumStaged: 3,
162+
},
163+
},
164+
want: "StyleClear" + "StyleStagedSymbolStaged StyleModSymbolMod StyleStashSymbolStash",
165+
},
166+
{
167+
name: "all flags without counts",
168+
styles: styles{
169+
Clear: "StyleClear",
170+
Conflict: "StyleConflict",
171+
Modified: "StyleMod",
172+
Stashed: "StyleStash",
173+
Staged: "StyleStaged",
174+
Untracked: "StyleUntracked",
175+
},
176+
symbols: symbols{
177+
Conflict: "SymbolConflict",
178+
Modified: "SymbolMod",
179+
Stashed: "SymbolStash",
180+
Staged: "SymbolStaged",
181+
Untracked: "SymbolUntracked",
182+
},
183+
options: options{
184+
FlagsWithoutCount: true,
185+
},
186+
st: &gitstatus.Status{
187+
NumStashed: 5,
188+
Porcelain: gitstatus.Porcelain{
189+
NumConflicts: 1,
190+
NumModified: 10,
191+
NumStaged: 3,
192+
NumUntracked: 7,
193+
},
194+
},
195+
want: "StyleClear" + "StyleStagedSymbolStaged StyleConflictSymbolConflict StyleModSymbolMod StyleStashSymbolStash StyleUntrackedSymbolUntracked",
196+
},
197+
{
198+
name: "clean with stash without count",
199+
styles: styles{
200+
Clear: "StyleClear",
201+
Clean: "StyleClean",
202+
Stashed: "StyleStash",
203+
},
204+
symbols: symbols{
205+
Clean: "SymbolClean",
206+
Stashed: "SymbolStash",
207+
},
208+
options: options{
209+
FlagsWithoutCount: true,
210+
},
211+
st: &gitstatus.Status{
212+
IsClean: true,
213+
NumStashed: 1,
214+
},
215+
want: "StyleClearStyleStashSymbolStash StyleCleanSymbolClean",
216+
},
217+
}
218+
for _, tt := range tests {
219+
t.Run(tt.name, func(t *testing.T) {
220+
f := &Formater{
221+
Config: Config{Styles: tt.styles, Symbols: tt.symbols, Options: tt.options},
222+
st: tt.st,
223+
}
224+
225+
compareStrings(t, tt.want, f.flags())
226+
})
227+
}
228+
}
229+
107230
func TestDivergence(t *testing.T) {
108231
tests := []struct {
109232
name string

0 commit comments

Comments
 (0)