2020-02-25 11:55:36 +02:00
|
|
|
package presentation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2020-02-25 11:55:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
|
|
|
)
|
|
|
|
|
2020-09-29 10:34:01 +02:00
|
|
|
func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string) [][]string {
|
2020-02-25 11:55:36 +02:00
|
|
|
lines := make([][]string, len(branches))
|
|
|
|
|
|
|
|
for i := range branches {
|
2020-03-29 05:34:17 +02:00
|
|
|
diffed := branches[i].Name == diffName
|
|
|
|
lines[i] = getBranchDisplayStrings(branches[i], fullDescription, diffed)
|
2020-02-25 11:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBranchDisplayStrings returns the display string of branch
|
2020-09-29 10:34:01 +02:00
|
|
|
func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed bool) []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
|
|
|
}
|
2021-08-01 08:02:49 +02:00
|
|
|
coloredName := nameTextStyle.Sprint(displayName)
|
2021-06-05 07:56:50 +02:00
|
|
|
if b.IsTrackingRemote() {
|
|
|
|
coloredName = fmt.Sprintf("%s %s", coloredName, ColoredBranchStatus(b))
|
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
|
|
|
}
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
res := []string{recencyColor.Sprint(b.Recency), coloredName}
|
2020-03-17 12:22:07 +02:00
|
|
|
if fullDescription {
|
2021-07-27 15:00:37 +02:00
|
|
|
return append(res, style.FgYellow.Sprint(b.UpstreamName))
|
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]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func ColoredBranchStatus(branch *models.Branch) string {
|
2021-07-27 15:00:37 +02:00
|
|
|
colour := style.FgYellow
|
2021-06-05 07:56:50 +02:00
|
|
|
if branch.MatchesUpstream() {
|
2021-07-27 15:00:37 +02:00
|
|
|
colour = style.FgGreen
|
2021-06-05 07:56:50 +02:00
|
|
|
} else if !branch.IsTrackingRemote() {
|
2021-07-27 15:00:37 +02:00
|
|
|
colour = style.FgRed
|
2021-06-05 07:56:50 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
return colour.Sprint(BranchStatus(branch))
|
2021-06-05 07:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BranchStatus(branch *models.Branch) string {
|
|
|
|
return fmt.Sprintf("↑%s↓%s", branch.Pushables, branch.Pullables)
|
|
|
|
}
|