Skip to main content

rab/tui/components/
lines_component.rs

1use std::cell::RefCell;
2
3use crate::tui::Component;
4
5/// A component that wraps a `Vec<String>` buffer.
6/// Used as a migration bridge: compose_ui() writes lines here,
7/// and this component renders them.
8///
9/// Once all sections are migrated to proper Component types,
10/// this component can be removed.
11pub struct LinesComponent {
12    /// The rendered lines buffer.
13    pub lines: RefCell<Vec<String>>,
14}
15
16impl LinesComponent {
17    pub fn new() -> Self {
18        Self {
19            lines: RefCell::new(Vec::new()),
20        }
21    }
22
23    /// Clear and extend from an iterator.
24    pub fn set_lines(&self, new_lines: Vec<String>) {
25        *self.lines.borrow_mut() = new_lines;
26    }
27
28    /// Push a line.
29    pub fn push(&self, line: String) {
30        self.lines.borrow_mut().push(line);
31    }
32
33    /// Extend with multiple lines.
34    pub fn extend(&self, lines: impl IntoIterator<Item = String>) {
35        self.lines.borrow_mut().extend(lines);
36    }
37
38    /// Clear all lines.
39    pub fn clear(&self) {
40        self.lines.borrow_mut().clear();
41    }
42}
43
44impl Default for LinesComponent {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl Component for LinesComponent {
51    fn render(&self, _width: usize) -> Vec<String> {
52        self.lines.borrow().clone()
53    }
54
55    fn invalidate(&mut self) {
56        // No cache to invalidate — always returns current buffer
57    }
58}