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
//! Notification Center implementation

use super::types::{Notification, NotificationPosition};
use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::utils::char_width;
use crate::widget::theme::SEPARATOR_COLOR;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Notification Center widget
pub struct NotificationCenter {
    /// Active notifications
    notifications: Vec<Notification>,
    /// Maximum visible notifications
    max_visible: usize,
    /// Position on screen
    position: NotificationPosition,
    /// Notification width
    width: u16,
    /// Show icons
    show_icons: bool,
    /// Show progress timer
    show_timer: bool,
    /// Spacing between notifications
    spacing: u16,
    /// Current tick counter
    tick_counter: u64,
    /// Selected notification (for dismissal)
    selected: Option<usize>,
    /// Focused state
    focused: bool,
    /// Widget properties
    props: WidgetProps,
}

impl NotificationCenter {
    /// Create a new notification center
    pub fn new() -> Self {
        Self {
            notifications: Vec::new(),
            max_visible: 5,
            position: NotificationPosition::TopRight,
            width: 40,
            show_icons: true,
            show_timer: true,
            spacing: 1,
            tick_counter: 0,
            selected: None,
            focused: false,
            props: WidgetProps::new(),
        }
    }

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

    /// Set max visible
    pub fn max_visible(mut self, max: usize) -> Self {
        self.max_visible = max.max(1);
        self
    }

    /// Set width
    pub fn width(mut self, width: u16) -> Self {
        self.width = width.max(20);
        self
    }

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

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

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

    /// Set focused state
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Push a new notification
    pub fn push(&mut self, mut notification: Notification) {
        notification.created_at = self.tick_counter;
        self.notifications.push(notification);
    }

    /// Push info notification
    pub fn info(&mut self, message: impl Into<String>) {
        self.push(Notification::info(message));
    }

    /// Push success notification
    pub fn success(&mut self, message: impl Into<String>) {
        self.push(Notification::success(message));
    }

    /// Push warning notification
    pub fn warning(&mut self, message: impl Into<String>) {
        self.push(Notification::warning(message));
    }

    /// Push error notification
    pub fn error(&mut self, message: impl Into<String>) {
        self.push(Notification::error(message));
    }

    /// Dismiss notification by ID
    pub fn dismiss(&mut self, id: u64) {
        self.notifications.retain(|n| n.id != id);
        if self.selected.is_some_and(|s| s >= self.notifications.len()) {
            self.selected = if self.notifications.is_empty() {
                None
            } else {
                Some(self.notifications.len() - 1)
            };
        }
    }

    /// Dismiss selected notification
    pub fn dismiss_selected(&mut self) {
        if let Some(idx) = self.selected {
            if idx < self.notifications.len() {
                let id = self.notifications[idx].id;
                self.dismiss(id);
            }
        }
    }

    /// Clear all notifications
    pub fn clear(&mut self) {
        self.notifications.clear();
        self.selected = None;
    }

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

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

    /// Tick - update timers and remove expired
    pub fn tick(&mut self) {
        self.tick_counter += 1;

        for notification in &mut self.notifications {
            notification.tick += 1;
        }

        self.notifications.retain(|n| !n.is_expired());

        // Adjust selection
        if self.selected.is_some_and(|s| s >= self.notifications.len()) {
            self.selected = if self.notifications.is_empty() {
                None
            } else {
                Some(self.notifications.len() - 1)
            };
        }
    }

    /// Select next notification
    pub fn select_next(&mut self) {
        if self.notifications.is_empty() {
            self.selected = None;
            return;
        }

        self.selected = Some(match self.selected {
            Some(idx) => (idx + 1) % self.notifications.len(),
            None => 0,
        });
    }

    /// Select previous notification
    pub fn select_prev(&mut self) {
        if self.notifications.is_empty() {
            self.selected = None;
            return;
        }

        self.selected = Some(match self.selected {
            Some(0) => self.notifications.len() - 1,
            Some(idx) => idx - 1,
            None => self.notifications.len() - 1,
        });
    }

