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
//! AI Streaming widget for LLM response display
//!
//! Displays streaming text with typing effects, markdown rendering,
//! and code block syntax highlighting.

use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::widget::theme::LIGHT_GRAY;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};
use std::time::{Duration, Instant};

/// Typing effect style
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TypingStyle {
    /// No effect, show all text immediately
    None,
    /// Character by character
    #[default]
    Character,
    /// Word by word
    Word,
    /// Line by line
    Line,
    /// Chunk by chunk (for streaming)
    Chunk,
}

/// Cursor style for typing effect
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum StreamCursor {
    /// Block cursor █
    #[default]
    Block,
    /// Underline cursor _
    Underline,
    /// Bar cursor |
    Bar,
    /// No cursor
    None,
}

/// Stream status
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum StreamStatus {
    /// Not started
    #[default]
    Idle,
    /// Currently streaming
    Streaming,
    /// Paused
    Paused,
    /// Completed
    Complete,
    /// Error occurred
    Error,
}

/// AI Stream widget
///
/// # Example
///
/// ```rust,ignore
/// use revue::prelude::*;
///
/// let mut stream = AiStream::new()
///     .typing_speed(30)  // ms per character
///     .cursor(StreamCursor::Block);
///
/// // Append streaming chunks
/// stream.append("Hello, ");
/// stream.append("I am an AI assistant.");
/// ```
pub struct AiStream {
    /// Full text content
    content: String,
    /// Currently visible characters
    visible_chars: usize,
    /// Typing style
    typing_style: TypingStyle,
    /// Cursor style
    cursor: StreamCursor,
    /// Typing speed (ms per unit)
    typing_speed: u64,
    /// Last update time
    last_update: Option<Instant>,
    /// Stream status
    status: StreamStatus,
    /// Text color
    fg: Color,
    /// Background color
    bg: Option<Color>,
    /// Cursor color
    cursor_color: Color,
    /// Show thinking indicator
    show_thinking: bool,
    /// Thinking animation frame
    thinking_frame: usize,
    /// Word wrap
    wrap: bool,
    /// Scroll offset
    scroll: usize,
    /// Markdown rendering
    render_markdown: bool,
    /// Widget properties
    props: WidgetProps,
}

impl AiStream {
    /// Create a new AI stream widget
    pub fn new() -> Self {
        Self {
            content: String::new(),
            visible_chars: 0,
            typing_style: TypingStyle::default(),
            cursor: StreamCursor::default(),
            typing_speed: 30,
            last_update: None,
            status: StreamStatus::Idle,
            fg: Color::WHITE,
            bg: None,
            cursor_color: Color::rgb(100, 200, 255),
            show_thinking: true,
            thinking_frame: 0,
            wrap: true,
            scroll: 0,
            render_markdown: true,
            props: WidgetProps::new(),
        }
    }

    /// Set typing style
    pub fn typing_style(mut self, style: TypingStyle) -> Self {
        self.typing_style = style;
        self
    }

    /// Set typing speed (ms per unit)
    pub fn typing_speed(mut self, ms: u64) -> Self {
        self.typing_speed = ms;
        self
    }

    /// Set cursor style
    pub fn cursor(mut self, cursor: StreamCursor) -> Self {
        self.cursor = cursor;
        self
    }

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

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

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

    /// Enable/disable thinking indicator
    pub fn thinking(mut self, show: bool) -> Self {
        self.show_thinking = show;
        self
    }

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

    /// Enable/disable markdown rendering
    pub fn markdown(mut self, enable: bool) -> Self {
        self.render_markdown = enable;
        self
    }

    /// Set initial content
    pub fn content(mut self, text: impl Into<String>) -> Self {
        self.content = text.into();
        self.visible_chars = 0;
        self.status = StreamStatus::Streaming;
        self.last_update = Some(Instant::now());
        self
    }

    /// Append text (for streaming)
    pub fn append(&mut self, text: &str) {
        self.content.push_str(text);
        if self.status == StreamStatus::Idle {
            self.status = StreamStatus::Streaming;
            self.last_update = Some(Instant::now());
        }
    }

    /// Set complete content (show immediately)
    pub fn set_content(&mut self, text: impl Into<String>) {
        self.content = text.into();
        self.visible_chars = self.content.chars().count();
        self.status = StreamStatus::Complete;
    }

    /// Clear content
    pub fn clear(&mut self) {
        self.content.clear();
        self.visible_chars = 0;
        self.status = StreamStatus::Idle;
        self.scroll = 0;
    }

    /// Mark as complete
    pub fn complete(&mut self) {
        self.status = StreamStatus::Complete;
        self.visible_chars = self.content.chars().count();
    }

    /// Mark as error
    pub fn error(&mut self) {
        self.status = StreamStatus::Error;
    }

    /// Pause streaming
    pub fn pause(&mut self) {
        if self.status == StreamStatus::Streaming {
            self.status = StreamStatus::Paused;
        }
    }

    /// Resume streaming
    pub fn resume(&mut self) {
        if self.status == StreamStatus::Paused {
            self.status = StreamStatus::Streaming;
            self.last_update = Some(Instant::now());
        }
    }

