/
formatter.go
48 lines (45 loc) · 988 Bytes
/
formatter.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
package internal
import (
"fmt"
"strings"
)
type Formatter struct {
indentUnit int
indent int
builder strings.Builder
}
func (f *Formatter) SetIndentUnit(n int) {
f.indentUnit = n
}
func (f *Formatter) GetIndentUnit() int {
if f.indentUnit <= 0 {
return 2
}
return f.indentUnit
}
func (f *Formatter) Indent() {
f.indent += 1
}
func (f *Formatter) Deindent() {
f.indent -= 1
}
func (f *Formatter) P(format string, a ...interface{}) {
spaces := strings.Repeat(" ", f.indent*f.GetIndentUnit())
f.builder.WriteString(spaces + fmt.Sprintf(format, a...) + "\n")
}
func (f *Formatter) PI(format string, a ...interface{}) {
f.P(format, a...)
f.Indent()
}
func (f *Formatter) PD(format string, a ...interface{}) {
f.Deindent()
f.P(format, a...)
}
func (f *Formatter) PDI(format string, a ...interface{}) {
f.Deindent()
f.P(format, a...)
f.Indent()
}
func (f *Formatter) String() string {
return f.builder.String()
}