presentar-terminal 0.3.5

Terminal backend for Presentar UI framework with zero-allocation rendering
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! `Selection` - Tufte-inspired selection highlighting primitives
//!
//! Framework widgets for making selections VISIBLE following Tufte's principle:
//! "Differences must be immediately perceivable" (Visual Display, 1983)
//!
//! # Components
//! - `RowHighlight` - Full row background + gutter indicator
//! - `CellHighlight` - Single cell emphasis
//! - `FocusRing` - Panel/container focus indicator
//!
//! # Design Principles
//! 1. **Multiple Redundant Cues**: Color + Shape + Position (accessibility)
//! 2. **High Contrast**: Selection must be visible in any terminal
//! 3. **Consistent Language**: Same visual language across all widgets

use presentar_core::{Canvas, Color, Point, Rect, TextStyle};

// =============================================================================
// TTOP-MATCHING SELECTION COLORS
// =============================================================================
// ttop uses SUBTLE selection: barely visible dark bg + ▶ gutter indicator
// The gutter indicator (▶) is the PRIMARY visual cue, not the background

/// Subtle dark selection background (matches ttop's barely-visible highlight)
/// Just slightly brighter than DIMMED_BG to indicate selection
pub const SELECTION_BG: Color = Color {
    r: 0.15,
    g: 0.12,
    b: 0.22,
    a: 1.0,
}; // Subtle dark purple - barely visible, ttop-style

/// Bright cyan for selection indicators (cursors, borders)
/// This is the PRIMARY visual cue for selection (the ▶ indicator)
pub const SELECTION_ACCENT: Color = Color {
    r: 0.4,
    g: 0.9,
    b: 0.4,
    a: 1.0,
}; // Bright green like ttop's ▶ cursor

/// Gutter indicator color (same as accent for consistency with ttop)
pub const SELECTION_GUTTER: Color = Color {
    r: 0.4,
    g: 0.9,
    b: 0.4,
    a: 1.0,
}; // Bright green ▶

/// Dimmed background for non-selected items
pub const DIMMED_BG: Color = Color {
    r: 0.08,
    g: 0.08,
    b: 0.1,
    a: 1.0,
};

// =============================================================================
// ROW HIGHLIGHT
// =============================================================================

/// Tufte-compliant row highlighting with multiple visual cues
///
/// Visual elements:
/// 1. Strong background color (immediate visibility)
/// 2. Left gutter indicator `▐` or `│` (spatial cue)
/// 3. Optional right border (framing)
/// 4. Text color change (contrast)
#[derive(Debug, Clone)]
pub struct RowHighlight {
    /// The row rectangle
    pub bounds: Rect,
    /// Is this row selected?
    pub selected: bool,
    /// Show gutter indicator
    pub show_gutter: bool,
    /// Gutter character (default: ▐)
    pub gutter_char: char,
}

impl RowHighlight {
    pub fn new(bounds: Rect, selected: bool) -> Self {
        Self {
            bounds,
            selected,
            show_gutter: true,
            gutter_char: '',
        }
    }

    pub fn with_gutter(mut self, show: bool) -> Self {
        self.show_gutter = show;
        self
    }

    pub fn with_gutter_char(mut self, ch: char) -> Self {
        self.gutter_char = ch;
        self
    }

    /// Paint the row highlight to a canvas
    ///
    /// CRITICAL: Always paints to clear previous frame artifacts.
    /// Terminal buffers retain pixels - non-selected rows need explicit background.
    pub fn paint(&self, canvas: &mut dyn Canvas) {
        if self.selected {
            // 1. Strong background fill for selected row
            canvas.fill_rect(self.bounds, SELECTION_BG);

            // 2. Left gutter indicator
            if self.show_gutter {
                canvas.draw_text(
                    &self.gutter_char.to_string(),
                    Point::new(self.bounds.x - 1.0, self.bounds.y),
                    &TextStyle {
                        color: SELECTION_GUTTER,
                        ..Default::default()
                    },
                );
            }
        } else {
            // Clear any previous selection artifact with dimmed background
            canvas.fill_rect(self.bounds, DIMMED_BG);
        }
    }

    /// Get the text style for content in this row
    pub fn text_style(&self) -> TextStyle {
        if self.selected {
            TextStyle {
                color: Color::WHITE,
                ..Default::default()
            }
        } else {
            TextStyle {
                color: Color::new(0.85, 0.85, 0.85, 1.0),
                ..Default::default()
            }
        }
    }
}

// =============================================================================
// FOCUS RING (Panel Focus)
// =============================================================================

