turbo-vision 1.0.7

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
// (C) 2025 - Enzo Lombardi

//! TextViewer view - scrollable text display for viewing large text content.

use crate::core::geometry::{Point, Rect};
use crate::core::event::{Event, EventType, KB_UP, KB_DOWN, KB_LEFT, KB_RIGHT, KB_PGUP, KB_PGDN, KB_HOME, KB_END};
use crate::core::draw::DrawBuffer;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
use super::scrollbar::ScrollBar;
use super::indicator::Indicator;
use std::cmp::min;

/// TextViewer displays text content with scrolling support.
/// Useful for viewing files, logs, or any multi-line text.
pub struct TextViewer {
    bounds: Rect,
    lines: Vec<String>,
    delta: Point,           // Current scroll offset
    cursor: Point,          // Current cursor position (0-based)
    h_scrollbar: Option<Box<ScrollBar>>,
    v_scrollbar: Option<Box<ScrollBar>>,
    indicator: Option<Box<Indicator>>,
    show_line_numbers: bool,
    owner: Option<*const dyn View>,
    owner_type: super::view::OwnerType,
}

impl TextViewer {
    pub fn new(bounds: Rect) -> Self {
        Self {
            bounds,
            lines: Vec::new(),
            delta: Point::zero(),
            cursor: Point::zero(),
            h_scrollbar: None,
            v_scrollbar: None,
            indicator: None,
            show_line_numbers: false,
            owner: None,
            owner_type: super::view::OwnerType::None,
        }
    }

    /// Create a TextViewer with scrollbars
    pub fn with_scrollbars(mut self, add_scrollbars: bool) -> Self {
        if add_scrollbars {
            // Vertical scrollbar on the right edge
            let v_bounds = Rect::new(
                self.bounds.b.x - 1,
                self.bounds.a.y + 1,  // Below indicator
                self.bounds.b.x,
                self.bounds.b.y - 1,  // Above horizontal scrollbar
            );
            self.v_scrollbar = Some(Box::new(ScrollBar::new_vertical(v_bounds)));

            // Horizontal scrollbar on the bottom edge
            let h_bounds = Rect::new(
                self.bounds.a.x,
                self.bounds.b.y - 1,
                self.bounds.b.x - 1,  // Before vertical scrollbar
                self.bounds.b.y,
            );
            self.h_scrollbar = Some(Box::new(ScrollBar::new_horizontal(h_bounds)));
        }
        self
    }

    /// Create a TextViewer with indicator
    pub fn with_indicator(mut self, add_indicator: bool) -> Self {
        if add_indicator {
            let indicator_bounds = Rect::new(
                self.bounds.a.x,
                self.bounds.a.y,
                self.bounds.b.x,
                self.bounds.a.y + 1,
            );
            self.indicator = Some(Box::new(Indicator::new(indicator_bounds)));
        }
        self
    }

    pub fn set_show_line_numbers(&mut self, show: bool) {
        self.show_line_numbers = show;
    }

    /// Set the text content
    pub fn set_text(&mut self, text: &str) {
        self.lines = text.lines().map(|s| s.to_string()).collect();
        if self.lines.is_empty() {
            self.lines.push(String::new());
        }
        self.cursor = Point::zero();
        self.delta = Point::zero();
        self.update_scrollbars();
        self.update_indicator();
    }

    /// Load text from a file
    pub fn load_file(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
        let content = std::fs::read_to_string(path.as_ref())?;
        self.set_text(&content);
        Ok(())
    }

    /// Get the maximum line length
    fn max_line_length(&self) -> i16 {
        self.lines
            .iter()
            .map(|line| line.len() as i16)
            .max()
            .unwrap_or(0)
    }

    /// Get the visible area (excluding scrollbars and indicator)
    fn get_content_area(&self) -> Rect {
        let mut area = self.bounds;

        // Account for indicator at top
        if self.indicator.is_some() {
            area.a.y += 1;
        }

        // Account for scrollbars
        if self.v_scrollbar.is_some() {
            area.b.x -= 1;
        }
        if self.h_scrollbar.is_some() {
            area.b.y -= 1;
        }

        area
    }

