envision 0.16.0

A ratatui framework for collaborative TUI development with headless testing support
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
//! Rendering helpers for the ConversationView component.

use super::types::{ConversationMessage, MessageBlock};
use super::{ConversationViewState, MessageSource};

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, List, ListItem};

use unicode_width::UnicodeWidthChar;
use unicode_width::UnicodeWidthStr;

use crate::theme::Theme;

/// Renders the full conversation view using the messages stored in state.
pub(super) fn render(
    state: &ConversationViewState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
    focused: bool,
    disabled: bool,
) {
    render_from(state, state, frame, area, theme, focused, disabled);
}

/// Renders the conversation view using messages from an external [`MessageSource`].
pub(super) fn render_from(
    source: &dyn MessageSource,
    state: &ConversationViewState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
    focused: bool,
    disabled: bool,
) {
    let border_style = if disabled {
        theme.disabled_style()
    } else if focused {
        theme.focused_border_style()
    } else {
        theme.border_style()
    };

    let title = state.title.as_deref().unwrap_or("Conversation");
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(border_style);

    let inner = block.inner(area);
    frame.render_widget(block, area);

    if inner.height == 0 || inner.width == 0 {
        return;
    }

    let (message_area, status_area) = if state.status.is_some() && inner.height > 1 {
        let msg = Rect {
            height: inner.height - 1,
            ..inner
        };
        let status = Rect {
            y: inner.y + inner.height - 1,
            height: 1,
            ..inner
        };
        (msg, Some(status))
    } else {
        (inner, None)
    };

    render_messages_from(source, state, frame, message_area, theme);

    if let Some((status_rect, text)) = status_area.zip(state.status.as_deref()) {
        let style = Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::ITALIC);
        let line = Line::from(Span::styled(text, style));
        let paragraph = ratatui::widgets::Paragraph::new(line);
        frame.render_widget(paragraph, status_rect);
    }
}

fn render_messages_from(
    source: &dyn MessageSource,
    state: &ConversationViewState,
    frame: &mut Frame,
    area: Rect,
    theme: &Theme,
) {
    let messages = source.source_messages();

    let preliminary_lines = build_display_lines(messages, state, area.width as usize, theme);
    let needs_scrollbar = preliminary_lines.len() > area.height as usize;
    let content_width = if needs_scrollbar {
        (area.width as usize).saturating_sub(1)
    } else {
        area.width as usize
    };

    let display_lines = if needs_scrollbar {
        build_display_lines(messages, state, content_width, theme)
    } else {
        preliminary_lines
    };

    let total_lines = display_lines.len();
    let visible_lines = area.height as usize;

    let line_offset = if state.auto_scroll {
        total_lines.saturating_sub(visible_lines)
    } else {
        let estimated_line = state.scroll.offset().saturating_mul(3);
        estimated_line.min(total_lines.saturating_sub(visible_lines))
    };

    let items: Vec<ListItem> = display_lines
        .into_iter()
        .skip(line_offset)
        .take(visible_lines)
        .map(ListItem::new)
        .collect();

    let list = List::new(items);
    frame.render_widget(list, area);

    if total_lines > visible_lines {
        let outer_area = Rect {
            x: area.x.saturating_sub(1),
            y: area.y.saturating_sub(1),
            width: area.width + 2,
            height: area.height + 2,
        };
        let mut bar_scroll = crate::scroll::ScrollState::new(total_lines);
        bar_scroll.set_viewport_height(visible_lines);
        bar_scroll.set_offset(line_offset);
        crate::scroll::render_scrollbar_inside_border(&bar_scroll, frame, outer_area, theme);
    }
}

pub(super) fn build_display_lines<'a>(
    messages: &[ConversationMessage],
    state: &ConversationViewState,
    width: usize,
    theme: &Theme,
) -> Vec<Line<'a>> {
    let mut lines = Vec::new();

    for (i, msg) in messages.iter().enumerate() {
        if i > 0 {
            lines.push(Line::from(""));
        }
        format_message(msg, state, width, theme, &mut lines);
    }

    lines
}

