-
Notifications
You must be signed in to change notification settings - Fork 296
/
render.go
186 lines (149 loc) · 3.63 KB
/
render.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package rty
import (
"github.com/gdamore/tcell"
)
func NewRTY(screen tcell.Screen, handler ErrorHandler) RTY {
return &rty{
screen: screen,
state: make(renderState),
handler: handler,
}
}
type rty struct {
screen tcell.Screen
state renderState
handler ErrorHandler
}
type renderState map[string]interface{}
func (r *rty) Render(c Component) {
g := &renderGlobals{
prev: r.state,
next: make(renderState),
}
f := renderFrame{
canvas: newScreenCanvas(r.screen, r.handler),
globals: g,
handler: r.handler,
}
f.RenderChild(c)
r.screen.Show()
r.state = g.next
}
func (r *rty) RegisterElementScroll(name string, children []string) (l *ElementScrollLayout, selectedChild string) {
r.state[name], selectedChild = adjustElementScroll(r.state[name], children)
return &ElementScrollLayout{
name: name,
}, selectedChild
}
func (r *rty) ElementScroller(name string) ElementScroller {
st, ok := r.state[name]
if !ok {
st = &ElementScrollState{}
r.state[name] = st
}
return &ElementScrollController{state: st.(*ElementScrollState)}
}
func (r *rty) TextScroller(name string) TextScroller {
st, ok := r.state[name]
if !ok {
st = &TextScrollState{}
r.state[name] = st
}
return &TextScrollController{state: st.(*TextScrollState)}
}
type renderGlobals struct {
prev renderState
next renderState
}
func (g *renderGlobals) Get(key string) interface{} {
return g.prev[key]
}
func (g *renderGlobals) Set(key string, d interface{}) {
g.next[key] = d
}
type renderFrame struct {
canvas Canvas
style tcell.Style
globals *renderGlobals
handler ErrorHandler
}
var _ Writer = renderFrame{}
func (f renderFrame) SetContent(x int, y int, mainc rune, combc []rune) {
if mainc == 0 {
mainc = ' '
}
f.canvas.SetContent(x, y, mainc, combc, f.style)
}
func (f renderFrame) Fill() (Writer, error) {
width, height := f.canvas.Size()
var err error
f.canvas, err = newSubCanvas(f.canvas, 0, 0, width, height, f.style, f.handler)
if err != nil {
return nil, err
}
return f, nil
}
func (f renderFrame) Divide(x, y, width, height int) (Writer, error) {
var err error
f.canvas, err = newSubCanvas(f.canvas, x, y, width, height, f.style, f.handler)
if err != nil {
return nil, err
}
return f, nil
}
func (f renderFrame) Foreground(c tcell.Color) Writer {
f.style = f.style.Foreground(c)
return f
}
func (f renderFrame) Background(c tcell.Color) Writer {
f.style = f.style.Background(c)
return f
}
func (f renderFrame) Invert() Writer {
f.style = f.style.Reverse(true)
return f
}
func (f renderFrame) error(err error) {
f.handler.Errorf("%v", err)
}
func (f renderFrame) RenderChild(c Component) int {
width, height := f.canvas.Size()
if err := c.Render(f, width, height); err != nil {
f.error(err)
}
_, height = f.canvas.Close()
return height
}
func (f renderFrame) RenderChildInTemp(c Component) Canvas {
width, _ := f.canvas.Size()
tmp := newTempCanvas(width, GROW, f.style, f.handler)
f.canvas = tmp
if err := c.Render(f, width, GROW); err != nil {
f.error(err)
}
tmp.Close()
return tmp
}
func (f renderFrame) Embed(src Canvas, srcY int, srcHeight int) error {
width, destLines := f.canvas.Size()
numLines := destLines
if srcHeight < destLines {
numLines = srcHeight
}
for i := 0; i < numLines; i++ {
for j := 0; j < width; j++ {
mainc, combc, style, _ := src.GetContent(j, srcY+i)
f.canvas.SetContent(j, i, mainc, combc, style)
}
}
return nil
}
func (f renderFrame) RenderStateful(c StatefulComponent, name string) {
prev := f.globals.Get(name)
width, height := f.canvas.Size()
next, err := c.RenderStateful(f, prev, width, height)
if err != nil {
f.error(err)
}
f.globals.Set(name, next)
}