freya_terminal/
buffer.rs

1use vt100::Cell;
2
3/// Selection range in the terminal grid.
4#[derive(Clone, PartialEq, Default, Debug)]
5pub struct TerminalSelection {
6    pub dragging: bool,
7    pub start_row: usize,
8    pub start_col: usize,
9    pub start_scroll: usize,
10    pub end_row: usize,
11    pub end_col: usize,
12    pub end_scroll: usize,
13}
14
15impl TerminalSelection {
16    /// Get normalized display positions for rendering at the given scroll offset.
17    pub fn display_positions(&self, current_scroll: usize) -> (i64, usize, i64, usize) {
18        let current_scroll = current_scroll as i64;
19        let start_display = self.start_row as i64 - self.start_scroll as i64 + current_scroll;
20        let end_display = self.end_row as i64 - self.end_scroll as i64 + current_scroll;
21        if start_display < end_display
22            || (start_display == end_display && self.start_col <= self.end_col)
23        {
24            (start_display, self.start_col, end_display, self.end_col)
25        } else {
26            (end_display, self.end_col, start_display, self.start_col)
27        }
28    }
29
30    pub fn is_empty(&self) -> bool {
31        let start_content = self.start_row as i64 - self.start_scroll as i64;
32        let end_content = self.end_row as i64 - self.end_scroll as i64;
33        start_content == end_content && self.start_col == self.end_col
34    }
35}
36
37/// Terminal buffer containing the current state of the terminal.
38#[derive(Clone, PartialEq, Default)]
39pub struct TerminalBuffer {
40    /// Terminal grid rows
41    pub rows: Vec<Vec<Cell>>,
42    /// Cursor row position
43    pub cursor_row: usize,
44    /// Cursor column position
45    pub cursor_col: usize,
46    /// Number of columns in the terminal
47    pub cols: usize,
48    /// Number of rows in the terminal
49    pub rows_count: usize,
50    /// Current text selection
51    pub selection: Option<TerminalSelection>,
52    /// Current scroll offset from the bottom (0 = no scroll, at latest output)
53    pub scroll_offset: usize,
54    /// Total number of scrollback lines available
55    pub total_scrollback: usize,
56}