1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-06 22:33:07 +02:00

Draw divergence from base branch right-aligned in branches view

Also, remove it from the status view. I'd say it isn't really needed there,
although I don't have a strong opinion about that.
This commit is contained in:
Stefan Haller
2025-07-24 18:32:33 +02:00
parent d7626d4ccf
commit abb7bed0c4
6 changed files with 53 additions and 44 deletions

View File

@ -57,12 +57,13 @@ func getBranchDisplayStrings(
checkedOutByWorkTree := git_commands.CheckedOutByOtherWorktree(b, worktrees)
showCommitHash := fullDescription || userConfig.Gui.ShowBranchCommitHash
branchStatus := BranchStatus(b, itemOperation, tr, now, userConfig)
divergence := divergenceStr(b, itemOperation, tr, userConfig)
worktreeIcon := lo.Ternary(icons.IsIconEnabled(), icons.LINKED_WORKTREE_ICON, fmt.Sprintf("(%s)", tr.LcWorktree))
// Recency is always three characters, plus one for the space
availableWidth := viewWidth - 4
if len(branchStatus) > 0 {
availableWidth -= utils.StringWidth(utils.Decolorise(branchStatus)) + 1
if len(divergence) > 0 {
availableWidth -= utils.StringWidth(divergence) + 1
}
if icons.IsIconEnabled() {
availableWidth -= 2 // one for the icon, one for the space
@ -73,6 +74,11 @@ func getBranchDisplayStrings(
if checkedOutByWorkTree {
availableWidth -= utils.StringWidth(worktreeIcon) + 1
}
paddingNeededForDivergence := availableWidth
if len(branchStatus) > 0 {
availableWidth -= utils.StringWidth(utils.Decolorise(branchStatus)) + 1
}
displayName := b.Name
if b.DisplayName != "" {
@ -114,6 +120,13 @@ func getBranchDisplayStrings(
res = append(res, utils.ShortHash(b.CommitHash))
}
if divergence != "" {
paddingNeededForDivergence -= utils.StringWidth(utils.Decolorise(coloredName)) - 1
if paddingNeededForDivergence > 0 {
coloredName += strings.Repeat(" ", paddingNeededForDivergence)
coloredName += style.FgCyan.Sprint(divergence)
}
}
res = append(res, coloredName)
if fullDescription {
@ -185,16 +198,23 @@ func BranchStatus(
}
}
if userConfig.Gui.ShowDivergenceFromBaseBranch != "none" {
return result
}
func divergenceStr(
branch *models.Branch,
itemOperation types.ItemOperation,
tr *i18n.TranslationSet,
userConfig *config.UserConfig,
) string {
result := ""
if ItemOperationToString(itemOperation, tr) == "" && userConfig.Gui.ShowDivergenceFromBaseBranch != "none" {
behind := branch.BehindBaseBranch.Load()
if behind != 0 {
if result != "" {
result += " "
}
if userConfig.Gui.ShowDivergenceFromBaseBranch == "arrowAndNumber" {
result += style.FgCyan.Sprintf("↓%d", behind)
result += fmt.Sprintf("↓%d", behind)
} else {
result += style.FgCyan.Sprintf("↓")
result += "↓"
}
}
}

View File

@ -113,11 +113,11 @@ func Test_getBranchDisplayStrings(t *testing.T) {
},
itemOperation: types.ItemOperationNone,
fullDescription: false,
viewWidth: 100,
viewWidth: 20,
useIcons: false,
checkedOutByWorktree: false,
showDivergenceCfg: "onlyArrow",
expected: []string{"1m", "branch_name ↓"},
expected: []string{"1m", "branch_name ↓"},
},
{
branch: &models.Branch{
@ -130,11 +130,11 @@ func Test_getBranchDisplayStrings(t *testing.T) {
},
itemOperation: types.ItemOperationNone,
fullDescription: false,
viewWidth: 100,
viewWidth: 22,
useIcons: false,
checkedOutByWorktree: false,
showDivergenceCfg: "arrowAndNumber",
expected: []string{"1m", "branch_name ✓ ↓2"},
expected: []string{"1m", "branch_name ✓ ↓2"},
},
{
branch: &models.Branch{
@ -147,11 +147,11 @@ func Test_getBranchDisplayStrings(t *testing.T) {
},
itemOperation: types.ItemOperationNone,
fullDescription: false,
viewWidth: 100,
viewWidth: 26,
useIcons: false,
checkedOutByWorktree: false,
showDivergenceCfg: "arrowAndNumber",
expected: []string{"1m", "branch_name ↓5↑3 ↓2"},
expected: []string{"1m", "branch_name ↓5↑3 ↓2"},
},
{
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
@ -240,6 +240,23 @@ func Test_getBranchDisplayStrings(t *testing.T) {
showDivergenceCfg: "none",
expected: []string{"1m", "branch_… ✓"},
},
{
branch: &models.Branch{
Name: "branch_name",
Recency: "1m",
UpstreamRemote: "origin",
AheadForPull: "3",
BehindForPull: "5",
BehindBaseBranch: makeAtomic(4),
},
itemOperation: types.ItemOperationNone,
fullDescription: false,
viewWidth: 21,
useIcons: false,
checkedOutByWorktree: false,
showDivergenceCfg: "arrowAndNumber",
expected: []string{"1m", "branch_n… ↓5↑3 ↓4"},
},
{
branch: &models.Branch{
Name: "branch_name",