/
jsontree.go
127 lines (105 loc) · 2.34 KB
/
jsontree.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package jsontree
import (
"unicode"
"github.com/nsf/termbox-go"
)
type JsonTree struct {
lines []Line
expandedLines map[int]struct{}
lineMap map[int]int
segments map[int]int
}
type Char struct {
Val rune
Color termbox.Attribute
}
type Line []Char
func New(lines []Line) *JsonTree {
model := &JsonTree{
lines: lines,
expandedLines: map[int]struct{}{},
lineMap: make(map[int]int),
segments: parseSegments(lines),
}
model.recalculateLineMap()
model.ToggleLine(0)
return model
}
func (t *JsonTree) ToggleLine(virtualLn int) {
actualLn := t.lineMap[virtualLn]
if !t.isBeginningOfSegment(actualLn) {
return
}
if t.isExpanded(actualLn) {
delete(t.expandedLines, actualLn)
} else {
t.expandedLines[actualLn] = struct{}{}
}
t.recalculateLineMap()
}
func (t *JsonTree) Line(virtualLn int) Line {
actualLn, ok := t.lineMap[virtualLn]
if ok {
ln := t.lines[actualLn]
if t.isBeginningOfSegment(actualLn) && !t.isExpanded(actualLn) {
ln = t.lineWithDots(actualLn)
}
return ln
}
return nil
}
func (t *JsonTree) lineWithDots(actualLn int) Line {
ln := t.lines[actualLn]
lastChar := ln[len(ln)-1]
ln = append(ln, Char{'…', lastChar.Color})
matchingBrace := t.lines[t.segments[actualLn]]
for _, c := range matchingBrace {
if !unicode.IsSpace(c.Val) {
ln = append(ln, c)
}
}
return ln
}
func (t *JsonTree) isExpanded(actualLn int) bool {
_, isExpanded := t.expandedLines[actualLn]
return isExpanded
}
func (t *JsonTree) isBeginningOfSegment(actualLn int) bool {
_, ok := t.segments[actualLn]
return ok
}
func (t *JsonTree) recalculateLineMap() {
t.lineMap = make(map[int]int)
skipTill := 0
virtualLn := 0
for actualLn := range t.lines {
if actualLn < skipTill {
continue
}
if t.isBeginningOfSegment(actualLn) && !t.isExpanded(actualLn) {
skipTill = t.segments[actualLn] + 1
}
t.lineMap[virtualLn] = actualLn
virtualLn++
}
}
func parseSegments(lines []Line) map[int]int {
resultSegments := make(map[int]int)
bracketBalances := make(map[int]int)
var bal int
for num, line := range lines {
for _, c := range line {
switch c.Val {
case '{', '[':
bal++
bracketBalances[bal] = num
case '}', ']':
if startLn := bracketBalances[bal]; startLn != num {
resultSegments[startLn] = num
}
bal--
}
}
}
return resultSegments
}