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
//! Empty State widget for displaying no-data scenarios gracefully
//!
//! A dedicated widget for consistent, helpful empty state displays.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::{EmptyState, EmptyStateType, empty_state};
//!
//! // Basic empty state
//! EmptyState::new("No items yet")
//!     .description("Create your first item to get started");
//!
//! // Search with no results
//! empty_state("No results found")
//!     .state_type(EmptyStateType::NoResults)
//!     .description("Try adjusting your search terms")
//!     .action("Clear search");
//!
//! // Error state
//! EmptyState::error("Failed to load data")
//!     .description("Check your connection and try again")
//!     .action("Retry");
//! ```

use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::utils::{char_width, display_width};
use crate::widget::theme::{LIGHT_GRAY, PLACEHOLDER_FG};
use crate::widget::traits::{RenderContext, View, WidgetProps, WidgetState};
use crate::{impl_styled_view, impl_widget_builders};

/// Empty state type/scenario
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum EmptyStateType {
    /// No data available (default)
    #[default]
    Empty,
    /// Search returned no results
    NoResults,
    /// Error occurred
    Error,
    /// No permission to view
    NoPermission,
    /// Offline/disconnected
    Offline,
    /// First-time user experience
    FirstUse,
}

impl EmptyStateType {
    /// Get the default icon for this state type
    pub fn icon(&self) -> char {
        match self {
            EmptyStateType::Empty => '📭',
            EmptyStateType::NoResults => '🔍',
            EmptyStateType::Error => '',
            EmptyStateType::NoPermission => '🔒',
            EmptyStateType::Offline => '📡',
            EmptyStateType::FirstUse => '🚀',
        }
    }

    /// Get the accent color for this state type
    pub fn color(&self) -> Color {
        match self {
            EmptyStateType::Empty => PLACEHOLDER_FG,
            EmptyStateType::NoResults => Color::rgb(100, 149, 237),
            EmptyStateType::Error => Color::rgb(220, 80, 80),
            EmptyStateType::NoPermission => Color::rgb(255, 165, 0),
            EmptyStateType::Offline => PLACEHOLDER_FG,
            EmptyStateType::FirstUse => Color::rgb(100, 200, 100),
        }
    }
}

/// Empty state visual variant
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum EmptyStateVariant {
    /// Full display with border (default)
    #[default]
    Full,
    /// Compact inline display
    Compact,
    /// Minimal text-only
    Minimal,
}

/// An empty state widget for no-data scenarios
///
/// Displays a consistent, helpful message when there's no content to show.
pub struct EmptyState {
    /// Primary message/title
    title: String,
    /// Optional description text
    description: Option<String>,
    /// State type
    state_type: EmptyStateType,
    /// Visual variant
    variant: EmptyStateVariant,
    /// Show icon
    show_icon: bool,
    /// Custom icon override
    custom_icon: Option<char>,
    /// Optional action button text
    action: Option<String>,
    /// Widget state
    state: WidgetState,
    /// Widget properties
    props: WidgetProps,
}

impl EmptyState {
    /// Create a new empty state with a title
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            description: None,
            state_type: EmptyStateType::default(),
            variant: EmptyStateVariant::default(),
            show_icon: true,
            custom_icon: None,
            action: None,
            state: WidgetState::new(),
            props: WidgetProps::new(),
        }
    }

    /// Create an empty state for no results
    pub fn no_results(title: impl Into<String>) -> Self {
        Self::new(title).state_type(EmptyStateType::NoResults)
    }

    /// Create an error empty state
    pub fn error(title: impl Into<String>) -> Self {
        Self::new(title).state_type(EmptyStateType::Error)
    }

    /// Create a no permission empty state
    pub fn no_permission(title: impl Into<String>) -> Self {
        Self::new(title).state_type(EmptyStateType::NoPermission)
    }

    /// Create an offline empty state
    pub fn offline(title: impl Into<String>) -> Self {
        Self::new(title).state_type(EmptyStateType::Offline)
    }

    /// Create a first-use empty state
    pub fn first_use(title: impl Into<String>) -> Self {
        Self::new(title).state_type(EmptyStateType::FirstUse)
    }

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

    /// Set the state type
    pub fn state_type(mut self, state_type: EmptyStateType) -> Self {
        self.state_type = state_type;
        self
    }

    /// Set the visual variant
    pub fn variant(mut self, variant: EmptyStateVariant) -> Self {
        self.variant = variant;
        self
    }

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

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

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

    /// Get the icon to display
    fn get_icon(&self) -> char {
        self.custom_icon.unwrap_or_else(|| self.state_type.icon())
    }

    /// Calculate the height needed for this empty state
    pub fn height(&self) -> u16 {
        match self.variant {
            EmptyStateVariant::Full => {
                let mut h = 5; // icon + title + padding
                if self.description.is_some() {
                    h += 1;
                }
                if self.action.is_some() {
                    h += 2;
                }
                h
            }
            EmptyStateVariant::Compact => {
                let mut h = 3;
                if self.description.is_some() {
                    h += 1;
                }
                h
            }
            EmptyStateVariant::Minimal => 1,
        }
    }
}

impl Default for EmptyState {
    fn default() -> Self {
        Self::new("No items")
    }
}

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

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

        match self.variant {
            EmptyStateVariant::Full => self.render_full(ctx),
            EmptyStateVariant::Compact => self.render_compact(ctx),
            EmptyStateVariant::Minimal => self.render_minimal(ctx),
        }
    }
}

