1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package text
import (
"strings"
)
type Alignment int
const (
NoAlign Alignment = iota
AlignLeft
AlignCenter
AlignRight
)
// LineAlign align the given line as asked and apply the needed padding to match the given
// lineWidth, while ignoring the terminal escape sequences.
// If the given lineWidth is too small to fit the given line, it's returned without
// padding, overflowing lineWidth.
func LineAlign(line string, lineWidth int, align Alignment) string {
switch align {
case NoAlign:
return line
case AlignLeft:
return LineAlignLeft(line, lineWidth)
case AlignCenter:
return LineAlignCenter(line, lineWidth)
case AlignRight:
return LineAlignRight(line, lineWidth)
}
panic("unknown alignment")
}
// LineAlignLeft align the given line on the left while ignoring the terminal escape sequences.
// If the given lineWidth is too small to fit the given line, it's returned without
// padding, overflowing lineWidth.
func LineAlignLeft(line string, lineWidth int) string {
return TrimSpace(line)
}
// LineAlignCenter align the given line on the center and apply the needed left
// padding, while ignoring the terminal escape sequences.
// If the given lineWidth is too small to fit the given line, it's returned without
// padding, overflowing lineWidth.
func LineAlignCenter(line string, lineWidth int) string {
trimmed := TrimSpace(line)
totalPadLen := lineWidth - Len(trimmed)
if totalPadLen < 0 {
totalPadLen = 0
}
pad := strings.Repeat(" ", totalPadLen/2)
return pad + trimmed
}
// LineAlignRight align the given line on the right and apply the needed left
// padding to match the given lineWidth, while ignoring the terminal escape sequences.
// If the given lineWidth is too small to fit the given line, it's returned without
// padding, overflowing lineWidth.
func LineAlignRight(line string, lineWidth int) string {
trimmed := TrimSpace(line)
padLen := lineWidth - Len(trimmed)
if padLen < 0 {
padLen = 0
}
pad := strings.Repeat(" ", padLen)
return pad + trimmed
}
|