revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Timeline widget for activity feeds and event logs
//!
//! Displays chronological events with timestamps and icons.

use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::utils::{char_width, display_width, truncate_to_width};
use crate::widget::theme::{DARK_GRAY, LIGHT_GRAY, MUTED_TEXT};
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Timeline event type
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum EventType {
    /// Informational event (default)
    #[default]
    Info,
    /// Success/completed event
    Success,
    /// Warning event
    Warning,
    /// Error/failed event
    Error,
    /// Custom event with icon
    Custom(char),
}

impl EventType {
    /// Get icon for event type
    pub fn icon(&self) -> char {
        match self {
            EventType::Info => '',
            EventType::Success => '',
            EventType::Warning => '',
            EventType::Error => '',
            EventType::Custom(c) => *c,
        }
    }

    /// Get color for event type
    pub fn color(&self) -> Color {
        match self {
            EventType::Info => Color::CYAN,
            EventType::Success => Color::GREEN,
            EventType::Warning => Color::YELLOW,
            EventType::Error => Color::RED,
            EventType::Custom(_) => Color::WHITE,
        }
    }
}

/// A timeline event
#[derive(Clone, Debug)]
pub struct TimelineEvent {
    /// Event title
    pub title: String,
    /// Event description
    pub description: Option<String>,
    /// Timestamp display
    pub timestamp: Option<String>,
    /// Event type
    pub event_type: EventType,
    /// Custom color override
    pub color: Option<Color>,
    /// Additional metadata
    pub metadata: Vec<(String, String)>,
}

impl TimelineEvent {
    /// Create a new event
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            description: None,
            timestamp: None,
            event_type: EventType::Info,
            color: None,
            metadata: Vec::new(),
        }
    }

    /// Set description
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set timestamp
    pub fn timestamp(mut self, ts: impl Into<String>) -> Self {
        self.timestamp = Some(ts.into());
        self
    }

    /// Set event type
    pub fn event_type(mut self, t: EventType) -> Self {
        self.event_type = t;
        self
    }

    /// Set as success event
    pub fn success(mut self) -> Self {
        self.event_type = EventType::Success;
        self
    }

    /// Set as warning event
    pub fn warning(mut self) -> Self {
        self.event_type = EventType::Warning;
        self
    }

    /// Set as error event
    pub fn error(mut self) -> Self {
        self.event_type = EventType::Error;
        self
    }

    /// Set custom color
    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Add metadata
    pub fn meta(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.push((key.into(), value.into()));
        self
    }

    /// Get display color
    pub fn display_color(&self) -> Color {
        self.color.unwrap_or_else(|| self.event_type.color())
    }
}

/// Timeline orientation
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TimelineOrientation {
    /// Vertical timeline (events stacked)
    #[default]
    Vertical,
    /// Horizontal timeline (events side by side)
    Horizontal,
}

/// Timeline style
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TimelineStyle {
    /// Simple line with dots
    #[default]
    Line,
    /// Connected boxes
    Boxed,
    /// Minimal (no line)
    Minimal,
    /// Alternating sides
    Alternating,
}

/// Timeline widget
pub struct Timeline {
    /// Events
    events: Vec<TimelineEvent>,
    /// Orientation
    orientation: TimelineOrientation,
    /// Style
    style: TimelineStyle,
    /// Selected event index
    selected: Option<usize>,
    /// Scroll offset
    scroll: usize,
    /// Show timestamps
    show_timestamps: bool,
    /// Show descriptions
    show_descriptions: bool,
    /// Line color
    line_color: Color,
    /// Timestamp color
    timestamp_color: Color,
    /// Title color
    title_color: Color,
    /// Description color
    desc_color: Color,
    /// Widget props for CSS integration
    props: WidgetProps,
}

