1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/presentation/commits.go

137 lines
3.6 KiB
Go
Raw Normal View History

2020-02-25 11:11:07 +02:00
package presentation
import (
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"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"
2020-02-25 11:11:07 +02:00
)
2021-07-31 04:54:28 +02:00
var cherryPickedCommitTextStyle = style.FgCyan.MergeStyle(style.BgBlue)
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 {
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 {
shaColor := theme.DefaultTextColor
2020-02-25 11:11:07 +02:00
switch c.Status {
case "unpushed":
shaColor = style.FgRed
2020-02-25 11:11:07 +02:00
case "pushed":
shaColor = style.FgYellow
2020-02-25 11:11:07 +02:00
case "merged":
shaColor = style.FgGreen
2020-02-25 11:11:07 +02:00
case "rebasing":
shaColor = style.FgBlue
2020-02-25 11:11:07 +02:00
case "reflog":
shaColor = style.FgBlue
2020-02-25 11:11:07 +02:00
}
if diffed {
shaColor = theme.DiffTerminalColor
} else if cherryPickedCommitShaMap[c.Sha] {
// 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-07-31 04:54:28 +02:00
shaColor = cherryPickedCommitTextStyle
2020-02-25 11:11:07 +02:00
}
tagString := ""
secondColumnString := style.FgBlue.Sprint(utils.UnixToDate(c.UnixTimestamp))
2020-02-25 11:11:07 +02:00
if c.Action != "" {
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
}
truncatedAuthor := utils.TruncateWithEllipsis(c.Author, 17)
2021-07-16 14:06:01 +02:00
name := c.Name
if parseEmoji {
name = emoji.Sprint(name)
}
return []string{
shaColor.Sprint(c.ShortSha()),
secondColumnString,
style.FgYellow.Sprint(truncatedAuthor),
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 {
shaColor := theme.DefaultTextColor
2020-02-25 11:11:07 +02:00
switch c.Status {
case "unpushed":
shaColor = style.FgRed
2020-02-25 11:11:07 +02:00
case "pushed":
shaColor = style.FgYellow
2020-02-25 11:11:07 +02:00
case "merged":
shaColor = style.FgGreen
2020-02-25 11:11:07 +02:00
case "rebasing":
shaColor = style.FgBlue
2020-02-25 11:11:07 +02:00
case "reflog":
shaColor = style.FgBlue
2020-02-25 11:11:07 +02:00
}
if diffed {
shaColor = theme.DiffTerminalColor
} else if cherryPickedCommitShaMap[c.Sha] {
// 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-07-31 04:54:28 +02:00
shaColor = cherryPickedCommitTextStyle
2020-02-25 11:11:07 +02:00
}
actionString := ""
tagString := ""
if c.Action != "" {
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)
}
return []string{
shaColor.Sprint(c.ShortSha()),
actionString + tagString + theme.DefaultTextColor.Sprint(name),
}
2020-02-25 11:11:07 +02:00
}
func actionColorMap(str string) style.TextStyle {
switch str {
case "pick":
return style.FgCyan
case "drop":
return style.FgRed
case "edit":
return style.FgGreen
case "fixup":
return style.FgMagenta
default:
return style.FgYellow
}
}