1package text
2
3import (
4 "bytes"
5 "fmt"
6 "github.com/mattn/go-runewidth"
7 "strings"
8)
9
10// LeftPadMaxLine pads a string on the left by a specified amount and pads the
11// string on the right to fill the maxLength
12func LeftPadMaxLine(text string, length, leftPad int) string {
13 rightPart := text
14
15 scrWidth := runewidth.StringWidth(text)
16 // truncate and ellipse if needed
17 if scrWidth+leftPad > length {
18 rightPart = runewidth.Truncate(text, length-leftPad, "…")
19 } else if scrWidth+leftPad < length {
20 rightPart = runewidth.FillRight(text, length-leftPad)
21 }
22
23 return fmt.Sprintf("%s%s",
24 strings.Repeat(" ", leftPad),
25 rightPart,
26 )
27}
28
29// LeftPad left pad each line of the given text
30func LeftPad(text string, leftPad int) string {
31 var result bytes.Buffer
32
33 pad := strings.Repeat(" ", leftPad)
34
35 for _, line := range strings.Split(text, "\n") {
36 result.WriteString(pad)
37 result.WriteString(line)
38 result.WriteString("\n")
39 }
40
41 return result.String()
42}