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
//! Panel border creation utilities for ptop.
//!
//! Creates visually distinct borders for focused and unfocused panels.

#![allow(dead_code)]

use crate::{Border, BorderStyle};
use presentar_core::Color;

/// Focus accent color (bright cyan) for blending with panel colors.
pub const FOCUS_ACCENT_COLOR: Color = Color {
    r: 0.2,
    g: 0.8,
    b: 1.0,
    a: 1.0,
};

/// Create a panel border with appropriate styling for focus state.
///
/// # Arguments
///
/// * `title` - Panel title text
/// * `color` - Base panel color
/// * `is_focused` - Whether this panel currently has focus
///
/// # Returns
///
/// A configured `Border` widget with:
/// - Double border style for focused panels (WCAG AAA compliant)
/// - Rounded border style for unfocused panels
/// - Accent-blended color for focused, dimmed for unfocused
/// - Focus arrow indicator (`►`) in title when focused
#[must_use]
pub fn create_panel_border(title: &str, color: Color, is_focused: bool) -> Border {
    let style = if is_focused {
        BorderStyle::Double // Double border for focused panel
    } else {
        BorderStyle::Rounded // Normal rounded border
    };

    // Use accent color for focused, dim for unfocused
    let border_color = if is_focused {
        blend_with_accent(color)
    } else {
        dim_color(color)
    };

    // Add focus indicator to title
    let display_title = if is_focused {
        format!("{title}")
    } else {
        title.to_string()
    };

    Border::new()
        .with_title(&display_title)
        .with_style(style)
        .with_color(border_color)
        .with_title_left_aligned()
}

/// Blend a color with the focus accent color (50/50 mix).
#[must_use]
pub fn blend_with_accent(color: Color) -> Color {
    Color {
        r: (color.r * 0.5 + FOCUS_ACCENT_COLOR.r * 0.5).min(1.0),
        g: (color.g * 0.5 + FOCUS_ACCENT_COLOR.g * 0.5).min(1.0),
        b: (color.b * 0.5 + FOCUS_ACCENT_COLOR.b * 0.5).min(1.0),
        a: color.a,
    }
}

/// Dim a color by 50% for unfocused state.
#[must_use]
pub fn dim_color(color: Color) -> Color {
    Color {
        r: color.r * 0.5,
        g: color.g * 0.5,
        b: color.b * 0.5,
        a: color.a,
    }
}

/// Brighten a color for highlights.
#[must_use]
pub fn brighten_color(color: Color, factor: f32) -> Color {
    Color {
        r: (color.r * factor).min(1.0),
        g: (color.g * factor).min(1.0),
        b: (color.b * factor).min(1.0),
        a: color.a,
    }
}

/// Darken a color for shadows.
#[must_use]
pub fn darken_color(color: Color, factor: f32) -> Color {
    Color {
        r: color.r * factor,
        g: color.g * factor,
        b: color.b * factor,
        a: color.a,
    }
}

