ratatui-toolkit 0.2.6

DEPRECATED: this crate was renamed to `ratkit`. Please migrate to `ratkit`.
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
426
427
428
429
430
431
//! Terminal grid with VecDeque-based scrollback (mprocs architecture)

use crate::primitives::termtui::row::Row;
use crate::primitives::termtui::size::Size;
use std::collections::VecDeque;

/// Cursor position
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Pos {
    pub col: u16,
    pub row: u16,
}

impl Pos {
    pub fn new(col: u16, row: u16) -> Self {
        Self { col, row }
    }
}

/// Terminal grid with scrollback buffer
///
/// Uses VecDeque to efficiently manage scrollback history.
/// The visible rows are at the end of the deque, with scrollback
/// history at the front.
#[derive(Clone, Debug)]
pub struct Grid {
    /// All rows (scrollback + visible)
    rows: VecDeque<Row>,
    /// Current size
    size: Size,
    /// Cursor position
    pos: Pos,
    /// Maximum scrollback lines
    scrollback_len: usize,
    /// Current scrollback offset (0 = showing latest)
    scrollback_offset: usize,
    /// Number of rows that have been used
    used_rows: usize,
    /// Scroll region top (0-indexed)
    scroll_top: u16,
    /// Scroll region bottom (0-indexed, exclusive)
    scroll_bottom: u16,
    /// Saved cursor position
    saved_pos: Option<Pos>,
}

impl Grid {
    /// Create a new grid
    pub fn new(size: Size, scrollback_len: usize) -> Self {
        let rows = (0..size.rows)
            .map(|_| Row::new(size.cols))
            .collect::<VecDeque<_>>();

        Self {
            rows,
            size,
            pos: Pos::default(),
            scrollback_len,
            scrollback_offset: 0,
            used_rows: 0,
            scroll_top: 0,
            scroll_bottom: size.rows,
            saved_pos: None,
        }
    }

    /// Get the grid size
    pub fn size(&self) -> Size {
        self.size
    }

    /// Get cursor position
    pub fn pos(&self) -> Pos {
        self.pos
    }

    /// Set cursor position
    pub fn set_pos(&mut self, pos: Pos) {
        self.pos = Pos {
            col: pos.col.min(self.size.cols.saturating_sub(1)),
            row: pos.row.min(self.size.rows.saturating_sub(1)),
        };
    }

    /// Move cursor to column
    pub fn set_col(&mut self, col: u16) {
        self.pos.col = col.min(self.size.cols.saturating_sub(1));
    }

    /// Move cursor to row
    pub fn set_row(&mut self, row: u16) {
        self.pos.row = row.min(self.size.rows.saturating_sub(1));
    }

    /// Save cursor position
    pub fn save_pos(&mut self) {
        self.saved_pos = Some(self.pos);
    }

    /// Restore cursor position
    pub fn restore_pos(&mut self) {
        if let Some(pos) = self.saved_pos {
            self.pos = pos;
        }
    }

    /// Get the index where visible rows begin in the deque
    fn row0(&self) -> usize {
        self.rows.len().saturating_sub(self.size.rows as usize)
    }

    /// Get current scrollback offset
    pub fn scrollback(&self) -> usize {
        self.scrollback_offset
    }

    /// Set scrollback offset
    pub fn set_scrollback(&mut self, offset: usize) {
        let max_offset = self.row0();
        self.scrollback_offset = offset.min(max_offset);
    }

    /// Get available scrollback lines
    pub fn scrollback_available(&self) -> usize {
        self.row0()
    }

    /// Set scroll region
    pub fn set_scroll_region(&mut self, top: u16, bottom: u16) {
        self.scroll_top = top.min(self.size.rows.saturating_sub(1));
        self.scroll_bottom = bottom.min(self.size.rows).max(self.scroll_top + 1);
    }

    /// Reset scroll region to full screen
    pub fn reset_scroll_region(&mut self) {
        self.scroll_top = 0;
        self.scroll_bottom = self.size.rows;
    }

    /// Get a visible row (accounting for scrollback offset)
    pub fn visible_row(&self, row: u16) -> Option<&Row> {
        let idx = self.row0() + row as usize;
        let idx = idx.saturating_sub(self.scrollback_offset);
        self.rows.get(idx)
    }

