~emersion/drmdb

ref: 9e10006451cd753ab55cc2fd062b1387d3e281bf drmdb/treefmt/memory.go -rw-r--r-- 741 bytes
9e100064Simon Ser Merge all property specs 1 year, 5 months ago
                                                                                
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
package treefmt

import (
	"fmt"
)

type Memory struct {
	Text     string
	Children []Memory
}

type MemoryFormatter struct {
	m *Memory
}

// NewMemoryFormatter pretty-prints a tree in an in-memory data structure.
func NewMemoryFormatter() *MemoryFormatter {
	return &MemoryFormatter{m: &Memory{}}
}

func (mf *MemoryFormatter) NewChild() Formatter {
	if len(mf.m.Children) == 0 {
		panic("treefmt: NewChild called before Printf")
	}
	m := &mf.m.Children[len(mf.m.Children)-1]
	return &MemoryFormatter{m: m}
}

func (mf *MemoryFormatter) Printf(format string, v ...interface{}) {
	t := fmt.Sprintf(format, v...)
	mf.m.Children = append(mf.m.Children, Memory{Text: t})
}

func (mf *MemoryFormatter) Tree() []Memory {
	return mf.m.Children
}