/// Interpolate between two colors.
#[must_use]
pub fn lerp_color(from: Color, to: Color, t: f32) -> Color {
    let t = t.clamp(0.0, 1.0);
    Color {
        r: from.r + (to.r - from.r) * t,
        g: from.g + (to.g - from.g) * t,
        b: from.b + (to.b - from.b) * t,
        a: from.a + (to.a - from.a) * t,
    }
}

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

    const TEST_COLOR: Color = Color {
        r: 0.5,
        g: 0.6,
        b: 0.7,
        a: 1.0,
    };

    // F-BORDER-001: Focus accent color is bright cyan
    #[test]
    fn test_focus_accent_color_is_cyan() {
        assert!(FOCUS_ACCENT_COLOR.b > FOCUS_ACCENT_COLOR.r);
        assert!(FOCUS_ACCENT_COLOR.g > 0.5);
        assert!(FOCUS_ACCENT_COLOR.b > 0.5);
    }

    // F-BORDER-002: Focused border has double style
    #[test]
    fn test_focused_border_double_style() {
        let border = create_panel_border("Test", TEST_COLOR, true);
        // Border should be created without panicking
        let _ = border;
    }

    // F-BORDER-003: Unfocused border has rounded style
    #[test]
    fn test_unfocused_border_rounded_style() {
        let border = create_panel_border("Test", TEST_COLOR, false);
        let _ = border;
    }

    // F-BORDER-004: Focused title has arrow indicator
    #[test]
    fn test_focused_title_has_arrow() {
        // We can't easily test the title directly, but the function should work
        let _border = create_panel_border("CPU", TEST_COLOR, true);
    }

    // F-BORDER-005: Unfocused title has no arrow
    #[test]
    fn test_unfocused_title_no_arrow() {
        let _border = create_panel_border("CPU", TEST_COLOR, false);
    }

    // F-BORDER-006: Blend with accent produces valid color
    #[test]
    fn test_blend_with_accent_valid() {
        let blended = blend_with_accent(TEST_COLOR);
        assert!(blended.r >= 0.0 && blended.r <= 1.0);
        assert!(blended.g >= 0.0 && blended.g <= 1.0);
        assert!(blended.b >= 0.0 && blended.b <= 1.0);
        assert!((blended.a - 1.0).abs() < 0.001);
    }

    // F-BORDER-007: Blend with accent increases blue component
    #[test]
    fn test_blend_with_accent_increases_blue() {
        let original = Color {
            r: 0.5,
            g: 0.5,
            b: 0.3,
            a: 1.0,
        };
        let blended = blend_with_accent(original);
        // Blue should increase towards accent's high blue
        assert!(blended.b > original.b * 0.5);
    }

    // F-BORDER-008: Dim color reduces brightness
    #[test]
    fn test_dim_color_reduces_brightness() {
        let dimmed = dim_color(TEST_COLOR);
        assert!(dimmed.r < TEST_COLOR.r);
        assert!(dimmed.g < TEST_COLOR.g);
        assert!(dimmed.b < TEST_COLOR.b);
    }

    // F-BORDER-009: Dim color is exactly 50%
    #[test]
    fn test_dim_color_is_half() {
        let dimmed = dim_color(TEST_COLOR);
        assert!((dimmed.r - TEST_COLOR.r * 0.5).abs() < 0.001);
        assert!((dimmed.g - TEST_COLOR.g * 0.5).abs() < 0.001);
        assert!((dimmed.b - TEST_COLOR.b * 0.5).abs() < 0.001);
    }

    // F-BORDER-010: Dim color preserves alpha
    #[test]
    fn test_dim_color_preserves_alpha() {
        let dimmed = dim_color(TEST_COLOR);
        assert!((dimmed.a - TEST_COLOR.a).abs() < 0.001);
    }

    // F-BORDER-011: Brighten color increases brightness
    #[test]
    fn test_brighten_color() {
        let brightened = brighten_color(TEST_COLOR, 1.5);
        assert!(brightened.r >= TEST_COLOR.r);
        assert!(brightened.g >= TEST_COLOR.g);
        assert!(brightened.b >= TEST_COLOR.b);
    }

    // F-BORDER-012: Brighten color clamps to 1.0
    #[test]
    fn test_brighten_color_clamps() {
        let bright = Color {
            r: 0.9,
            g: 0.9,
            b: 0.9,
            a: 1.0,
        };
        let brightened = brighten_color(bright, 2.0);
        assert!(brightened.r <= 1.0);
        assert!(brightened.g <= 1.0);
        assert!(brightened.b <= 1.0);
    }

    // F-BORDER-013: Darken color reduces brightness
    #[test]
    fn test_darken_color() {
        let darkened = darken_color(TEST_COLOR, 0.5);
        assert!(darkened.r < TEST_COLOR.r);
        assert!(darkened.g < TEST_COLOR.g);
        assert!(darkened.b < TEST_COLOR.b);
    }

    // F-BORDER-014: Darken preserves alpha
    #[test]
    fn test_darken_preserves_alpha() {
        let darkened = darken_color(TEST_COLOR, 0.5);
        assert!((darkened.a - TEST_COLOR.a).abs() < 0.001);
    }

    // F-BORDER-015: Lerp at t=0 returns from color
    #[test]
    fn test_lerp_t_zero() {
        let from = Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        };
        let to = Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
            a: 1.0,
        };
        let result = lerp_color(from, to, 0.0);
        assert!((result.r - 0.0).abs() < 0.001);
        assert!((result.g - 0.0).abs() < 0.001);
        assert!((result.b - 0.0).abs() < 0.001);
    }

    // F-BORDER-016: Lerp at t=1 returns to color
    #[test]
    fn test_lerp_t_one() {
        let from = Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        };
        let to = Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
            a: 1.0,
        };
        let result = lerp_color(from, to, 1.0);
        assert!((result.r - 1.0).abs() < 0.001);
        assert!((result.g - 1.0).abs() < 0.001);
        assert!((result.b - 1.0).abs() < 0.001);
    }

    // F-BORDER-017: Lerp at t=0.5 returns midpoint
    #[test]
    fn test_lerp_t_half() {
        let from = Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        };
        let to = Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
            a: 1.0,
        };
        let result = lerp_color(from, to, 0.5);
        assert!((result.r - 0.5).abs() < 0.001);
        assert!((result.g - 0.5).abs() < 0.001);
        assert!((result.b - 0.5).abs() < 0.001);
    }

    // F-BORDER-018: Lerp clamps t below 0
    #[test]
    fn test_lerp_clamps_below_zero() {
        let from = Color {
            r: 0.5,
            g: 0.5,
            b: 0.5,
            a: 1.0,
        };
        let to = Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
            a: 1.0,
        };
        let result = lerp_color(from, to, -0.5);
        assert!((result.r - 0.5).abs() < 0.001);
    }

    // F-BORDER-019: Lerp clamps t above 1
    #[test]
    fn test_lerp_clamps_above_one() {
        let from = Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        };
        let to = Color {
            r: 0.5,
            g: 0.5,
            b: 0.5,
            a: 1.0,
        };
        let result = lerp_color(from, to, 1.5);
        assert!((result.r - 0.5).abs() < 0.001);
    }

    // F-BORDER-020: Lerp interpolates alpha
    #[test]
    fn test_lerp_interpolates_alpha() {
        let from = Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 0.0,
        };
        let to = Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
            a: 1.0,
        };
        let result = lerp_color(from, to, 0.5);
        assert!((result.a - 0.5).abs() < 0.001);
    }

    // F-BORDER-021: Empty title creates border
    #[test]
    fn test_empty_title_border() {
        let _border = create_panel_border("", TEST_COLOR, false);
    }

    // F-BORDER-022: Long title creates border
    #[test]
    fn test_long_title_border() {
        let long_title = "This is a very long title that might overflow";
        let _border = create_panel_border(long_title, TEST_COLOR, false);
    }

    // F-BORDER-023: White color dims correctly
    #[test]
    fn test_white_dims_correctly() {
        let white = Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
            a: 1.0,
        };
        let dimmed = dim_color(white);
        assert!((dimmed.r - 0.5).abs() < 0.001);
        assert!((dimmed.g - 0.5).abs() < 0.001);
        assert!((dimmed.b - 0.5).abs() < 0.001);
    }

    // F-BORDER-024: Black color dims to black
    #[test]
    fn test_black_dims_to_black() {
        let black = Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        };
        let dimmed = dim_color(black);
        assert!(dimmed.r.abs() < 0.001);
        assert!(dimmed.g.abs() < 0.001);
        assert!(dimmed.b.abs() < 0.001);
    }

    // F-BORDER-025: Blend produces distinct color
    #[test]
    fn test_blend_produces_distinct_color() {
        let original = Color {
            r: 1.0,
            g: 0.0,
            b: 0.0,
            a: 1.0,
        };
        let blended = blend_with_accent(original);
        // Should be different from original
        assert!((blended.r - original.r).abs() > 0.1);
    }
}