impl Timeline {
    /// Create a new timeline
    pub fn new() -> Self {
        Self {
            events: Vec::new(),
            orientation: TimelineOrientation::Vertical,
            style: TimelineStyle::Line,
            selected: None,
            scroll: 0,
            show_timestamps: true,
            show_descriptions: true,
            line_color: DARK_GRAY,
            timestamp_color: LIGHT_GRAY,
            title_color: Color::WHITE,
            desc_color: MUTED_TEXT,
            props: WidgetProps::new(),
        }
    }

    /// Add an event
    pub fn event(mut self, event: TimelineEvent) -> Self {
        self.events.push(event);
        self
    }

    /// Add events
    pub fn events(mut self, events: Vec<TimelineEvent>) -> Self {
        self.events.extend(events);
        self
    }

    /// Set orientation
    pub fn orientation(mut self, orientation: TimelineOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Set vertical orientation
    pub fn vertical(mut self) -> Self {
        self.orientation = TimelineOrientation::Vertical;
        self
    }

    /// Set horizontal orientation
    pub fn horizontal(mut self) -> Self {
        self.orientation = TimelineOrientation::Horizontal;
        self
    }

    /// Set style
    pub fn style(mut self, style: TimelineStyle) -> Self {
        self.style = style;
        self
    }

    /// Show/hide timestamps
    pub fn timestamps(mut self, show: bool) -> Self {
        self.show_timestamps = show;
        self
    }

    /// Show/hide descriptions
    pub fn descriptions(mut self, show: bool) -> Self {
        self.show_descriptions = show;
        self
    }

    /// Set line color
    pub fn line_color(mut self, color: Color) -> Self {
        self.line_color = color;
        self
    }

    /// Select an event
    pub fn select(&mut self, index: Option<usize>) {
        self.selected = index;
    }

    /// Select next event
    pub fn select_next(&mut self) {
        match self.selected {
            Some(i) if i < self.events.len() - 1 => self.selected = Some(i + 1),
            None if !self.events.is_empty() => self.selected = Some(0),
            _ => {}
        }
    }

    /// Select previous event
    pub fn select_prev(&mut self) {
        match self.selected {
            Some(i) if i > 0 => self.selected = Some(i - 1),
            _ => {}
        }
    }

    /// Get selected event
    pub fn selected_event(&self) -> Option<&TimelineEvent> {
        self.selected.and_then(|i| self.events.get(i))
    }

    /// Clear events
    pub fn clear(&mut self) {
        self.events.clear();
        self.selected = None;
        self.scroll = 0;
    }

    /// Add event dynamically
    pub fn push(&mut self, event: TimelineEvent) {
        self.events.push(event);
    }

    /// Get event count
    pub fn len(&self) -> usize {
        self.events.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.events.is_empty()
    }
}

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

impl View for Timeline {
    crate::impl_view_meta!("Timeline");

