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
//! Status Indicator widget for displaying online/offline/busy states
//!
//! Provides visual feedback for connection status, user availability, or system health.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::{StatusIndicator, Status, status_indicator};
//!
//! // Basic online indicator
//! StatusIndicator::online();
//!
//! // With label
//! StatusIndicator::busy().label("Do not disturb");
//!
//! // Custom status
//! status_indicator(Status::Away)
//!     .size(StatusSize::Large)
//!     .pulsing(true);
//! ```

use crate::render::Cell;
use crate::style::Color;
use crate::widget::theme::{DARK_BG, SECONDARY_TEXT};
use crate::widget::traits::{RenderContext, View, WidgetProps, WidgetState};
use crate::{impl_styled_view, impl_widget_builders};
use unicode_width::UnicodeWidthChar;

/// Predefined status states
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Status {
    /// Online/available (green)
    #[default]
    Online,
    /// Offline/disconnected (gray)
    Offline,
    /// Busy/do not disturb (red)
    Busy,
    /// Away/idle (yellow)
    Away,
    /// Unknown/connecting (gray with question)
    Unknown,
    /// Error state (red with warning)
    Error,
    /// Custom status with a color
    Custom(Color),
}

impl Status {
    /// Get the color for this status
    pub fn color(&self) -> Color {
        match self {
            Status::Online => Color::rgb(34, 197, 94),    // Green
            Status::Offline => Color::rgb(107, 114, 128), // Gray
            Status::Busy => Color::rgb(239, 68, 68),      // Red
            Status::Away => Color::rgb(234, 179, 8),      // Yellow
            Status::Unknown => Color::rgb(156, 163, 175), // Light gray
            Status::Error => Color::rgb(220, 38, 38),     // Darker red
            Status::Custom(color) => *color,
        }
    }

    /// Get the default label for this status
    pub fn label(&self) -> &'static str {
        match self {
            Status::Online => "Online",
            Status::Offline => "Offline",
            Status::Busy => "Busy",
            Status::Away => "Away",
            Status::Unknown => "Unknown",
            Status::Error => "Error",
            Status::Custom(_) => "Custom",
        }
    }

    /// Get the icon for this status
    pub fn icon(&self) -> char {
        match self {
            Status::Online => '',
            Status::Offline => '',
            Status::Busy => '',
            Status::Away => '',
            Status::Unknown => '?',
            Status::Error => '!',
            Status::Custom(_) => '',
        }
    }
}

/// Size variants for the status indicator
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum StatusSize {
    /// Small dot (1 char)
    Small,
    /// Medium dot (default)
    #[default]
    Medium,
    /// Large dot with more visual presence
    Large,
}

impl StatusSize {
    /// Get the dot character for this size
    pub fn dot(&self) -> char {
        match self {
            StatusSize::Small => '',
            StatusSize::Medium => '',
            StatusSize::Large => '',
        }
    }

    /// Get the width for this size (for rendering with label)
    pub fn width(&self) -> u16 {
        match self {
            StatusSize::Small => 1,
            StatusSize::Medium => 1,
            StatusSize::Large => 2,
        }
    }
}

/// Status indicator style
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum StatusStyle {
    /// Just the dot indicator
    #[default]
    Dot,
    /// Dot with text label
    DotWithLabel,
    /// Text label only
    LabelOnly,
    /// Badge style (filled background)
    Badge,
}

/// A status indicator widget for displaying availability/connection states
///
/// Shows online/offline/busy states with consistent visual styling.
#[derive(Clone)]
pub struct StatusIndicator {
    /// Current status
    status: Status,
    /// Size variant
    size: StatusSize,
    /// Display style
    style: StatusStyle,
    /// Custom label (overrides default)
    custom_label: Option<String>,
    /// Enable pulsing animation
    pulsing: bool,
    /// Animation frame counter
    frame: usize,
    /// Widget state
    state: WidgetState,
    /// Widget properties
    props: WidgetProps,
}

