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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use crate::term_grid::{Color, CursorShape, NamedColor, TerminalGrid};
use vte::{Params, Perform};
/// ANSI escape sequence handler that implements the VTE Perform trait
pub struct AnsiHandler<'a> {
pub grid: &'a mut TerminalGrid,
}
impl<'a> AnsiHandler<'a> {
pub fn new(grid: &'a mut TerminalGrid) -> Self {
Self { grid }
}
/// Parse SGR (Select Graphic Rendition) parameters
fn handle_sgr(&mut self, params: &Params) {
if params.is_empty() {
// Reset all attributes
self.grid.current_attrs = Default::default();
self.grid.current_fg = Color::Named(NamedColor::White);
self.grid.current_bg = Color::Named(NamedColor::Black);
return;
}
let mut iter = params.iter();
while let Some(param) = iter.next() {
match param[0] {
0 => {
// Reset
self.grid.current_attrs = Default::default();
self.grid.current_fg = Color::Named(NamedColor::White);
self.grid.current_bg = Color::Named(NamedColor::Black);
}
1 => self.grid.current_attrs.bold = true,
2 => self.grid.current_attrs.dim = true,
3 => self.grid.current_attrs.italic = true,
4 => self.grid.current_attrs.underline = true,
5 => self.grid.current_attrs.blink = true,
7 => self.grid.current_attrs.reverse = true,
8 => self.grid.current_attrs.hidden = true,
9 => self.grid.current_attrs.strikethrough = true,
22 => {
// Normal intensity (not bold, not dim)
self.grid.current_attrs.bold = false;
self.grid.current_attrs.dim = false;
}
23 => self.grid.current_attrs.italic = false,
24 => self.grid.current_attrs.underline = false,
25 => self.grid.current_attrs.blink = false,
27 => self.grid.current_attrs.reverse = false,
28 => self.grid.current_attrs.hidden = false,
29 => self.grid.current_attrs.strikethrough = false,
// Foreground colors (30-37: normal, 90-97: bright)
30 => self.grid.current_fg = Color::Named(NamedColor::Black),
31 => self.grid.current_fg = Color::Named(NamedColor::Red),
32 => self.grid.current_fg = Color::Named(NamedColor::Green),
33 => self.grid.current_fg = Color::Named(NamedColor::Yellow),
34 => self.grid.current_fg = Color::Named(NamedColor::Blue),
35 => self.grid.current_fg = Color::Named(NamedColor::Magenta),
36 => self.grid.current_fg = Color::Named(NamedColor::Cyan),
37 => self.grid.current_fg = Color::Named(NamedColor::White),
38 => {
// Extended foreground color
if let Some(next_param) = iter.next() {
match next_param[0] {
2 => {
// RGB color
if let (Some(r), Some(g), Some(b)) =
(iter.next(), iter.next(), iter.next())
{
self.grid.current_fg =
Color::Rgb(r[0] as u8, g[0] as u8, b[0] as u8);
}
}
5 => {
// 256-color palette
if let Some(idx) = iter.next() {
self.grid.current_fg = Color::Indexed(idx[0] as u8);
}
}
_ => {}
}
}
}
39 => self.grid.current_fg = Color::Named(NamedColor::White), // Default
// Background colors (40-47: normal, 100-107: bright)
40 => self.grid.current_bg = Color::Named(NamedColor::Black),
41 => self.grid.current_bg = Color::Named(NamedColor::Red),
42 => self.grid.current_bg = Color::Named(NamedColor::Green),
43 => self.grid.current_bg = Color::Named(NamedColor::Yellow),
44 => self.grid.current_bg = Color::Named(NamedColor::Blue),
45 => self.grid.current_bg = Color::Named(NamedColor::Magenta),
46 => self.grid.current_bg = Color::Named(NamedColor::Cyan),
47 => self.grid.current_bg = Color::Named(NamedColor::White),
48 => {
// Extended background color
if let Some(next_param) = iter.next() {
match next_param[0] {
2 => {
// RGB color
if let (Some(r), Some(g), Some(b)) =
(iter.next(), iter.next(), iter.next())
{
self.grid.current_bg =
Color::Rgb(r[0] as u8, g[0] as u8, b[0] as u8);
}
}
5 => {
// 256-color palette
if let Some(idx) = iter.next() {
self.grid.current_bg = Color::Indexed(idx[0] as u8);
}
}
_ => {}
}
}
}
49 => self.grid.current_bg = Color::Named(NamedColor::Black), // Default
// Bright foreground colors (90-97)
90 => self.grid.current_fg = Color::Named(NamedColor::BrightBlack),
91 => self.grid.current_fg = Color::Named(NamedColor::BrightRed),
92 => self.grid.current_fg = Color::Named(NamedColor::BrightGreen),
93 => self.grid.current_fg = Color::Named(NamedColor::BrightYellow),
94 => self.grid.current_fg = Color::Named(NamedColor::BrightBlue),
95 => self.grid.current_fg = Color::Named(NamedColor::BrightMagenta),
96 => self.grid.current_fg = Color::Named(NamedColor::BrightCyan),
97 => self.grid.current_fg = Color::Named(NamedColor::BrightWhite),
// Bright background colors (100-107)
100 => self.grid.current_bg = Color::Named(NamedColor::BrightBlack),
101 => self.grid.current_bg = Color::Named(NamedColor::BrightRed),
102 => self.grid.current_bg = Color::Named(NamedColor::BrightGreen),
103 => self.grid.current_bg = Color::Named(NamedColor::BrightYellow),
104 => self.grid.current_bg = Color::Named(NamedColor::BrightBlue),
105 => self.grid.current_bg = Color::Named(NamedColor::BrightMagenta),
106 => self.grid.current_bg = Color::Named(NamedColor::BrightCyan),
107 => self.grid.current_bg = Color::Named(NamedColor::BrightWhite),
_ => {}
}
}
}
}
impl Perform for AnsiHandler<'_> {
fn print(&mut self, c: char) {
self.grid.put_char(c);
}
fn execute(&mut self, byte: u8) {
match byte {
b'\n' => self.grid.put_char('\n'),
b'\r' => self.grid.put_char('\r'),
b'\t' => self.grid.put_char('\t'),
b'\x08' => self.grid.put_char('\x08'), // Backspace
b'\x07' => {} // Bell (ignore for now)
b'\x0c' => {
// Form feed (Ctrl+L) - clear screen and move cursor to home
self.grid.clear_screen();
self.grid.goto(0, 0);
}
_ => {}
}
}
fn hook(&mut self, _params: &Params, _intermediates: &[u8], _ignore: bool, _c: char) {
// DCS sequences (not commonly used)
}
fn put(&mut self, _byte: u8) {
// Used with hook for DCS sequences
}
fn unhook(&mut self) {
// End of DCS sequence
}
fn osc_dispatch(&mut self, _params: &[&[u8]], _bell_terminated: bool) {
// OSC (Operating System Command) sequences
// Could be used for window title, clipboard, etc.
}
fn csi_dispatch(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char) {
if ignore {
return;
}
// CSI (Control Sequence Introducer) sequences
match (c, intermediates) {
// Cursor movement
('A', []) => {
// Cursor Up
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.move_cursor(0, -(n as isize));
}
('B', []) => {
// Cursor Down
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.move_cursor(0, n as isize);
}
('C', []) => {
// Cursor Forward
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.move_cursor(n as isize, 0);
}
('D', []) => {
// Cursor Back
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.move_cursor(-(n as isize), 0);
}
('E', []) => {
// Cursor Next Line
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.move_cursor(0, n as isize);
self.grid.cursor.x = 0;
}
('F', []) => {
// Cursor Previous Line
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.move_cursor(0, -(n as isize));
self.grid.cursor.x = 0;
}
('G', []) => {
// Cursor Horizontal Absolute
let col = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.cursor.x = col
.saturating_sub(1)
.min(self.grid.cols().saturating_sub(1));
}
('H', []) | ('f', []) => {
// Cursor Position
let mut iter = params.iter();
let row = iter.next().and_then(|p| p.first()).copied().unwrap_or(1) as usize;
let col = iter.next().and_then(|p| p.first()).copied().unwrap_or(1) as usize;
self.grid.goto(col.saturating_sub(1), row.saturating_sub(1));
}
('J', []) => {
// Erase in Display
let mode = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(0);
match mode {
0 => self.grid.erase_to_eos(), // Erase below
1 => {} // Erase above (TODO)
2 | 3 => self.grid.clear_screen(), // Erase all
_ => {}
}
}
('K', []) => {
// Erase in Line
let mode = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(0);
match mode {
0 => self.grid.erase_to_eol(), // Erase to right
1 => {} // Erase to left (TODO)
2 => self.grid.clear_line(), // Erase all
_ => {}
}
}
('L', []) => {
// Insert Lines
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.scroll_down(n);
}
('M', []) => {
// Delete Lines
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.scroll_up(n);
}
('S', []) => {
// Scroll Up
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.scroll_up(n);
}
('T', []) => {
// Scroll Down
let n = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.scroll_down(n);
}
('d', []) => {
// Vertical Position Absolute
let row = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(1) as usize;
self.grid.cursor.y = row
.saturating_sub(1)
.min(self.grid.rows().saturating_sub(1));
}
('h', [b'?']) => {
// DEC Private Mode Set
for param in params.iter() {
match param[0] {
25 => self.grid.cursor.visible = true, // Show cursor
1049 => self.grid.use_alt_screen(), // Alt screen
_ => {}
}
}
}
('l', [b'?']) => {
// DEC Private Mode Reset
for param in params.iter() {
match param[0] {
25 => self.grid.cursor.visible = false, // Hide cursor
1049 => self.grid.use_main_screen(), // Main screen
_ => {}
}
}
}
('m', []) => {
// SGR (Select Graphic Rendition)
self.handle_sgr(params);
}
('r', []) => {
// Set Scroll Region
let mut iter = params.iter();
let top = iter.next().and_then(|p| p.first()).copied().unwrap_or(1) as usize;
let bottom = iter
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(self.grid.rows() as u16) as usize;
self.grid
.set_scroll_region(top.saturating_sub(1), bottom.saturating_sub(1));
}
('s', []) => {
// Save Cursor Position
self.grid.save_cursor();
}
('u', []) => {
// Restore Cursor Position
self.grid.restore_cursor();
}
('q', [b' ']) => {
// Set Cursor Shape (DECSCUSR)
let shape = params
.iter()
.next()
.and_then(|p| p.first())
.copied()
.unwrap_or(0);
self.grid.cursor.shape = match shape {
1 | 2 => CursorShape::Block,
3 | 4 => CursorShape::Underline,
5 | 6 => CursorShape::Bar,
_ => CursorShape::Block,
};
}
_ => {
// Unknown or unimplemented sequence
}
}
}
fn esc_dispatch(&mut self, _intermediates: &[u8], _ignore: bool, _byte: u8) {
// ESC sequences (less common than CSI)
// Could implement things like:
// - ESC M: Reverse index (move up, scroll if needed)
// - ESC c: Reset terminal
}
}