1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-05 15:15:49 +02:00
lazygit/pkg/gui/presentation/branches.go

155 lines
3.8 KiB
Go
Raw Normal View History

2020-02-25 20:55:36 +11:00
package presentation
import (
"fmt"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
2022-11-07 16:35:36 +11:00
"github.com/jesseduffield/lazygit/pkg/config"
2022-04-23 10:57:51 +09:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2022-03-24 17:49:25 +11:00
"github.com/jesseduffield/lazygit/pkg/i18n"
2020-02-25 20:55:36 +11:00
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils"
2023-07-16 11:45:08 +10:00
"github.com/samber/lo"
2020-02-25 20:55:36 +11:00
)
var branchPrefixColorCache = make(map[string]style.TextStyle)
2022-11-07 16:35:36 +11:00
func GetBranchListDisplayStrings(
branches []*models.Branch,
fullDescription bool,
diffName string,
tr *i18n.TranslationSet,
userConfig *config.UserConfig,
worktrees []*models.Worktree,
2022-11-07 16:35:36 +11:00
) [][]string {
return lo.Map(branches, func(branch *models.Branch, _ int) []string {
2022-03-19 19:12:58 +11:00
diffed := branch.Name == diffName
return getBranchDisplayStrings(branch, fullDescription, diffed, tr, userConfig, worktrees)
2022-03-19 19:12:58 +11:00
})
2020-02-25 20:55:36 +11:00
}
// getBranchDisplayStrings returns the display string of branch
2022-11-07 16:35:36 +11:00
func getBranchDisplayStrings(
b *models.Branch,
fullDescription bool,
diffed bool,
tr *i18n.TranslationSet,
userConfig *config.UserConfig,
worktrees []*models.Worktree,
2022-11-07 16:35:36 +11:00
) []string {
2020-03-26 20:29:35 +11:00
displayName := b.Name
if b.DisplayName != "" {
displayName = b.DisplayName
}
2021-08-01 16:02:49 +10:00
nameTextStyle := GetBranchTextStyle(b.Name)
if diffed {
2021-08-01 16:02:49 +10:00
nameTextStyle = theme.DiffTerminalColor
}
2022-04-18 10:44:45 +10:00
2021-08-01 16:02:49 +10:00
coloredName := nameTextStyle.Sprint(displayName)
branchStatus := utils.WithPadding(ColoredBranchStatus(b, tr), 2, utils.AlignLeft)
if git_commands.CheckedOutByOtherWorktree(b, worktrees) {
2023-07-17 09:29:56 +10:00
worktreeIcon := lo.Ternary(icons.IsIconEnabled(), icons.LINKED_WORKTREE_ICON, fmt.Sprintf("(%s)", tr.LcWorktree))
2023-07-16 11:45:08 +10:00
coloredName = fmt.Sprintf("%s %s", coloredName, style.FgDefault.Sprint(worktreeIcon))
}
2022-04-18 10:44:45 +10:00
coloredName = fmt.Sprintf("%s %s", coloredName, branchStatus)
2020-02-25 20:55:36 +11:00
recencyColor := style.FgCyan
if b.Recency == " *" {
recencyColor = style.FgGreen
}
2022-11-07 16:35:36 +11:00
res := make([]string, 0, 6)
2022-04-23 10:57:51 +09:00
res = append(res, recencyColor.Sprint(b.Recency))
2023-07-16 11:45:08 +10:00
2022-04-23 10:57:51 +09:00
if icons.IsIconEnabled() {
res = append(res, nameTextStyle.Sprint(icons.IconForBranch(b)))
}
2022-11-07 16:35:36 +11:00
if fullDescription || userConfig.Gui.ShowBranchCommitHash {
res = append(res, utils.ShortSha(b.CommitHash))
2022-11-07 16:35:36 +11:00
}
2022-04-23 10:57:51 +09:00
res = append(res, coloredName)
2022-11-07 16:35:36 +11:00
if fullDescription {
2022-04-23 10:57:51 +09:00
res = append(
res,
fmt.Sprintf("%s %s",
style.FgYellow.Sprint(b.UpstreamRemote),
style.FgYellow.Sprint(b.UpstreamBranch),
),
2022-11-07 16:35:36 +11:00
utils.TruncateWithEllipsis(b.Subject, 60),
)
}
return res
2020-02-25 20:55:36 +11:00
}
2021-08-01 16:02:49 +10:00
// GetBranchTextStyle branch color
func GetBranchTextStyle(name string) style.TextStyle {
2020-02-25 20:55:36 +11:00
branchType := strings.Split(name, "/")[0]
if value, ok := branchPrefixColorCache[branchType]; ok {
return value
}
2020-02-25 20:55:36 +11:00
switch branchType {
case "feature":
return style.FgGreen
2020-02-25 20:55:36 +11:00
case "bugfix":
return style.FgYellow
2020-02-25 20:55:36 +11:00
case "hotfix":
return style.FgRed
2020-02-25 20:55:36 +11:00
default:
return theme.DefaultTextColor
}
}
2021-06-05 15:56:50 +10:00
2022-03-24 17:49:25 +11:00
func ColoredBranchStatus(branch *models.Branch, tr *i18n.TranslationSet) string {
colour := style.FgYellow
2022-04-18 10:44:45 +10:00
if branch.UpstreamGone {
colour = style.FgRed
2022-03-24 17:49:25 +11:00
} else if branch.MatchesUpstream() {
colour = style.FgGreen
2022-04-18 10:44:45 +10:00
} else if branch.RemoteBranchNotStoredLocally() {
colour = style.FgMagenta
2021-06-05 15:56:50 +10:00
}
2022-03-24 17:49:25 +11:00
return colour.Sprint(BranchStatus(branch, tr))
2021-06-05 15:56:50 +10:00
}
2022-03-24 17:49:25 +11:00
func BranchStatus(branch *models.Branch, tr *i18n.TranslationSet) string {
2022-04-18 10:44:45 +10:00
if !branch.IsTrackingRemote() {
return ""
}
2022-03-24 17:49:25 +11:00
if branch.UpstreamGone {
return tr.UpstreamGone
}
2022-04-18 10:44:45 +10:00
if branch.MatchesUpstream() {
return "✓"
}
if branch.RemoteBranchNotStoredLocally() {
return "?"
}
result := ""
if branch.HasCommitsToPush() {
result = fmt.Sprintf("↑%s", branch.Pushables)
}
if branch.HasCommitsToPull() {
result = fmt.Sprintf("%s↓%s", result, branch.Pullables)
}
return result
2021-06-05 15:56:50 +10:00
}
func SetCustomBranches(customBranchColors map[string]string) {
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
}