fn format_message<'a>(
    msg: &ConversationMessage,
    state: &ConversationViewState,
    width: usize,
    theme: &Theme,
    lines: &mut Vec<Line<'a>>,
) {
    let role = msg.role();
    let role_style = state
        .role_style_overrides
        .get(role)
        .copied()
        .unwrap_or_else(|| Style::default().fg(role.color()));
    let bold_role_style = role_style.add_modifier(Modifier::BOLD);

    if state.show_role_labels {
        let mut header_spans = Vec::new();

        if state.show_timestamps {
            if let Some(ts) = msg.timestamp() {
                header_spans.push(Span::styled(
                    format!("[{}] ", ts),
                    Style::default().fg(Color::DarkGray),
                ));
            }
        }

        header_spans.push(Span::styled(
            format!("{} {}", role.indicator(), role.label()),
            bold_role_style,
        ));

        if msg.is_streaming() {
            header_spans.push(Span::styled(
                " \u{2588}",
                role_style.add_modifier(Modifier::SLOW_BLINK),
            ));
        }

        lines.push(Line::from(header_spans));
    }

    let indent = if state.show_role_labels { "  " } else { "" };
    for block in msg.blocks() {
        format_block(block, state, width, indent, role_style, theme, lines);
    }
}

fn format_block<'a>(
    block: &MessageBlock,
    state: &ConversationViewState,
    width: usize,
    indent: &str,
    role_style: Style,
    theme: &Theme,
    lines: &mut Vec<Line<'a>>,
) {
    match block {
        MessageBlock::Text(text) => {
            format_text_block(
                text,
                width,
                indent,
                role_style,
                state.markdown_enabled,
                theme,
                lines,
            );
        }
        MessageBlock::Code { code, language } => {
            format_code_block(code, language.as_deref(), width, indent, lines);
        }
        MessageBlock::ToolUse {
            name,
            input,
            output,
        } => {
            format_tool_use_block(
                name,
                input.as_deref(),
                output.as_deref(),
                width,
                indent,
                state,
                lines,
            );
        }
        MessageBlock::Thinking(content) => {
            format_thinking_block(content, width, indent, state, lines);
        }
        MessageBlock::Error(content) => {
            format_error_block(content, width, indent, lines);
        }
    }
}

fn wrap_lines(text: &str, prefix: &str, width: usize) -> Vec<String> {
    let prefix_width = UnicodeWidthStr::width(prefix);
    let effective_width = width.saturating_sub(prefix_width);
    if effective_width == 0 {
        return text.lines().map(|l| format!("{}{}", prefix, l)).collect();
    }

    let mut result = Vec::new();
    for line in text.lines() {
        let line_width = UnicodeWidthStr::width(line);
        if line_width <= effective_width {
            result.push(format!("{}{}", prefix, line));
        } else {
            let mut remaining = line;
            while !remaining.is_empty() {
                let rem_width = UnicodeWidthStr::width(remaining);
                if rem_width <= effective_width {
                    result.push(format!("{}{}", prefix, remaining));
                    break;
                }
                let mut col = 0;
                let mut last_space_byte = None;
                let mut byte_at_width = remaining.len();
                for (byte_idx, ch) in remaining.char_indices() {
                    let ch_w = UnicodeWidthChar::width(ch).unwrap_or(0);
                    if col + ch_w > effective_width {
                        byte_at_width = byte_idx;
                        break;
                    }
                    if ch == ' ' {
                        last_space_byte = Some(byte_idx + 1);
                    }
                    col += ch_w;
                }
                let break_at = last_space_byte.unwrap_or(byte_at_width);
                let segment = &remaining[..break_at];
                result.push(format!("{}{}", prefix, segment.trim_end()));
                remaining = &remaining[break_at..];
                if remaining.starts_with(' ') {
                    remaining = &remaining[1..];
                }
            }
        }
    }
    if result.is_empty() {
        result.push(prefix.to_string());
    }
    result
}

fn format_text_block<'a>(
    text: &str,
    width: usize,
    indent: &str,
    style: Style,
    markdown_enabled: bool,
    theme: &Theme,
    lines: &mut Vec<Line<'a>>,
) {
    // theme is used by the markdown rendering path below; suppress
    // the unused-variable warning when the markdown feature is off.
    let _ = &theme;

    if text.is_empty() {
        lines.push(Line::from(Span::styled(indent.to_string(), style)));
        return;
    }

    #[cfg(feature = "markdown")]
    if markdown_enabled {
        let indent_display_width = UnicodeWidthStr::width(indent);
        let available_width = width.saturating_sub(indent_display_width);
        let md_lines = crate::component::markdown_renderer::render::render_markdown(
            text,
            available_width as u16,
            theme,
        );
        for mut md_line in md_lines {
            for span in md_line.spans.iter_mut() {
                if span.style.fg.is_none() || span.style.fg == Some(theme.foreground) {
                    span.style.fg = style.fg;
                }
            }
            if indent.is_empty() {
                lines.push(md_line);
            } else {
                let mut spans: Vec<Span> = vec![Span::styled(indent.to_string(), style)];
                spans.extend(md_line.spans);
                lines.push(Line::from(spans));
            }
        }
        return;
    }

    let _ = markdown_enabled;

    for wrapped in wrap_lines(text, indent, width) {
        lines.push(Line::from(Span::styled(wrapped, style)));
    }
}

