fix(yay): fix -Si alignment when language is Chinese (#1799) (#1803)

This commit is contained in:
linweiyuan 2022-08-29 23:33:48 +08:00 committed by GitHub
parent 14cc8d4ef0
commit 7ebe64871d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"unicode"
"github.com/leonelquinteros/gotext" "github.com/leonelquinteros/gotext"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -84,13 +85,29 @@ func getColumnCount() int {
} }
func PrintInfoValue(key string, values ...string) { func PrintInfoValue(key string, values ...string) {
// 16 (text) + 1 (:) + 1 ( )
const ( const (
keyLength = 18 keyLength = 32
delimCount = 2 delimCount = 2
) )
str := fmt.Sprintf(Bold("%-16s: "), key) specialWordsCount := 0
for _, runeValue := range key {
// CJK handling: the character 'ー' is Katakana
// but if use unicode.Katakana, it will return false
if unicode.IsOneOf([]*unicode.RangeTable{
unicode.Han,
unicode.Hiragana,
unicode.Katakana,
unicode.Hangul,
}, runeValue) || runeValue == 'ー' {
specialWordsCount++
}
}
keyTextCount := specialWordsCount - keyLength + delimCount
str := fmt.Sprintf(Bold("%-*s: "), keyTextCount, key)
if len(values) == 0 || (len(values) == 1 && values[0] == "") { if len(values) == 0 || (len(values) == 1 && values[0] == "") {
fmt.Fprintf(os.Stdout, "%s%s\n", str, gotext.Get("None")) fmt.Fprintf(os.Stdout, "%s%s\n", str, gotext.Get("None"))
return return