/// Focus indicator for panels/containers
///
/// Uses Tufte's layering principle:
/// 1. Border style change (Double vs Single)
/// 2. Color intensity change (bright vs dim)
/// 3. Optional indicator character (►)
#[derive(Debug, Clone)]
pub struct FocusRing {
    /// Panel bounds
    pub bounds: Rect,
    /// Is focused?
    pub focused: bool,
    /// Base color (panel's theme color)
    pub base_color: Color,
}

impl FocusRing {
    pub fn new(bounds: Rect, focused: bool, base_color: Color) -> Self {
        Self {
            bounds,
            focused,
            base_color,
        }
    }

    /// Get the border color based on focus state
    pub fn border_color(&self) -> Color {
        if self.focused {
            // Blend with cyan accent for visibility
            Color {
                r: (self.base_color.r * 0.4 + SELECTION_ACCENT.r * 0.6).min(1.0),
                g: (self.base_color.g * 0.4 + SELECTION_ACCENT.g * 0.6).min(1.0),
                b: (self.base_color.b * 0.4 + SELECTION_ACCENT.b * 0.6).min(1.0),
                a: 1.0,
            }
        } else {
            // Dim unfocused panels
            Color {
                r: self.base_color.r * 0.4,
                g: self.base_color.g * 0.4,
                b: self.base_color.b * 0.4,
                a: 1.0,
            }
        }
    }

    /// Get title prefix (► for focused)
    pub fn title_prefix(&self) -> &'static str {
        if self.focused {
            ""
        } else {
            ""
        }
    }
}

// =============================================================================
// COLUMN HIGHLIGHT
// =============================================================================

/// Column header highlight for sortable tables
#[derive(Debug, Clone)]
pub struct ColumnHighlight {
    /// Column bounds
    pub bounds: Rect,
    /// Is this column selected for navigation?
    pub selected: bool,
    /// Is this column the sort column?
    pub sorted: bool,
    /// Sort direction (true = descending)
    pub sort_descending: bool,
}

impl ColumnHighlight {
    pub fn new(bounds: Rect) -> Self {
        Self {
            bounds,
            selected: false,
            sorted: false,
            sort_descending: true,
        }
    }

    pub fn with_selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    pub fn with_sorted(mut self, sorted: bool, descending: bool) -> Self {
        self.sorted = sorted;
        self.sort_descending = descending;
        self
    }

    /// Get background color
    pub fn background(&self) -> Option<Color> {
        if self.selected {
            Some(Color::new(0.15, 0.35, 0.55, 1.0))
        } else {
            None
        }
    }

    /// Get sort indicator character
    pub fn sort_indicator(&self) -> &'static str {
        if self.sorted {
            if self.sort_descending {
                ""
            } else {
                ""
            }
        } else {
            ""
        }
    }

    /// Get text style
    pub fn text_style(&self) -> TextStyle {
        let color = if self.sorted {
            SELECTION_ACCENT
        } else if self.selected {
            Color::WHITE
        } else {
            Color::new(0.6, 0.6, 0.6, 1.0)
        };

        TextStyle {
            color,
            ..Default::default()
        }
    }
}

// =============================================================================
// CURSOR INDICATOR
// =============================================================================

/// Universal cursor/pointer indicator
pub struct Cursor;

impl Cursor {
    /// Row cursor (appears in gutter)
    pub const ROW: &'static str = "";

    /// Column cursor (appears above header)
    pub const COLUMN: &'static str = "";

    /// Panel cursor (appears in title)
    pub const PANEL: &'static str = "";

    /// Get cursor color
    pub fn color() -> Color {
        SELECTION_ACCENT
    }

