turbo-vision 0.9.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// (C) 2025 - Enzo Lombardi

//! SortedListBox view - automatically sorted list with binary search support.
// SortedListBox - A sorted list with binary search capability
//
// Matches Borland: TSortedListBox (extends TListBox)
//
// A sorted listbox maintains items in sorted order and provides efficient
// binary search for finding items by prefix or exact match.
//
// Architecture: Extends ListBox functionality with sorting
//
// Usage:
//   let sorted = SortedListBox::new(Rect::new(5, 5, 35, 15), 1001);
//   sorted.add_item("Zebra");
//   sorted.add_item("Apple");
//   sorted.add_item("Banana");
//   // Items are automatically sorted: Apple, Banana, Zebra
//
//   // Binary search for item starting with "B"
//   if let Some(idx) = sorted.find_prefix("B") {
//       sorted.set_selection(idx);
//   }

use crate::core::geometry::Rect;
use crate::core::event::Event;
use crate::core::state::StateFlags;
use crate::core::command::CommandId;
use crate::terminal::Terminal;
use super::view::View;
use super::list_viewer::{ListViewer, ListViewerState};

/// SortedListBox - A list that maintains items in sorted order
///
/// Extends ListBox with automatic sorting and binary search.
/// Matches Borland: TSortedListBox (extends TListBox)
pub struct SortedListBox {
    bounds: Rect,
    items: Vec<String>,
    list_state: ListViewerState,
    state: StateFlags,
    on_select_command: CommandId,
    case_sensitive: bool,
}

impl SortedListBox {
    /// Create a new sorted list box
    pub fn new(bounds: Rect, on_select_command: CommandId) -> Self {
        Self {
            bounds,
            items: Vec::new(),
            list_state: ListViewerState::new(),
            state: 0,
            on_select_command,
            case_sensitive: false,
        }
    }

    /// Set whether sorting is case-sensitive (default: false)
    pub fn set_case_sensitive(&mut self, case_sensitive: bool) {
        if self.case_sensitive != case_sensitive {
            self.case_sensitive = case_sensitive;
            self.sort_items();
        }
    }

    /// Set the items in the list (will be automatically sorted)
    pub fn set_items(&mut self, items: Vec<String>) {
        self.items = items;
        self.sort_items();
        self.list_state.set_range(self.items.len());
    }

    /// Add an item to the list (maintains sorted order)
    pub fn add_item(&mut self, item: String) {
        // Use binary search to find insertion point
        let insertion_point = self.find_insertion_point(&item);
        self.items.insert(insertion_point, item);
        self.list_state.set_range(self.items.len());
    }

    /// Clear all items
    pub fn clear(&mut self) {
        self.items.clear();
        self.list_state.set_range(0);
    }

    /// Get the currently selected item index
    pub fn get_selection(&self) -> Option<usize> {
        self.list_state.focused
    }

    /// Get the currently selected item text
    pub fn get_selected_item(&self) -> Option<&str> {
        self.list_state.focused.and_then(|idx| self.items.get(idx).map(|s| s.as_str()))
    }

    /// Set the selected item by index
    pub fn set_selection(&mut self, index: usize) {
        if index < self.items.len() {
            let visible_rows = self.bounds.height() as usize;
            self.list_state.focus_item(index, visible_rows);
        }
    }

    /// Get the number of items
    pub fn item_count(&self) -> usize {
        self.items.len()
    }

    /// Find item by exact match (case-insensitive by default)
    ///
    /// Returns the index of the item if found using binary search.
    pub fn find_exact(&self, text: &str) -> Option<usize> {
        if self.case_sensitive {
            self.items.binary_search_by(|item| item.as_str().cmp(text)).ok()
        } else {
            self.items.binary_search_by(|item| {
                item.to_lowercase().as_str().cmp(&text.to_lowercase())
            }).ok()
        }
    }

    /// Find first item starting with the given prefix
    ///
    /// Returns the index of the first item that starts with the prefix.
    /// Uses binary search for efficiency.
    pub fn find_prefix(&self, prefix: &str) -> Option<usize> {
        if prefix.is_empty() {
            return if self.items.is_empty() { None } else { Some(0) };
        }

        if self.case_sensitive {
            self.find_prefix_case_sensitive(prefix)
        } else {
            self.find_prefix_case_insensitive(prefix)
        }
    }

