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
//! Status bar widget for header/footer displays
//!
//! Provides configurable status bars with sections for displaying
//! application state, key hints, and other information.

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

/// Status bar position
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum StatusBarPosition {
    /// At the top of the area
    Top,
    /// At the bottom of the area
    #[default]
    Bottom,
}

/// Section alignment
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SectionAlign {
    /// Left-aligned section (default)
    #[default]
    Left,
    /// Center-aligned section
    Center,
    /// Right-aligned section
    Right,
}

/// A section in the status bar
#[derive(Clone)]
pub struct StatusSection {
    /// Section content
    pub content: String,
    /// Foreground color
    pub fg: Option<Color>,
    /// Background color
    pub bg: Option<Color>,
    /// Bold text
    pub bold: bool,
    /// Minimum width
    pub min_width: u16,
    /// Priority (higher = more important, kept when space is limited)
    pub priority: u8,
}

impl StatusSection {
    /// Create a new section
    pub fn new(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            fg: None,
            bg: None,
            bold: false,
            min_width: 0,
            priority: 0,
        }
    }

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

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

    /// Set bold
    pub fn bold(mut self) -> Self {
        self.bold = true;
        self
    }

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

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

    /// Get display width
    pub fn width(&self) -> u16 {
        self.content.chars().count().max(self.min_width as usize) as u16
    }
}

/// Key hint for display in status bar
#[derive(Clone)]
pub struct KeyHint {
    /// Key combination
    pub key: String,
    /// Description
    pub description: String,
}

impl KeyHint {
    /// Create a new key hint
    pub fn new(key: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            description: description.into(),
        }
    }
}

/// Status bar widget
pub struct StatusBar {
    /// Left-aligned sections
    left: Vec<StatusSection>,
    /// Center-aligned sections
    center: Vec<StatusSection>,
    /// Right-aligned sections
    right: Vec<StatusSection>,
    /// Position
    position: StatusBarPosition,
    /// Background color
    bg: Color,
    /// Default foreground color
    fg: Color,
    /// Key hints
    key_hints: Vec<KeyHint>,
    /// Key hint foreground
    key_fg: Color,
    /// Key hint background
    key_bg: Color,
    /// Separator between sections
    separator: Option<char>,
    /// Height (usually 1)
    height: u16,
    /// Widget props for CSS integration
    props: WidgetProps,
}

impl StatusBar {
    /// Create a new status bar
    pub fn new() -> Self {
        Self {
            left: Vec::new(),
            center: Vec::new(),
            right: Vec::new(),
            position: StatusBarPosition::Bottom,
            bg: DARK_BG,
            fg: Color::WHITE,
            key_hints: Vec::new(),
            key_fg: Color::BLACK,
            key_bg: SECONDARY_TEXT,
            separator: None,
            height: 1,
            props: WidgetProps::new(),
        }
    }

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

    /// Set as header (top position)
    pub fn header(mut self) -> Self {
        self.position = StatusBarPosition::Top;
        self
    }

    /// Set as footer (bottom position)
    pub fn footer(mut self) -> Self {
        self.position = StatusBarPosition::Bottom;
        self
    }

    /// Add left section
    pub fn left(mut self, section: StatusSection) -> Self {
        self.left.push(section);
        self
    }

    /// Add center section
    pub fn center(mut self, section: StatusSection) -> Self {
        self.center.push(section);
        self
    }

    /// Add right section
    pub fn right(mut self, section: StatusSection) -> Self {
        self.right.push(section);
        self
    }

    /// Add left text
    pub fn left_text(self, text: impl Into<String>) -> Self {
        self.left(StatusSection::new(text))
    }

    /// Add center text
    pub fn center_text(self, text: impl Into<String>) -> Self {
        self.center(StatusSection::new(text))
    }

    /// Add right text
    pub fn right_text(self, text: impl Into<String>) -> Self {
        self.right(StatusSection::new(text))
    }

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

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

    /// Add key hint
    pub fn key(mut self, key: impl Into<String>, description: impl Into<String>) -> Self {
        self.key_hints.push(KeyHint::new(key, description));
        self
    }

    /// Add multiple key hints
    pub fn keys(mut self, hints: Vec<KeyHint>) -> Self {
        self.key_hints.extend(hints);
        self
    }

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

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

    /// Update a left section by index
    pub fn update_left(&mut self, index: usize, content: impl Into<String>) {
        if let Some(section) = self.left.get_mut(index) {
            section.content = content.into();
        }
    }

    /// Update a center section by index
    pub fn update_center(&mut self, index: usize, content: impl Into<String>) {
        if let Some(section) = self.center.get_mut(index) {
            section.content = content.into();
        }
    }

    /// Update a right section by index
    pub fn update_right(&mut self, index: usize, content: impl Into<String>) {
        if let Some(section) = self.right.get_mut(index) {
            section.content = content.into();
        }
    }

    /// Clear all sections
    pub fn clear(&mut self) {
        self.left.clear();
        self.center.clear();
        self.right.clear();
        self.key_hints.clear();
    }

    /// Get render Y position
    fn render_y(&self, area_height: u16) -> u16 {
        match self.position {
            StatusBarPosition::Top => 0,
            StatusBarPosition::Bottom => area_height.saturating_sub(self.height),
        }
    }

    // Getters for testing
    #[doc(hidden)]
    pub fn get_left(&self) -> &[StatusSection] {
        &self.left
    }

    #[doc(hidden)]
    pub fn get_center(&self) -> &[StatusSection] {
        &self.center
    }

    #[doc(hidden)]
    pub fn get_right(&self) -> &[StatusSection] {
        &self.right
    }

    #[doc(hidden)]
    pub fn get_position(&self) -> StatusBarPosition {
        self.position
    }