    /// Handle key input
    pub fn handle_key(&mut self, key: &crate::event::Key) -> bool {
        use crate::event::Key;

        if !self.focused || self.notifications.is_empty() {
            return false;
        }

        match key {
            Key::Up | Key::Char('k') => {
                self.select_prev();
                true
            }
            Key::Down | Key::Char('j') => {
                self.select_next();
                true
            }
            Key::Char('d') | Key::Delete => {
                self.dismiss_selected();
                true
            }
            Key::Char('c') => {
                self.clear();
                true
            }
            _ => false,
        }
    }

    /// Calculate notification height
    fn notification_height(&self, notification: &Notification) -> u16 {
        let mut height = 1; // Message line
        if notification.title.is_some() {
            height += 1;
        }
        if notification.progress.is_some() {
            height += 1;
        }
        if notification.action.is_some() {
            height += 1;
        }
        height + 2 // Border top and bottom
    }
}

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

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

    fn render(&self, ctx: &mut RenderContext) {
        if self.notifications.is_empty() {
            return;
        }

        let area = ctx.area;
        let visible = self
            .notifications
            .iter()
            .rev()
            .take(self.max_visible)
            .collect::<Vec<_>>();

        // Calculate starting position based on notification position (relative coordinates)
        let (start_x, mut current_y, direction): (u16, u16, i16) = match self.position {
            NotificationPosition::TopRight => (area.width.saturating_sub(self.width), 0, 1),
            NotificationPosition::TopLeft => (0, 0, 1),
            NotificationPosition::TopCenter => ((area.width.saturating_sub(self.width)) / 2, 0, 1),
            NotificationPosition::BottomRight => {
                (area.width.saturating_sub(self.width), area.height, -1)
            }
            NotificationPosition::BottomLeft => (0, area.height, -1),
            NotificationPosition::BottomCenter => {
                ((area.width.saturating_sub(self.width)) / 2, area.height, -1)
            }
        };

        // Render each notification
        for (idx, notification) in visible.iter().enumerate() {
            let height = self.notification_height(notification);
            let is_selected = self.selected == Some(self.notifications.len() - 1 - idx);

            // Adjust Y position for bottom positions
            let y = if direction < 0 {
                current_y.saturating_sub(height)
            } else {
                current_y
            };

            if y >= area.height || y + height > area.height {
                continue;
            }

            self.render_notification(ctx, notification, start_x, y, is_selected);

            if direction < 0 {
                current_y = y.saturating_sub(self.spacing);
            } else {
                current_y = y + height + self.spacing;
            }
        }
    }
}

