Skip to content

Commit 0323aec

Browse files
arlmarcoSven
andauthored
Feature/space between divergence (#117)
* feat: option to add space between behind & ahead upstream Signed-off-by: marcoSven <me@marcosven.com> * add tests and rename option Signed-off-by: marcoSven <me@marcosven.com> * .gitmux.yml: document new option * Update README: reformat * tmux: gofmt * tmux: fix divergence-space and improve test cases --------- Signed-off-by: marcoSven <me@marcosven.com> Co-authored-by: marcoSven <me@marcosven.com>
1 parent b821624 commit 0323aec

File tree

4 files changed

+116
-34
lines changed

4 files changed

+116
-34
lines changed

.gitmux.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ tmux:
8181
ellipsis:
8282
# Hides the clean flag
8383
hide_clean: false
84-
# Swaps order of behind & ahead upstream counts - "↓·1↑·1" -> "↑·1↓·1"
84+
# Swaps order of behind & ahead upstream counts - "↓·1↑·1" -> "↑·1↓·1".
8585
swap_divergence: false
86+
# Add a space between behind & ahead upstream counts.
87+
divergence_space: false

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ tmux:
133133
ellipsis:
134134
hide_clean: false
135135
swap_divergence: false
136+
divergence_space: false
136137
```
137138
138139
First, save the default configuration to a new file:
@@ -264,13 +265,14 @@ layout: [branch, "|", flags, "|", stats]
264265

265266
This is the list of additional configuration `options`:
266267

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 or right end of the branch (`right` or `left`) | `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` |
268+
| Option | Description | Default |
269+
| :----------------- | :--------------------------------------------------------- | :----------------: |
270+
| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) |
271+
| `branch_trim` | Trim left or right end of the branch (`right` or `left`) | `right` (trailing) |
272+
| `ellipsis` | Character to show branch name has been truncated | `…` |
273+
| `hide_clean` | Hides the clean flag entirely | `false` |
274+
| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` |
275+
| `divergence_space` | Add a space between behind & ahead upstream counts | `false` |
274276

275277

276278
## Troubleshooting

tmux/formater.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ 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"`
92-
SwapDivergence bool `yaml:"swap_divergence"`
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+
DivergenceSpace bool `yaml:"divergence_space"`
93+
SwapDivergence bool `yaml:"swap_divergence"`
9394
}
9495

9596
// A Formater formats git status to a tmux style string.
@@ -254,15 +255,20 @@ func (f *Formater) divergence() string {
254255
ahead = fmt.Sprintf("%s%d", f.Symbols.Ahead, f.st.AheadCount)
255256
}
256257

258+
// Handle 'swap divergence'
259+
var left, right string
257260
if !f.Options.SwapDivergence {
258-
// Behind first, ahead second
259-
s += behind + ahead
261+
left, right = behind, ahead
260262
} else {
261-
// Ahead first, behind second
262-
s += ahead + behind
263+
left, right = ahead, behind
263264
}
264265

265-
return s
266+
// Handle 'divergence space'
267+
space := ""
268+
if f.Options.DivergenceSpace && right != "" && left != "" {
269+
space = " "
270+
}
271+
return s + left + space + right
266272
}
267273