impl StatusIndicator {
    /// Create a new status indicator with the given status
    pub fn new(status: Status) -> Self {
        Self {
            status,
            size: StatusSize::default(),
            style: StatusStyle::default(),
            custom_label: None,
            pulsing: false,
            frame: 0,
            state: WidgetState::new(),
            props: WidgetProps::new(),
        }
    }

    /// Create an online status indicator
    pub fn online() -> Self {
        Self::new(Status::Online)
    }

    /// Create an offline status indicator
    pub fn offline() -> Self {
        Self::new(Status::Offline)
    }

    /// Create a busy status indicator
    pub fn busy() -> Self {
        Self::new(Status::Busy)
    }

    /// Create an away status indicator
    pub fn away() -> Self {
        Self::new(Status::Away)
    }

    /// Create an unknown status indicator
    pub fn unknown() -> Self {
        Self::new(Status::Unknown)
    }

    /// Create an error status indicator
    pub fn error() -> Self {
        Self::new(Status::Error)
    }

    /// Create a custom status indicator with a specific color
    pub fn custom(color: Color) -> Self {
        Self::new(Status::Custom(color))
    }

    /// Set the status
    pub fn status(mut self, status: Status) -> Self {
        self.status = status;
        self
    }

    /// Set the size
    pub fn size(mut self, size: StatusSize) -> Self {
        self.size = size;
        self
    }

    /// Set the display style
    pub fn indicator_style(mut self, style: StatusStyle) -> Self {
        self.style = style;
        self
    }

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

    /// Enable/disable pulsing animation
    pub fn pulsing(mut self, pulsing: bool) -> Self {
        self.pulsing = pulsing;
        self
    }

    /// Update animation frame
    pub fn tick(&mut self) {
        self.frame = self.frame.wrapping_add(1);
    }

    /// Get current status
    pub fn get_status(&self) -> Status {
        self.status
    }

    /// Set status mutably
    pub fn set_status(&mut self, status: Status) {
        self.status = status;
    }

    /// Get the label to display
    fn get_label(&self) -> &str {
        self.custom_label
            .as_deref()
            .unwrap_or_else(|| self.status.label())
    }

    /// Check if currently visible (for pulsing animation)
    fn is_visible(&self) -> bool {
        if !self.pulsing {
            return true;
        }
        // Pulse every 8 frames (visible for 6, hidden for 2)
        (self.frame % 8) < 6
    }

    /// Calculate total width needed
    pub fn width(&self) -> u16 {
        match self.style {
            StatusStyle::Dot => self.size.width(),
            StatusStyle::DotWithLabel => {
                let label_len = crate::utils::display_width(self.get_label()) as u16;
                self.size.width() + 1 + label_len // dot + space + label
            }
            StatusStyle::LabelOnly => crate::utils::display_width(self.get_label()) as u16,
            StatusStyle::Badge => {
                let label_len = crate::utils::display_width(self.get_label()) as u16;
                label_len + 4 // padding + dot + space + label + padding
            }
        }
    }
}

impl Default for StatusIndicator {
    fn default() -> Self {
        Self::new(Status::Online)
    }
}

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

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        if area.width < 1 || area.height < 1 {
            return;
        }

        let color = self.status.color();
        let visible = self.is_visible();

        match self.style {
            StatusStyle::Dot => {
                self.render_dot(ctx, color, visible);
            }
            StatusStyle::DotWithLabel => {
                self.render_dot_with_label(ctx, color, visible);
            }
            StatusStyle::LabelOnly => {
                self.render_label_only(ctx, color);
            }
            StatusStyle::Badge => {
                self.render_badge(ctx, color, visible);
            }
        }
    }
}

