turbo-vision 1.1.0

A Rust implementation of the classic Borland Turbo Vision text-mode UI framework
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
// (C) 2025 - Enzo Lombardi

//! Outline view - generic hierarchical tree control for displaying expandable/collapsible data.
//!
//! Matches Borland: TOutline, TOutlineViewer, TNode (from Turbo Vision Professional)
//!
//! Provides a hierarchical tree view with:
//! - Expand/collapse nodes
//! - Visual tree structure (├─, └─, │)
//! - Keyboard navigation
//! - Custom node data

use crate::core::geometry::Rect;
use crate::core::event::{Event, EventType, KB_ENTER, KB_LEFT, KB_RIGHT};
use crate::core::state::StateFlags;
use crate::core::draw::DrawBuffer;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
use super::list_viewer::{ListViewer, ListViewerState};
use std::rc::Rc;
use std::cell::RefCell;

/// A node in the tree
/// Matches Borland: TNode
pub struct Node<T> {
    /// Node data (user-provided)
    pub data: T,
    /// Child nodes
    pub children: Vec<Rc<RefCell<Node<T>>>>,
    /// Whether this node is expanded (showing children)
    pub expanded: bool,
}

impl<T> Node<T> {
    /// Create a new leaf node (no children)
    pub fn new(data: T) -> Self {
        Self {
            data,
            children: Vec::new(),
            expanded: false,
        }
    }

    /// Create a new node with children
    pub fn with_children(data: T, children: Vec<Rc<RefCell<Node<T>>>>) -> Self {
        Self {
            data,
            children,
            expanded: false,
        }
    }

    /// Add a child node
    pub fn add_child(&mut self, child: Rc<RefCell<Node<T>>>) {
        self.children.push(child);
    }

    /// Check if this node has children
    pub fn has_children(&self) -> bool {
        !self.children.is_empty()
    }

    /// Toggle expanded state
    pub fn toggle(&mut self) {
        self.expanded = !self.expanded;
    }
}

/// Flattened node for display (with nesting level and display text)
struct DisplayNode {
    /// Display text
    text: String,
    /// Nesting level (0 = root)
    level: usize,
    /// Node index in the original tree (for lookup)
    node_index: usize,
    /// Whether this node has children
    has_children: bool,
    /// Whether this node is expanded
    expanded: bool,
    /// Whether this is the last child at its level
    is_last: bool,
    /// Track which parent levels continue (for vertical lines)
    parent_continues: Vec<bool>,
}

impl DisplayNode {
    /// Format with tree characters
    fn display_text(&self) -> String {
        let mut result = String::new();

        // Add vertical lines for parent levels
        for i in 0..self.level {
            if i < self.parent_continues.len() && self.parent_continues[i] {
                result.push_str("");
            } else {
                result.push_str("  ");
            }
        }

        // Add branch and expansion indicator
        if self.level > 0 {
            if self.is_last {
                result.push_str("└─");
            } else {
                result.push_str("├─");
            }
        }

        // Add expansion indicator
        if self.has_children {
            if self.expanded {
                result.push_str("[-] ");
            } else {
                result.push_str("[+] ");
            }
        }

        result.push_str(&self.text);
        result
    }
}

/// OutlineViewer - displays a hierarchical tree of nodes
/// Matches Borland: TOutlineViewer
pub struct OutlineViewer<T> {
    bounds: Rect,
    state: StateFlags,
    /// Root nodes
    roots: Vec<Rc<RefCell<Node<T>>>>,
    /// Flattened list for display
    display_nodes: Vec<DisplayNode>,
    /// All nodes (flattened)
    all_nodes: Vec<Rc<RefCell<Node<T>>>>,
    /// List viewer state for scrolling/selection
    list_state: ListViewerState,
    /// Function to convert data to display string
    format_fn: Box<dyn Fn(&T) -> String>,
    palette_chain: Option<crate::core::palette_chain::PaletteChainNode>,
}

