Skip to content

Commit

Permalink
Adding swap_divergence flag (#98)
Browse files Browse the repository at this point in the history
* Add swap_divergence flag

* Update readme, improve style

* Add tests for swap_divergence option

* Add swap_divergence option to default gitmux.yml
  • Loading branch information
hqkhan committed Apr 10, 2023
1 parent 3e552aa commit 5479145
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitmux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ tmux:
ellipsis:
# Hides the clean flag
hide_clean: false
# Swaps order of behind & ahead upstream counts - "↓·1↑·1" -> "↑·1↓·1"
swap_divergence: false
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ tmux:
branch_trim: right
ellipsis:
hide_clean: false
swap_divergence: false
```

First, save the default configuration to a new file:
Expand Down Expand Up @@ -269,6 +270,7 @@ This is the list of additional configuration `options`:
| `branch_trim` | Trim left or right end of the branch (`right` or `left`) | `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` |


## Troubleshooting
Expand Down
23 changes: 17 additions & 6 deletions tmux/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ 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"`
BranchMaxLen int `yaml:"branch_max_len"`
BranchTrim direction `yaml:"branch_trim"`
Ellipsis string `yaml:"ellipsis"`
HideClean bool `yaml:"hide_clean"`
SwapDivergence bool `yaml:"swap_divergence"`
}

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

behind := ""
ahead := ""
s := f.Styles.Clear + f.Styles.Divergence
if f.st.BehindCount != 0 {
s += fmt.Sprintf("%s%d", f.Symbols.Behind, f.st.BehindCount)
behind = fmt.Sprintf("%s%d", f.Symbols.Behind, f.st.BehindCount)
}

if f.st.AheadCount != 0 {
s += fmt.Sprintf("%s%d", f.Symbols.Ahead, f.st.AheadCount)
ahead = fmt.Sprintf("%s%d", f.Symbols.Ahead, f.st.AheadCount)
}

if !f.Options.SwapDivergence {
// Behind first, ahead second
s += behind + ahead
} else {
// Ahead first, behind second
s += ahead + behind
}

return s
Expand Down
63 changes: 62 additions & 1 deletion tmux/formater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestDivergence(t *testing.T) {
name string
styles styles
symbols symbols
options options
st *gitstatus.Status
want string
}{
Expand Down Expand Up @@ -180,11 +181,71 @@ func TestDivergence(t *testing.T) {
},
want: "StyleClear" + "↑·128↓·41",
},
{
name: "swap divergence ahead only",
styles: styles{
Clear: "StyleClear",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 4,
BehindCount: 0,
},
},
want: "StyleClear" + "↓·4",
},
{
name: "swap divergence behind only",
styles: styles{
Clear: "StyleClear",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 0,
BehindCount: 12,
},
},
want: "StyleClear" + "↑·12",
},
{
name: "swap divergence both ways",
styles: styles{
Clear: "StyleClear",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 41,
BehindCount: 128,
},
},
want: "StyleClear" + "↓·41↑·128",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &Formater{
Config: Config{Styles: tt.styles, Symbols: tt.symbols},
Config: Config{Styles: tt.styles, Symbols: tt.symbols, Options: tt.options},
st: tt.st,
}

Expand Down

0 comments on commit 5479145

Please sign in to comment.