-
Notifications
You must be signed in to change notification settings - Fork 0
/
leveled.go
63 lines (54 loc) · 1.14 KB
/
leveled.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 writerutil
import (
"fmt"
"io"
"strings"
)
// LeveledOutput :
type LeveledOutput struct {
W io.Writer
i int
prefix string
}
// Indent :
func (lw *LeveledOutput) Indent() {
lw.i++
lw.prefix = strings.Repeat(" ", lw.i)
}
// UnIndent :
func (lw *LeveledOutput) UnIndent() {
lw.i--
lw.prefix = strings.Repeat(" ", lw.i)
}
// WithBlock :
func (lw *LeveledOutput) WithBlock(prefix string, callback func()) {
lw.Println(prefix + " {")
lw.Indent()
callback()
lw.UnIndent()
lw.Println("}")
}
// WithIfAndElse :
func (lw *LeveledOutput) WithIfAndElse(prefix string, callback func(), callback2 func()) {
lw.Println("if " + prefix + " {")
lw.Indent()
callback()
lw.UnIndent()
lw.Println("} else {")
lw.Indent()
callback2()
lw.UnIndent()
lw.Println("}")
}
// Newline :
func (lw *LeveledOutput) Newline() (int, error) {
return io.WriteString(lw.W, "\n")
}
// Println :
func (lw *LeveledOutput) Println(s string) (int, error) {
return fmt.Fprintf(lw.W, "%s%s\n", lw.prefix, s)
}
// Printf :
func (lw *LeveledOutput) Printf(format string, args ...interface{}) (int, error) {
return fmt.Fprintf(lw.W, (lw.prefix + format), args...)
}