desktop-tui 0.3.2

A desktop environment without graphics (tmux-like)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use appcui::prelude::{CharFlags, Character, Color, Surface};

#[derive(Debug, Clone, Copy)]
struct TerminalState {
    default_foreground_color: Color,
    default_background_color: Color,
    foreground: Color,
    background: Color,
    bold: bool,
    dim: bool,
    italic: bool,
    underline: bool,
    cursor_x: i32,
    cursor_y: i32,
}

impl TerminalState {
    fn reset(&mut self) {
        self.foreground = self.default_foreground_color;
        self.background = self.default_background_color;
        self.bold = false;
        self.dim = false;
        self.italic = false;
        self.underline = false;
        self.cursor_x = 0;
        self.cursor_y = 0;
    }
}

pub struct TerminalParser {
    width: u32,
    height: u32,
    state: TerminalState,
}

impl TerminalParser {
    pub fn new(width: u32, height: u32, default_background_color: Color) -> Self {
        Self {
            width,
            height,
            state: TerminalState {
                default_foreground_color: Color::RGB(255, 255, 255),
                default_background_color,
                foreground: Color::RGB(255, 255, 255),
                background: default_background_color,
                bold: false,
                dim: false,
                italic: false,
                underline: false,
                cursor_x: 0,
                cursor_y: 0,
            }
        }
    }

    pub fn parse_to_surface(&mut self, data: &[u8], mut surface: Surface) -> Surface {
        let text = String::from_utf8_lossy(data);
        let chars: Vec<char> = text.chars().collect();

        let mut i = 0;

        while i < chars.len() {
            if chars[i] == '\u{1b}' && i + 1 < chars.len() && chars[i + 1] == '[' {
                // Re-encode remaining chars into bytes for ANSI parsing
                let slice: String = chars[i..].iter().collect();
                let consumed = self.parse_ansi_sequence(slice.as_bytes(), &mut surface);
                // convert consumed bytes -> consumed chars
                let consumed_chars = String::from_utf8_lossy(&slice.as_bytes()[..consumed])
                    .chars()
                    .count();
                i += consumed_chars;
            }
            else {
                // Handle regular character
                self.write_character(chars[i], &mut surface);
                i += 1;
            }
        }
        
        surface
    }

    pub fn resize(&mut self, width: u32, height: u32) {
        self.width = width;
        self.height = height;
    }

    fn parse_ansi_sequence(&mut self, data: &[u8], surface: &mut Surface) -> usize {
        if data.len() < 3 {
            return 1; // Skip invalid sequence
        }

        let mut i = 2; // Skip '\x1b['
        let mut params = Vec::new();
        let mut current_param = String::new();
        let mut private_mode = false;

        // Handle private mode prefix '?'
        if i < data.len() && data[i] == b'?' {
            private_mode = true;
            i += 1;
        }

        // Parse parameters
        while i < data.len() {
            let byte = data[i];
            match byte {
                b'0'..=b'9' => current_param.push(byte as char),
                b';' => {
                    params.push(current_param.parse::<u32>().unwrap_or(0));
                    current_param.clear();
                }
                b'A'..=b'Z' | b'a'..=b'z' => {
                    // End of sequence
                    if !current_param.is_empty() {
                        params.push(current_param.parse::<u32>().unwrap_or(0));
                    }
                    if private_mode {
                        self.handle_private_ansi_command(byte as char, &params, surface);
                    }
                    else {
                        self.handle_ansi_command(byte as char, &params, surface);
                    }
                    return i + 1;
                }
                _ => break,
            }
            i += 1;
        }

        1 // Skip if we couldn't parse
    }

