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
|
//! Leaf components for each content type and factory functions for building
//! the component tree from the view model.
use atuin_client::theme::{Meaning, Theme};
use ratatui::{
Frame,
backend::FromCrossterm,
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
widgets::{Paragraph, Wrap},
};
use super::component::{Component, RenderContext, Separator, Spacer, SymbolRow, VStack};
use super::spinner::active_frame;
use super::view_model::{Block, Content, WarningKind};
// ---------------------------------------------------------------------------
// Text measurement utilities
// ---------------------------------------------------------------------------
/// Count lines when text is wrapped at given width.
/// Uses ratatui's Paragraph::line_count for accurate wrapping calculation.
pub(crate) fn line_count_wrapped(text: &str, width: usize) -> u16 {
if width == 0 {
return 1;
}
let paragraph = Paragraph::new(text).wrap(Wrap { trim: false });
paragraph.line_count(width as u16).max(1) as u16
}
/// Count lines using word-wrap algorithm (matches TextArea's WrapMode::Word).
/// Words won't be broken mid-word, so this may produce more lines than character wrapping.
/// Returns (line_count, last_line_width) so caller can determine if cursor needs extra space.
pub(crate) fn word_wrap_line_count_with_last_width(text: &str, width: usize) -> (u16, usize) {
if width == 0 || text.is_empty() {
return (1, 0);
}
let mut line_count = 0u16;
let mut current_line_width = 0usize;
for line in text.lines() {
if line.is_empty() {
line_count += 1;
current_line_width = 0;
continue;
}
let mut line_started = false;
for word in line.split_whitespace() {
let word_width = unicode_width::UnicodeWidthStr::width(word);
if !line_started {
if word_width > width {
line_count += word_width.div_ceil(width) as u16;
current_line_width = word_width % width;
if current_line_width == 0 {
current_line_width = 0;
line_started = false;
} else {
line_started = true;
}
} else {
current_line_width = word_width;
line_started = true;
}
} else {
let needed = current_line_width + 1 + word_width;
if needed > width {
line_count += 1;
if word_width > width {
line_count += word_width.div_ceil(width) as u16;
current_line_width = word_width % width;
if current_line_width == 0 {
line_started = false;
}
} else {
current_line_width = word_width;
}
} else {
current_line_width = needed;
}
}
}
if line_started {
line_count += 1;
}
}
if line_count == 0 {
line_count = 1;
current_line_width = 0;
}
(line_count, current_line_width)
}
// ---------------------------------------------------------------------------
// Inline markdown formatting
// ---------------------------------------------------------------------------
/// Parse inline markdown formatting (**bold** and `code`) into styled spans.
/// Preserves all other text — list prefixes, indentation, and line structure
/// are left exactly as-is.
fn style_inline_markdown(text: &str, theme: &Theme) -> Vec<Line<'static>> {
let base_style = Style::from_crossterm(theme.as_style(Meaning::Base));
let code_style = Style::from_crossterm(theme.as_style(Meaning::Guidance));
let bold_style = base_style.add_modifier(Modifier::BOLD);
text.lines()
.map(|line| {
Line::from(parse_inline_formatting(
line, base_style, bold_style, code_style,
))
})
.collect()
}
/// Parse a single line for `code` and **bold** markers, returning styled spans.
fn parse_inline_formatting(
line: &str,
base: Style,
bold: Style,
code: Style,
) -> Vec<Span<'static>> {
let mut spans = Vec::new();
let mut current = String::new();
let mut chars = line.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '`' {
// Flush accumulated plain text
if !current.is_empty() {
spans.push(Span::styled(std::mem::take(&mut current), base));
}
// Collect until closing backtick
let mut code_text = String::new();
let mut closed = false;
for next in chars.by_ref() {
if next == '`' {
closed = true;
break;
}
code_text.push(next);
}
if closed {
spans.push(Span::styled(code_text, code));
} else {
// Unclosed backtick — render as-is
current.push('`');
current.push_str(&code_text);
}
} else if ch == '*' && chars.peek() == Some(&'*') {
chars.next(); // consume second *
// Flush accumulated plain text
if !current.is_empty() {
spans.push(Span::styled(std::mem::take(&mut current), base));
}
// Collect until closing **
let mut bold_text = String::new();
let mut closed = false;
while let Some(next) = chars.next() {
if next == '*' && chars.peek() == Some(&'*') {
chars.next();
closed = true;
break;
}
bold_text.push(next);
}
if closed {
spans.push(Span::styled(bold_text, bold));
} else {
// Unclosed ** — render as-is
current.push_str("**");
current.push_str(&bold_text);
}
} else {
current.push(ch);
}
}
if !current.is_empty() {
spans.push(Span::styled(current, base));
}
spans
}
// ---------------------------------------------------------------------------
// Leaf components
// ---------------------------------------------------------------------------
/// User input display (active textarea or static text).
pub struct InputContent {
pub text: String,
pub active: bool,
}
impl Component for InputContent {
fn height(&self, width: u16) -> u16 {
let w = width as usize;
if self.active {
let (lines, last_width) = word_wrap_line_count_with_last_width(&self.text, w);
if last_width >= w {
lines.saturating_add(1)
} else {
lines
}
} else {
line_count_wrapped(&self.text, w)
}
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
if self.active {
if let Some(textarea) = ctx.textarea {
frame.render_widget(textarea, area);
}
} else {
let style = Style::from_crossterm(ctx.theme.as_style(Meaning::Base));
frame.render_widget(
Paragraph::new(self.text.as_str())
.style(style)
.wrap(Wrap { trim: false }),
area,
);
}
}
}
/// Command suggestion ($ prefix).
pub struct CommandContent {
pub text: String,
pub faded: bool,
}
impl Component for CommandContent {
fn height(&self, width: u16) -> u16 {
line_count_wrapped(&self.text, width as usize)
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
let mut style = Style::from_crossterm(ctx.theme.as_style(Meaning::Base));
if self.faded {
style = style.add_modifier(Modifier::DIM);
}
frame.render_widget(
Paragraph::new(self.text.as_str())
.style(style)
.wrap(Wrap { trim: false }),
area,
);
}
}
/// Markdown text content (indented, no symbol).
pub struct TextContent {
pub markdown: String,
}
impl Component for TextContent {
fn height(&self, width: u16) -> u16 {
// Height uses raw text — slightly overestimates since markdown syntax
// characters (**, `) are stripped in rendering, but this is harmless
// (allocates equal or more space than needed, never less).
line_count_wrapped(&self.markdown, width as usize)
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
let lines = style_inline_markdown(&self.markdown, ctx.theme);
let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false });
frame.render_widget(paragraph, area);
}
}
/// Error message (! prefix).
pub struct ErrorContent {
pub message: String,
}
impl Component for ErrorContent {
fn height(&self, width: u16) -> u16 {
line_count_wrapped(&self.message, width as usize)
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
let style = Style::from_crossterm(ctx.theme.as_style(Meaning::Base));
frame.render_widget(
Paragraph::new(self.message.as_str())
.style(style)
.wrap(Wrap { trim: false }),
area,
);
}
}
/// Warning for dangerous or low-confidence commands.
pub struct WarningContent {
pub kind: WarningKind,
pub text: String,
pub pending_confirm: bool,
}
impl Component for WarningContent {
fn height(&self, width: u16) -> u16 {
let display_text = if self.pending_confirm {
"Press Enter again to run this dangerous command"
} else {
self.text.as_str()
};
line_count_wrapped(display_text, width as usize)
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
let style = Style::from_crossterm(ctx.theme.as_style(Meaning::Base));
let display_text = if self.pending_confirm {
"Press Enter again to run this dangerous command"
} else {
self.text.as_str()
};
frame.render_widget(
Paragraph::new(display_text)
.style(style)
.wrap(Wrap { trim: false }),
area,
);
}
}
/// Animated spinner with status text.
pub struct SpinnerContent {
pub status_text: String,
}
impl Component for SpinnerContent {
fn height(&self, _width: u16) -> u16 {
1
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
let style = Style::from_crossterm(ctx.theme.as_style(Meaning::Annotation));
frame.render_widget(Paragraph::new(self.status_text.as_str()).style(style), area);
}
}
/// Tool call progress (in-flight spinner or completed checkmark).
pub struct ToolStatusContent {
pub completed_count: usize,
pub current_label: Option<String>,
pub frame: usize,
}
impl Component for ToolStatusContent {
fn height(&self, _width: u16) -> u16 {
1
}
fn render(&self, frame: &mut Frame, area: Rect, ctx: &RenderContext) {
let style = Style::from_crossterm(ctx.theme.as_style(Meaning::Annotation));
let text = if let Some(ref label) = self.current_label {
if self.completed_count > 0 {
format!(
"{} (used {} tool{})",
label,
self.completed_count,
if self.completed_count == 1 { "" } else { "s" }
)
} else {
label.clone()
}
} else {
format!(
"Used {} tool{}",
self.completed_count,
if self.completed_count == 1 { "" } else { "s" }
)
};
frame.render_widget(Paragraph::new(text).style(style), area);
}
}
// ---------------------------------------------------------------------------
// Factory functions
// ---------------------------------------------------------------------------
/// Convert a view model `Content` item into a `SymbolRow`-wrapped component.
fn content_to_component(content: &Content) -> Box<dyn Component> {
match content {
Content::Input { text, active, .. } => Box::new(SymbolRow {
symbol: ">".to_string(),
symbol_meaning: Meaning::Guidance,
inner: Box::new(InputContent {
text: text.clone(),
active: *active,
}),
}),
Content::Command { text, faded } => Box::new(SymbolRow {
symbol: "$".to_string(),
symbol_meaning: Meaning::Important,
inner: Box::new(CommandContent {
text: text.clone(),
faded: *faded,
}),
}),
Content::Text { markdown } => Box::new(SymbolRow {
symbol: " ".to_string(),
symbol_meaning: Meaning::Base,
inner: Box::new(TextContent {
markdown: markdown.clone(),
}),
}),
Content::Error { message } => Box::new(SymbolRow {
symbol: "!".to_string(),
symbol_meaning: Meaning::AlertError,
inner: Box::new(ErrorContent {
message: message.clone(),
}),
}),
Content::Warning {
kind,
text,
pending_confirm,
} => {
let (symbol, meaning) = match kind {
WarningKind::Danger => ("!", Meaning::AlertError),
WarningKind::LowConfidence => ("?", Meaning::AlertWarn),
};
Box::new(SymbolRow {
symbol: symbol.to_string(),
symbol_meaning: meaning,
inner: Box::new(WarningContent {
kind: *kind,
text: text.clone(),
pending_confirm: *pending_confirm,
}),
})
}
Content::Spinner { frame, status_text } => Box::new(SymbolRow {
symbol: active_frame(*frame).to_string(),
symbol_meaning: Meaning::Annotation,
inner: Box::new(SpinnerContent {
status_text: status_text.clone(),
}),
}),
Content::ToolStatus {
completed_count,
current_label,
frame,
} => {
let symbol = if current_label.is_some() {
active_frame(*frame).to_string()
} else {
"\u{2713}".to_string() // ✓
};
Box::new(SymbolRow {
symbol,
symbol_meaning: Meaning::Annotation,
inner: Box::new(ToolStatusContent {
completed_count: *completed_count,
current_label: current_label.clone(),
frame: *frame,
}),
})
}
}
}
/// Convert a view model `Block` into a `VStack` of content components.
fn build_block_component(block: &Block) -> Box<dyn Component> {
let mut children: Vec<Box<dyn Component>> = Vec::new();
for (idx, content) in block.content.iter().enumerate() {
if idx > 0 {
children.push(Box::new(Spacer(1))); // blank line between items
}
children.push(content_to_component(content));
}
// Trailing blank line (padding after content)
children.push(Box::new(Spacer(1)));
Box::new(VStack::new(children))
}
/// Build the full component tree from an ordered list of view model blocks.
///
/// The tree is a `VStack` with blocks separated by `Separator` + `Spacer` pairs.
/// The caller sets `scroll_offset` on the returned `VStack` before rendering.
pub fn build_component_tree(items: &[&Block], card_width: u16) -> VStack {
let mut children: Vec<Box<dyn Component>> = Vec::new();
for (idx, block) in items.iter().enumerate() {
if idx > 0 {
children.push(Box::new(Separator { card_width }));
children.push(Box::new(Spacer(1))); // leading blank after separator
}
children.push(build_block_component(block));
}
VStack::new(children)
}
|