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
//! Avatar widget for user/entity representation

use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::widget::theme::DISABLED_FG;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Avatar size
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AvatarSize {
    /// Small (1 char)
    Small,
    /// Medium (3 chars)
    #[default]
    Medium,
    /// Large (5 chars with border)
    Large,
}

/// Avatar shape
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AvatarShape {
    /// Circle (using Unicode characters)
    #[default]
    Circle,
    /// Square/box
    Square,
    /// Rounded square
    Rounded,
}

/// An avatar widget for user representation
///
/// # Example
///
/// ```rust,ignore
/// use revue::prelude::*;
///
/// hstack()
///     .child(avatar("John Doe").circle())
///     .child(text("John Doe"))
/// ```
pub struct Avatar {
    /// Name to derive initials from
    name: String,
    /// Custom initials (overrides name-derived)
    initials: Option<String>,
    /// Size
    size: AvatarSize,
    /// Shape
    shape: AvatarShape,
    /// Background color
    bg_color: Option<Color>,
    /// Foreground color
    fg_color: Option<Color>,
    /// Status indicator color (online/offline dot)
    status: Option<Color>,
    /// Icon character (instead of initials)
    icon: Option<char>,
    /// Widget props for CSS integration
    props: WidgetProps,
}

impl Avatar {
    /// Create a new avatar from a name
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            initials: None,
            size: AvatarSize::Medium,
            shape: AvatarShape::Circle,
            bg_color: None,
            fg_color: None,
            status: None,
            icon: None,
            props: WidgetProps::new(),
        }
    }

    /// Create an avatar with custom initials
    pub fn from_initials(initials: impl Into<String>) -> Self {
        Self {
            name: String::new(),
            initials: Some(initials.into()),
            size: AvatarSize::Medium,
            shape: AvatarShape::Circle,
            bg_color: None,
            fg_color: None,
            status: None,
            icon: None,
            props: WidgetProps::new(),
        }
    }

    /// Create an avatar with an icon
    pub fn from_icon(icon: char) -> Self {
        Self {
            name: String::new(),
            initials: None,
            size: AvatarSize::Medium,
            shape: AvatarShape::Circle,
            bg_color: None,
            fg_color: None,
            status: None,
            icon: Some(icon),
            props: WidgetProps::new(),
        }
    }

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

    /// Small size shorthand
    pub fn small(mut self) -> Self {
        self.size = AvatarSize::Small;
        self
    }

    /// Medium size shorthand
    pub fn medium(mut self) -> Self {
        self.size = AvatarSize::Medium;
        self
    }

    /// Large size shorthand
    pub fn large(mut self) -> Self {
        self.size = AvatarSize::Large;
        self
    }

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

    /// Circle shape shorthand
    pub fn circle(mut self) -> Self {
        self.shape = AvatarShape::Circle;
        self
    }

    /// Square shape shorthand
    pub fn square(mut self) -> Self {
        self.shape = AvatarShape::Square;
        self
    }

    /// Rounded shape shorthand
    pub fn rounded(mut self) -> Self {
        self.shape = AvatarShape::Rounded;
        self
    }

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

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

    /// Set colors
    pub fn colors(mut self, bg: Color, fg: Color) -> Self {
        self.bg_color = Some(bg);
        self.fg_color = Some(fg);
        self
    }

    /// Set online status
    pub fn online(mut self) -> Self {
        self.status = Some(Color::rgb(40, 200, 80));
        self
    }

    /// Set offline status
    pub fn offline(mut self) -> Self {
        self.status = Some(DISABLED_FG);
        self
    }

    /// Set away status
    pub fn away(mut self) -> Self {
        self.status = Some(Color::rgb(200, 180, 40));
        self
    }

    /// Set busy status
    pub fn busy(mut self) -> Self {
        self.status = Some(Color::rgb(200, 60, 60));
        self
    }

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

    /// Set icon
    pub fn icon(mut self, icon: char) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Get initials from name
    fn get_initials(&self) -> String {
        if let Some(ref initials) = self.initials {
            return initials.clone();
        }

        if let Some(icon) = self.icon {
            return icon.to_string();
        }

        // Derive initials from name
        self.name
            .split_whitespace()
            .filter_map(|word| word.chars().next())
            .take(2)
            .collect::<String>()
            .to_uppercase()
    }

    /// Get background color (auto-generate from name if not set)
    fn get_bg_color(&self) -> Color {
        if let Some(color) = self.bg_color {
            return color;
        }

        // Generate color from name hash
        let hash: u32 = self
            .name
            .bytes()
            .fold(0u32, |acc, b| acc.wrapping_add(b as u32));
        let hue = (hash % 360) as u8;

        // Convert HSL to RGB (simplified)
        let h = hue as f32 / 60.0;
        let s = 0.6_f32;
        let l = 0.4_f32;

        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
        let x = c * (1.0 - ((h % 2.0) - 1.0).abs());
        let m = l - c / 2.0;

        let (r1, g1, b1) = match h as u8 {
            0 => (c, x, 0.0),
            1 => (x, c, 0.0),
            2 => (0.0, c, x),
            3 => (0.0, x, c),
            4 => (x, 0.0, c),
            _ => (c, 0.0, x),
        };

        Color::rgb(
            ((r1 + m) * 255.0) as u8,
            ((g1 + m) * 255.0) as u8,
            ((b1 + m) * 255.0) as u8,
        )
    }
}

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

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

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let initials = self.get_initials();
        let bg = self.get_bg_color();
        let fg = self.fg_color.unwrap_or(Color::WHITE);

        match self.size {
            AvatarSize::Small => {
                // Single character
                let ch = initials.chars().next().unwrap_or('?');
                let mut cell = Cell::new(ch);
                cell.fg = Some(fg);
                cell.bg = Some(bg);
                cell.modifier |= Modifier::BOLD;
                ctx.set(0, 0, cell);

                // Status dot
                if let Some(status_color) = self.status {
                    let mut dot = Cell::new('');
                    dot.fg = Some(status_color);
                    ctx.set(1, 0, dot);
                }
            }
            AvatarSize::Medium => {
                // 3 chars wide: [XY] or ⬤XY⬤ for circle
                match self.shape {
                    AvatarShape::Circle => {
                        // Use half-blocks for pseudo-circle: ◖XY◗
                        let mut left = Cell::new('');
                        left.fg = Some(bg);
                        ctx.set(0, 0, left);

                        for (i, ch) in initials.chars().take(2).enumerate() {
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(fg);
                            cell.bg = Some(bg);
                            cell.modifier |= Modifier::BOLD;
                            ctx.set(1 + i as u16, 0, cell);
                        }

                        let mut right = Cell::new('');
                        right.fg = Some(bg);
                        ctx.set(3, 0, right);

                        // Status dot
                        if let Some(status_color) = self.status {
                            let mut dot = Cell::new('');
                            dot.fg = Some(status_color);
                            ctx.set(4, 0, dot);
                        }
                    }
                    AvatarShape::Square | AvatarShape::Rounded => {
                        // [XY] format
                        let left = if self.shape == AvatarShape::Rounded {
                            '('
                        } else {
                            '['
                        };
                        let right = if self.shape == AvatarShape::Rounded {
                            ')'
                        } else {
                            ']'
                        };

                        let mut lc = Cell::new(left);
                        lc.fg = Some(bg);
                        ctx.set(0, 0, lc);

                        for (i, ch) in initials.chars().take(2).enumerate() {
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(fg);
                            cell.bg = Some(bg);
                            cell.modifier |= Modifier::BOLD;
                            ctx.set(1 + i as u16, 0, cell);
                        }

                        let mut rc = Cell::new(right);
                        rc.fg = Some(bg);
                        ctx.set(3, 0, rc);

                        // Status dot
                        if let Some(status_color) = self.status {
                            let mut dot = Cell::new('');
                            dot.fg = Some(status_color);
                            ctx.set(4, 0, dot);
                        }
                    }
                }
            }
            AvatarSize::Large => {
                // 3 lines tall, 5+ chars wide
                if area.height < 3 {
                    // Fall back to medium
                    let mut cell = Cell::new(initials.chars().next().unwrap_or('?'));
                    cell.fg = Some(fg);
                    cell.bg = Some(bg);
                    ctx.set(0, 0, cell);
                    return;
                }

                match self.shape {
                    AvatarShape::Circle => {
                        // Top: ╭───╮
                        // Mid: │XY │
                        // Bot: ╰───╯
                        let chars_top = ['', '', '', '', ''];
                        let chars_bot = ['', '', '', '', ''];

                        for (i, ch) in chars_top.iter().enumerate() {
                            let mut cell = Cell::new(*ch);
                            cell.fg = Some(bg);
                            ctx.set(i as u16, 0, cell);
                        }

                        // Middle row
                        let mut left = Cell::new('');
                        left.fg = Some(bg);
                        ctx.set(0, 1, left);

                        // Pre-collect initials chars for O(1) access
                        let initials_chars: Vec<char> = initials.chars().collect();
                        for i in 1..4 {
                            let ch = if i == 1 || i == 2 {
                                initials_chars.get(i - 1).copied().unwrap_or(' ')
                            } else {
                                ' '
                            };
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(fg);
                            cell.bg = Some(bg);
                            cell.modifier |= Modifier::BOLD;
                            ctx.set(i as u16, 1, cell);
                        }

                        let mut right = Cell::new('');
                        right.fg = Some(bg);
                        ctx.set(4, 1, right);

                        for (i, ch) in chars_bot.iter().enumerate() {
                            let mut cell = Cell::new(*ch);
                            cell.fg = Some(bg);
                            ctx.set(i as u16, 2, cell);
                        }

                        // Status dot
                        if let Some(status_color) = self.status {
                            let mut dot = Cell::new('');
                            dot.fg = Some(status_color);
                            ctx.set(5, 2, dot);
                        }
                    }
                    AvatarShape::Square => {
                        // Top: ┌───┐
                        let chars_top = ['', '', '', '', ''];
                        let chars_bot = ['', '', '', '', ''];

                        for (i, ch) in chars_top.iter().enumerate() {
                            let mut cell = Cell::new(*ch);
                            cell.fg = Some(bg);
                            ctx.set(i as u16, 0, cell);
                        }

                        let mut left = Cell::new('');
                        left.fg = Some(bg);
                        ctx.set(0, 1, left);

                        // Pre-collect initials chars for O(1) access
                        let initials_chars: Vec<char> = initials.chars().collect();
                        for i in 1..4 {
                            let ch = if i == 1 || i == 2 {
                                initials_chars.get(i - 1).copied().unwrap_or(' ')
                            } else {
                                ' '
                            };
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(fg);
                            cell.bg = Some(bg);
                            cell.modifier |= Modifier::BOLD;
                            ctx.set(i as u16, 1, cell);
                        }

                        let mut right = Cell::new('');
                        right.fg = Some(bg);
                        ctx.set(4, 1, right);

                        for (i, ch) in chars_bot.iter().enumerate() {
                            let mut cell = Cell::new(*ch);
                            cell.fg = Some(bg);
                            ctx.set(i as u16, 2, cell);
                        }

                        if let Some(status_color) = self.status {
                            let mut dot = Cell::new('');
                            dot.fg = Some(status_color);
                            ctx.set(5, 2, dot);
                        }
                    }
                    AvatarShape::Rounded => {
                        // Same as circle for large
                        let chars_top = ['', '', '', '', ''];
                        let chars_bot = ['', '', '', '', ''];

                        for (i, ch) in chars_top.iter().enumerate() {
                            let mut cell = Cell::new(*ch);
                            cell.fg = Some(bg);
                            ctx.set(i as u16, 0, cell);
                        }

                        let mut left = Cell::new('');
                        left.fg = Some(bg);
                        ctx.set(0, 1, left);

                        // Pre-collect initials chars for O(1) access
                        let initials_chars: Vec<char> = initials.chars().collect();
                        for i in 1..4 {
                            let ch = if i == 1 || i == 2 {
                                initials_chars.get(i - 1).copied().unwrap_or(' ')
                            } else {
                                ' '
                            };
                            let mut cell = Cell::new(ch);
                            cell.fg = Some(fg);
                            cell.bg = Some(bg);
                            cell.modifier |= Modifier::BOLD;
                            ctx.set(i as u16, 1, cell);
                        }

                        let mut right = Cell::new('');
                        right.fg = Some(bg);
                        ctx.set(4, 1, right);

                        for (i, ch) in chars_bot.iter().enumerate() {
                            let mut cell = Cell::new(*ch);
                            cell.fg = Some(bg);
                            ctx.set(i as u16, 2, cell);
                        }

                        if let Some(status_color) = self.status {
                            let mut dot = Cell::new('');
                            dot.fg = Some(status_color);
                            ctx.set(5, 2, dot);
                        }
                    }
                }
            }
        }
    }
}

impl_styled_view!(Avatar);
impl_props_builders!(Avatar);

/// Create a new avatar from a name
pub fn avatar(name: impl Into<String>) -> Avatar {
    Avatar::new(name)
}

/// Create an avatar with an icon
pub fn avatar_icon(icon: char) -> Avatar {
    Avatar::from_icon(icon)
}