Skip to content

Commit

Permalink
tmux: fix divergence-space and improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
arl committed Jun 9, 2024
1 parent 4f7a4f2 commit 9ea0685
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 62 deletions.
22 changes: 11 additions & 11 deletions tmux/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,29 @@ func (f *Formater) divergence() string {

behind := ""
ahead := ""
space := ""

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

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

// Handle 'swap divergence'
var left, right string
if !f.Options.SwapDivergence {
// Behind first, ahead second
s += behind + ahead
left, right = behind, ahead
} else {
// Ahead first, behind second
s += ahead + behind
left, right = ahead, behind
}

return s
// Handle 'divergence space'
space := ""
if f.Options.DivergenceSpace && right != "" && left != "" {
space = " "
}
return s + left + space + right
}

func (f *Formater) currentRef() string {
Expand Down
92 changes: 41 additions & 51 deletions tmux/formater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func TestDivergence(t *testing.T) {
{
name: "no divergence",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Expand All @@ -133,7 +134,8 @@ func TestDivergence(t *testing.T) {
{
name: "ahead only",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Expand All @@ -145,12 +147,13 @@ func TestDivergence(t *testing.T) {
BehindCount: 0,
},
},
want: "StyleClear" + "↓·4",
want: "StyleClearStyleDivergence" + "↓·4",
},
{
name: "behind only",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Expand All @@ -162,12 +165,13 @@ func TestDivergence(t *testing.T) {
BehindCount: 12,
},
},
want: "StyleClear" + "↑·12",
want: "StyleClearStyleDivergence" + "↑·12",
},
{
name: "diverged both ways",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Expand All @@ -179,12 +183,13 @@ func TestDivergence(t *testing.T) {
BehindCount: 128,
},
},
want: "StyleClear" + "↑·128↓·41",
want: "StyleClearStyleDivergence" + "↑·128↓·41",
},
{
name: "space between behind only",
name: "divergence-space:true and ahead:0",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Expand All @@ -199,129 +204,114 @@ func TestDivergence(t *testing.T) {
BehindCount: 12,
},
},
want: "StyleClear" + "↑·12",
want: "StyleClearStyleDivergence" + "↑·12",
},
{
name: "space between diverged both way",
name: "divergence-space:false and diverged both ways",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
DivergenceSpace: true,
SwapDivergence: false
SwapDivergence: false,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 41,
BehindCount: 128,
AheadCount: 41,
BehindCount: 128,
},
},
want: "StyleClear" + "↑·128 ↓·41",
want: "StyleClearStyleDivergence" + "↑·128 ↓·41",
},
{
name: "space between swap divergence both ways",
name: "divergence-space:true and diverged both ways",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
DivergenceSpace: true,
SwapDivergence: true,
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 41,
BehindCount: 128,
},
},
want: "StyleClear" + "↓·41 ↑·128",
},
{
name: "no space between diverged both way",
styles: styles{
Clear: "StyleClear",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
DivergenceSpace: false,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 41,
BehindCount: 128,
AheadCount: 41,
BehindCount: 128,
},
},
want: "StyleClear" + "↑·128↓·41",
want: "StyleClearStyleDivergence" + "↓·41 ↑·128",
},
{
name: "swap divergence ahead only",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
SwapDivergence: true,
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 4,
BehindCount: 0,
},
},
want: "StyleClear" + "↓·4",
want: "StyleClearStyleDivergence" + "↓·4",
},
{
name: "swap divergence behind only",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
SwapDivergence: true,
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 0,
BehindCount: 12,
},
},
want: "StyleClear" + "↑·12",
want: "StyleClearStyleDivergence" + "↑·12",
},
{
name: "swap divergence both ways",
styles: styles{
Clear: "StyleClear",
Clear: "StyleClear",
Divergence: "StyleDivergence",
},
symbols: symbols{
Ahead: "↓·",
Behind: "↑·",
},
options: options{
SwapDivergence: true,
SwapDivergence: true,
},
st: &gitstatus.Status{
Porcelain: gitstatus.Porcelain{
AheadCount: 41,
BehindCount: 128,
},
},
want: "StyleClear" + "↓·41↑·128",
want: "StyleClearStyleDivergence" + "↓·41↑·128",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 9ea0685

Please sign in to comment.