This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppretty.go
80 lines (72 loc) · 1.47 KB
/
ppretty.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package xsx
import (
"errors"
"fmt"
"io"
)
// PrettyPrinter currently is a quick'n'dirty impl that is about to change
type PrettyPrinter struct {
Writer io.Writer
ilvl int
istr []byte
ends []rune
}
func Pretty(wr io.Writer, indent string) *PrettyPrinter {
res := &PrettyPrinter{
Writer: wr,
istr: []byte(indent),
}
return res
}
func (pp *PrettyPrinter) indent() (err error) {
for i := pp.ilvl; i > 0; i-- {
_, err = pp.Writer.Write(pp.istr)
if err != nil {
return err
}
}
return err
}
func (pp *PrettyPrinter) Begin(bracket rune, meta bool) (err error) {
err = pp.indent()
if err != nil {
return err
}
if meta {
_, err = pp.Writer.Write(metaAtom)
if err != nil {
return err
}
}
_, err = fmt.Fprintf(pp.Writer, "%c\n", bracket)
pp.ilvl++
pp.ends = append(pp.ends, closingRune(bracket))
return err
}
func (pp *PrettyPrinter) End() (err error) {
if len(pp.ends) == 0 {
return errors.New("nothing to end")
}
end := pp.ends[len(pp.ends)-1]
pp.ends = pp.ends[:len(pp.ends)-1]
pp.ilvl--
err = pp.indent()
if err != nil {
return err
}
_, err = fmt.Fprintf(pp.Writer, "%c\n", end)
return err
}
func (pp *PrettyPrinter) Atom(atom string, meta bool, quote QuoteMode) (err error) {
err = pp.indent()
if err != nil {
return err
}
err = printAtom(pp.Writer, atom, meta, quote)
if err != nil {
return err
}
_, err = fmt.Fprintln(pp.Writer)
return err
}
func (p *PrettyPrinter) Newline(count int, indent int) error { return nil }