impl StatusIndicator {
    fn render_dot(&self, ctx: &mut RenderContext, color: Color, visible: bool) {
        let area = ctx.area;
        let dot = if visible { self.size.dot() } else { ' ' };

        let mut cell = Cell::new(dot);
        cell.fg = Some(color);
        ctx.set(0, 0, cell);

        // For large size, add extra visual
        if self.size == StatusSize::Large && area.width > 1 {
            let mut cell2 = Cell::new(' ');
            cell2.bg = Some(color);
            ctx.set(1, 0, cell2);
        }
    }

    fn render_dot_with_label(&self, ctx: &mut RenderContext, color: Color, visible: bool) {
        let area = ctx.area;

        // Render dot
        let dot = if visible { self.size.dot() } else { ' ' };
        let mut dot_cell = Cell::new(dot);
        dot_cell.fg = Some(color);
        ctx.set(0, 0, dot_cell);

        // Render label
        let label = self.get_label();
        let label_start = self.size.width() + 1;
        let max_label_width = area.width.saturating_sub(self.size.width() + 1);

        let mut offset = 0u16;
        for ch in label.chars() {
            let char_width = ch.width().unwrap_or(0) as u16;
            if char_width == 0 {
                continue;
            }
            if offset + char_width > max_label_width {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(SECONDARY_TEXT);
            ctx.set(label_start + offset, 0, cell);
            for i in 1..char_width {
                ctx.set(label_start + offset + i, 0, Cell::continuation());
            }
            offset += char_width;
        }
    }

    fn render_label_only(&self, ctx: &mut RenderContext, color: Color) {
        let area = ctx.area;
        let label = self.get_label();

        let mut offset = 0u16;
        for ch in label.chars() {
            let char_width = ch.width().unwrap_or(0) as u16;
            if char_width == 0 {
                continue;
            }
            if offset + char_width > area.width {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(color);
            ctx.set(offset, 0, cell);
            for i in 1..char_width {
                ctx.set(offset + i, 0, Cell::continuation());
            }
            offset += char_width;
        }
    }

    fn render_badge(&self, ctx: &mut RenderContext, color: Color, visible: bool) {
        let area = ctx.area;
        let label = self.get_label();

        // Background
        let bg_color = DARK_BG;
        let total_width = self.width().min(area.width);

        for i in 0..total_width {
            let mut cell = Cell::new(' ');
            cell.bg = Some(bg_color);
            ctx.set(i, 0, cell);
        }

        // Dot
        let dot = if visible { '' } else { ' ' };
        let mut dot_cell = Cell::new(dot);
        dot_cell.fg = Some(color);
        dot_cell.bg = Some(bg_color);
        ctx.set(1, 0, dot_cell);

        // Label
        let label_start: u16 = 3;
        let max_label_width = total_width.saturating_sub(4);
        let mut offset = 0u16;
        for ch in label.chars() {
            let char_width = ch.width().unwrap_or(0) as u16;
            if char_width == 0 {
                continue;
            }
            if offset + char_width > max_label_width {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(Color::WHITE);
            cell.bg = Some(bg_color);
            ctx.set(label_start + offset, 0, cell);
            for i in 1..char_width {
                let mut cont = Cell::continuation();
                cont.bg = Some(bg_color);
                ctx.set(label_start + offset + i, 0, cont);
            }
            offset += char_width;
        }
    }
}

impl_styled_view!(StatusIndicator);
impl_widget_builders!(StatusIndicator);

/// Helper function to create a StatusIndicator
pub fn status_indicator(status: Status) -> StatusIndicator {
    StatusIndicator::new(status)
}

/// Helper function to create an online indicator
pub fn online() -> StatusIndicator {
    StatusIndicator::online()
}

/// Helper function to create an offline indicator
pub fn offline() -> StatusIndicator {
    StatusIndicator::offline()
}

/// Helper function to create a busy indicator
pub fn busy_indicator() -> StatusIndicator {
    StatusIndicator::busy()
}

/// Helper function to create an away indicator
pub fn away_indicator() -> StatusIndicator {
    StatusIndicator::away()
}