    #[doc(hidden)]
    pub fn get_bg(&self) -> Color {
        self.bg
    }

    #[doc(hidden)]
    pub fn get_fg(&self) -> Color {
        self.fg
    }

    #[doc(hidden)]
    pub fn get_key_hints(&self) -> &[KeyHint] {
        &self.key_hints
    }

    #[doc(hidden)]
    pub fn get_separator(&self) -> Option<char> {
        self.separator
    }

    #[doc(hidden)]
    pub fn get_height(&self) -> u16 {
        self.height
    }

    #[doc(hidden)]
    pub fn get_render_y(&self, area_height: u16) -> u16 {
        self.render_y(area_height)
    }
}

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

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

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let y = self.render_y(area.height);

        if y >= area.height {
            return;
        }

        // Fill background
        for row in 0..self.height {
            if y + row >= area.height {
                break;
            }
            for x in 0..area.width {
                let mut cell = Cell::new(' ');
                cell.bg = Some(self.bg);
                ctx.set(x, y + row, cell);
            }
        }

        // Calculate section widths
        let left_width: u16 = self.left.iter().map(|s| s.width() + 1).sum();
        let center_width: u16 = self.center.iter().map(|s| s.width() + 1).sum();
        let right_width: u16 = self.right.iter().map(|s| s.width() + 1).sum();

        // Render left sections
        let mut x: u16 = 0;
        for section in &self.left {
            x = self.render_section(ctx, section, x, y);
            if self.separator.is_some() && x < area.width {
                x += 1;
            }
        }

        // Render center sections
        let center_start = (area.width.saturating_sub(center_width)) / 2;
        let mut x = center_start.max(x + 1);
        for section in &self.center {
            x = self.render_section(ctx, section, x, y);
            if self.separator.is_some() && x < area.width {
                x += 1;
            }
        }

        // Render right sections
        let mut x = area.width - right_width;
        for section in &self.right {
            x = self.render_section(ctx, section, x, y);
            if self.separator.is_some() && x < area.width {
                x += 1;
            }
        }

        // Render key hints on second row if height > 1
        if self.height > 1 && !self.key_hints.is_empty() {
            self.render_key_hints(ctx, 0, y + 1, area.width);
        } else if self.height == 1 && !self.key_hints.is_empty() {
            // Render key hints in remaining space
            let hints_start = left_width + 2;
            let hints_end = area.width - right_width - 2;
            if hints_start < hints_end {
                self.render_key_hints_inline(ctx, hints_start, y, hints_end - hints_start);
            }
        }
    }
}

impl_styled_view!(StatusBar);
impl_props_builders!(StatusBar);

impl StatusBar {
    fn render_section(
        &self,
        ctx: &mut RenderContext,
        section: &StatusSection,
        x: u16,
        y: u16,
    ) -> u16 {
        let fg = section.fg.unwrap_or(self.fg);
        let bg = section.bg.unwrap_or(self.bg);

        let mut current_x = x;
        for ch in section.content.chars() {
            if current_x >= ctx.area.width {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(fg);
            cell.bg = Some(bg);
            if section.bold {
                cell.modifier |= Modifier::BOLD;
            }
            ctx.set(current_x, y, cell);
            current_x += 1;
        }

        // Pad to min_width
        while current_x < x + section.min_width && current_x < ctx.area.width {
            let mut cell = Cell::new(' ');
            cell.bg = Some(bg);
            ctx.set(current_x, y, cell);
            current_x += 1;
        }

        current_x
    }

    fn render_key_hints(&self, ctx: &mut RenderContext, x: u16, y: u16, width: u16) {
        let mut current_x = x;

        for hint in &self.key_hints {
            if current_x >= x + width {
                break;
            }

            // Render key
            for ch in hint.key.chars() {
                if current_x >= x + width {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(self.key_fg);
                cell.bg = Some(self.key_bg);
                cell.modifier |= Modifier::BOLD;
                ctx.set(current_x, y, cell);
                current_x += 1;
            }

            // Render description
            let desc = format!(" {} ", hint.description);
            for ch in desc.chars() {
                if current_x >= x + width {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(self.fg);
                cell.bg = Some(self.bg);
                ctx.set(current_x, y, cell);
                current_x += 1;
            }
        }
    }

    fn render_key_hints_inline(&self, ctx: &mut RenderContext, x: u16, y: u16, width: u16) {
        let mut current_x = x;

        for hint in &self.key_hints {
            let hint_width = hint.key.len() + hint.description.len() + 3;
            if current_x + hint_width as u16 > x + width {
                break;
            }

            // Render key
            for ch in hint.key.chars() {
                let mut cell = Cell::new(ch);
                cell.fg = Some(self.key_fg);
                cell.bg = Some(self.key_bg);
                ctx.set(current_x, y, cell);
                current_x += 1;
            }

            // Space
            current_x += 1;

            // Render description
            for ch in hint.description.chars() {
                let mut cell = Cell::new(ch);
                cell.fg = Some(self.fg);
                cell.bg = Some(self.bg);
                ctx.set(current_x, y, cell);
                current_x += 1;
            }

            // Separator
            current_x += 2;
        }
    }
}

// Helper functions

/// Create a new status bar
pub fn statusbar() -> StatusBar {
    StatusBar::new()
}

/// Create a header status bar (positioned at top)
pub fn header() -> StatusBar {
    StatusBar::new().header()
}

/// Create a footer status bar (positioned at bottom)
pub fn footer() -> StatusBar {
    StatusBar::new().footer()
}

/// Create a status bar section with content
pub fn section(content: impl Into<String>) -> StatusSection {
    StatusSection::new(content)
}

/// Create a key hint with key and description
pub fn key_hint(key: impl Into<String>, description: impl Into<String>) -> KeyHint {
    KeyHint::new(key, description)
}