    fn update_scrollbars(&mut self) {
        let content_area = self.get_content_area();
        let max_x = self.max_line_length();
        let max_y = self.lines.len() as i16;

        if let Some(ref mut h_bar) = self.h_scrollbar {
            h_bar.set_params(
                self.delta.x as i32,
                0,
                max_x.saturating_sub(content_area.width()) as i32,
                content_area.width() as i32,
                1,
            );
        }

        if let Some(ref mut v_bar) = self.v_scrollbar {
            v_bar.set_params(
                self.delta.y as i32,
                0,
                max_y.saturating_sub(content_area.height()) as i32,
                content_area.height() as i32,
                1,
            );
        }
    }

    fn update_indicator(&mut self) {
        if let Some(ref mut indicator) = self.indicator {
            // Display 1-based line and column
            indicator.set_value(
                Point::new(self.cursor.x + 1, self.cursor.y + 1),
                false,
            );
        }
    }

    fn scroll_to(&mut self, x: i16, y: i16) {
        let content_area = self.get_content_area();
        let max_x = self.max_line_length().saturating_sub(content_area.width());
        let max_y = (self.lines.len() as i16).saturating_sub(content_area.height());

        self.delta.x = x.max(0).min(max_x.max(0));
        self.delta.y = y.max(0).min(max_y.max(0));

        // Update cursor to match scroll position (for indicator display)
        self.cursor.x = self.delta.x;
        self.cursor.y = self.delta.y;

        self.update_scrollbars();
        self.update_indicator();
    }
}

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

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

        // Update scrollbar positions
        if let Some(ref mut v_bar) = self.v_scrollbar {
            let v_bounds = Rect::new(
                bounds.b.x - 1,
                bounds.a.y + if self.indicator.is_some() { 1 } else { 0 },
                bounds.b.x,
                bounds.b.y - if self.h_scrollbar.is_some() { 1 } else { 0 },
            );
            v_bar.set_bounds(v_bounds);
        }

        if let Some(ref mut h_bar) = self.h_scrollbar {
            let h_bounds = Rect::new(
                bounds.a.x,
                bounds.b.y - 1,
                bounds.b.x - if self.v_scrollbar.is_some() { 1 } else { 0 },
                bounds.b.y,
            );
            h_bar.set_bounds(h_bounds);
        }

        if let Some(ref mut indicator) = self.indicator {
            let indicator_bounds = Rect::new(
                bounds.a.x,
                bounds.a.y,
                bounds.b.x,
                bounds.a.y + 1,
            );
            indicator.set_bounds(indicator_bounds);
        }

        self.update_scrollbars();
    }

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

        // Line number width (if shown)
        let line_num_width = if self.show_line_numbers {
            5  // " 999 "
        } else {
            0
        };

        for y in 0..height {
            let line_idx = (self.delta.y + y as i16) as usize;
            let mut buf = DrawBuffer::new(width);

            // Use palette index 1 for normal text from CP_SCROLLER
            let color = self.map_color(1);

            // Fill with spaces
            buf.move_char(0, ' ', color, width);

            if line_idx < self.lines.len() {
                let line = &self.lines[line_idx];
                let mut x_offset = 0;

                // Draw line number if enabled
                if self.show_line_numbers {
                    let line_num = format!("{:4} ", line_idx + 1);
                    buf.move_str(0, &line_num, color);
                    x_offset = line_num_width;
                }

                // Draw line content
                let start_col = self.delta.x as usize;
                if start_col < line.len() {
                    let visible_width = width - x_offset;
                    let end_col = min(start_col + visible_width, line.len());
                    let visible_text = &line[start_col..end_col];
                    buf.move_str(x_offset, visible_text, color);
                }
            }

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

        // Draw indicator
        if let Some(ref mut indicator) = self.indicator {
            indicator.draw(terminal);
        }

        // Draw scrollbars
        if let Some(ref mut h_bar) = self.h_scrollbar {
            h_bar.draw(terminal);
        }
        if let Some(ref mut v_bar) = self.v_scrollbar {
            v_bar.draw(terminal);
        }
    }

    fn handle_event(&mut self, event: &mut Event) {
        match event.what {
            EventType::Keyboard => {
                let content_area = self.get_content_area();

                match event.key_code {
                    KB_UP => {
                        self.scroll_to(self.delta.x, self.delta.y - 1);
                        event.clear();
                    }
                    KB_DOWN => {
                        self.scroll_to(self.delta.x, self.delta.y + 1);
                        event.clear();
                    }
                    KB_LEFT => {
                        self.scroll_to(self.delta.x - 1, self.delta.y);
                        event.clear();
                    }
                    KB_RIGHT => {
                        self.scroll_to(self.delta.x + 1, self.delta.y);
                        event.clear();
                    }
                    KB_PGUP => {
                        self.scroll_to(self.delta.x, self.delta.y - content_area.height());
                        event.clear();
                    }
                    KB_PGDN => {
                        self.scroll_to(self.delta.x, self.delta.y + content_area.height());
                        event.clear();
                    }
                    KB_HOME => {
                        self.scroll_to(0, self.delta.y);
                        event.clear();
                    }
                    KB_END => {
                        self.scroll_to(self.max_line_length(), self.delta.y);
                        event.clear();
                    }
                    _ => {}
                }
            }
            EventType::MouseWheelUp => {
                let mouse_pos = event.mouse.pos;
                let content_area = self.get_content_area();
                // Check if mouse is within the text viewer content area
                if mouse_pos.x >= content_area.a.x && mouse_pos.x < content_area.b.x &&
                   mouse_pos.y >= content_area.a.y && mouse_pos.y < content_area.b.y {
                    self.scroll_to(self.delta.x, self.delta.y - 1);
                    event.clear();
                }
            }
            EventType::MouseWheelDown => {
                let mouse_pos = event.mouse.pos;
                let content_area = self.get_content_area();
                // Check if mouse is within the text viewer content area
                if mouse_pos.x >= content_area.a.x && mouse_pos.x < content_area.b.x &&
                   mouse_pos.y >= content_area.a.y && mouse_pos.y < content_area.b.y {
                    self.scroll_to(self.delta.x, self.delta.y + 1);
                    event.clear();
                }
            }
            _ => {}
        }

        // Let scrollbars handle events too
        let old_delta = self.delta;

        if let Some(ref mut h_bar) = self.h_scrollbar {
            h_bar.handle_event(event);
            self.delta.x = h_bar.get_value() as i16;
        }

        if let Some(ref mut v_bar) = self.v_scrollbar {
            v_bar.handle_event(event);
            self.delta.y = v_bar.get_value() as i16;
        }

        if old_delta != self.delta {
            event.clear();
        }
    }

    fn set_owner(&mut self, owner: *const dyn View) {
        self.owner = Some(owner);
    }

    fn get_owner(&self) -> Option<*const dyn View> {
        self.owner
    }

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

    fn get_owner_type(&self) -> super::view::OwnerType {
        self.owner_type
    }

    fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
        self.owner_type = owner_type;
    }
}