    /// Paint a row cursor at position
    pub fn paint_row(canvas: &mut dyn Canvas, pos: Point) {
        canvas.draw_text(
            Self::ROW,
            pos,
            &TextStyle {
                color: Self::color(),
                ..Default::default()
            },
        );
    }
}

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

    // =========================================================================
    // COLOR CONSTANTS TESTS (ttop-matching subtle style)
    // =========================================================================

    #[test]
    fn test_row_highlight_colors() {
        // Selection background should be subtle dark purple (ttop-style)
        // Just slightly different from DIMMED_BG, barely visible
        assert!(SELECTION_BG.r < 0.25, "Selection bg should be dark");
        assert!(
            SELECTION_BG.b > SELECTION_BG.r,
            "Selection bg should have purple tint"
        );
    }

    #[test]
    fn test_selection_accent_is_green() {
        // ttop uses bright green for the ▶ indicator
        assert!(SELECTION_ACCENT.g > 0.8, "Accent should be bright green");
        assert!(
            SELECTION_ACCENT.r > 0.3,
            "Accent has some red for visibility"
        );
    }

    #[test]
    fn test_selection_gutter_matches_accent() {
        // Gutter should match accent for consistency (ttop-style)
        assert_eq!(SELECTION_GUTTER.r, SELECTION_ACCENT.r);
        assert_eq!(SELECTION_GUTTER.g, SELECTION_ACCENT.g);
        assert_eq!(SELECTION_GUTTER.b, SELECTION_ACCENT.b);
    }

    #[test]
    fn test_dimmed_bg_is_dark() {
        assert!(DIMMED_BG.r < 0.15);
        assert!(DIMMED_BG.g < 0.15);
        assert!(DIMMED_BG.b < 0.15);
    }

    // =========================================================================
    // ROW HIGHLIGHT TESTS
    // =========================================================================

    #[test]
    fn test_row_highlight_new() {
        let bounds = Rect::new(0.0, 0.0, 100.0, 1.0);
        let highlight = RowHighlight::new(bounds, true);

        assert_eq!(highlight.bounds, bounds);
        assert!(highlight.selected);
        assert!(highlight.show_gutter);
        assert_eq!(highlight.gutter_char, '');
    }

    #[test]
    fn test_row_highlight_not_selected() {
        let bounds = Rect::new(0.0, 0.0, 100.0, 1.0);
        let highlight = RowHighlight::new(bounds, false);

        assert!(!highlight.selected);
    }

    #[test]
    fn test_row_highlight_with_gutter() {
        let highlight = RowHighlight::new(Rect::default(), true).with_gutter(false);
        assert!(!highlight.show_gutter);

        let highlight2 = highlight.with_gutter(true);
        assert!(highlight2.show_gutter);
    }

    #[test]
    fn test_row_highlight_with_gutter_char() {
        let highlight = RowHighlight::new(Rect::default(), true).with_gutter_char('');
        assert_eq!(highlight.gutter_char, '');
    }

    #[test]
    fn test_row_highlight_text_style_selected() {
        let highlight = RowHighlight::new(Rect::default(), true);
        let style = highlight.text_style();
        assert_eq!(style.color, Color::WHITE);
    }

    #[test]
    fn test_row_highlight_text_style_not_selected() {
        let highlight = RowHighlight::new(Rect::default(), false);
        let style = highlight.text_style();
        // Should be gray-ish
        assert!(style.color.r > 0.8);
        assert!(style.color.g > 0.8);
        assert!(style.color.b > 0.8);
    }

    #[test]
    fn test_row_highlight_paint_selected() {
        use crate::direct::{CellBuffer, DirectTerminalCanvas};

        let mut buffer = CellBuffer::new(20, 5);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        let bounds = Rect::new(2.0, 1.0, 10.0, 1.0);
        let highlight = RowHighlight::new(bounds, true);
        highlight.paint(&mut canvas);

        // The gutter char should be drawn at x-1
        // And background should be filled
    }

    #[test]
    fn test_row_highlight_paint_not_selected() {
        use crate::direct::{CellBuffer, DirectTerminalCanvas};

        let mut buffer = CellBuffer::new(20, 5);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        let bounds = Rect::new(2.0, 1.0, 10.0, 1.0);
        let highlight = RowHighlight::new(bounds, false);
        highlight.paint(&mut canvas);

        // Should paint dimmed background
    }

    #[test]
    fn test_row_highlight_paint_selected_no_gutter() {
        use crate::direct::{CellBuffer, DirectTerminalCanvas};

        let mut buffer = CellBuffer::new(20, 5);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        let bounds = Rect::new(2.0, 1.0, 10.0, 1.0);
        let highlight = RowHighlight::new(bounds, true).with_gutter(false);
        highlight.paint(&mut canvas);

        // Should only fill background, no gutter indicator
    }

    // =========================================================================
    // FOCUS RING TESTS
    // =========================================================================

    #[test]
    fn test_focus_ring_new() {
        let bounds = Rect::new(0.0, 0.0, 50.0, 20.0);
        let color = Color::new(0.5, 0.5, 1.0, 1.0);
        let ring = FocusRing::new(bounds, true, color);

        assert_eq!(ring.bounds, bounds);
        assert!(ring.focused);
        assert_eq!(ring.base_color, color);
    }

    #[test]
    fn test_focus_ring_color_blend() {
        let base = Color::new(0.5, 0.5, 1.0, 1.0); // Purple-ish
        let ring = FocusRing::new(Rect::default(), true, base);

        let color = ring.border_color();
        // Should be blended toward cyan
        assert!(color.g > base.g);
    }

    #[test]
    fn test_focus_ring_not_focused_is_dimmed() {
        let base = Color::new(1.0, 0.0, 0.0, 1.0); // Red
        let ring = FocusRing::new(Rect::default(), false, base);

        let color = ring.border_color();
        // Should be dimmed to 40%
        assert!((color.r - 0.4).abs() < 0.01);
        assert!(color.g < 0.01);
        assert!(color.b < 0.01);
    }

    #[test]
    fn test_focus_ring_title_prefix_focused() {
        let ring = FocusRing::new(Rect::default(), true, Color::WHITE);
        assert_eq!(ring.title_prefix(), "");
    }

    #[test]
    fn test_focus_ring_title_prefix_not_focused() {
        let ring = FocusRing::new(Rect::default(), false, Color::WHITE);
        assert_eq!(ring.title_prefix(), "");
    }

    // =========================================================================
    // COLUMN HIGHLIGHT TESTS
    // =========================================================================

    #[test]
    fn test_column_highlight_new() {
        let bounds = Rect::new(10.0, 0.0, 20.0, 1.0);
        let col = ColumnHighlight::new(bounds);

        assert_eq!(col.bounds, bounds);
        assert!(!col.selected);
        assert!(!col.sorted);
        assert!(col.sort_descending);
    }

    #[test]
    fn test_column_highlight_with_selected() {
        let col = ColumnHighlight::new(Rect::default()).with_selected(true);
        assert!(col.selected);

        let col2 = col.with_selected(false);
        assert!(!col2.selected);
    }

    #[test]
    fn test_column_highlight_with_sorted() {
        let col = ColumnHighlight::new(Rect::default()).with_sorted(true, true);
        assert!(col.sorted);
        assert!(col.sort_descending);

        let col2 = col.with_sorted(true, false);
        assert!(col2.sorted);
        assert!(!col2.sort_descending);
    }

    #[test]
    fn test_column_highlight_sort_indicator_descending() {
        let col = ColumnHighlight::new(Rect::default()).with_sorted(true, true);
        assert_eq!(col.sort_indicator(), "");
    }

    #[test]
    fn test_column_highlight_sort_indicator_ascending() {
        let col = ColumnHighlight::new(Rect::default()).with_sorted(true, false);
        assert_eq!(col.sort_indicator(), "");
    }

    #[test]
    fn test_column_highlight_sort_indicator_not_sorted() {
        let col = ColumnHighlight::new(Rect::default());
        assert_eq!(col.sort_indicator(), "");
    }

    #[test]
    fn test_column_highlight_background_selected() {
        let col = ColumnHighlight::new(Rect::default()).with_selected(true);
        let bg = col.background();
        assert!(bg.is_some());
        let bg = bg.unwrap();
        assert!(bg.b > bg.r); // Blue-ish
    }

    #[test]
    fn test_column_highlight_background_not_selected() {
        let col = ColumnHighlight::new(Rect::default());
        assert!(col.background().is_none());
    }

    #[test]
    fn test_column_highlight_text_style_sorted() {
        let col = ColumnHighlight::new(Rect::default()).with_sorted(true, true);
        let style = col.text_style();
        assert_eq!(style.color, SELECTION_ACCENT);
    }

    #[test]
    fn test_column_highlight_text_style_selected() {
        let col = ColumnHighlight::new(Rect::default()).with_selected(true);
        let style = col.text_style();
        assert_eq!(style.color, Color::WHITE);
    }

    #[test]
    fn test_column_highlight_text_style_neither() {
        let col = ColumnHighlight::new(Rect::default());
        let style = col.text_style();
        // Should be gray
        assert!(style.color.r > 0.5 && style.color.r < 0.7);
    }

    // =========================================================================
    // CURSOR TESTS
    // =========================================================================

    #[test]
    fn test_cursor_constants() {
        assert_eq!(Cursor::ROW, "");
        assert_eq!(Cursor::COLUMN, "");
        assert_eq!(Cursor::PANEL, "");
    }

    #[test]
    fn test_cursor_color() {
        assert_eq!(Cursor::color(), SELECTION_ACCENT);
    }

    #[test]
    fn test_cursor_paint_row() {
        use crate::direct::{CellBuffer, DirectTerminalCanvas};

        let mut buffer = CellBuffer::new(20, 5);
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        Cursor::paint_row(&mut canvas, Point::new(0.0, 0.0));
        // Cursor should be painted at position
    }
}