/
termrenderer.go
228 lines (209 loc) · 6.4 KB
/
termrenderer.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
package shell
import (
"bytes"
"fmt"
"image"
"image/color"
"image/draw"
"os"
"github.com/driusan/de/demodel"
"github.com/driusan/de/renderer"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
// colours to use for ANSI escape codes
var (
ansiBlack image.Image = &image.Uniform{renderer.TextColour}
ansiRed image.Image = &image.Uniform{color.RGBA{0x80, 0, 0, 0xff}}
ansiGreen image.Image = &image.Uniform{color.RGBA{0, 0x80, 0, 0xff}}
ansiYellow image.Image = &image.Uniform{color.RGBA{0x80, 0x80, 0, 0xff}}
ansiBlue image.Image = &image.Uniform{color.RGBA{0, 0, 0x80, 0xff}}
ansiMagenta image.Image = &image.Uniform{color.RGBA{0x80, 0, 0x80, 0xff}}
ansiCyan image.Image = &image.Uniform{color.RGBA{0, 0x80, 0x80, 0xff}}
ansiGray image.Image = &image.Uniform{color.RGBA{0x80, 0x80, 0x80, 0xff}}
)
func init() {
renderer.RegisterRenderer("xterm", &TerminalRenderer{})
}
type TerminalRenderer struct {
renderer.DefaultSizeCalcer
renderer.DefaultImageMapper
}
func (rd *TerminalRenderer) InvalidateCache() {
rd.DefaultSizeCalcer.InvalidateCache()
rd.DefaultImageMapper.InvalidateCache()
}
func (rd *TerminalRenderer) CanRender(buf *demodel.CharBuffer) bool {
return true
}
func (rd *TerminalRenderer) RenderInto(dst draw.Image, buf *demodel.CharBuffer, viewport image.Rectangle) error {
metrics := renderer.MonoFontFace.Metrics()
bounds := dst.Bounds()
var foreground, background color.Color = renderer.TextColour, renderer.NormalBackground
writer := font.Drawer{
Dst: dst,
Src: &image.Uniform{foreground},
Dot: fixed.P(bounds.Min.X, bounds.Min.Y+metrics.Ascent.Floor()),
Face: renderer.MonoFontFace,
}
runes := bytes.Runes(buf.Buffer)
// Used for calculating the size of a tab.
_, MglyphWidth, _ := renderer.MonoFontFace.GlyphBounds('M')
escapeStart := -1
col := 0
for i, r := range runes {
// Do this inside the loop anyways, in case someone changes it to a
// variable width font..
_, glyphWidth, _ := renderer.MonoFontFace.GlyphBounds(r)
switch r {
case 0x1B:
escapeStart = i
continue
case 0x8:
// Backspace. Just move the cursor backspace, because
// backspace and writing over it seems to be how OpenBSD
// indicates bold in its man pages.
writer.Dot.X -= glyphWidth
continue
}
if escapeStart >= 0 {
switch runes[escapeStart+1] {
case ']':
switch r {
case '\b', '\007', '\233':
escapeStart = -1
}
continue
case '[':
if r >= 64 && r <= 126 && i != escapeStart+1 {
switch r {
case 'm':
// escapeStart is the \033 character,
// +1 is the [, after that is what
// we care about.
args := string(runes[escapeStart+2 : i])
switch args {
case "0", "":
// Reset everything
writer.Face = renderer.MonoFontFace
background = renderer.NormalBackground
writer.Src = ansiBlack
case "1":
writer.Face = renderer.MonoFontFaceBold
case "22":
writer.Face = renderer.MonoFontFace
case "30", "39":
writer.Src = ansiBlack
case "31":
writer.Src = ansiRed
case "32":
writer.Src = ansiGreen
case "33":
writer.Src = ansiYellow
case "34":
writer.Src = ansiBlue
case "35":
writer.Src = ansiMagenta
case "36":
writer.Src = ansiCyan
case "37":
writer.Src = ansiGray
case "38":
fmt.Fprintf(os.Stderr, "ANSI extended colours not implemented\n")
}
case 'K':
args := string(runes[escapeStart+2 : i])
fmt.Fprintf(os.Stderr, "ANSI command sequence K args: %s\n", args)
switch args {
case "", "0":
fmt.Fprintf(os.Stderr, "ANSI command sequence clear to end of line: %s\n", args)
case "1":
fmt.Fprintf(os.Stderr, "ANSI command sequence clear to beginning of line: %s\n", args)
case "2":
fmt.Fprintf(os.Stderr, "ANSI command sequence clear entire line: %s\n", args)
col = 0
default:
fmt.Fprintf(os.Stderr, "Invalid ANSI command sequence K, args: %s\n", args)
}
default:
fmt.Fprintf(os.Stderr, "ANSI command sequence %c not yet implemented.\n", r)
}
escapeStart = -1
}
continue
}
}
runeRectangle := image.Rectangle{}
runeRectangle.Min.X = writer.Dot.X.Ceil()
runeRectangle.Min.Y = writer.Dot.Y.Ceil() - metrics.Ascent.Floor() + 1
switch r {
case '\t':
tw := 8 - (col % 8)
runeRectangle.Max.X = runeRectangle.Min.X + tw*MglyphWidth.Ceil()
case '\n':
runeRectangle.Max.X = viewport.Max.X
case 27: // ESC
continue
default:
runeRectangle.Max.X = runeRectangle.Min.X + glyphWidth.Ceil()
}
runeRectangle.Max.Y = runeRectangle.Min.Y + metrics.Height.Ceil() + 1
if runeRectangle.Min.Y > viewport.Max.Y {
// exit the loop early, we've already gotten past the part that we care about.
return nil
}
// Don't draw or calculate the image map if we're outside of the viewport. We can't
// break out, because things not being drawn might still affect the rendering (ie.
// the start of the screen might be in the middle of a comment that needs to be syntax
// highlighted)
// im.IMap = append(im.IMap, renderer.ImageLoc{runeRectangle, uint(i)})
if runeRectangle.Intersect(viewport) != image.ZR {
if uint(i) >= buf.Dot.Start && uint(i) <= buf.Dot.End {
// it's in dot, so highlight the background (unless it's outside of the viewport
// clipping rectangle)
draw.Draw(
dst,
image.Rectangle{
runeRectangle.Min.Sub(viewport.Min),
runeRectangle.Max.Sub(viewport.Min)},
&image.Uniform{renderer.TextHighlight},
image.ZP,
draw.Src,
)
} else {
draw.Draw(
dst,
image.Rectangle{
runeRectangle.Min.Sub(viewport.Min),
runeRectangle.Max.Sub(viewport.Min)},
&image.Uniform{background},
image.ZP,
draw.Src,
)
}
}
switch r {
case '\t':
tw := 8 - (col % 8)
writer.Dot.X += glyphWidth * fixed.Int26_6(tw)
col += tw
continue
case '\n':
writer.Dot.Y += metrics.Height
writer.Dot.X = fixed.I(bounds.Min.X)
col = 0
continue
}
if runeRectangle.Max.Y > viewport.Min.Y {
// as a hack to align viewport.Min with dst.Min, we subtract it from
// Dot before drawing, then add it back.
writer.Dot.X -= fixed.I(viewport.Min.X)
writer.Dot.Y -= fixed.I(viewport.Min.Y)
writer.DrawString(string(r))
writer.Dot.X += fixed.I(viewport.Min.X)
writer.Dot.Y += fixed.I(viewport.Min.Y)
}
col++
}
return nil
}