rust-expect 0.1.0

Next-generation Expect-style terminal automation library for Rust
Documentation
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
//! Screen buffer integration for sessions.
//!
//! This module provides integration between sessions and the screen buffer,
//! allowing for terminal emulation and screen-based operations.

use crate::types::Dimensions;

/// Screen position (row, column).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Position {
    /// Row (0-indexed).
    pub row: usize,
    /// Column (0-indexed).
    pub col: usize,
}

impl Position {
    /// Create a new position.
    #[must_use]
    pub const fn new(row: usize, col: usize) -> Self {
        Self { row, col }
    }
}

/// A rectangular region of the screen.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Region {
    /// Top-left corner.
    pub start: Position,
    /// Bottom-right corner (exclusive).
    pub end: Position,
}

impl Region {
    /// Create a new region.
    #[must_use]
    pub const fn new(start: Position, end: Position) -> Self {
        Self { start, end }
    }

    /// Create a region from coordinates.
    #[must_use]
    pub const fn from_coords(
        start_row: usize,
        start_col: usize,
        end_row: usize,
        end_col: usize,
    ) -> Self {
        Self {
            start: Position::new(start_row, start_col),
            end: Position::new(end_row, end_col),
        }
    }

    /// Get the width of the region.
    #[must_use]
    pub const fn width(&self) -> usize {
        self.end.col.saturating_sub(self.start.col)
    }

    /// Get the height of the region.
    #[must_use]
    pub const fn height(&self) -> usize {
        self.end.row.saturating_sub(self.start.row)
    }

    /// Check if a position is within this region.
    #[must_use]
    pub const fn contains(&self, pos: Position) -> bool {
        pos.row >= self.start.row
            && pos.row < self.end.row
            && pos.col >= self.start.col
            && pos.col < self.end.col
    }
}

/// Text attributes for a cell.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[allow(clippy::struct_excessive_bools)]
pub struct CellAttributes {
    /// Bold text.
    pub bold: bool,
    /// Italic text.
    pub italic: bool,
    /// Underlined text.
    pub underline: bool,
    /// Blinking text.
    pub blink: bool,
    /// Inverse video.
    pub inverse: bool,
    /// Hidden text.
    pub hidden: bool,
    /// Strikethrough text.
    pub strikethrough: bool,
    /// Foreground color (ANSI color code or RGB).
    pub foreground: Option<Color>,
    /// Background color (ANSI color code or RGB).
    pub background: Option<Color>,
}

/// Color representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
    /// ANSI color index (0-255).
    Indexed(u8),
    /// RGB color.
    Rgb(u8, u8, u8),
}

/// A single cell in the screen buffer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cell {
    /// The character in this cell.
    pub char: char,
    /// Text attributes.
    pub attrs: CellAttributes,
    /// Width of the character (1 for normal, 2 for wide chars).
    pub width: u8,
}

impl Default for Cell {
    fn default() -> Self {
        Self {
            char: ' ',
            attrs: CellAttributes::default(),
            width: 1,
        }
    }
}

/// A simple screen buffer for terminal content.
///
/// This provides basic screen buffer functionality. For full terminal
/// emulation, use the `screen` feature which provides a more complete
/// implementation.
pub struct ScreenBuffer {
    /// Screen cells.
    cells: Vec<Vec<Cell>>,
    /// Screen dimensions.
    dimensions: Dimensions,
    /// Cursor position.
    cursor: Position,
    /// Saved cursor position.
    saved_cursor: Option<Position>,
    /// Scroll region.
    scroll_region: Option<(usize, usize)>,
}

impl ScreenBuffer {
    /// Create a new screen buffer.
    #[must_use]
    pub fn new(dimensions: Dimensions) -> Self {
        let rows = dimensions.rows as usize;
        let cols = dimensions.cols as usize;

        let cells = (0..rows).map(|_| vec![Cell::default(); cols]).collect();

        Self {
            cells,
            dimensions,
            cursor: Position::default(),
            saved_cursor: None,
            scroll_region: None,
        }
    }

    /// Get the screen dimensions.
    #[must_use]
    pub const fn dimensions(&self) -> Dimensions {
        self.dimensions
    }

    /// Get the cursor position.
    #[must_use]
    pub const fn cursor(&self) -> Position {
        self.cursor
    }

    /// Set the cursor position.
    pub fn set_cursor(&mut self, pos: Position) {
        self.cursor = Position {
            row: pos.row.min(self.dimensions.rows as usize - 1),
            col: pos.col.min(self.dimensions.cols as usize - 1),
        };
    }

    /// Move the cursor.
    pub fn move_cursor(&mut self, rows: isize, cols: isize) {
        let new_row = (self.cursor.row as isize + rows)
            .max(0)
            .min(self.dimensions.rows as isize - 1) as usize;
        let new_col = (self.cursor.col as isize + cols)
            .max(0)
            .min(self.dimensions.cols as isize - 1) as usize;
        self.cursor = Position::new(new_row, new_col);
    }

    /// Save the cursor position.
    pub const fn save_cursor(&mut self) {
        self.saved_cursor = Some(self.cursor);
    }

    /// Restore the cursor position.
    pub const fn restore_cursor(&mut self) {
        if let Some(pos) = self.saved_cursor {
            self.cursor = pos;
        }
    }

    /// Get a cell at a position.
    #[must_use]
    pub fn get(&self, row: usize, col: usize) -> Option<&Cell> {
        self.cells.get(row).and_then(|r| r.get(col))
    }

    /// Get a mutable cell at a position.
    pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut Cell> {
        self.cells.get_mut(row).and_then(|r| r.get_mut(col))
    }

