1package vt
2
3import renderer "github.com/bnema/vev-vt/core"
4
5func (s *Screen) putRune(r rune) {
6 switch r {
7 case '\a':
8 if s.OnBell != nil {
9 s.OnBell()
10 }
11 return
12 case 0x00, 0x0e, 0x0f, 0x7f:
13 return
14 case '\r':
15 s.Col = 0
16 case '\n', '\v', '\f':
17 if s.frame.Width > 0 && s.Col >= s.frame.Width {
18 s.Col = s.frame.Width - 1
19 }
20 s.buffer.hard(s.Row)
21 s.index()
22 case '\b':
23 if s.Col > 0 {
24 s.Col--
25 }
26 case '\t':
27 // HT moves to the next tab stop without modifying the frame. Clamp at
28 // the right edge rather than triggering deferred wrap or a scroll.
29 if s.frame.Width > 0 {
30 s.Col = min(s.Col+8-s.Col%8, s.frame.Width-1)
31 }
32 default:
33 // Drop C1 control characters (U+0080-U+009F).
34 if r >= 0x80 && r <= 0x9F {
35 return
36 }
37 if r >= 0x20 {
38 s.putPrintable(r)
39 }
40 }
41}
42
43// relinkWrap restores the soft link a deferred wrap just set when index()
44// scrolled the row out from under it. buffer.scrollUp severs the last moved
45// row on purpose — a linefeed there really does stop preceding anything — but
46// a wrap is the one case where that row flowed into the row below it, so the
47// link has to be put back after the rotation moved it to s.Row-1. movedUp is
48// false when nothing survived the scroll, and then there is no row to link.
49func (s *Screen) relinkWrap(movedUp bool) {
50 if movedUp {
51 s.buffer.continueRow(s.Row - 1)
52 }
53}
54
55func (s *Screen) putPrintable(r rune) {
56 w := renderer.RuneWidth(r)
57 // Skip combining marks and zero-width characters.
58 if w == 0 {
59 return
60 }
61 if s.frame.Width == 0 || s.frame.Height == 0 {
62 return
63 }
64 // Deferred wrap: cursor sits past the last column. Disabling DECAWM
65 // cancels a pending wrap and leaves subsequent output at the right margin.
66 if s.Col >= s.frame.Width {
67 if s.autoWrapMode {
68 s.buffer.soft(s.Row)
69 s.Col = 0
70 s.relinkWrap(s.index())
71 } else {
72 s.Col = s.frame.Width - 1
73 }
74 }
75 if s.Row >= s.frame.Height {
76 s.Row = s.frame.Height - 1
77 }
78
79 // A wide rune must never straddle the right edge. If it does not fit on the
80 // current line, clear the abandoned last cell and wrap to the next line.
81 if w == 2 && s.Col+1 >= s.frame.Width {
82 if s.frame.Width < 2 || !s.autoWrapMode {
83 // A wide rune cannot be represented at the right margin without an
84 // orphaned continuation cell. Use the narrow replacement glyph.
85 r = '\uFFFD'
86 w = renderer.RuneWidth(r)
87 } else {
88 // The abandoned cell may itself be the continuation of a pair whose
89 // left half sits one column back; report damage over the full span.
90 cx := s.Col
91 if s.Col > 0 && s.frame.At(s.Col, s.Row).Continuation {
92 cx = s.Col - 1
93 }
94 s.clearWidePairAt(s.Col, s.Row)
95 s.frame.Set(s.Col, s.Row, renderer.BlankCell())
96 s.buffer.truncate(s.Row, cx)
97 s.buffer.continueRow(s.Row)
98 s.record(renderer.Damage{Kind: renderer.DamageText, X: cx, Y: s.Row, Width: s.Col - cx + 1, Height: 1, Count: 1})
99 s.Col = 0
100 s.relinkWrap(s.index())
101 if s.Row >= s.frame.Height {
102 s.Row = s.frame.Height - 1
103 }
104 }
105 }
106
107 insertDamageX := s.Col
108 insertDamageWidth := 0
109 if s.insertMode {
110 leftSplit := s.Col > 0 && s.frame.At(s.Col, s.Row).Continuation
111 s.frame.CopyRow(s.Row, s.Col+w, s.Col, s.frame.Width-s.Col-w)
112 s.frame.FillRow(s.Row, s.Col, s.Col+w, renderer.BlankCell())
113 s.repairRow(s.Row)
114 s.buffer.insert(s.Row, s.Col, w)
115 if leftSplit {
116 insertDamageX = s.Col - 1
117 }
118 insertDamageWidth = s.frame.Width - insertDamageX
119 }
120
121 // Determine the range of cells actually modified, extending over any wide
122 // pair the write lands on so no orphaned half is left behind.
123 lo, hi := s.Col, s.Col+w-1
124 if s.frame.At(s.Col, s.Row).Continuation {
125 lo = s.Col - 1
126 }
127 if right := s.Col + w; right < s.frame.Width && s.frame.At(right, s.Row).Continuation {
128 hi = right
129 }
130 for x := lo; x <= hi; x++ {
131 s.frame.Set(x, s.Row, renderer.BlankCell())
132 }
133 s.frame.Set(s.Col, s.Row, renderer.Cell{Rune: r, Style: s.Style})
134 s.buffer.content(s.Row, s.Col+w)
135 if w == 2 {
136 s.frame.Set(s.Col+1, s.Row, renderer.Cell{Continuation: true, Style: s.Style})
137 }
138 if insertDamageWidth > 0 {
139 lo = min(lo, insertDamageX)
140 hi = max(hi, insertDamageX+insertDamageWidth-1)
141 }
142 s.record(renderer.Damage{Kind: renderer.DamageText, X: lo, Y: s.Row, Width: hi - lo + 1, Height: 1, Count: 1})
143 s.Col += w
144 if !s.autoWrapMode && s.Col >= s.frame.Width {
145 s.Col = s.frame.Width - 1
146 }
147}
148
149// clearWidePairAt blanks both halves of a wide-character pair when the cell at
150// (x,y) is either the left (wide) half or the right (continuation) half. It is
151// O(1) and assumes the pair invariant (a continuation is preceded by its wide
152// left half) holds — which the writer maintains.
153
154func (s *Screen) clearWidePairAt(x, y int) {
155 if x < 0 || x >= s.frame.Width || y < 0 || y >= s.frame.Height {
156 return
157 }
158 if s.frame.At(x, y).Continuation {
159 if x-1 >= 0 {
160 s.frame.Set(x-1, y, renderer.BlankCell())
161 }
162 s.frame.Set(x, y, renderer.BlankCell())
163 return
164 }
165 if x+1 < s.frame.Width && s.frame.At(x+1, y).Continuation {
166 s.frame.Set(x, y, renderer.BlankCell())
167 s.frame.Set(x+1, y, renderer.BlankCell())
168 }
169}
170
171func (s *Screen) eraseCell() renderer.Cell {
172 style := renderer.DefaultStyle()
173 if s.Style.HasBackgroundRGB {
174 style.HasBackgroundRGB = true
175 style.BackgroundRGB = s.Style.BackgroundRGB
176 } else {
177 style.Background = s.Style.Background
178 }
179 return renderer.Cell{Rune: ' ', Style: style}
180}
181
182// clearRow blanks cells [x0,x1) on row y, extending the range to swallow either
183// half of a wide pair that straddles a boundary so no orphan half is left. It
184// returns the actual modified span [start, start+width).
185
186func (s *Screen) clearRow(y, x0, x1 int) (start, width int) {
187 if y < 0 || y >= s.frame.Height {
188 return x0, 0
189 }
190 if x0 < 0 {
191 x0 = 0
192 }
193 if x1 > s.frame.Width {
194 x1 = s.frame.Width
195 }
196 if x0 >= x1 {
197 return x0, 0
198 }
199 // Left boundary: a continuation at x0 means its wide left half sits at x0-1.
200 if x0 > 0 && s.frame.At(x0, y).Continuation {
201 x0--
202 }
203 // Right boundary: a continuation at x1 means its wide left half (at x1-1) is
204 // inside the range and will be blanked, so swallow the continuation too.
205 if x1 < s.frame.Width && s.frame.At(x1, y).Continuation {
206 x1++
207 }
208 fullRow := x0 <= 0 && x1 >= s.frame.Width
209 blank := s.eraseCell()
210 s.frame.FillRow(y, x0, x1, blank)
211 s.buffer.clear(y, x0, x1)
212 if fullRow {
213 s.buffer.rowIDs[y] = s.nextRowIDValue()
214 }
215 return x0, x1 - x0
216}
217
218// repairRow blanks any orphaned wide-character halves on row y: a continuation
219// cell not preceded by its wide left half, or a wide rune not followed by its
220// continuation. Used after erase/insert/delete operations that may split a pair
221// at a boundary or by shifting cells.
222
223func (s *Screen) repairRow(y int) {
224 repairFrameRow(s.frame, y)
225}
226
227func repairFrameRow(frame renderer.Frame, y int) {
228 if y < 0 || y >= frame.Height {
229 return
230 }
231 w := frame.Width
232 for x := range w {
233 c := frame.At(x, y)
234 if c.Continuation {
235 if x == 0 {
236 frame.Set(x, y, renderer.BlankCell())
237 continue
238 }
239 left := frame.At(x-1, y)
240 if left.Continuation || renderer.RuneWidth(left.Rune) != 2 {
241 frame.Set(x, y, renderer.BlankCell())
242 }
243 continue
244 }
245 if renderer.RuneWidth(c.Rune) == 2 {
246 if x+1 >= w || !frame.At(x+1, y).Continuation {
247 frame.Set(x, y, renderer.BlankCell())
248 }
249 }
250 }
251}