aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/left_padded.go
diff options
context:
space:
mode:
authorYang Zhang <yang_zhang@iapcm.ac.cn>2018-12-26 22:49:25 +0800
committerYang Zhang <yang_zhang@iapcm.ac.cn>2018-12-26 22:55:16 +0800
commit3fa2d15fb899c937900083fd7de696599371ce47 (patch)
tree7d8fd43e4eca7dfde65567714d3d35a6bfaf6a65 /util/text/left_padded.go
parent8a6a8055d723e523d9943244b042c778d75b02cc (diff)
downloadgit-bug-3fa2d15fb899c937900083fd7de696599371ce47.tar.gz
Implement displaying CJK contents
Diffstat (limited to 'util/text/left_padded.go')
-rw-r--r--util/text/left_padded.go19
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,
)
}