    /// Put a character at the cursor position.
    pub fn put_char(&mut self, c: char, attrs: CellAttributes) {
        if self.cursor.row < self.cells.len() && self.cursor.col < self.cells[0].len() {
            self.cells[self.cursor.row][self.cursor.col] = Cell {
                char: c,
                attrs,
                width: if c.is_ascii() { 1 } else { 2 },
            };
            self.cursor.col += 1;
            if self.cursor.col >= self.dimensions.cols as usize {
                self.cursor.col = 0;
                self.cursor.row += 1;
            }
        }
    }

    /// Get a line as a string.
    #[must_use]
    pub fn line(&self, row: usize) -> Option<String> {
        self.cells.get(row).map(|cells| {
            cells
                .iter()
                .map(|c| c.char)
                .collect::<String>()
                .trim_end()
                .to_string()
        })
    }

    /// Get all lines as strings.
    #[must_use]
    pub fn lines(&self) -> Vec<String> {
        (0..self.dimensions.rows as usize)
            .filter_map(|row| self.line(row))
            .collect()
    }

    /// Get the screen content as a single string.
    #[must_use]
    pub fn content(&self) -> String {
        self.lines().join("\n")
    }

    /// Get text in a region.
    #[must_use]
    pub fn region_text(&self, region: Region) -> String {
        let mut result = String::new();
        for row in region.start.row..region.end.row.min(self.cells.len()) {
            if row < self.cells.len() {
                let start = region.start.col;
                let end = region.end.col.min(self.cells[row].len());
                for col in start..end {
                    result.push(self.cells[row][col].char);
                }
                if row < region.end.row - 1 {
                    result.push('\n');
                }
            }
        }
        result.trim_end().to_string()
    }

    /// Clear the screen.
    pub fn clear(&mut self) {
        for row in &mut self.cells {
            for cell in row {
                *cell = Cell::default();
            }
        }
        self.cursor = Position::default();
    }

    /// Clear a region.
    pub fn clear_region(&mut self, region: Region) {
        for row in region.start.row..region.end.row.min(self.cells.len()) {
            let start = region.start.col;
            let end = region.end.col.min(self.cells[row].len());
            for col in start..end {
                self.cells[row][col] = Cell::default();
            }
        }
    }

    /// Scroll the screen up by n lines.
    pub fn scroll_up(&mut self, n: usize) {
        let (start, end) = self
            .scroll_region
            .unwrap_or((0, self.dimensions.rows as usize));

        for _ in 0..n {
            if start < end && end <= self.cells.len() {
                self.cells.remove(start);
                self.cells.insert(
                    end - 1,
                    vec![Cell::default(); self.dimensions.cols as usize],
                );
            }
        }
    }

    /// Scroll the screen down by n lines.
    pub fn scroll_down(&mut self, n: usize) {
        let (start, end) = self
            .scroll_region
            .unwrap_or((0, self.dimensions.rows as usize));

        for _ in 0..n {
            if start < end && end <= self.cells.len() {
                self.cells.remove(end - 1);
                self.cells
                    .insert(start, vec![Cell::default(); self.dimensions.cols as usize]);
            }
        }
    }

    /// Set the scroll region.
    pub const fn set_scroll_region(&mut self, top: usize, bottom: usize) {
        if top < bottom && bottom <= self.dimensions.rows as usize {
            self.scroll_region = Some((top, bottom));
        } else {
            self.scroll_region = None;
        }
    }

    /// Resize the screen.
    pub fn resize(&mut self, dimensions: Dimensions) {
        let new_rows = dimensions.rows as usize;
        let new_cols = dimensions.cols as usize;

        // Resize rows
        self.cells
            .resize_with(new_rows, || vec![Cell::default(); new_cols]);

        // Resize columns in each row
        for row in &mut self.cells {
            row.resize_with(new_cols, Cell::default);
        }

        self.dimensions = dimensions;

        // Adjust cursor if necessary
        self.cursor.row = self.cursor.row.min(new_rows.saturating_sub(1));
        self.cursor.col = self.cursor.col.min(new_cols.saturating_sub(1));
    }
}

impl std::fmt::Debug for ScreenBuffer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ScreenBuffer")
            .field("dimensions", &self.dimensions)
            .field("cursor", &self.cursor)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn screen_buffer_basic() {
        let mut screen = ScreenBuffer::new(Dimensions { rows: 24, cols: 80 });

        screen.put_char('H', CellAttributes::default());
        screen.put_char('i', CellAttributes::default());

        assert_eq!(screen.line(0), Some("Hi".to_string()));
    }

    #[test]
    fn screen_buffer_region() {
        let mut screen = ScreenBuffer::new(Dimensions { rows: 24, cols: 80 });

        for c in "Hello".chars() {
            screen.put_char(c, CellAttributes::default());
        }

        let text = screen.region_text(Region::from_coords(0, 0, 1, 5));
        assert_eq!(text, "Hello");
    }

    #[test]
    fn screen_buffer_resize() {
        let mut screen = ScreenBuffer::new(Dimensions { rows: 24, cols: 80 });
        screen.resize(Dimensions {
            rows: 40,
            cols: 120,
        });

        assert_eq!(screen.dimensions().rows, 40);
        assert_eq!(screen.dimensions().cols, 120);
    }

    #[test]
    fn position_region() {
        let region = Region::from_coords(0, 0, 10, 20);

        assert!(region.contains(Position::new(5, 10)));
        assert!(!region.contains(Position::new(10, 10)));
        assert!(!region.contains(Position::new(5, 20)));

        assert_eq!(region.width(), 20);
        assert_eq!(region.height(), 10);
    }
}