2020-02-25 11:55:36 +02:00
|
|
|
package presentation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2022-04-23 03:57:51 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2022-03-24 08:49:25 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/i18n"
|
2020-02-25 11:55:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
2022-02-01 07:23:55 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2020-02-25 11:55:36 +02:00
|
|
|
)
|
|
|
|
|
2022-02-01 07:23:55 +02:00
|
|
|
var branchPrefixColorCache = make(map[string]style.TextStyle)
|
2022-01-29 07:23:57 +02:00
|
|
|
|
2022-03-24 08:49:25 +02:00
|
|
|
func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string, tr *i18n.TranslationSet) [][]string {
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(branches, func(branch *models.Branch) []string {
|
|
|
|
diffed := branch.Name == diffName
|
|
|
|
return getBranchDisplayStrings(branch, fullDescription, diffed, tr)
|
|
|
|
})
|
2020-02-25 11:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getBranchDisplayStrings returns the display string of branch
|
2022-03-24 08:49:25 +02:00
|
|
|
func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed bool, tr *i18n.TranslationSet) []string {
|
2020-03-26 11:29:35 +02:00
|
|
|
displayName := b.Name
|
|
|
|
if b.DisplayName != "" {
|
|
|
|
displayName = b.DisplayName
|
|
|
|
}
|
2020-03-29 05:34:17 +02:00
|
|
|
|
2021-08-01 08:02:49 +02:00
|
|
|
nameTextStyle := GetBranchTextStyle(b.Name)
|
2020-03-29 05:34:17 +02:00
|
|
|
if diffed {
|
2021-08-01 08:02:49 +02:00
|
|
|
nameTextStyle = theme.DiffTerminalColor
|
2020-03-29 05:34:17 +02:00
|
|
|
}
|
2022-04-18 02:44:45 +02:00
|
|
|
|
2021-08-01 08:02:49 +02:00
|
|
|
coloredName := nameTextStyle.Sprint(displayName)
|
2022-04-18 02:44:45 +02:00
|
|
|
branchStatus := utils.WithPadding(ColoredBranchStatus(b, tr), 2)
|
|
|
|
coloredName = fmt.Sprintf("%s %s", coloredName, branchStatus)
|
2020-02-25 11:55:36 +02:00
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
recencyColor := style.FgCyan
|
2020-03-17 12:22:07 +02:00
|
|
|
if b.Recency == " *" {
|
2021-07-27 15:00:37 +02:00
|
|
|
recencyColor = style.FgGreen
|
2020-03-17 12:22:07 +02:00
|
|
|
}
|
|
|
|
|
2022-04-23 03:57:51 +02:00
|
|
|
res := make([]string, 0, 4)
|
|
|
|
res = append(res, recencyColor.Sprint(b.Recency))
|
|
|
|
if icons.IsIconEnabled() {
|
|
|
|
res = append(res, nameTextStyle.Sprint(icons.IconForBranch(b)))
|
|
|
|
}
|
|
|
|
res = append(res, coloredName)
|
2020-03-17 12:22:07 +02:00
|
|
|
if fullDescription {
|
2022-04-23 03:57:51 +02:00
|
|
|
res = append(
|
2022-01-15 05:20:09 +02:00
|
|
|
res,
|
|
|
|
fmt.Sprintf("%s %s",
|
|
|
|
style.FgYellow.Sprint(b.UpstreamRemote),
|
|
|
|
style.FgYellow.Sprint(b.UpstreamBranch),
|
|
|
|
),
|
|
|
|
)
|
2020-03-17 12:22:07 +02:00
|
|
|
}
|
2021-07-27 15:00:37 +02:00
|
|
|
return res
|
2020-02-25 11:55:36 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 08:02:49 +02:00
|
|
|
// GetBranchTextStyle branch color
|
|
|
|
func GetBranchTextStyle(name string) style.TextStyle {
|
2020-02-25 11:55:36 +02:00
|
|
|
branchType := strings.Split(name, "/")[0]
|
|
|
|
|
2022-02-01 07:23:55 +02:00
|
|
|
if value, ok := branchPrefixColorCache[branchType]; ok {
|
2022-01-29 07:23:57 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2020-02-25 11:55:36 +02:00
|
|
|
switch branchType {
|
|
|
|
case "feature":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgGreen
|
2020-02-25 11:55:36 +02:00
|
|
|
case "bugfix":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgYellow
|
2020-02-25 11:55:36 +02:00
|
|
|
case "hotfix":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgRed
|
2020-02-25 11:55:36 +02:00
|
|
|
default:
|
|
|
|
return theme.DefaultTextColor
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 07:56:50 +02:00
|
|
|
|
2022-03-24 08:49:25 +02:00
|
|
|
func ColoredBranchStatus(branch *models.Branch, tr *i18n.TranslationSet) string {
|
2021-07-27 15:00:37 +02:00
|
|
|
colour := style.FgYellow
|
2022-04-18 02:44:45 +02:00
|
|
|
if branch.UpstreamGone {
|
2021-07-27 15:00:37 +02:00
|
|
|
colour = style.FgRed
|
2022-03-24 08:49:25 +02:00
|
|
|
} else if branch.MatchesUpstream() {
|
|
|
|
colour = style.FgGreen
|
2022-04-18 02:44:45 +02:00
|
|
|
} else if branch.RemoteBranchNotStoredLocally() {
|
|
|
|
colour = style.FgMagenta
|
2021-06-05 07:56:50 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 08:49:25 +02:00
|
|
|
return colour.Sprint(BranchStatus(branch, tr))
|
2021-06-05 07:56:50 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 08:49:25 +02:00
|
|
|
func BranchStatus(branch *models.Branch, tr *i18n.TranslationSet) string {
|
2022-04-18 02:44:45 +02:00
|
|
|
if !branch.IsTrackingRemote() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-03-24 08:49:25 +02:00
|
|
|
if branch.UpstreamGone {
|
|
|
|
return tr.UpstreamGone
|
|
|
|
}
|
2022-04-18 02:44:45 +02: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 07:56:50 +02:00
|
|
|
}
|
2022-01-29 07:23:57 +02:00
|
|
|
|
2022-02-01 07:23:55 +02:00
|
|
|
func SetCustomBranches(customBranchColors map[string]string) {
|
|
|
|
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
|
2022-01-29 07:23:57 +02:00
|
|
|
}
|