impl NotificationCenter {
    fn render_notification(
        &self,
        ctx: &mut RenderContext,
        notification: &Notification,
        x: u16,
        y: u16,
        is_selected: bool,
    ) {
        let width = self.width;
        let color = notification.level.color();
        let bg = notification.level.bg_color();
        let border_color = if is_selected { Color::WHITE } else { color };

        // Top border
        let mut tl = Cell::new('');
        tl.fg = Some(border_color);
        ctx.set(x, y, tl);

        for dx in 1..width - 1 {
            let mut h = Cell::new('');
            h.fg = Some(border_color);
            ctx.set(x + dx, y, h);
        }

        let mut tr = Cell::new('');
        tr.fg = Some(border_color);
        ctx.set(x + width - 1, y, tr);

        let mut current_y = y + 1;

        // Title line (if present)
        if let Some(ref title) = notification.title {
            let mut left = Cell::new('');
            left.fg = Some(border_color);
            ctx.set(x, current_y, left);

            // Fill background
            for dx in 1..width - 1 {
                let mut cell = Cell::new(' ');
                cell.bg = Some(bg);
                ctx.set(x + dx, current_y, cell);
            }

            // Icon
            let mut content_x = x + 1;
            if self.show_icons {
                let mut icon = Cell::new(notification.level.icon());
                icon.fg = Some(color);
                icon.bg = Some(bg);
                ctx.set(content_x, current_y, icon);
                content_x += 2;
            }

            // Title text
            let mut dx: u16 = 0;
            for ch in title.chars() {
                let cw = char_width(ch) as u16;
                if content_x + dx >= x + width - 2 {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(Color::WHITE);
                cell.bg = Some(bg);
                cell.modifier |= Modifier::BOLD;
                ctx.set(content_x + dx, current_y, cell);
                dx += cw;
            }

            let mut right = Cell::new('');
            right.fg = Some(border_color);
            ctx.set(x + width - 1, current_y, right);

            current_y += 1;
        }

        // Message line
        {
            let mut left = Cell::new('');
            left.fg = Some(border_color);
            ctx.set(x, current_y, left);

            // Fill background
            for dx in 1..width - 1 {
                let mut cell = Cell::new(' ');
                cell.bg = Some(bg);
                ctx.set(x + dx, current_y, cell);
            }

            // Icon (if no title)
            let mut content_x = x + 1;
            if self.show_icons && notification.title.is_none() {
                let mut icon = Cell::new(notification.level.icon());
                icon.fg = Some(color);
                icon.bg = Some(bg);
                ctx.set(content_x, current_y, icon);
                content_x += 2;
            }

            // Message text
            let mut dx: u16 = 0;
            for ch in notification.message.chars() {
                let cw = char_width(ch) as u16;
                if content_x + dx >= x + width - 2 {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(Color::WHITE);
                cell.bg = Some(bg);
                ctx.set(content_x + dx, current_y, cell);
                dx += cw;
            }

            let mut right = Cell::new('');
            right.fg = Some(border_color);
            ctx.set(x + width - 1, current_y, right);

            current_y += 1;
        }

        // Progress line (if present)
        if let Some(progress) = notification.progress {
            let mut left = Cell::new('');
            left.fg = Some(border_color);
            ctx.set(x, current_y, left);

            // Fill background
            for dx in 1..width - 1 {
                let mut cell = Cell::new(' ');
                cell.bg = Some(bg);
                ctx.set(x + dx, current_y, cell);
            }

            // Progress bar
            let bar_width = width - 4;
            let filled = (progress * bar_width as f64).round() as u16;
            for dx in 0..bar_width {
                let ch = if dx < filled { '' } else { '' };
                let fg = if dx < filled { color } else { SEPARATOR_COLOR };
                let mut cell = Cell::new(ch);
                cell.fg = Some(fg);
                cell.bg = Some(bg);
                ctx.set(x + 2 + dx, current_y, cell);
            }

            let mut right = Cell::new('');
            right.fg = Some(border_color);
            ctx.set(x + width - 1, current_y, right);

            current_y += 1;
        }

        // Bottom border with timer
        let mut bl = Cell::new('');
        bl.fg = Some(border_color);
        ctx.set(x, current_y, bl);

        // Timer indicator
        if self.show_timer && notification.duration > 0 {
            let remaining = notification.remaining();
            let timer_width = (width - 4) as f64;
            let timer_filled = (remaining * timer_width).round() as u16;

            for dx in 1..width - 1 {
                let ch = if dx <= timer_filled { '' } else { '' };
                let fg = if dx <= timer_filled {
                    color
                } else {
                    border_color
                };
                let mut cell = Cell::new(ch);
                cell.fg = Some(fg);
                ctx.set(x + dx, current_y, cell);
            }
        } else {
            for dx in 1..width - 1 {
                let mut h = Cell::new('');
                h.fg = Some(border_color);
                ctx.set(x + dx, current_y, h);
            }
        }

        let mut br = Cell::new('');
        br.fg = Some(border_color);
        ctx.set(x + width - 1, current_y, br);
    }
}

impl_styled_view!(NotificationCenter);
impl_props_builders!(NotificationCenter);

/// Helper to create a notification center
pub fn notification_center() -> NotificationCenter {
    NotificationCenter::new()
}