diff options
author | Yang Zhang <yang_zhang@iapcm.ac.cn> | 2018-12-26 23:05:58 +0800 |
---|---|---|
committer | Yang Zhang <yang_zhang@iapcm.ac.cn> | 2018-12-26 23:05:58 +0800 |
commit | d31891504d201423f512d897d44dd71b06dad93d (patch) | |
tree | 0d5d5ba5597cb0fcb14a1adcc23ca6eb79c95fe6 /util/text/left_padded.go | |
parent | 32b3e263fc8443f6089b9de8fbce833369461982 (diff) | |
parent | 3fa2d15fb899c937900083fd7de696599371ce47 (diff) | |
download | git-bug-d31891504d201423f512d897d44dd71b06dad93d.tar.gz |
Implement almost full CJK support.
Display of CJK contents are supported. Adding CJK tags are problematic.
Diffstat (limited to 'util/text/left_padded.go')
-rw-r--r-- | util/text/left_padded.go | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/util/text/left_padded.go b/util/text/left_padded.go index 729834db..3b8e13c6 100644 --- a/util/text/left_padded.go +++ b/util/text/left_padded.go @@ -3,25 +3,26 @@ package text import ( "bytes" "fmt" + "github.com/mattn/go-runewidth" "strings" ) -// LeftPadMaxLine pads a string on the left by a specified amount and pads the string on the right to fill the maxLength +// LeftPadMaxLine pads a string on the left by a specified amount and pads the +// string on the right to fill the maxLength func LeftPadMaxLine(text string, length, leftPad int) string { - runes := []rune(text) + rightPart := text + scrWidth := runewidth.StringWidth(text) // truncate and ellipse if needed - if len(runes)+leftPad > length { - runes = append(runes[:(length-leftPad-1)], '…') - } - - if len(runes)+leftPad < length { - runes = append(runes, []rune(strings.Repeat(" ", length-len(runes)-leftPad))...) + if scrWidth+leftPad > length { + rightPart = runewidth.Truncate(text, length-leftPad, "…") + } else if scrWidth+leftPad < length { + rightPart = runewidth.FillRight(text, length-leftPad) } return fmt.Sprintf("%s%s", strings.Repeat(" ", leftPad), - string(runes), + rightPart, ) } |