diff options
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, ) } |