impl<T: 'static> OutlineViewer<T> {
    /// Create a new outline viewer
    /// format_fn converts node data to display string
    pub fn new<F>(bounds: Rect, format_fn: F) -> Self
    where
        F: Fn(&T) -> String + 'static,
    {
        Self {
            bounds,
            state: 0,
            roots: Vec::new(),
            display_nodes: Vec::new(),
            all_nodes: Vec::new(),
            list_state: ListViewerState::new(),
            format_fn: Box::new(format_fn),
        palette_chain: None,
        }
    }

    /// Set the root nodes of the tree
    pub fn set_roots(&mut self, roots: Vec<Rc<RefCell<Node<T>>>>) {
        self.roots = roots;
        self.rebuild_display();
    }

    /// Add a root node
    pub fn add_root(&mut self, root: Rc<RefCell<Node<T>>>) {
        self.roots.push(root);
        self.rebuild_display();
    }

    /// Rebuild the flattened display list
    fn rebuild_display(&mut self) {
        self.display_nodes.clear();
        self.all_nodes.clear();

        // Clone roots to avoid borrow checker issues
        let roots = self.roots.clone();
        for (i, root) in roots.iter().enumerate() {
            let is_last_root = i == roots.len() - 1;
            self.flatten_node(root.clone(), 0, is_last_root, &mut Vec::new());
        }

        // Update list state with new count
        self.list_state.set_range(self.display_nodes.len());
    }

    /// Recursively flatten a node and its visible children
    fn flatten_node(
        &mut self,
        node: Rc<RefCell<Node<T>>>,
        level: usize,
        is_last: bool,
        parent_continues: &mut Vec<bool>,
    ) {
        let node_index = self.all_nodes.len();
        self.all_nodes.push(node.clone());

        let node_borrow = node.borrow();
        let text = (self.format_fn)(&node_borrow.data);
        let has_children = node_borrow.has_children();
        let expanded = node_borrow.expanded;

        self.display_nodes.push(DisplayNode {
            text,
            level,
            node_index,
            has_children,
            expanded,
            is_last,
            parent_continues: parent_continues.clone(),
        });

        // If expanded, show children
        if expanded && has_children {
            let children_len = node_borrow.children.len();
            drop(node_borrow); // Release borrow before recursing

            // Track which parent levels have more siblings
            parent_continues.push(!is_last);

            for (i, child) in node.borrow().children.iter().enumerate() {
                let child_is_last = i == children_len - 1;
                self.flatten_node(child.clone(), level + 1, child_is_last, parent_continues);
            }

            parent_continues.pop();
        }
    }

    /// Get the currently selected node
    pub fn selected_node(&self) -> Option<Rc<RefCell<Node<T>>>> {
        if let Some(focused) = self.list_state.focused {
            if focused < self.display_nodes.len() {
                let node_index = self.display_nodes[focused].node_index;
                if node_index < self.all_nodes.len() {
                    return Some(self.all_nodes[node_index].clone());
                }
            }
        }
        None
    }

    /// Expand the currently selected node
    fn expand_selected(&mut self) {
        if let Some(node) = self.selected_node() {
            let mut node_borrow = node.borrow_mut();
            if node_borrow.has_children() && !node_borrow.expanded {
                node_borrow.expanded = true;
                drop(node_borrow);
                self.rebuild_display();
            }
        }
    }

    /// Collapse the currently selected node
    fn collapse_selected(&mut self) {
        if let Some(node) = self.selected_node() {
            let mut node_borrow = node.borrow_mut();
            if node_borrow.has_children() && node_borrow.expanded {
                node_borrow.expanded = false;
                drop(node_borrow);
                self.rebuild_display();
            }
        }
    }

    /// Toggle expand/collapse on the currently selected node
    fn toggle_selected(&mut self) {
        if let Some(node) = self.selected_node() {
            let mut node_borrow = node.borrow_mut();
            if node_borrow.has_children() {
                node_borrow.toggle();
                drop(node_borrow);
                self.rebuild_display();
            }
        }
    }
}

impl<T: 'static> View for OutlineViewer<T> {
    fn bounds(&self) -> Rect {
        self.bounds
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.bounds = bounds;
    }

    fn draw(&mut self, terminal: &mut Terminal) {
        let width = self.bounds.width_clamped() as usize;
        let height = self.bounds.height_clamped() as usize;

        use crate::core::palette::colors::{LISTBOX_FOCUSED, LISTBOX_NORMAL, LISTBOX_SELECTED_FOCUSED, LISTBOX_SELECTED};
        let color_normal = if self.is_focused() {
            LISTBOX_FOCUSED
        } else {
            LISTBOX_NORMAL
        };
        let color_selected = if self.is_focused() {
            LISTBOX_SELECTED_FOCUSED
        } else {
            LISTBOX_SELECTED
        };

        // Draw visible items
        for i in 0..height {
            let mut buf = DrawBuffer::new(width);
            let item_idx = self.list_state.top_item + i;

            if item_idx < self.display_nodes.len() {
                let is_selected = Some(item_idx) == self.list_state.focused;
                let color = if is_selected { color_selected } else { color_normal };

                let display_node = &self.display_nodes[item_idx];
                let text = display_node.display_text();
                buf.move_str(0, &text, color);

                // Fill rest of line with spaces
                let text_len = text.chars().count();
                if text_len < width {
                    buf.move_char(text_len, ' ', color, width - text_len);
                }
            } else {
                // Empty line
                buf.move_char(0, ' ', color_normal, width);
            }

            write_line_to_terminal(terminal, self.bounds.a.x, self.bounds.a.y + i as i16, &buf);
        }
    }

    fn handle_event(&mut self, event: &mut Event) {
        // Handle expand/collapse keys first
        if event.what == EventType::Keyboard {
            match event.key_code {
                KB_RIGHT => {
                    self.expand_selected();
                    event.clear();
                    return;
                }
                KB_LEFT => {
                    self.collapse_selected();
                    event.clear();
                    return;
                }
                KB_ENTER => {
                    self.toggle_selected();
                    event.clear();
                    return;
                }
                _ => {}
            }
        }

        // Let list viewer handle standard navigation (arrows, page up/down, etc.)
        self.handle_list_event(event);
    }

    fn state(&self) -> StateFlags {
        self.state
    }

    fn set_state(&mut self, state: StateFlags) {
        self.state = state;
    }

    fn can_focus(&self) -> bool {
        true
    }

    fn set_palette_chain(&mut self, node: Option<crate::core::palette_chain::PaletteChainNode>) {
        self.palette_chain = node;
    }

    fn get_palette_chain(&self) -> Option<&crate::core::palette_chain::PaletteChainNode> {
        self.palette_chain.as_ref()
    }

    fn get_palette(&self) -> Option<crate::core::palette::Palette> {
        use crate::core::palette::{palettes, Palette};
        Some(Palette::from_slice(palettes::CP_LISTBOX))
    }

}

// Implement ListViewer trait to get standard list navigation
impl<T: 'static> ListViewer for OutlineViewer<T> {
    fn list_state(&self) -> &ListViewerState {
        &self.list_state
    }

    fn list_state_mut(&mut self) -> &mut ListViewerState {
        &mut self.list_state
    }

    fn get_text(&self, item: usize, _max_len: usize) -> String {
        if item < self.display_nodes.len() {
            self.display_nodes[item].display_text()
        } else {
            String::new()
        }
    }
}