forked from diamondburned/gotk4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
42 lines (35 loc) · 779 Bytes
/
block.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
package pen
import (
"fmt"
"strings"
)
// Block writes a scoped Go block.
type Block struct {
str strings.Builder
nest []Block
}
// NewBlock creates a new preallocated block.
func NewBlock() *Block {
b := Block{}
b.str.Grow(4096)
return &b
}
func (b *Block) EmptyLine() { b.Line("") }
// Line writes a line.
func (b *Block) Line(line string) {
b.str.WriteString(line)
b.str.WriteByte('\n')
}
// Linef writes a line using Printf. If v is none, then f is taken literally.
func (b *Block) Linef(f string, v ...interface{}) {
if len(v) == 0 {
b.str.WriteString(f)
} else {
fmt.Fprintf(&b.str, f, v...)
}
b.str.WriteByte('\n')
}
// String returns the block.
func (b *Block) String() string {
return "{\n" + strings.TrimSuffix(b.str.String(), "\n") + "\n}"
}