Skip to content

Commit 5479145

Browse files
authored
Adding swap_divergence flag (#98)
* Add swap_divergence flag * Update readme, improve style * Add tests for swap_divergence option * Add swap_divergence option to default gitmux.yml
1 parent 3e552aa commit 5479145

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

.gitmux.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ tmux:
8181
ellipsis:
8282
# Hides the clean flag
8383
hide_clean: false
84+
# Swaps order of behind & ahead upstream counts - "↓·1↑·1" -> "↑·1↓·1"
85+
swap_divergence: false

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ tmux:
132132
branch_trim: right
133133
ellipsis:
134134
hide_clean: false
135+
swap_divergence: false
135136
```
136137
137138
First, save the default configuration to a new file:
@@ -269,6 +270,7 @@ This is the list of additional configuration `options`:
269270
| `branch_trim` | Trim left or right end of the branch (`right` or `left`) | `right` (trailing) |
270271
| `ellipsis` | Character to show branch name has been truncated | `…` |
271272
| `hide_clean` | Hides the clean flag entirely | `false` |
273+
| `swap_divergence`| Swaps order of behind & ahead upstream counts | `false` |
272274

273275

274276
## Troubleshooting

tmux/formater.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error {
8585
}
8686

8787
type options struct {
88-
BranchMaxLen int `yaml:"branch_max_len"`
89-
BranchTrim direction `yaml:"branch_trim"`
90-
Ellipsis string `yaml:"ellipsis"`
91-
HideClean bool `yaml:"hide_clean"`
88+
BranchMaxLen int `yaml:"branch_max_len"`
89+
BranchTrim direction `yaml:"branch_trim"`
90+
Ellipsis string `yaml:"ellipsis"`
91+
HideClean bool `yaml:"hide_clean"`
92+
SwapDivergence bool `yaml:"swap_divergence"`
9293
}
9394

9495
// A Formater formats git status to a tmux style string.
@@ -240,13 +241,23 @@ func (f *Formater) divergence() string {
240241
return ""
241242
}
242243

244+
behind := ""
245+
ahead := ""
243246
s := f.Styles.Clear + f.Styles.Divergence
244247
if f.st.BehindCount != 0 {
245-
s += fmt.Sprintf("%s%d", f.Symbols.Behind, f.st.BehindCount)
248+
behind = fmt.Sprintf("%s%d", f.Symbols.Behind, f.st.BehindCount)
246249
}
247250

248251
if f.st.AheadCount != 0 {
249-
s += fmt.Sprintf("%s%d", f.Symbols.Ahead, f.st.AheadCount)
252+
ahead = fmt.Sprintf("%s%d", f.Symbols.Ahead, f.st.AheadCount)
253+
}
254+
255+
if !f.Options.SwapDivergence {
256+
// Behind first, ahead second
257+
s += behind + ahead
258+
} else {
259+
// Ahead first, behind second
260+
s += ahead + behind
250261
}
251262

252263
return s

tmux/formater_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func TestDivergence(t *testing.T) {
109109
name string
110110
styles styles
111111
symbols symbols
112+
options options
112113
st *gitstatus.Status
113114
want string
114115
}{
@@ -180,11 +181,71 @@ func TestDivergence(t *testing.T) {
180181
},
181182
want: "StyleClear" + "↑·128↓·41",
182183
},
184+
{
185+
name: "swap divergence ahead only",
186+
styles: styles{
187+
Clear: "StyleClear",
188+
},
189+
symbols: symbols{
190+
Ahead: "↓·",
191+
Behind: "↑·",
192+
},
193+
options: options{
194+
SwapDivergence: true,
195+
},
196+
st: &gitstatus.Status{
197+
Porcelain: gitstatus.Porcelain{
198+
AheadCount: 4,
199+
BehindCount: 0,
200+
},
201+
},
202+
want: "StyleClear" + "↓·4",
203+
},
204+
{
205+
name: "swap divergence behind only",
206+
styles: styles{
207+
Clear: "StyleClear",
208+
},
209+
symbols: symbols{
210+
Ahead: "↓·",
211+
Behind: "↑·",
212+
},
213+
options: options{
214+
SwapDivergence: true,
215+
},
216+
st: &gitstatus.Status{
217+
Porcelain: gitstatus.Porcelain{
218+
AheadCount: 0,
219+
BehindCount: 12,
220+
},
221+
},
222+
want: "StyleClear" + "↑·12",
223+
},
224+
{
225+
name: "swap divergence both ways",
226+
styles: styles{
227+
Clear: "StyleClear",
228+
},
229+
symbols: symbols{
230+
Ahead: "↓·",
231+
Behind: "↑·",
232+
},
233+
options: options{
234+
SwapDivergence: true,
235+
},
236+
st: &gitstatus.Status{
237+
Porcelain: gitstatus.Porcelain{
238+
AheadCount: 41,
239+
BehindCount: 128,
240+
},
241+
},
242+
want: "StyleClear" + "↓·41↑·128",
243+
},
183244
}
184245
for _, tt := range tests {
185246
t.Run(tt.name, func(t *testing.T) {
186247
f := &Formater{
187-
Config: Config{Styles: tt.styles, Symbols: tt.symbols},
248+
Config: Config{Styles: tt.styles, Symbols: tt.symbols, Options: tt.options},
188249
st: tt.st,
189250
}
190251

0 commit comments

Comments
 (0)