fn format_code_block<'a>(
    code: &str,
    language: Option<&str>,
    _width: usize,
    indent: &str,
    lines: &mut Vec<Line<'a>>,
) {
    let code_style = Style::default().fg(Color::White);
    let border_style = Style::default().fg(Color::DarkGray);

    let lang_label = language.unwrap_or("code");
    lines.push(Line::from(vec![
        Span::styled(format!("{}\u{2502} ", indent), border_style),
        Span::styled(
            lang_label.to_string(),
            Style::default().fg(Color::Cyan).add_modifier(Modifier::DIM),
        ),
    ]));

    let code_prefix = format!("{}\u{2502} ", indent);
    for line in code.lines() {
        lines.push(Line::from(vec![
            Span::styled(code_prefix.clone(), border_style),
            Span::styled(line.to_string(), code_style),
        ]));
    }

    if code.is_empty() {
        lines.push(Line::from(vec![Span::styled(code_prefix, border_style)]));
    }
}

fn format_tool_use_block<'a>(
    name: &str,
    input: Option<&str>,
    output: Option<&str>,
    width: usize,
    indent: &str,
    state: &ConversationViewState,
    lines: &mut Vec<Line<'a>>,
) {
    let tool_style = Style::default().fg(Color::Yellow);
    let dim_style = Style::default()
        .fg(Color::Yellow)
        .add_modifier(Modifier::DIM);

    let block_key = format!("tool:{}", name);
    let collapsed = state.collapsed_blocks.contains(&block_key);
    let toggle_char = if collapsed { "\u{25b8}" } else { "\u{25be}" };

    let inner_indent = format!("{}  ", indent);
    lines.push(Line::from(vec![
        Span::styled(format!("{}{} ", indent, toggle_char), dim_style),
        Span::styled(
            format!("Tool: {}", name),
            tool_style.add_modifier(Modifier::BOLD),
        ),
    ]));

    if !collapsed {
        match input {
            Some(text) if !text.is_empty() => {
                for wrapped in wrap_lines(text, &inner_indent, width) {
                    lines.push(Line::from(Span::styled(wrapped, dim_style)));
                }
            }
            _ => {
                lines.push(Line::from(Span::styled(
                    format!("{}(no input)", inner_indent),
                    dim_style,
                )));
            }
        }
        if let Some(out) = output {
            let output_style = Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::DIM | Modifier::ITALIC);
            let out_prefix = format!("{}-> ", inner_indent);
            for wrapped in wrap_lines(out, &out_prefix, width) {
                lines.push(Line::from(Span::styled(wrapped, output_style)));
            }
        }
    }
}

fn format_thinking_block<'a>(
    content: &str,
    width: usize,
    indent: &str,
    state: &ConversationViewState,
    lines: &mut Vec<Line<'a>>,
) {
    let thinking_style = Style::default()
        .fg(Color::Magenta)
        .add_modifier(Modifier::DIM);
    let header_style = Style::default()
        .fg(Color::Magenta)
        .add_modifier(Modifier::ITALIC);

    let collapsed = state.collapsed_blocks.contains("thinking");
    let toggle_char = if collapsed { "\u{25b8}" } else { "\u{25be}" };

    let inner_indent = format!("{}  ", indent);
    lines.push(Line::from(vec![
        Span::styled(format!("{}{} ", indent, toggle_char), thinking_style),
        Span::styled("Thinking...", header_style),
    ]));

    if !collapsed {
        for wrapped in wrap_lines(content, &inner_indent, width) {
            lines.push(Line::from(Span::styled(wrapped, thinking_style)));
        }
    }
}

fn format_error_block<'a>(content: &str, width: usize, indent: &str, lines: &mut Vec<Line<'a>>) {
    let error_style = Style::default().fg(Color::Red).add_modifier(Modifier::BOLD);

    let prefix = format!("{}\u{2716} Error: ", indent);
    for wrapped in wrap_lines(content, &prefix, width) {
        lines.push(Line::from(Span::styled(wrapped, error_style)));
    }
}

/// Calculates the total number of display lines for the current message list.
pub(super) fn total_display_lines(state: &ConversationViewState, width: usize) -> usize {
    let theme = Theme::default();
    let messages = state.source_messages();
    let lines = build_display_lines(messages, state, width, &theme);
    lines.len()
}