    fn handle_ansi_command(&mut self, command: char, params: &[u32], surface: &mut Surface) {
        match command {
            'H' | 'f' => {
                // Cursor position
                let row = params.get(0).unwrap_or(&1).saturating_sub(1) as i32;
                let col = params.get(1).unwrap_or(&1).saturating_sub(1) as i32;
                self.state.cursor_x = col.min(self.width as i32 - 1);
                self.state.cursor_y = row.min(self.height as i32 - 1);
            }
            'A' => {
                // Cursor up
                let count = params.get(0).unwrap_or(&1);
                self.state.cursor_y = (self.state.cursor_y - *count as i32).max(0);
            }
            'B' => {
                // Cursor down
                let count = params.get(0).unwrap_or(&1);
                self.state.cursor_y = (self.state.cursor_y + *count as i32).min(self.height as i32 - 1);
            }
            'C' => {
                // Cursor right
                let count = params.get(0).unwrap_or(&1);
                self.state.cursor_x = (self.state.cursor_x + *count as i32).min(self.width as i32 - 1);
            }
            'D' => {
                // Cursor left
                let count = params.get(0).unwrap_or(&1);
                self.state.cursor_x = (self.state.cursor_x - *count as i32).max(0);
            }
            'm' => {
                // SGR (Select Graphic Rendition) - colors and attributes
                if params.is_empty() {
                    // Reset all attributes
                    self.state.reset();
                }
                else {
                    self.handle_sgr_params(params);
                }
            }
            'J' => {
                // Clear screen
                let mode = params.get(0).copied().unwrap_or(0);
                self.handle_erase_display(mode, surface);
            }
            'K' => {
                // Clear line
                let mode = params.get(0).copied().unwrap_or(0);
                self.handle_erase_line(mode, surface);
            }
            _ => {
                // Ignore unknown sequences
            }
        }
    }

    fn handle_private_ansi_command(&mut self, command: char, _params: &[u32], surface: &mut Surface) {
        match command {
            'l' => {
                // Hide cursor
                surface.hide_cursor()
            }
            'h' => {
                // Show cursor
                surface.set_cursor(self.state.cursor_x, self.state.cursor_y);
            }
            _ => {
                // ignore unknown private sequences
            }
        }
    }

    fn handle_erase_display(&mut self, param: u32, surface: &mut Surface) {
        match param {
            0 => {
                // clear from cursor to end of screen
                for y in self.state.cursor_y..self.height as i32 {
                    let start_x = if y == self.state.cursor_y { self.state.cursor_x } else { 0 };
                    for x in start_x..self.width as i32 {
                        surface.write_char(x, y, Character::new(' ', self.state.foreground, self.state.background, CharFlags::None));
                    }
                }
            }
            1 => {
                // clear from beginning of screen to cursor
                for y in 0..=self.state.cursor_y {
                    let end_x = if y == self.state.cursor_y { self.state.cursor_x } else { self.width as i32 - 1 };
                    for x in 0..=end_x {
                        surface.write_char(x, y, Character::new(' ', self.state.foreground, self.state.background, CharFlags::None));
                    }
                }
            }
            2 => {
                // clear entire screen
                surface.clear(Character::new(' ', self.state.foreground, self.state.background, CharFlags::None));
            }
            _ => {}
        }
    }

    fn handle_erase_line(&mut self, param: u32, surface: &mut Surface) {
        match param {
            0 => {
                // clear from cursor to end of line
                for x in self.state.cursor_x..self.width as i32 {
                    surface.write_char(x, self.state.cursor_y, Character::new(' ', self.state.foreground, self.state.background, CharFlags::None));
                }
            }
            1 => {
                // clear from beginning of line to cursor
                for x in 0..=self.state.cursor_x {
                    surface.write_char(x, self.state.cursor_y, Character::new(' ', self.state.foreground, self.state.background, CharFlags::None));
                }
            }
            2 => {
                // clear entire line
                for x in 0..self.width as i32 {
                    surface.write_char(x, self.state.cursor_y, Character::new(' ', self.state.foreground, self.state.background, CharFlags::None));
                }
            }
            _ => {}
        }
    }

    fn handle_sgr_params(&mut self, params: &[u32]) {
        let mut iter = params.iter().copied().peekable();

        while let Some(param) = iter.next() {
            match param {
                0 => self.state.reset(), // Reset
                1 => self.state.bold = true,
                2 => self.state.dim = true,
                3 => self.state.italic = true,
                4 => self.state.underline = true,
                22 => {
                    self.state.bold = false;
                    self.state.dim = false;
                }
                23 => self.state.italic = false,
                24 => self.state.underline = false,

                39 => self.state.foreground = self.state.default_foreground_color,
                49 => self.state.background = self.state.default_background_color,

                // 16-color standard + bright
                30..=37 => self.state.foreground = ansi_16_color(param - 30, false),
                40..=47 => self.state.background = ansi_16_color(param - 40, false),
                90..=97 => self.state.foreground = ansi_16_color(param - 90, true),
                100..=107 => self.state.background = ansi_16_color(param - 100, true),

                // Extended color sequences
                38 | 48 => {
                    let is_foreground = param == 38;

                    if let Some(mode) = iter.next() {
                        match mode {
                            5 => {
                                // 256-color: 38;5;<idx> or 48;5;<idx>
                                if let Some(idx) = iter.next() {
                                    let color = ansi_256_color(idx);
                                    if is_foreground {
                                        self.state.foreground = color;
                                    } else {
                                        self.state.background = color;
                                    }
                                }
                            }
                            2 => {
                                // Truecolor: 38;2;<r>;<g>;<b> or 48;2;<r>;<g>;<b>
                                if let (Some(r), Some(g), Some(b)) = (iter.next(), iter.next(), iter.next()) {
                                    let color = Color::RGB(r as u8, g as u8, b as u8);

                                    if is_foreground {
                                        self.state.foreground = color;
                                    } else {
                                        self.state.background = color;
                                    }
                                }
                            }
                            _ => {}
                        }
                    }
                }

                _ => {
                    // Ignore unknown
                }
            }
        }
    }

