aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/left_padded.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-01-09 19:47:42 +0100
committerGitHub <noreply@github.com>2019-01-09 19:47:42 +0100
commit6bfbf36cd96408ac44899a19280ed08aa3bcf327 (patch)
tree87f784e9018ba9d167d84243030ce23f4b6e87f4 /util/text/left_padded.go
parent44643efedc7c4f2b83e649ba102bd52cb35ec287 (diff)
parent8ea5c6b41de8a0ccdff245456c317a2ad98cd630 (diff)
downloadgit-bug-6bfbf36cd96408ac44899a19280ed08aa3bcf327.tar.gz
Merge pull request #86 from ProgramFan/master
Implement CJK support in termui
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..eae65d34 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)
+ var rightPart string = 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,
)
}