forked from elves/elvish
/
render.go
326 lines (274 loc) · 8 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package edit
import (
"strings"
"unicode/utf8"
"github.com/elves/elvish/edit/highlight"
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/util"
)
type renderer interface {
render(b *buffer)
}
func render(r renderer, width int) *buffer {
if r == nil {
return nil
}
b := newBuffer(width)
r.render(b)
return b
}
type modeLineRenderer struct {
title string
filter string
}
func (ml modeLineRenderer) render(b *buffer) {
b.writes(ml.title, styleForMode.String())
b.writePadding(1, "")
b.writes(ml.filter, styleForFilter.String())
b.dot = b.cursor()
}
type modeLineWithScrollBarRenderer struct {
modeLineRenderer
n, low, high int
}
func (ml modeLineWithScrollBarRenderer) render(b *buffer) {
ml.modeLineRenderer.render(b)
scrollbarWidth := b.width - cellsWidth(b.lines[len(b.lines)-1]) - 2
if scrollbarWidth >= 3 {
b.writePadding(1, "")
writeHorizontalScrollbar(b, ml.n, ml.low, ml.high, scrollbarWidth)
}
}
type placeholderRenderer string
func (lp placeholderRenderer) render(b *buffer) {
b.writes(util.TrimWcwidth(string(lp), b.width), "")
}
type listingRenderer struct {
lines []ui.Styled
}
func (ls listingRenderer) render(b *buffer) {
for i, line := range ls.lines {
if i > 0 {
b.newline()
}
b.writes(util.ForceWcwidth(line.Text, b.width), line.Styles.String())
}
}
type listingWithScrollBarRenderer struct {
listingRenderer
n, low, high, height int
}
func (ls listingWithScrollBarRenderer) render(b *buffer) {
b1 := render(ls.listingRenderer, b.width-1)
b.extendRight(b1, 0)
scrollbar := renderScrollbar(ls.n, ls.low, ls.high, ls.height)
b.extendRight(scrollbar, b.width-1)
}
type navRenderer struct {
maxHeight int
fwParent, fwCurrent, fwPreview int
parent, current, preview renderer
}
func makeNavRenderer(h int, w1, w2, w3 int, r1, r2, r3 renderer) renderer {
return &navRenderer{h, w1, w2, w3, r1, r2, r3}
}
func (nr *navRenderer) render(b *buffer) {
margin := navigationListingColMargin
w := b.width - margin*2
ws := distributeWidths(w,
[]float64{parentColumnWeight, currentColumnWeight, previewColumnWeight},
[]int{nr.fwParent, nr.fwCurrent, nr.fwPreview},
)
wParent, wCurrent, wPreview := ws[0], ws[1], ws[2]
bParent := render(nr.parent, wParent)
b.extendRight(bParent, 0)
bCurrent := render(nr.current, wCurrent)
b.extendRight(bCurrent, wParent+margin)
if wPreview > 0 {
bPreview := render(nr.preview, wPreview)
b.extendRight(bPreview, wParent+wCurrent+2*margin)
}
}
// linesRenderer renders lines with a uniform style.
type linesRenderer struct {
lines []string
style string
}
func (nr linesRenderer) render(b *buffer) {
b.writes(strings.Join(nr.lines, "\n"), "")
}
// cmdlineRenderer renders the command line, including the prompt, the user's
// input and the rprompt.
type cmdlineRenderer struct {
prompt []*ui.Styled
line string
styling *highlight.Styling
dot int
rprompt []*ui.Styled
hasComp bool
compBegin int
compEnd int
compText string
hasHist bool
histBegin int
histText string
}
func newCmdlineRenderer(p []*ui.Styled, l string, s *highlight.Styling, d int, rp []*ui.Styled) *cmdlineRenderer {
return &cmdlineRenderer{prompt: p, line: l, styling: s, dot: d, rprompt: rp}
}
func (clr *cmdlineRenderer) setComp(b, e int, t string) {
clr.hasComp = true
clr.compBegin, clr.compEnd, clr.compText = b, e, t
}
func (clr *cmdlineRenderer) setHist(b int, t string) {
clr.hasHist = true
clr.histBegin, clr.histText = b, t
}
func (clr *cmdlineRenderer) render(b *buffer) {
b.eagerWrap = true
b.writeStyleds(clr.prompt)
// If the prompt takes less than half of a line, set the indent.
if len(b.lines) == 1 && b.col*2 < b.width {
b.indent = b.col
}
// i keeps track of number of bytes written.
i := 0
applier := clr.styling.Apply()
// nowAt is called at every rune boundary.
nowAt := func(i int) {
applier.At(i)
if clr.hasComp && i == clr.compBegin {
b.writes(clr.compText, styleForCompleted.String())
}
if i == clr.dot {
b.dot = b.cursor()
}
}
nowAt(0)
for _, r := range clr.line {
if clr.hasComp && clr.compBegin <= i && i < clr.compEnd {
// Do nothing. This part is replaced by the completion candidate.
} else {
b.write(r, applier.Get())
}
i += utf8.RuneLen(r)
nowAt(i)
if clr.hasHist && i == clr.histBegin {
break
}
}
if clr.hasHist {
// Put the rest of current history and position the cursor at the
// end of the line.
b.writes(clr.histText, styleForCompletedHistory.String())
b.dot = b.cursor()
}
// Write rprompt
if len(clr.rprompt) > 0 {
padding := b.width - b.col
for _, s := range clr.rprompt {
padding -= util.Wcswidth(s.Text)
}
if padding >= 1 {
b.eagerWrap = false
b.writePadding(padding, "")
b.writeStyleds(clr.rprompt)
}
}
}
// editorRenderer renders the entire editor.
type editorRenderer struct {
*editorState
height int
bufNoti *buffer
}
func (er *editorRenderer) render(buf *buffer) {
height, width, es := er.height, buf.width, er.editorState
var bufNoti, bufLine, bufMode, bufTips, bufListing *buffer
// butNoti
if len(es.notifications) > 0 {
bufNoti = render(linesRenderer{es.notifications, ""}, width)
es.notifications = nil
}
// bufLine
clr := newCmdlineRenderer(es.promptContent, es.line, es.styling, es.dot, es.rpromptContent)
// TODO(xiaq): Instead of doing a type switch, expose an API for modes to
// modify the text (and mark their part as modified).
switch es.mode.(type) {
case *completion:
c := es.completion
clr.setComp(c.begin, c.end, c.selectedCandidate().code)
case *hist:
begin := len(es.hist.Prefix())
clr.setHist(begin, es.hist.CurrentCmd()[begin:])
}
bufLine = render(clr, width)
// bufMode
bufMode = render(es.mode.ModeLine(), width)
// bufTips
// TODO tips is assumed to contain no newlines.
if len(es.tips) > 0 {
bufTips = render(linesRenderer{es.tips, styleForTip.String()}, width)
}
hListing := 0
// Trim lines and determine the maximum height for bufListing
// TODO come up with a UI to tell the user that something is not shown.
switch {
case height >= buffersHeight(bufNoti, bufLine, bufMode, bufTips):
hListing = height - buffersHeight(bufLine, bufMode, bufTips)
case height >= buffersHeight(bufNoti, bufLine, bufTips):
bufMode = nil
case height >= buffersHeight(bufNoti, bufLine):
bufMode = nil
if bufTips != nil {
bufTips.trimToLines(0, height-buffersHeight(bufNoti, bufLine))
}
case height >= buffersHeight(bufLine):
bufTips, bufMode = nil, nil
if bufNoti != nil {
n := len(bufNoti.lines)
bufNoti.trimToLines(n-(height-buffersHeight(bufLine)), n)
}
case height >= 1:
bufNoti, bufTips, bufMode = nil, nil, nil
dotLine := bufLine.dot.line
bufLine.trimToLines(dotLine+1-height, dotLine+1)
default:
// Broken terminal. Still try to render one line of bufLine.
bufNoti, bufTips, bufMode = nil, nil, nil
dotLine := bufLine.dot.line
bufLine.trimToLines(dotLine, dotLine+1)
}
// bufListing.
if hListing > 0 {
if lister, ok := es.mode.(ListRenderer); ok {
bufListing = lister.ListRender(width, hListing)
} else if lister, ok := es.mode.(Lister); ok {
bufListing = render(lister.List(hListing), width)
}
// XXX When in completion mode, we re-render the mode line, since the
// scrollbar in the mode line depends on completion.lastShown which is
// only known after the listing has been rendered. Since rendering the
// scrollbar never adds additional lines to bufMode, we may do this
// without recalculating the layout.
if _, ok := es.mode.(*completion); ok {
bufMode = render(es.mode.ModeLine(), width)
}
}
if logWriterDetail {
logger.Printf("bufLine %d, bufMode %d, bufTips %d, bufListing %d",
buffersHeight(bufLine), buffersHeight(bufMode), buffersHeight(bufTips), buffersHeight(bufListing))
}
// XXX
buf.lines = nil
// Combine buffers (reusing bufLine)
buf.extend(bufLine, true)
cursorOnModeLine := false
if coml, ok := es.mode.(CursorOnModeLiner); ok {
cursorOnModeLine = coml.CursorOnModeLine()
}
buf.extend(bufMode, cursorOnModeLine)
buf.extend(bufTips, false)
buf.extend(bufListing, false)
er.bufNoti = bufNoti
}