From e122f421e60e56aa3959668234db8d1fb90df289 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 24 Oct 2021 15:54:20 +1100 Subject: [PATCH] only use a single initial for double sized runes --- pkg/gui/presentation/commits.go | 23 +++++++++++++++-------- pkg/gui/presentation/commits_test.go | 9 ++++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/pkg/gui/presentation/commits.go b/pkg/gui/presentation/commits.go index 2531f7b81..0018d45df 100644 --- a/pkg/gui/presentation/commits.go +++ b/pkg/gui/presentation/commits.go @@ -11,6 +11,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/utils" "github.com/kyokomi/emoji/v2" colorful "github.com/lucasb-eyer/go-colorful" + "github.com/mattn/go-runewidth" ) func GetCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaMap map[string]bool, diffName string, parseEmoji bool) [][]string { @@ -166,19 +167,16 @@ func randFloat(hash []byte) float64 { return float64(sum) / 100 } -var authorStyles = []style.TextStyle{ - style.FgGreen, - style.FgYellow, - style.FgMagenta, - style.FgCyan, - style.FgRed, -} - func getInitials(authorName string) string { if authorName == "" { return authorName } + firstRune := getFirstRune(authorName) + if runewidth.RuneWidth(firstRune) > 1 { + return string(firstRune) + } + split := strings.Split(authorName, " ") if len(split) == 1 { return utils.LimitStr(authorName, 2) @@ -187,6 +185,15 @@ func getInitials(authorName string) string { return utils.LimitStr(split[0], 1) + utils.LimitStr(split[1], 1) } +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 +} + func actionColorMap(str string) style.TextStyle { switch str { case "pick": diff --git a/pkg/gui/presentation/commits_test.go b/pkg/gui/presentation/commits_test.go index 78b798010..578091166 100644 --- a/pkg/gui/presentation/commits_test.go +++ b/pkg/gui/presentation/commits_test.go @@ -3,15 +3,18 @@ package presentation import "testing" func TestGetInitials(t *testing.T) { - for input, output := range map[string]string{ + for input, expectedOutput := range map[string]string{ "Jesse Duffield": "JD", "Jesse Duffield Man": "JD", "JesseDuffield": "Je", "J": "J", + "六书六書": "六", + "書": "書", "": "", } { - if output != getInitials(input) { - t.Errorf("Expected %s to be %s", input, output) + output := getInitials(input) + if output != expectedOutput { + t.Errorf("Expected %s to be %s", output, expectedOutput) } } }