-
Notifications
You must be signed in to change notification settings - Fork 1
/
padding.go
49 lines (43 loc) · 896 Bytes
/
padding.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
package util
import (
"math"
"strings"
)
const (
ALIGN_LEFT = 0
ALIGN_CENTER = 1
ALIGN_RIGHT = 2
)
func Pad(s, pad string, width int, align int) string {
switch align {
case ALIGN_CENTER:
return PadCenter(s, pad, width)
case ALIGN_RIGHT:
return PadLeft(s, pad, width)
default:
return PadRight(s, pad, width)
}
}
func PadRight(s, pad string, width int) string {
gap := width - len(s)
if gap > 0 {
return s + strings.Repeat(string(pad), gap)
}
return s
}
func PadLeft(s, pad string, width int) string {
gap := width - len(s)
if gap > 0 {
return strings.Repeat(string(pad), gap) + s
}
return s
}
func PadCenter(s, pad string, width int) string {
gap := width - len(s)
if gap > 0 {
gapLeft := int(math.Ceil(float64(gap / 2)))
gapRight := gap - gapLeft
return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight)
}
return s
}