Skip to content

Adding swap_divergence flag #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 behind & ahead upstream counts' order | `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