    /// Get a drawing row (for writing, ignores scrollback offset)
    pub fn drawing_row(&self, row: u16) -> Option<&Row> {
        let idx = self.row0() + row as usize;
        self.rows.get(idx)
    }

    /// Get a mutable drawing row
    pub fn drawing_row_mut(&mut self, row: u16) -> Option<&mut Row> {
        let idx = self.row0() + row as usize;
        if row as usize >= self.used_rows {
            self.used_rows = row as usize + 1;
        }
        self.rows.get_mut(idx)
    }

    /// Get current row (at cursor position)
    pub fn current_row(&self) -> Option<&Row> {
        self.drawing_row(self.pos.row)
    }

    /// Get mutable current row
    pub fn current_row_mut(&mut self) -> Option<&mut Row> {
        let row = self.pos.row;
        self.drawing_row_mut(row)
    }

    /// Scroll up within scroll region
    pub fn scroll_up(&mut self, count: usize) {
        for _ in 0..count {
            // If scroll region is full screen, add to scrollback
            if self.scroll_top == 0 && self.scroll_bottom == self.size.rows {
                // Add new row at the end
                self.rows.push_back(Row::new(self.size.cols));

                // Trim scrollback if needed
                while self.rows.len() > self.size.rows as usize + self.scrollback_len {
                    self.rows.pop_front();
                }
            } else {
                // Scroll within region only
                let top_idx = self.row0() + self.scroll_top as usize;
                let bottom_idx = self.row0() + self.scroll_bottom as usize - 1;

                if top_idx < self.rows.len() && bottom_idx < self.rows.len() {
                    // Remove top row of region
                    self.rows.remove(top_idx);
                    // Insert new row at bottom of region
                    self.rows.insert(bottom_idx, Row::new(self.size.cols));
                }
            }
        }
    }

    /// Scroll down within scroll region
    pub fn scroll_down(&mut self, count: usize) {
        for _ in 0..count {
            let top_idx = self.row0() + self.scroll_top as usize;
            let bottom_idx = self.row0() + self.scroll_bottom as usize - 1;

            if top_idx < self.rows.len() && bottom_idx < self.rows.len() {
                // Remove bottom row of region
                self.rows.remove(bottom_idx);
                // Insert new row at top of region
                self.rows.insert(top_idx, Row::new(self.size.cols));
            }
        }
    }

    /// Clear all rows
    pub fn clear(&mut self) {
        for row in self.rows.iter_mut() {
            row.clear();
        }
        self.used_rows = 0;
    }

    /// Clear from cursor to end of screen
    pub fn clear_below(&mut self) {
        let pos_row = self.pos.row;
        let pos_col = self.pos.col;
        let cols = self.size.cols;
        let rows = self.size.rows;

        // Clear current row from cursor
        if let Some(row) = self.drawing_row_mut(pos_row) {
            row.erase(pos_col, cols);
        }

        // Clear all rows below
        for r in (pos_row + 1)..rows {
            if let Some(row) = self.drawing_row_mut(r) {
                row.clear();
            }
        }
    }

    /// Clear from start of screen to cursor
    pub fn clear_above(&mut self) {
        let pos_row = self.pos.row;
        let pos_col = self.pos.col;

        // Clear all rows above
        for r in 0..pos_row {
            if let Some(row) = self.drawing_row_mut(r) {
                row.clear();
            }
        }

        // Clear current row up to cursor
        if let Some(row) = self.drawing_row_mut(pos_row) {
            row.erase(0, pos_col + 1);
        }
    }

    /// Resize the grid
    pub fn resize(&mut self, new_size: Size) {
        // Resize existing rows
        for row in self.rows.iter_mut() {
            row.resize(new_size.cols);
        }

        // Add or remove rows as needed
        while self.rows.len() < new_size.rows as usize {
            self.rows.push_back(Row::new(new_size.cols));
        }

        // Update size
        self.size = new_size;

        // Clamp cursor and scroll region
        self.pos.col = self.pos.col.min(new_size.cols.saturating_sub(1));
        self.pos.row = self.pos.row.min(new_size.rows.saturating_sub(1));
        self.scroll_bottom = new_size.rows;
    }