impl EmptyState {
    fn render_full(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let accent = self.state_type.color();

        // Calculate vertical centering
        let content_height = self.height();
        let start_y = if area.height > content_height {
            (area.height - content_height) / 2
        } else {
            0u16
        };

        let mut y = start_y;

        // Icon (centered, large)
        if self.show_icon && y < area.height {
            let icon = self.get_icon();
            let icon_x = area.width / 2;
            let mut cell = Cell::new(icon);
            cell.fg = Some(accent);
            ctx.set(icon_x, y, cell);
            y += 2;
        }

        // Title (centered, bold)
        if y < area.height {
            let title_len = display_width(&self.title) as u16;
            let title_x = area.width.saturating_sub(title_len) / 2;
            let mut dx: u16 = 0;
            for ch in self.title.chars() {
                let cw = char_width(ch) as u16;
                if title_x + dx + cw > area.width {
                    break;
                }
                let mut cell = Cell::new(ch);
                cell.fg = Some(Color::WHITE);
                cell.modifier |= Modifier::BOLD;
                ctx.set(title_x + dx, y, cell);
                dx += cw;
            }
            y += 1;
        }

        // Description (centered, dimmed)
        if let Some(ref desc) = self.description {
            if y < area.height {
                let desc_len = display_width(desc) as u16;
                let desc_x = area.width.saturating_sub(desc_len) / 2;
                let mut dx: u16 = 0;
                for ch in desc.chars() {
                    let cw = char_width(ch) as u16;
                    if desc_x + dx + cw > area.width {
                        break;
                    }
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(LIGHT_GRAY);
                    ctx.set(desc_x + dx, y, cell);
                    dx += cw;
                }
                y += 2;
            }
        }

        // Action button (centered)
        if let Some(ref action_text) = self.action {
            if y < area.height {
                let btn_text = format!("[ {} ]", action_text);
                let btn_len = display_width(&btn_text) as u16;
                let btn_x = area.width.saturating_sub(btn_len) / 2;
                let mut dx: u16 = 0;
                for ch in btn_text.chars() {
                    let cw = char_width(ch) as u16;
                    if btn_x + dx + cw > area.width {
                        break;
                    }
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(accent);
                    ctx.set(btn_x + dx, y, cell);
                    dx += cw;
                }
            }
        }
    }

    fn render_compact(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let accent = self.state_type.color();
        let mut y: u16 = 0;

        // Icon + Title on same line
        let mut x: u16 = 0;
        if self.show_icon {
            let icon = self.get_icon();
            let mut cell = Cell::new(icon);
            cell.fg = Some(accent);
            ctx.set(x, y, cell);
            x += 2;
        }

        let mut dx: u16 = 0;
        for ch in self.title.chars() {
            let cw = char_width(ch) as u16;
            if x + dx + cw > area.width {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(Color::WHITE);
            cell.modifier |= Modifier::BOLD;
            ctx.set(x + dx, y, cell);
            dx += cw;
        }
        y += 1;

        // Description
        if let Some(ref desc) = self.description {
            if y < area.height {
                let desc_x: u16 = if self.show_icon { 2 } else { 0 };
                let mut dx: u16 = 0;
                for ch in desc.chars() {
                    let cw = char_width(ch) as u16;
                    if desc_x + dx + cw > area.width {
                        break;
                    }
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(LIGHT_GRAY);
                    ctx.set(desc_x + dx, y, cell);
                    dx += cw;
                }
                y += 1;
            }
        }

        // Action
        if let Some(ref action_text) = self.action {
            if y < area.height {
                let action_x: u16 = if self.show_icon { 2 } else { 0 };
                let btn_text = format!("[{}]", action_text);
                let mut dx: u16 = 0;
                for ch in btn_text.chars() {
                    let cw = char_width(ch) as u16;
                    if action_x + dx + cw > area.width {
                        break;
                    }
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(accent);
                    ctx.set(action_x + dx, y, cell);
                    dx += cw;
                }
            }
        }
    }

    fn render_minimal(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let accent = self.state_type.color();
        let mut x: u16 = 0;

        // Icon
        if self.show_icon {
            let icon = self.get_icon();
            let mut cell = Cell::new(icon);
            cell.fg = Some(accent);
            ctx.set(x, 0, cell);
            x += 2;
        }

        // Title
        let mut dx: u16 = 0;
        for ch in self.title.chars() {
            let cw = char_width(ch) as u16;
            if x + dx + cw > area.width {
                break;
            }
            let mut cell = Cell::new(ch);
            cell.fg = Some(LIGHT_GRAY);
            ctx.set(x + dx, 0, cell);
            dx += cw;
        }
    }
}

impl_styled_view!(EmptyState);
impl_widget_builders!(EmptyState);

/// Helper function to create an EmptyState
pub fn empty_state(title: impl Into<String>) -> EmptyState {
    EmptyState::new(title)
}

/// Helper function to create a no-results EmptyState
pub fn no_results(title: impl Into<String>) -> EmptyState {
    EmptyState::no_results(title)
}

/// Helper function to create an error EmptyState
pub fn empty_error(title: impl Into<String>) -> EmptyState {
    EmptyState::error(title)
}

/// Helper function to create a first-use EmptyState
pub fn first_use(title: impl Into<String>) -> EmptyState {
    EmptyState::first_use(title)
}