/// Builder for creating text viewers with a fluent API.
pub struct TextViewerBuilder {
    bounds: Option<Rect>,
    with_scrollbars: bool,
    with_indicator: bool,
    show_line_numbers: bool,
}

impl TextViewerBuilder {
    pub fn new() -> Self {
        Self {
            bounds: None,
            with_scrollbars: false,
            with_indicator: false,
            show_line_numbers: false,
        }
    }

    #[must_use]
    pub fn bounds(mut self, bounds: Rect) -> Self {
        self.bounds = Some(bounds);
        self
    }

    #[must_use]
    pub fn with_scrollbars(mut self, with_scrollbars: bool) -> Self {
        self.with_scrollbars = with_scrollbars;
        self
    }

    #[must_use]
    pub fn with_indicator(mut self, with_indicator: bool) -> Self {
        self.with_indicator = with_indicator;
        self
    }

    #[must_use]
    pub fn show_line_numbers(mut self, show_line_numbers: bool) -> Self {
        self.show_line_numbers = show_line_numbers;
        self
    }

    pub fn build(self) -> TextViewer {
        let bounds = self.bounds.expect("TextViewer bounds must be set");
        let mut viewer = TextViewer::new(bounds)
            .with_scrollbars(self.with_scrollbars)
            .with_indicator(self.with_indicator);
        viewer.set_show_line_numbers(self.show_line_numbers);
        viewer
    }

    pub fn build_boxed(self) -> Box<TextViewer> {
        Box::new(self.build())
    }
}

impl Default for TextViewerBuilder {
    fn default() -> Self {
        Self::new()
    }
}