2020-02-25 11:11:07 +02:00
|
|
|
package presentation
|
|
|
|
|
|
|
|
import (
|
2021-10-23 09:17:35 +02:00
|
|
|
"crypto/md5"
|
2020-02-25 11:11:07 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-10-23 09:17:35 +02:00
|
|
|
"github.com/gookit/color"
|
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:11:07 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2021-07-16 14:06:01 +02:00
|
|
|
"github.com/kyokomi/emoji/v2"
|
2021-10-23 09:17:35 +02:00
|
|
|
colorful "github.com/lucasb-eyer/go-colorful"
|
2021-10-24 06:54:20 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2020-02-25 11:11:07 +02:00
|
|
|
)
|
|
|
|
|
2021-07-16 14:06:01 +02:00
|
|
|
func GetCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaMap map[string]bool, diffName string, parseEmoji bool) [][]string {
|
2020-02-25 11:11:07 +02:00
|
|
|
lines := make([][]string, len(commits))
|
|
|
|
|
2021-07-16 14:06:01 +02:00
|
|
|
var displayFunc func(*models.Commit, map[string]bool, bool, bool) []string
|
2020-02-25 11:11:07 +02:00
|
|
|
if fullDescription {
|
|
|
|
displayFunc = getFullDescriptionDisplayStringsForCommit
|
|
|
|
} else {
|
|
|
|
displayFunc = getDisplayStringsForCommit
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range commits {
|
2020-03-29 05:34:17 +02:00
|
|
|
diffed := commits[i].Sha == diffName
|
2021-07-16 14:06:01 +02:00
|
|
|
lines[i] = displayFunc(commits[i], cherryPickedCommitShaMap, diffed, parseEmoji)
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:06:01 +02:00
|
|
|
func getFullDescriptionDisplayStringsForCommit(c *models.Commit, cherryPickedCommitShaMap map[string]bool, diffed, parseEmoji bool) []string {
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor := theme.DefaultTextColor
|
2020-02-25 11:11:07 +02:00
|
|
|
switch c.Status {
|
|
|
|
case "unpushed":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgRed
|
2020-02-25 11:11:07 +02:00
|
|
|
case "pushed":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgYellow
|
2020-02-25 11:11:07 +02:00
|
|
|
case "merged":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgGreen
|
2020-02-25 11:11:07 +02:00
|
|
|
case "rebasing":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgBlue
|
2020-02-25 11:11:07 +02:00
|
|
|
case "reflog":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgBlue
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 05:34:17 +02:00
|
|
|
if diffed {
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = theme.DiffTerminalColor
|
2020-03-29 05:34:17 +02:00
|
|
|
} else if cherryPickedCommitShaMap[c.Sha] {
|
2021-07-27 15:00:37 +02:00
|
|
|
// for some reason, setting the background to blue pads out the other commits
|
|
|
|
// horizontally. For the sake of accessibility I'm considering this a feature,
|
|
|
|
// not a bug
|
2021-09-29 13:53:31 +02:00
|
|
|
shaColor = theme.CherryPickedCommitTextStyle
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tagString := ""
|
2021-07-27 15:00:37 +02:00
|
|
|
secondColumnString := style.FgBlue.Sprint(utils.UnixToDate(c.UnixTimestamp))
|
2020-02-25 11:11:07 +02:00
|
|
|
if c.Action != "" {
|
2021-07-27 15:00:37 +02:00
|
|
|
secondColumnString = actionColorMap(c.Action).Sprint(c.Action)
|
2020-02-25 11:55:36 +02:00
|
|
|
} else if c.ExtraInfo != "" {
|
2021-08-01 08:02:49 +02:00
|
|
|
tagString = style.FgMagenta.SetBold().Sprint(c.ExtraInfo) + " "
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:06:01 +02:00
|
|
|
name := c.Name
|
|
|
|
if parseEmoji {
|
|
|
|
name = emoji.Sprint(name)
|
|
|
|
}
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
return []string{
|
|
|
|
shaColor.Sprint(c.ShortSha()),
|
|
|
|
secondColumnString,
|
2021-10-23 09:17:35 +02:00
|
|
|
longAuthor(c.Author),
|
2021-07-27 15:00:37 +02:00
|
|
|
tagString + theme.DefaultTextColor.Sprint(name),
|
|
|
|
}
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:06:01 +02:00
|
|
|
func getDisplayStringsForCommit(c *models.Commit, cherryPickedCommitShaMap map[string]bool, diffed, parseEmoji bool) []string {
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor := theme.DefaultTextColor
|
2020-02-25 11:11:07 +02:00
|
|
|
switch c.Status {
|
|
|
|
case "unpushed":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgRed
|
2020-02-25 11:11:07 +02:00
|
|
|
case "pushed":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgYellow
|
2020-02-25 11:11:07 +02:00
|
|
|
case "merged":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgGreen
|
2020-02-25 11:11:07 +02:00
|
|
|
case "rebasing":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgBlue
|
2020-02-25 11:11:07 +02:00
|
|
|
case "reflog":
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = style.FgBlue
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 05:34:17 +02:00
|
|
|
if diffed {
|
2021-07-27 15:00:37 +02:00
|
|
|
shaColor = theme.DiffTerminalColor
|
2020-03-29 05:34:17 +02:00
|
|
|
} else if cherryPickedCommitShaMap[c.Sha] {
|
2021-07-27 15:00:37 +02:00
|
|
|
// for some reason, setting the background to blue pads out the other commits
|
|
|
|
// horizontally. For the sake of accessibility I'm considering this a feature,
|
|
|
|
// not a bug
|
2021-09-29 13:53:31 +02:00
|
|
|
shaColor = theme.CherryPickedCommitTextStyle
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
actionString := ""
|
|
|
|
tagString := ""
|
|
|
|
if c.Action != "" {
|
2021-07-27 15:00:37 +02:00
|
|
|
actionString = actionColorMap(c.Action).Sprint(utils.WithPadding(c.Action, 7)) + " "
|
2020-02-25 11:11:07 +02:00
|
|
|
} else if len(c.Tags) > 0 {
|
2021-07-31 04:54:28 +02:00
|
|
|
tagString = theme.DiffTerminalColor.SetBold().Sprint(strings.Join(c.Tags, " ")) + " "
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 14:06:01 +02:00
|
|
|
name := c.Name
|
|
|
|
if parseEmoji {
|
|
|
|
name = emoji.Sprint(name)
|
|
|
|
}
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
return []string{
|
|
|
|
shaColor.Sprint(c.ShortSha()),
|
2021-10-23 09:17:35 +02:00
|
|
|
shortAuthor(c.Author),
|
2021-07-27 15:00:37 +02:00
|
|
|
actionString + tagString + theme.DefaultTextColor.Sprint(name),
|
|
|
|
}
|
2020-02-25 11:11:07 +02:00
|
|
|
}
|
2020-08-27 09:00:43 +02:00
|
|
|
|
2021-10-23 09:17:35 +02:00
|
|
|
var authorInitialCache = make(map[string]string)
|
|
|
|
var authorNameCache = make(map[string]string)
|
|
|
|
|
|
|
|
func shortAuthor(authorName string) string {
|
2021-10-23 13:46:37 +02:00
|
|
|
if value, ok := authorInitialCache[authorName]; ok {
|
|
|
|
return value
|
2021-10-23 09:17:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initials := getInitials(authorName)
|
|
|
|
if initials == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
value := authorColor(authorName).Sprint(initials)
|
|
|
|
authorInitialCache[authorName] = value
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func longAuthor(authorName string) string {
|
2021-10-23 13:46:37 +02:00
|
|
|
if value, ok := authorNameCache[authorName]; ok {
|
|
|
|
return value
|
2021-10-23 09:17:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
truncatedName := utils.TruncateWithEllipsis(authorName, 17)
|
|
|
|
value := authorColor(authorName).Sprint(truncatedName)
|
|
|
|
authorNameCache[authorName] = value
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func authorColor(authorName string) style.TextStyle {
|
|
|
|
hash := md5.Sum([]byte(authorName))
|
|
|
|
c := colorful.Hsl(randFloat(hash[0:4])*360.0, 0.6+0.4*randFloat(hash[4:8]), 0.4+randFloat(hash[8:12])*0.2)
|
|
|
|
|
|
|
|
return style.New().SetFg(style.NewRGBColor(color.RGB(uint8(c.R*255), uint8(c.G*255), uint8(c.B*255))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func randFloat(hash []byte) float64 {
|
|
|
|
sum := 0
|
|
|
|
for _, b := range hash {
|
|
|
|
sum = (sum + int(b)) % 100
|
|
|
|
}
|
|
|
|
return float64(sum) / 100
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInitials(authorName string) string {
|
|
|
|
if authorName == "" {
|
|
|
|
return authorName
|
|
|
|
}
|
|
|
|
|
2021-10-24 06:54:20 +02:00
|
|
|
firstRune := getFirstRune(authorName)
|
|
|
|
if runewidth.RuneWidth(firstRune) > 1 {
|
|
|
|
return string(firstRune)
|
|
|
|
}
|
|
|
|
|
2021-10-23 09:17:35 +02:00
|
|
|
split := strings.Split(authorName, " ")
|
|
|
|
if len(split) == 1 {
|
|
|
|
return utils.LimitStr(authorName, 2)
|
|
|
|
}
|
|
|
|
|
2021-10-23 13:46:37 +02:00
|
|
|
return utils.LimitStr(split[0], 1) + utils.LimitStr(split[1], 1)
|
2021-10-23 09:17:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-24 06:54:20 +02:00
|
|
|
func getFirstRune(str string) rune {
|
|
|
|
// just using the loop for the sake of getting the first rune
|
|
|
|
for _, r := range str {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
// should never land here
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
func actionColorMap(str string) style.TextStyle {
|
2020-08-27 09:00:43 +02:00
|
|
|
switch str {
|
|
|
|
case "pick":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgCyan
|
2020-08-27 09:00:43 +02:00
|
|
|
case "drop":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgRed
|
2020-08-27 09:00:43 +02:00
|
|
|
case "edit":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgGreen
|
2020-08-27 09:00:43 +02:00
|
|
|
case "fixup":
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgMagenta
|
2020-08-27 09:00:43 +02:00
|
|
|
default:
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgYellow
|
2020-08-27 09:00:43 +02:00
|
|
|
}
|
|
|
|
}
|