-
Notifications
You must be signed in to change notification settings - Fork 27
/
align.go
63 lines (55 loc) · 1.57 KB
/
align.go
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
package strutil
import (
"strings"
)
//AlignType text align variable like center or left
type AlignType string
// Align type to use with align function
const (
Center AlignType = "center"
Left AlignType = "left"
Right AlignType = "right"
)
// Align aligns string to the "alignTo" which should be one of
// - strutil.Center
// - strutil.Left
// - strutil.Right
func Align(str string, alignTo AlignType, width int) string {
switch alignTo {
case Center:
return AlignCenter(str, width)
case Right:
return AlignRight(str, width)
case Left:
return AlignLeft(str)
default:
return str
}
}
// AlignLeft aligns string to the left. To achieve that it left trims every line.
func AlignLeft(str string) string {
return MapLines(str, func(line string) string {
return strings.TrimLeft(line, " ")
})
}
// AlignRight aligns string to the right. It trims and left pads all the lines
// in the text with space to the size of width.
func AlignRight(str string, width int) string {
return MapLines(str, func(line string) string {
line = strings.Trim(line, " ")
return PadLeft(line, width, " ")
})
}
// AlignCenter centers str. It trims and then centers all the lines in the
// text with space
func AlignCenter(str string, width int) string {
return MapLines(str, func(line string) string {
line = strings.Trim(line, " ")
return CenterText(line, width)
})
}
// CenterText centers the text by adding spaces to the left and right.
// It assumes the text is one line. For multiple lines use AlignCenter.
func CenterText(str string, width int) string {
return Pad(str, width, " ", " ")
}