    /// Update animation state (call this each frame)
    pub fn tick(&mut self) {
        // Update thinking animation
        self.thinking_frame = (self.thinking_frame + 1) % 4;

        // Update typing animation
        if self.status != StreamStatus::Streaming {
            return;
        }

        if self.typing_style == TypingStyle::None {
            self.visible_chars = self.content.chars().count();
            self.status = StreamStatus::Complete;
            return;
        }

        let now = Instant::now();
        let elapsed = self
            .last_update
            .map(|t| now.duration_since(t))
            .unwrap_or(Duration::ZERO);

        if elapsed.as_millis() >= self.typing_speed as u128 {
            self.last_update = Some(now);
            let total_chars = self.content.chars().count();

            match self.typing_style {
                TypingStyle::Character => {
                    if self.visible_chars < total_chars {
                        self.visible_chars += 1;
                    } else {
                        self.status = StreamStatus::Complete;
                    }
                }
                TypingStyle::Word => {
                    // Find next word boundary
                    let chars: Vec<char> = self.content.chars().collect();
                    let mut pos = self.visible_chars;

                    // Skip current word
                    while pos < chars.len() && !chars[pos].is_whitespace() {
                        pos += 1;
                    }
                    // Skip whitespace
                    while pos < chars.len() && chars[pos].is_whitespace() {
                        pos += 1;
                    }

                    self.visible_chars = pos;
                    if pos >= total_chars {
                        self.status = StreamStatus::Complete;
                    }
                }
                TypingStyle::Line => {
                    // Find next line boundary
                    let chars: Vec<char> = self.content.chars().collect();
                    let mut pos = self.visible_chars;

                    while pos < chars.len() && chars[pos] != '\n' {
                        pos += 1;
                    }
                    if pos < chars.len() {
                        pos += 1; // Include newline
                    }

                    self.visible_chars = pos;
                    if pos >= total_chars {
                        self.status = StreamStatus::Complete;
                    }
                }
                TypingStyle::Chunk => {
                    // Show 5-10 characters at a time
                    self.visible_chars = (self.visible_chars + 5).min(total_chars);
                    if self.visible_chars >= total_chars {
                        self.status = StreamStatus::Complete;
                    }
                }
                TypingStyle::None => {}
            }
        }
    }

    /// Get visible text
    fn visible_text(&self) -> String {
        self.content.chars().take(self.visible_chars).collect()
    }

    /// Get status
    pub fn status(&self) -> StreamStatus {
        self.status
    }

    /// Check if complete
    pub fn is_complete(&self) -> bool {
        self.status == StreamStatus::Complete
    }

    /// Get progress (0.0 to 1.0)
    pub fn progress(&self) -> f32 {
        let total = self.content.chars().count();
        if total == 0 {
            return 1.0;
        }
        self.visible_chars as f32 / total as f32
    }

    /// Scroll down
    pub fn scroll_down(&mut self, amount: usize) {
        self.scroll = self.scroll.saturating_add(amount);
    }

    /// Scroll up
    pub fn scroll_up(&mut self, amount: usize) {
        self.scroll = self.scroll.saturating_sub(amount);
    }

    /// Render thinking indicator
    fn render_thinking(&self, ctx: &mut RenderContext) {
        let indicators = ['', '', '', ''];
        let ch = indicators[self.thinking_frame % indicators.len()];

        let mut cell = Cell::new(ch);
        cell.fg = Some(self.cursor_color);
        ctx.set(0, 0, cell);

        let text = " Thinking...";
        for (i, c) in text.chars().enumerate() {
            let mut cell = Cell::new(c);
            cell.fg = Some(LIGHT_GRAY);
            cell.modifier = Modifier::ITALIC;
            ctx.set(1 + i as u16, 0, cell);
        }
    }
}

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

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

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

        // Show thinking indicator if no content yet
        if self.content.is_empty() && self.show_thinking && self.status == StreamStatus::Streaming {
            self.render_thinking(ctx);
            return;
        }

        let visible = self.visible_text();
        let _width = area.width as usize;

        // Simple word wrap and render
        let mut x = 0u16;
        let mut y = 0u16;

        for ch in visible.chars() {
            if ch == '\n' {
                x = 0;
                y += 1;
                continue;
            }

            if self.wrap && x >= area.width {
                x = 0;
                y += 1;
            }

            if y >= area.height {
                break;
            }

            let mut cell = Cell::new(ch);
            cell.fg = Some(self.fg);
            cell.bg = self.bg;
            ctx.set(x, y, cell);

            x += 1;
        }

        // Render cursor
        if self.status == StreamStatus::Streaming
            && self.cursor != StreamCursor::None
            && y < area.height
        {
            let cursor_char = match self.cursor {
                StreamCursor::Block => '',
                StreamCursor::Underline => '_',
                StreamCursor::Bar => '',
                StreamCursor::None => ' ',
            };

            let mut cell = Cell::new(cursor_char);
            cell.fg = Some(self.cursor_color);
            // Blink effect
            if self.thinking_frame.is_multiple_of(2) {
                ctx.set(x, y, cell);
            }
        }
    }
}

impl_styled_view!(AiStream);
impl_props_builders!(AiStream);

/// Create a new AI stream widget
pub fn ai_stream() -> AiStream {
    AiStream::new()
}

/// Create an AI stream with initial content
pub fn ai_response(content: impl Into<String>) -> AiStream {
    AiStream::new().content(content)
}