268274
func (f *Formater) currentRef() string {

tmux/formater_test.go

+88-16
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func TestDivergence(t *testing.T) {
116116
{
117117
name: "no divergence",
118118
styles: styles{
119-
Clear: "StyleClear",
119+
Clear: "StyleClear",
120+
Divergence: "StyleDivergence",
120121
},
121122
symbols: symbols{
122123
Ahead: "↓·",
@@ -133,7 +134,8 @@ func TestDivergence(t *testing.T) {
133134
{
134135
name: "ahead only",
135136
styles: styles{
136-
Clear: "StyleClear",
137+
Clear: "StyleClear",
138+
Divergence: "StyleDivergence",
137139
},
138140
symbols: symbols{
139141
Ahead: "↓·",
@@ -145,12 +147,13 @@ func TestDivergence(t *testing.T) {
145147
BehindCount: 0,
146148
},
147149
},
148-
want: "StyleClear" + "↓·4",
150+
want: "StyleClearStyleDivergence" + "↓·4",
149151
},
150152
{
151153
name: "behind only",
152154
styles: styles{
153-
Clear: "StyleClear",
155+
Clear: "StyleClear",
156+
Divergence: "StyleDivergence",
154157
},
155158
symbols: symbols{
156159
Ahead: "↓·",
@@ -162,12 +165,13 @@ func TestDivergence(t *testing.T) {
162165
BehindCount: 12,
163166
},
164167
},
165-
want: "StyleClear" + "↑·12",
168+
want: "StyleClearStyleDivergence" + "↑·12",
166169
},
167170
{
168171
name: "diverged both ways",
169172
styles: styles{
170-
Clear: "StyleClear",
173+
Clear: "StyleClear",
174+
Divergence: "StyleDivergence",
171175
},
172176
symbols: symbols{
173177
Ahead: "↓·",
@@ -179,67 +183,135 @@ func TestDivergence(t *testing.T) {
179183
BehindCount: 128,
180184
},
181185
},
182-
want: "StyleClear" + "↑·128↓·41",
186+
want: "StyleClearStyleDivergence" + "↑·128↓·41",
187+
},
188+
{
189+
name: "divergence-space:true and ahead:0",
190+
styles: styles{
191+
Clear: "StyleClear",
192+
Divergence: "StyleDivergence",
193+
},
194+
symbols: symbols{
195+
Ahead: "↓·",
196+
Behind: "↑·",
197+
},
198+
options: options{
199+
DivergenceSpace: true,
200+
},
201+
st: &gitstatus.Status{
202+
Porcelain: gitstatus.Porcelain{
203+
AheadCount: 0,
204+
BehindCount: 12,
205+
},
206+
},
207+
want: "StyleClearStyleDivergence" + "↑·12",
208+
},
209+
{
210+
name: "divergence-space:false and diverged both ways",
211+
styles: styles{
212+
Clear: "StyleClear",
213+
Divergence: "StyleDivergence",
214+
},
215+
symbols: symbols{
216+
Ahead: "↓·",
217+
Behind: "↑·",
218+
},
219+
options: options{
220+
DivergenceSpace: true,
221+
SwapDivergence: false,
222+
},
223+
st: &gitstatus.Status{
224+
Porcelain: gitstatus.Porcelain{
225+
AheadCount: 41,
226+
BehindCount: 128,
227+
},
228+
},
229+
want: "StyleClearStyleDivergence" + "↑·128 ↓·41",
230+
},
231+
{
232+
name: "divergence-space:true and diverged both ways",
233+
styles: styles{
234+
Clear: "StyleClear",
235+
Divergence: "StyleDivergence",
236+
},
237+
symbols: symbols{
238+
Ahead: "↓·",
239+
Behind: "↑·",
240+
},
241+
options: options{
242+
DivergenceSpace: true,
243+
SwapDivergence: true,
244+
},
245+
st: &gitstatus.Status{
246+
Porcelain: gitstatus.Porcelain{
247+
AheadCount: 41,
248+
BehindCount: 128,
249+
},
250+
},
251+
want: "StyleClearStyleDivergence" + "↓·41 ↑·128",
183252
},
184253
{
185254
name: "swap divergence ahead only",
186255
styles: styles{
187-
Clear: "StyleClear",
256+
Clear: "StyleClear",
257+
Divergence: "StyleDivergence",
188258
},
189259
symbols: symbols{
190260
Ahead: "↓·",
191261
Behind: "↑·",
192262
},
193263
options: options{
194-
SwapDivergence: true,
264+
SwapDivergence: true,
195265
},
196266
st: &gitstatus.Status{
197267
Porcelain: gitstatus.Porcelain{
198268
AheadCount: 4,
199269
BehindCount: 0,
200270
},
201271
},
202-
want: "StyleClear" + "↓·4",
272+
want: "StyleClearStyleDivergence" + "↓·4",
203273
},
204274
{
205275
name: "swap divergence behind only",
206276
styles: styles{
207-
Clear: "StyleClear",
277+
Clear: "StyleClear",
278+
Divergence: "StyleDivergence",
208279
},
209280
symbols: symbols{
210281
Ahead: "↓·",
211282
Behind: "↑·",
212283
},
213284
options: options{
214-
SwapDivergence: true,
285+
SwapDivergence: true,
215286
},
216287
st: &gitstatus.Status{
217288
Porcelain: gitstatus.Porcelain{
218289
AheadCount: 0,
219290
BehindCount: 12,
220291
},
221292
},
222-
want: "StyleClear" + "↑·12",
293+
want: "StyleClearStyleDivergence" + "↑·12",
223294
},
224295
{
225296
name: "swap divergence both ways",
226297
styles: styles{
227-
Clear: "StyleClear",
298+
Clear: "StyleClear",
299+
Divergence: "StyleDivergence",
228300
},
229301
symbols: symbols{
230302
Ahead: "↓·",
231303
Behind: "↑·",
232304
},
233305
options: options{
234-
SwapDivergence: true,
306+
SwapDivergence: true,
235307
},
236308
st: &gitstatus.Status{
237309
Porcelain: gitstatus.Porcelain{
238310
AheadCount: 41,
239311
BehindCount: 128,
240312
},
241313
},
242-
want: "StyleClear" + "↓·41↑·128",
314+
want: "StyleClearStyleDivergence" + "↓·41↑·128",
243315
},
244316
}
245317
for _, tt := range tests {

0 commit comments

Comments
 (0)