    fn render(&self, ctx: &mut RenderContext) {
        match self.orientation {
            TimelineOrientation::Vertical => self.render_vertical(ctx),
            TimelineOrientation::Horizontal => self.render_horizontal(ctx),
        }
    }
}

impl_styled_view!(Timeline);
impl_props_builders!(Timeline);

impl Timeline {
    fn render_vertical(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        if self.events.is_empty() || area.height < 2 {
            return;
        }

        let timestamp_width = if self.show_timestamps { 12 } else { 0 };
        let icon_x = timestamp_width;
        let content_x = icon_x + 3;
        let content_width = area.width.saturating_sub(timestamp_width + 3);

        let mut y = 0u16;

        for (i, event) in self.events.iter().enumerate().skip(self.scroll) {
            if y >= area.height {
                break;
            }

            let is_selected = self.selected == Some(i);
            let color = event.display_color();

            // Draw timestamp
            if self.show_timestamps {
                if let Some(ref ts) = event.timestamp {
                    let truncated = truncate_to_width(ts, timestamp_width as usize - 1);
                    let mut dx: u16 = 0;
                    for ch in truncated.chars() {
                        let cw = char_width(ch) as u16;
                        if dx + cw > timestamp_width - 1 {
                            break;
                        }
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(self.timestamp_color);
                        ctx.set(dx, y, cell);
                        dx += cw;
                    }
                }
            }

            // Draw icon
            let icon = event.event_type.icon();
            let mut icon_cell = Cell::new(icon);
            icon_cell.fg = Some(color);
            if is_selected {
                icon_cell.modifier |= Modifier::BOLD;
            }
            ctx.set(icon_x, y, icon_cell);

            // Draw line (except for last item)
            if i < self.events.len() - 1 && self.style != TimelineStyle::Minimal {
                let line_char = match self.style {
                    TimelineStyle::Line => '',
                    TimelineStyle::Boxed => '',
                    TimelineStyle::Alternating => '',
                    TimelineStyle::Minimal => ' ',
                };
                let line_y = y + 1;
                if line_y < area.height {
                    let mut line_cell = Cell::new(line_char);
                    line_cell.fg = Some(self.line_color);
                    ctx.set(icon_x, line_y, line_cell);
                }
            }

            // Draw connector
            let connector = match self.style {
                TimelineStyle::Line | TimelineStyle::Alternating => '',
                TimelineStyle::Boxed => '',
                TimelineStyle::Minimal => ' ',
            };
            if self.style != TimelineStyle::Minimal {
                let mut conn_cell = Cell::new(connector);
                conn_cell.fg = Some(self.line_color);
                ctx.set(icon_x + 1, y, conn_cell);
            }

            // Draw title
            let title_fg = if is_selected { color } else { self.title_color };
            let title_truncated = truncate_to_width(&event.title, content_width as usize);
            let mut dx: u16 = 0;
            for ch in title_truncated.chars() {
                let cw = char_width(ch) as u16;
                if dx + cw > content_width {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(title_fg);
                if is_selected {
                    cell.modifier |= Modifier::BOLD;
                }
                ctx.set(content_x + dx, y, cell);
                dx += cw;
            }

            y += 1;

            // Draw description
            if self.show_descriptions {
                if let Some(ref desc) = event.description {
                    if y < area.height {
                        // Draw line continuation
                        if i < self.events.len() - 1 && self.style != TimelineStyle::Minimal {
                            let mut line_cell = Cell::new('');
                            line_cell.fg = Some(self.line_color);
                            ctx.set(icon_x, y, line_cell);
                        }

                        // Draw description text
                        let desc_truncated = truncate_to_width(desc, content_width as usize);
                        let mut dx: u16 = 0;
                        for ch in desc_truncated.chars() {
                            let cw = char_width(ch) as u16;
                            if dx + cw > content_width {
                                break;
                            }
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(self.desc_color);
                            ctx.set(content_x + dx, y, cell);
                            dx += cw;
                        }

                        y += 1;
                    }
                }
            }

            // Add spacing between events
            if y < area.height && i < self.events.len() - 1 {
                if self.style != TimelineStyle::Minimal {
                    let mut line_cell = Cell::new('');
                    line_cell.fg = Some(self.line_color);
                    ctx.set(icon_x, y, line_cell);
                }
                y += 1;
            }
        }
    }

    fn render_horizontal(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        if self.events.is_empty() || area.width < 10 {
            return;
        }

        let event_width = 15u16;
        let line_y = 1u16;

        // Draw horizontal line
        for x in 0..area.width {
            let mut cell = Cell::new('');
            cell.fg = Some(self.line_color);
            ctx.set(x, line_y, cell);
        }

        // Draw events
        let mut x = 0u16;
        for (i, event) in self.events.iter().enumerate() {
            if x >= area.width {
                break;
            }

            let is_selected = self.selected == Some(i);
            let color = event.display_color();

            // Draw icon
            let icon = event.event_type.icon();
            let mut icon_cell = Cell::new(icon);
            icon_cell.fg = Some(color);
            if is_selected {
                icon_cell.modifier |= Modifier::BOLD;
            }
            ctx.set(x + event_width / 2, line_y, icon_cell);

            // Draw title above
            let title = truncate_to_width(&event.title, event_width as usize - 1);
            let title_x = x + (event_width.saturating_sub(display_width(title) as u16)) / 2;
            let mut dx: u16 = 0;
            for ch in title.chars() {
                let cw = char_width(ch) as u16;
                if dx + cw > event_width - 1 {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(if is_selected { color } else { self.title_color });
                ctx.set(title_x + dx, 0, cell);
                dx += cw;
            }

            // Draw timestamp below
            if self.show_timestamps {
                if let Some(ref ts) = event.timestamp {
                    let ts_str = truncate_to_width(ts, event_width as usize - 1);
                    let ts_x = x + (event_width.saturating_sub(display_width(ts_str) as u16)) / 2;
                    let mut dx: u16 = 0;
                    for ch in ts_str.chars() {
                        let cw = char_width(ch) as u16;
                        if dx + cw > event_width - 1 {
                            break;
                        }
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(self.timestamp_color);
                        ctx.set(ts_x + dx, line_y + 1, cell);
                        dx += cw;
                    }
                }
            }

            x += event_width;
        }
    }
}

// Helper functions

/// Create a new timeline widget
pub fn timeline() -> Timeline {
    Timeline::new()
}

/// Create a new timeline event with title
pub fn timeline_event(title: impl Into<String>) -> TimelineEvent {
    TimelineEvent::new(title)
}

// Private tests - KEEP HERE: uses internal RenderContext, Buffer, or private fields
#[cfg(test)]
mod tests {
    use super::*;
    use crate::layout::Rect;
    use crate::render::Buffer;

    // KEEP HERE: uses internal RenderContext and Buffer
    #[test]
    fn test_timeline_render() {
        let mut buffer = Buffer::new(60, 20);
        let area = Rect::new(0, 0, 60, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let tl = Timeline::new()
            .event(TimelineEvent::new("Event 1").timestamp("10:00"))
            .event(TimelineEvent::new("Event 2").timestamp("11:00"));

        tl.render(&mut ctx);
        // Smoke test
    }

    // KEEP HERE: uses internal RenderContext and Buffer
    #[test]
    fn test_render_horizontal() {
        let mut buffer = Buffer::new(60, 10);
        let area = Rect::new(0, 0, 60, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let tl = Timeline::new()
            .horizontal()
            .event(TimelineEvent::new("Event 1").timestamp("10:00"))
            .event(TimelineEvent::new("Event 2").timestamp("11:00"));

        tl.render(&mut ctx); // Should not panic
    }

    // KEEP HERE: uses internal RenderContext and Buffer
    #[test]
    fn test_render_empty() {
        let mut buffer = Buffer::new(60, 10);
        let area = Rect::new(0, 0, 60, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let tl = Timeline::new();
        tl.render(&mut ctx); // Should return early without panicking
    }

    // KEEP HERE: uses internal RenderContext and Buffer
    #[test]
    fn test_render_with_descriptions() {
        let mut buffer = Buffer::new(60, 10);
        let area = Rect::new(0, 0, 60, 10);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let tl = Timeline::new()
            .descriptions(true)
            .event(TimelineEvent::new("Event").description("Details here"));

        tl.render(&mut ctx);
    }

    // KEEP HERE: accesses private field `scroll`
    #[test]
    fn test_clear() {
        let mut tl = Timeline::new()
            .event(TimelineEvent::new("A"))
            .event(TimelineEvent::new("B"))
            .event(TimelineEvent::new("C"));

        tl.select_next();
        tl.clear();

        assert!(tl.is_empty());
        assert_eq!(tl.selected, None);
        assert_eq!(tl.scroll, 0);
    }
}

// Keep private tests that require private field access here

#[test]
fn test_timeline_render_private() {
    // Test private render methods - keeping in source
    let _t = Timeline::new().event(TimelineEvent::new("Test"));

    // This would require accessing private render methods
    // Test kept inline due to private access
}