    /// Get selected text from coordinates
    ///
    /// Handles wrapped lines correctly (no newline for soft-wrapped rows)
    pub fn get_selected_text(&self, low_x: i32, low_y: i32, high_x: i32, high_y: i32) -> String {
        let mut contents = String::new();

        let row0 = self.row0() as i32;
        let start_row = (row0 + low_y).max(0) as usize;
        let end_row = (row0 + high_y).max(0) as usize;

        for (i, row) in self.rows.iter().enumerate() {
            if i < start_row || i > end_row {
                continue;
            }

            let width = row.width();

            // Determine start and end columns for this row
            let start_col = if i == start_row {
                (low_x.max(0) as u16).min(width)
            } else {
                0
            };

            let end_col = if i == end_row {
                (high_x.max(0) as u16).min(width)
            } else {
                width
            };

            // Extract text from this row
            row.write_contents(&mut contents, start_col, end_col);

            // Add newline unless this row wraps (soft wrap)
            if i != end_row && !row.wrapped() {
                contents.push('\n');
            }
        }

        // Trim trailing whitespace from each line
        contents
            .lines()
            .map(|line| line.trim_end())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Iterate over visible rows
    pub fn visible_rows(&self) -> impl Iterator<Item = &Row> {
        let start = self.row0().saturating_sub(self.scrollback_offset);
        let end = start + self.size.rows as usize;
        self.rows.iter().skip(start).take(end - start)
    }

    /// Iterate over drawing rows (ignoring scrollback offset)
    pub fn drawing_rows(&self) -> impl Iterator<Item = &Row> {
        let start = self.row0();
        self.rows.iter().skip(start).take(self.size.rows as usize)
    }
}

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

    #[test]
    fn test_grid_new() {
        let grid = Grid::new(Size::new(80, 24), 1000);
        assert_eq!(grid.size().cols, 80);
        assert_eq!(grid.size().rows, 24);
        assert_eq!(grid.pos().col, 0);
        assert_eq!(grid.pos().row, 0);
    }

    #[test]
    fn test_grid_cursor() {
        let mut grid = Grid::new(Size::new(80, 24), 1000);

        grid.set_pos(Pos::new(10, 5));
        assert_eq!(grid.pos(), Pos::new(10, 5));

        // Should clamp to bounds
        grid.set_pos(Pos::new(100, 50));
        assert_eq!(grid.pos(), Pos::new(79, 23));
    }

    #[test]
    fn test_grid_scroll_up() {
        let mut grid = Grid::new(Size::new(80, 24), 100);

        // Write something to first row
        if let Some(row) = grid.drawing_row_mut(0) {
            if let Some(cell) = row.get_mut(0) {
                cell.set_text("A");
            }
        }

        // Scroll up
        grid.scroll_up(1);

        // Check scrollback available
        assert_eq!(grid.scrollback_available(), 1);
    }

    #[test]
    fn test_grid_scrollback() {
        let mut grid = Grid::new(Size::new(80, 24), 100);

        // Fill with some content and scroll
        for _ in 0..10 {
            grid.scroll_up(1);
        }

        assert_eq!(grid.scrollback_available(), 10);

        // Set scrollback offset
        grid.set_scrollback(5);
        assert_eq!(grid.scrollback(), 5);

        // Should clamp to available
        grid.set_scrollback(1000);
        assert_eq!(grid.scrollback(), 10);
    }

    #[test]
    fn test_grid_get_selected_text() {
        let mut grid = Grid::new(Size::new(80, 24), 100);

        // Write "Hello World" on first row
        if let Some(row) = grid.drawing_row_mut(0) {
            for (i, c) in "Hello World".chars().enumerate() {
                if let Some(cell) = row.get_mut(i as u16) {
                    cell.set_text(c.to_string());
                }
            }
        }

        let text = grid.get_selected_text(0, 0, 5, 0);
        assert_eq!(text, "Hello");
    }

    #[test]
    fn test_grid_resize() {
        let mut grid = Grid::new(Size::new(80, 24), 100);

        grid.resize(Size::new(120, 40));
        assert_eq!(grid.size().cols, 120);
        assert_eq!(grid.size().rows, 40);
    }
}