    /// Helper for case-sensitive prefix search
    fn find_prefix_case_sensitive(&self, prefix: &str) -> Option<usize> {
        let compare_fn = |item: &String| -> std::cmp::Ordering {
            let item_prefix = &item[..prefix.len().min(item.len())];
            item_prefix.cmp(prefix)
        };

        match self.items.binary_search_by(compare_fn) {
            Ok(idx) => {
                // Found exact prefix match, walk backwards to find the first match
                let mut first_idx = idx;
                while first_idx > 0 && compare_fn(&self.items[first_idx - 1]) == std::cmp::Ordering::Equal {
                    first_idx -= 1;
                }
                Some(first_idx)
            }
            Err(insertion_point) => {
                // Check if the item at insertion_point starts with prefix
                if insertion_point < self.items.len() && self.items[insertion_point].starts_with(prefix) {
                    Some(insertion_point)
                } else {
                    None
                }
            }
        }
    }

    /// Helper for case-insensitive prefix search
    fn find_prefix_case_insensitive(&self, prefix: &str) -> Option<usize> {
        let prefix_lower = prefix.to_lowercase();

        let compare_fn = |item: &String| -> std::cmp::Ordering {
            let item_prefix = &item[..prefix_lower.len().min(item.len())];
            item_prefix.to_lowercase().as_str().cmp(&prefix_lower)
        };

        match self.items.binary_search_by(compare_fn) {
            Ok(idx) => {
                // Found exact prefix match, walk backwards to find the first match
                let mut first_idx = idx;
                while first_idx > 0 && compare_fn(&self.items[first_idx - 1]) == std::cmp::Ordering::Equal {
                    first_idx -= 1;
                }
                Some(first_idx)
            }
            Err(insertion_point) => {
                // Check if the item at insertion_point starts with prefix
                if insertion_point < self.items.len() {
                    let item = &self.items[insertion_point];
                    if item.to_lowercase().starts_with(&prefix_lower) {
                        Some(insertion_point)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
        }
    }

    /// Focus on the first item starting with the given prefix
    ///
    /// Returns true if an item was found and focused.
    pub fn focus_prefix(&mut self, prefix: &str) -> bool {
        if let Some(idx) = self.find_prefix(prefix) {
            self.set_selection(idx);
            true
        } else {
            false
        }
    }

    // Private helper methods

    /// Sort all items according to current settings
    fn sort_items(&mut self) {
        if self.case_sensitive {
            self.items.sort();
        } else {
            self.items.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
        }
    }

    /// Find the insertion point for a new item using binary search
    fn find_insertion_point(&self, item: &str) -> usize {
        if self.case_sensitive {
            self.items.binary_search_by(|probe| probe.as_str().cmp(item))
                .unwrap_or_else(|idx| idx)
        } else {
            self.items.binary_search_by(|probe| {
                probe.to_lowercase().as_str().cmp(&item.to_lowercase())
            }).unwrap_or_else(|idx| idx)
        }
    }
}

impl View for SortedListBox {
    fn bounds(&self) -> Rect {
        self.bounds
    }

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

    fn draw(&mut self, terminal: &mut Terminal) {
        use crate::core::palette::colors;
        use crate::core::draw::DrawBuffer;
        use super::view::write_line_to_terminal;

        let width = self.bounds.width() as usize;
        let height = self.bounds.height() as usize;

        let color_normal = if self.is_focused() {
            colors::LISTBOX_FOCUSED
        } else {
            colors::LISTBOX_NORMAL
        };
        let color_selected = if self.is_focused() {
            colors::LISTBOX_SELECTED_FOCUSED
        } else {
            colors::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.items.len() {
                let is_selected = Some(item_idx) == self.list_state.focused;
                let color = if is_selected { color_selected } else { color_normal };

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

                // Fill rest of line with spaces
                let text_len = text.len();
                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) {
        // Use ListViewer trait's standard event handling
        self.handle_list_event(event);

        // TODO: Add incremental search on keyboard input
        // When user types letters, find and focus on matching items
    }

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

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

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

    fn set_list_selection(&mut self, index: usize) {
        self.set_selection(index);
    }

    fn get_list_selection(&self) -> usize {
        self.list_state.focused.unwrap_or(0)
    }
}

// Implement ListViewer trait for standard navigation
impl ListViewer for SortedListBox {
    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 {
        self.items.get(item).cloned().unwrap_or_default()
    }
}

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

    #[test]
    fn test_sorted_listbox_creation() {
        let listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        assert_eq!(listbox.item_count(), 0);
        assert_eq!(listbox.get_selection(), None);
    }

    #[test]
    fn test_sorted_listbox_add_items_maintains_order() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.add_item("Zebra".to_string());
        listbox.add_item("Apple".to_string());
        listbox.add_item("Banana".to_string());
        listbox.add_item("Mango".to_string());

        assert_eq!(listbox.item_count(), 4);
        assert_eq!(listbox.items[0], "Apple");
        assert_eq!(listbox.items[1], "Banana");
        assert_eq!(listbox.items[2], "Mango");
        assert_eq!(listbox.items[3], "Zebra");
    }

    #[test]
    fn test_sorted_listbox_set_items_sorts() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.set_items(vec![
            "Dog".to_string(),
            "Cat".to_string(),
            "Ant".to_string(),
            "Bear".to_string(),
        ]);

        assert_eq!(listbox.item_count(), 4);
        assert_eq!(listbox.items[0], "Ant");
        assert_eq!(listbox.items[1], "Bear");
        assert_eq!(listbox.items[2], "Cat");
        assert_eq!(listbox.items[3], "Dog");
    }

    #[test]
    fn test_sorted_listbox_find_exact() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.set_items(vec![
            "Apple".to_string(),
            "Banana".to_string(),
            "Cherry".to_string(),
            "Date".to_string(),
        ]);

        assert_eq!(listbox.find_exact("Banana"), Some(1));
        assert_eq!(listbox.find_exact("banana"), Some(1)); // Case-insensitive by default
        assert_eq!(listbox.find_exact("Cherry"), Some(2));
        assert_eq!(listbox.find_exact("Grape"), None);
    }

    #[test]
    fn test_sorted_listbox_find_prefix() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.set_items(vec![
            "Apple".to_string(),
            "Apricot".to_string(),
            "Banana".to_string(),
            "Berry".to_string(),
            "Cherry".to_string(),
        ]);

        // Should find first item starting with "Ap"
        assert_eq!(listbox.find_prefix("Ap"), Some(0)); // Apple
        assert_eq!(listbox.find_prefix("B"), Some(2)); // Banana
        assert_eq!(listbox.find_prefix("Be"), Some(3)); // Berry
        assert_eq!(listbox.find_prefix("C"), Some(4)); // Cherry
        assert_eq!(listbox.find_prefix("D"), None); // No match
    }