    fn write_character(&mut self, ch: char, surface: &mut Surface) {
        match ch {
            '\r' => {
                self.state.cursor_x = 0;
            }
            '\n' => {
                self.state.cursor_x = 0;
                self.state.cursor_y += 1;
                if self.state.cursor_y >= self.height as i32 {
                    self.state.cursor_y = self.height as i32 - 1;
                }
            }
            '\t' => {
                // Tab to next 8-character boundary
                self.state.cursor_x = ((self.state.cursor_x / 8) + 1) * 8;
                self.cursor_forward()
            }
            '\x08' => {
                // Backspace
                if self.state.cursor_x > 0 {
                    self.state.cursor_x -= 1;
                }
            }
            c if c.is_control() => {
                // Ignore other control characters
            }
            c => {
                // Regular printable character
                let mut flags = CharFlags::None;
                if self.state.bold {
                    flags |= CharFlags::Bold;
                }
                if self.state.dim {
                    //flags |= CharFlags::Dim;
                }
                if self.state.italic {
                    flags |= CharFlags::Italic;
                }
                if self.state.underline {
                    flags |= CharFlags::Underline;
                }

                let character = Character::new(c, self.state.foreground, self.state.background, flags);
                surface.write_char(self.state.cursor_x, self.state.cursor_y, character);
                self.cursor_forward();
            }
        }
    }

    pub fn cursor_forward(&mut self) {
        // Advance cursor
        self.state.cursor_x += 1;
        if self.state.cursor_x >= self.width as i32 {
            self.state.cursor_x = 0;
            self.state.cursor_y += 1;
            if self.state.cursor_y >= self.height as i32 {
                self.state.cursor_y = self.height as i32 - 1;
            }
        }
    }
}

/// Map 16 ANSI colors to RGB
fn ansi_16_color(code: u32, bright: bool) -> Color {
    let (r, g, b): (u8, u8, u8) = match code {
        0 => (0, 0, 0),       // Black
        1 => (128, 0, 0),     // Red
        2 => (0, 128, 0),     // Green
        3 => (128, 128, 0),   // Yellow
        4 => (0, 0, 128),     // Blue
        5 => (128, 0, 128),   // Magenta
        6 => (0, 128, 128),   // Cyan
        7 => (192, 192, 192), // White (light gray)
        _ => (255, 255, 255),
    };

    if bright {
        Color::RGB(
            r.saturating_mul(2).min(255),
            g.saturating_mul(2).min(255),
            b.saturating_mul(2).min(255)
        )
    }
    else {
        Color::RGB(r, g, b)
    }
}

/// Map 256-color palette to RGB
fn ansi_256_color(idx: u32) -> Color {
    match idx {
        0..=15 => {
            // Reuse 16 ANSI
            if idx < 8 {
                ansi_16_color(idx, false)
            } else {
                ansi_16_color(idx - 8, true)
            }
        }
        16..=231 => {
            // 6x6x6 color cube
            let n = idx - 16;
            let r = (n / 36) % 6;
            let g = (n / 6) % 6;
            let b = n % 6;
            Color::RGB(
                (r * 51) as u8,
                (g * 51) as u8,
                (b * 51) as u8,
            )
        }
        232..=255 => {
            // Grayscale ramp (24 shades)
            let level = 8 + (idx - 232) * 10;
            Color::RGB(level as u8, level as u8, level as u8)
        }
        _ => Color::RGB(0, 0, 0),
    }
}