Skip to content

Commit a02f1e8

Browse files
authored
Draw divergence from base branch right-aligned in branches view (#4785)
- **PR Description** I didn't enable the `showDivergenceFromBaseBranch` config by default yet (even though I find it very useful) because I'm worried that people might find the display too noisy. Drawing the divergence information right-aligned helps reduce that noise a little bit.
2 parents d7626d4 + abb7bed commit a02f1e8

File tree

6 files changed

+53
-44
lines changed

6 files changed

+53
-44
lines changed

pkg/gui/presentation/branches.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ func getBranchDisplayStrings(
5757
checkedOutByWorkTree := git_commands.CheckedOutByOtherWorktree(b, worktrees)
5858
showCommitHash := fullDescription || userConfig.Gui.ShowBranchCommitHash
5959
branchStatus := BranchStatus(b, itemOperation, tr, now, userConfig)
60+
divergence := divergenceStr(b, itemOperation, tr, userConfig)
6061
worktreeIcon := lo.Ternary(icons.IsIconEnabled(), icons.LINKED_WORKTREE_ICON, fmt.Sprintf("(%s)", tr.LcWorktree))
6162

6263
// Recency is always three characters, plus one for the space
6364
availableWidth := viewWidth - 4
64-
if len(branchStatus) > 0 {
65-
availableWidth -= utils.StringWidth(utils.Decolorise(branchStatus)) + 1
65+
if len(divergence) > 0 {
66+
availableWidth -= utils.StringWidth(divergence) + 1
6667
}
6768
if icons.IsIconEnabled() {
6869
availableWidth -= 2 // one for the icon, one for the space
@@ -73,6 +74,11 @@ func getBranchDisplayStrings(
7374
if checkedOutByWorkTree {
7475
availableWidth -= utils.StringWidth(worktreeIcon) + 1
7576
}
77+
paddingNeededForDivergence := availableWidth
78+
79+
if len(branchStatus) > 0 {
80+
availableWidth -= utils.StringWidth(utils.Decolorise(branchStatus)) + 1
81+
}
7682

7783
displayName := b.Name
7884
if b.DisplayName != "" {
@@ -114,6 +120,13 @@ func getBranchDisplayStrings(
114120
res = append(res, utils.ShortHash(b.CommitHash))
115121
}
116122

123+
if divergence != "" {
124+
paddingNeededForDivergence -= utils.StringWidth(utils.Decolorise(coloredName)) - 1
125+
if paddingNeededForDivergence > 0 {
126+
coloredName += strings.Repeat(" ", paddingNeededForDivergence)
127+
coloredName += style.FgCyan.Sprint(divergence)
128+
}
129+
}
117130
res = append(res, coloredName)
118131

119132
if fullDescription {
@@ -185,16 +198,23 @@ func BranchStatus(
185198
}
186199
}
187200

188-
if userConfig.Gui.ShowDivergenceFromBaseBranch != "none" {
201+
return result
202+
}
203+
204+
func divergenceStr(
205+
branch *models.Branch,
206+
itemOperation types.ItemOperation,
207+
tr *i18n.TranslationSet,
208+
userConfig *config.UserConfig,
209+
) string {
210+
result := ""
211+
if ItemOperationToString(itemOperation, tr) == "" && userConfig.Gui.ShowDivergenceFromBaseBranch != "none" {
189212
behind := branch.BehindBaseBranch.Load()
190213
if behind != 0 {
191-
if result != "" {
192-
result += " "
193-
}
194214
if userConfig.Gui.ShowDivergenceFromBaseBranch == "arrowAndNumber" {
195-
result += style.FgCyan.Sprintf("↓%d", behind)
215+
result += fmt.Sprintf("↓%d", behind)
196216
} else {
197-
result += style.FgCyan.Sprintf("↓")
217+
result += "↓"
198218
}
199219
}
200220
}

pkg/gui/presentation/branches_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ func Test_getBranchDisplayStrings(t *testing.T) {
113113
},
114114
itemOperation: types.ItemOperationNone,
115115
fullDescription: false,
116-
viewWidth: 100,
116+
viewWidth: 20,
117117
useIcons: false,
118118
checkedOutByWorktree: false,
119119
showDivergenceCfg: "onlyArrow",
120-
expected: []string{"1m", "branch_name ↓"},
120+
expected: []string{"1m", "branch_name ↓"},
121121
},
122122
{
123123
branch: &models.Branch{
@@ -130,11 +130,11 @@ func Test_getBranchDisplayStrings(t *testing.T) {
130130
},
131131
itemOperation: types.ItemOperationNone,
132132
fullDescription: false,
133-
viewWidth: 100,
133+
viewWidth: 22,
134134
useIcons: false,
135135
checkedOutByWorktree: false,
136136
showDivergenceCfg: "arrowAndNumber",
137-
expected: []string{"1m", "branch_name ✓ ↓2"},
137+
expected: []string{"1m", "branch_name ✓ ↓2"},
138138
},
139139
{
140140
branch: &models.Branch{
@@ -147,11 +147,11 @@ func Test_getBranchDisplayStrings(t *testing.T) {
147147
},
148148
itemOperation: types.ItemOperationNone,
149149
fullDescription: false,
150-
viewWidth: 100,
150+
viewWidth: 26,
151151
useIcons: false,
152152
checkedOutByWorktree: false,
153153
showDivergenceCfg: "arrowAndNumber",
154-
expected: []string{"1m", "branch_name ↓5↑3 ↓2"},
154+
expected: []string{"1m", "branch_name ↓5↑3 ↓2"},
155155
},
156156
{
157157
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
@@ -240,6 +240,23 @@ func Test_getBranchDisplayStrings(t *testing.T) {
240240
showDivergenceCfg: "none",
241241
expected: []string{"1m", "branch_… ✓"},
242242
},
243+
{
244+
branch: &models.Branch{
245+
Name: "branch_name",
246+
Recency: "1m",
247+
UpstreamRemote: "origin",
248+
AheadForPull: "3",
249+
BehindForPull: "5",
250+
BehindBaseBranch: makeAtomic(4),
251+
},
252+
itemOperation: types.ItemOperationNone,
253+
fullDescription: false,
254+
viewWidth: 21,
255+
useIcons: false,
256+
checkedOutByWorktree: false,
257+
showDivergenceCfg: "arrowAndNumber",
258+
expected: []string{"1m", "branch_n… ↓5↑3 ↓4"},
259+
},
243260
{
244261
branch: &models.Branch{
245262
Name: "branch_name",

pkg/integration/tests/branch/rebase_onto_base_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var RebaseOntoBaseBranch = NewIntegrationTest(NewIntegrationTestArgs{
3232
t.Views().Branches().
3333
Focus().
3434
Lines(
35-
Contains("feature ↓1").IsSelected(),
35+
MatchesRegexp(`feature\s+↓1`).IsSelected(),
3636
Contains("master"),
3737
).
3838
Press(keys.Branches.RebaseBranch)

pkg/integration/tests/branch/show_divergence_from_base_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var ShowDivergenceFromBaseBranch = NewIntegrationTest(NewIntegrationTestArgs{
2525
t.Views().Branches().
2626
Focus().
2727
Lines(
28-
Contains("feature ↓1").IsSelected(),
28+
MatchesRegexp(`feature\s+↓1`).IsSelected(),
2929
Contains("master"),
3030
).
3131
Press(keys.Branches.SetUpstream)

pkg/integration/tests/status/show_divergence_from_base_branch.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

pkg/integration/tests/test_list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ var tests = []*components.IntegrationTest{
373373
status.ClickWorkingTreeStateToOpenRebaseOptionsMenu,
374374
status.LogCmd,
375375
status.LogCmdStatusPanelAllBranchesLog,
376-
status.ShowDivergenceFromBaseBranch,
377376
submodule.Add,
378377
submodule.Enter,
379378
submodule.EnterNested,

0 commit comments

Comments
 (0)