    #[test]
    fn test_sorted_listbox_focus_prefix() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.set_items(vec![
            "Apple".to_string(),
            "Apricot".to_string(),
            "Banana".to_string(),
            "Cherry".to_string(),
        ]);

        assert!(listbox.focus_prefix("B"));
        assert_eq!(listbox.get_selection(), Some(2));
        assert_eq!(listbox.get_selected_item(), Some("Banana"));

        assert!(listbox.focus_prefix("Ap"));
        assert_eq!(listbox.get_selection(), Some(0));
        assert_eq!(listbox.get_selected_item(), Some("Apple"));

        assert!(!listbox.focus_prefix("Z"));
        // Selection should remain unchanged
        assert_eq!(listbox.get_selection(), Some(0));
    }

    #[test]
    fn test_sorted_listbox_case_sensitive() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.set_case_sensitive(true);
        listbox.set_items(vec![
            "apple".to_string(),
            "Apple".to_string(),
            "APPLE".to_string(),
            "banana".to_string(),
        ]);

        // With case-sensitive sorting: APPLE, Apple, apple, banana
        assert_eq!(listbox.items[0], "APPLE");
        assert_eq!(listbox.items[1], "Apple");
        assert_eq!(listbox.items[2], "apple");
        assert_eq!(listbox.items[3], "banana");

        // Case-sensitive search
        assert_eq!(listbox.find_exact("Apple"), Some(1));
        assert_eq!(listbox.find_exact("apple"), Some(2));
        assert_eq!(listbox.find_exact("APPLE"), Some(0));
    }

    #[test]
    fn test_sorted_listbox_case_insensitive_default() {
        let mut listbox = SortedListBox::new(Rect::new(0, 0, 20, 10), 1000);
        listbox.set_items(vec![
            "ZEBRA".to_string(),
            "apple".to_string(),
            "Banana".to_string(),
        ]);

        // Case-insensitive: apple, Banana, ZEBRA (sorted alphabetically ignoring case)
        assert_eq!(listbox.items[0], "apple");
        assert_eq!(listbox.items[1], "Banana");
        assert_eq!(listbox.items[2], "ZEBRA");
    }
}