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
/
Copy pathpnewline.go
98 lines (90 loc) · 1.87 KB
/
pnewline.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package xsx
import (
"fmt"
"io"
)
type IndentingPrinter struct {
Writer io.Writer
Indent string
nest []rune
indlvl int
needind bool
needsep bool
}
func Indenting(wr io.Writer, indent string) *IndentingPrinter {
res := &IndentingPrinter{
Writer: wr,
Indent: indent}
return res
}
func (p *IndentingPrinter) doIndent() (err error) {
if p.needind {
for i := 0; i < p.indlvl; i++ {
if _, err = p.Writer.Write([]byte(p.Indent)); err != nil {
return err
}
}
p.needind = false
}
return nil
}
func (p *IndentingPrinter) Begin(bracket rune, meta bool) (err error) {
if err = p.doIndent(); err != nil {
return err
}
if p.needsep {
if _, err = p.Writer.Write([]byte(" ")); err != nil {
return err
}
p.needsep = false
}
if meta {
if _, err := p.Writer.Write([]byte(MetaStr)); err != nil {
return err
}
}
if isAny(byte(bracket), ccBegin) {
if _, err := p.Writer.Write([]byte{byte(bracket)}); err != nil {
return err
}
p.nest = append(p.nest, closingRune(bracket))
} else {
return fmt.Errorf("illegal opening bracket '%c'", bracket)
}
return nil
}
func (p *IndentingPrinter) End() (err error) {
if err = p.doIndent(); err != nil {
return err
}
b := p.nest[len(p.nest)-1]
p.nest = p.nest[:len(p.nest)-1]
_, err = p.Writer.Write([]byte(string(b)))
p.needsep = true
return err
}
func (p *IndentingPrinter) Atom(atom string, meta bool, quote QuoteMode) (err error) {
if err = p.doIndent(); err != nil {
return err
}
if p.needsep {
if _, err = p.Writer.Write([]byte(" ")); err != nil {
return err
}
} else {
p.needsep = true
}
return printAtom(p.Writer, atom, meta, quote)
}
func (p *IndentingPrinter) Newline(count int, indent int) (err error) {
for count > 0 {
if _, err = p.Writer.Write([]byte("\n")); err != nil {
return err
}
count--
}
p.indlvl += indent
p.needind = true
p.needsep = false
return err
}