From 636acb89f00938c571c2d876d92320d68c1ecade Mon Sep 17 00:00:00 2001 From: Tom McKeesick Date: Thu, 4 Apr 2024 23:26:54 +1100 Subject: [PATCH 1/3] =?UTF-8?q?fix=20the=20width=20detection=20for=20nidor?= =?UTF-8?q?an=20=E2=99=80/=E2=99=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pokesay/print.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pokesay/print.go b/src/pokesay/print.go index 43f06030..d4408345 100644 --- a/src/pokesay/print.go +++ b/src/pokesay/print.go @@ -5,6 +5,7 @@ import ( "embed" "fmt" "os" + "slices" "strings" "github.com/fatih/color" @@ -69,6 +70,7 @@ var ( RightArrow: "→", CategorySeparator: "/", } + SingleWidthCars []string = []string{"♀", "♂"} ) func DetermineBoxCharacters(unicodeBox bool) *BoxCharacters { @@ -135,13 +137,19 @@ func printWrappedText(boxCharacters *BoxCharacters, line string, width int, tabS func nameLength(names []string) int { totalLen := 0 + for _, name := range names { for _, c := range name { // check if ascii if c < 128 { totalLen++ } else { - totalLen += 2 + // check if single width character + if slices.Contains(SingleWidthCars, string(c)) { + totalLen++ + } else { + totalLen += 2 + } } } } @@ -170,7 +178,6 @@ func printPokemon(args Args, index int, names []string, categoryKeys []string, G args.BoxCharacters.RightArrow, strings.Join(namesFmt, fmt.Sprintf(" %s ", args.BoxCharacters.Separator)), ) - } else { infoLine = fmt.Sprintf( "%s %s %s %s", @@ -182,7 +189,7 @@ func printPokemon(args Args, index int, names []string, categoryKeys []string, G for _, category := range categoryKeys { width += len(category) } - width += len(categoryKeys) - 1 + 1 + 2 + width += len(categoryKeys) - 1 + 1 + 2 // lol why did I do this } if args.DrawInfoBorder { From 655870da1fc605d23d6e5fe383c8713aa389eca7 Mon Sep 17 00:00:00 2001 From: Tom McKeesick Date: Thu, 4 Apr 2024 23:40:45 +1100 Subject: [PATCH 2/3] remove usage of the slices package found this sweet bool map trick, as non-existent keys return the "0 value" for the value type in the map (i.e. false when bool) --- src/pokesay/print.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pokesay/print.go b/src/pokesay/print.go index d4408345..d6f05a85 100644 --- a/src/pokesay/print.go +++ b/src/pokesay/print.go @@ -5,7 +5,6 @@ import ( "embed" "fmt" "os" - "slices" "strings" "github.com/fatih/color" @@ -70,7 +69,10 @@ var ( RightArrow: "→", CategorySeparator: "/", } - SingleWidthCars []string = []string{"♀", "♂"} + SingleWidthCars map[string]bool = map[string]bool{ + "♀": true, + "♂": true, + } ) func DetermineBoxCharacters(unicodeBox bool) *BoxCharacters { @@ -145,7 +147,7 @@ func nameLength(names []string) int { totalLen++ } else { // check if single width character - if slices.Contains(SingleWidthCars, string(c)) { + if SingleWidthCars[string(c)] { totalLen++ } else { totalLen += 2 From 4a21bb0fe54d088189442836d98c046a65f0b29e Mon Sep 17 00:00:00 2001 From: Tom McKeesick Date: Thu, 4 Apr 2024 23:45:09 +1100 Subject: [PATCH 3/3] simplify if statements --- src/pokesay/print.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/pokesay/print.go b/src/pokesay/print.go index d6f05a85..786f83fd 100644 --- a/src/pokesay/print.go +++ b/src/pokesay/print.go @@ -142,16 +142,11 @@ func nameLength(names []string) int { for _, name := range names { for _, c := range name { - // check if ascii - if c < 128 { + // check if ascii or single-width unicode + if (c < 128) || (SingleWidthCars[string(c)]) { totalLen++ } else { - // check if single width character - if SingleWidthCars[string(c)] { - totalLen++ - } else { - totalLen += 2 - } + totalLen += 2 } } }