vtcode-ui 0.178.0

Unified UI crate for VT Code: design system, theme registry, and TUI framework
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
//! Pure text-measurement, label, and large-paste preview helpers for the composer input.
//
//Extracted from the session input module; all functions here are stateless
//and take only plain data, so they can be unit-tested and reused without a
//live `Session`.

use ratatui::{
    buffer::Buffer,
    style::{Modifier, Style},
    text::Text,
};
use regex::Regex;
use std::fmt::Write;
use std::path::Path;
use std::sync::LazyLock;
use unicode_width::UnicodeWidthChar;
use vtcode_commons::formatting::contains_ignore_ascii_case;
use vtcode_commons::fs::{is_image_path, trim_trailing_image_path_str, unescape_whitespace};

use crate::tui::config::constants::ui;
use crate::tui::core_tui::blocked_status::is_git_status;

use super::CompactInputPreview;
use super::tokenizer::{InputTokenKind, tokenize_input};

pub(super) fn inline_prompt_suggestion_suffix(current: &str, suggestion: &str) -> Option<String> {
    if current.trim().is_empty() {
        return Some(suggestion.to_string());
    }

    let suggestion_lower = suggestion.to_lowercase();
    let current_lower = current.to_lowercase();
    if !suggestion_lower.starts_with(&current_lower) {
        return None;
    }

    Some(suggestion.chars().skip(current.chars().count()).collect())
}

pub(super) fn compact_image_label(content: &str) -> Option<String> {
    let trimmed = content.trim();
    if trimmed.is_empty() {
        return None;
    }

    let unquoted = trimmed
        .strip_prefix('"')
        .and_then(|value| value.strip_suffix('"'))
        .or_else(|| trimmed.strip_prefix('\'').and_then(|value| value.strip_suffix('\'')))
        .unwrap_or(trimmed);

    if unquoted.starts_with("data:image/") {
        return Some("inline image".to_string());
    }

    let windows_drive = unquoted.as_bytes().get(1).is_some_and(|ch| *ch == b':')
        && unquoted.as_bytes().get(2).is_some_and(|ch| *ch == b'\\' || *ch == b'/');
    let starts_like_path = unquoted.starts_with('@')
        || unquoted.starts_with("file://")
        || unquoted.starts_with('/')
        || unquoted.starts_with("./")
        || unquoted.starts_with("../")
        || unquoted.starts_with("~/")
        || windows_drive;
    if !starts_like_path {
        return None;
    }

    let without_at = unquoted.strip_prefix('@').unwrap_or(unquoted);

    // Skip npm scoped package patterns like @scope/package@version
    if without_at.contains('/')
        && !without_at.starts_with('.')
        && !without_at.starts_with('/')
        && !without_at.starts_with("~/")
    {
        // Check if this looks like @scope/package (npm package)
        let parts: Vec<&str> = without_at.split('/').collect();
        if parts.len() >= 2 && !parts[0].is_empty() {
            // Reject if it looks like a package name (no extension on second component)
            if !parts[parts.len() - 1].contains('.') {
                return None;
            }
        }
    }

    let without_scheme = without_at.strip_prefix("file://").unwrap_or(without_at);
    let path = Path::new(without_scheme);
    if !is_image_path(path) {
        return None;
    }

    let label = path.file_name().and_then(|name| name.to_str()).unwrap_or(without_scheme);
    Some(label.to_string())
}

static IMAGE_PATH_INLINE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(
        r#"(?ix)
        (?:^|[\s\(\[\{<\"'`])
        (
            @?
            (?:file://)?
            (?:
                ~/(?:[^\n/]+/)+
              | /(?:[^\n/]+/)+
              | [A-Za-z]:[\\/](?:[^\n\\\/]+[\\/])+
            )
            [^\n]+?
            \.(?:png|jpe?g|gif|webp)
        )"#,
    )
    .expect("Failed to compile inline image path regex")
});

pub(super) fn compact_image_placeholders(content: &str) -> Option<String> {
    let mut matches = Vec::new();
    for capture in IMAGE_PATH_INLINE_REGEX.captures_iter(content) {
        let Some(path_match) = capture.get(1) else {
            continue;
        };
        let raw = path_match.as_str();
        // The regex may consume trailing text after the image extension.
        // Try progressively shorter suffixes to find the actual image path.
        let trimmed_raw = trim_trailing_image_path_str(raw);
        let Some(label) = image_label_for_path(trimmed_raw) else {
            continue;
        };
        let end = path_match.start() + trimmed_raw.len();
        matches.push((path_match.start(), end, label));
    }

    if matches.is_empty() {
        return None;
    }

    let mut result = String::with_capacity(content.len());
    let mut last_end = 0usize;
    for (start, end, label) in matches {
        if start < last_end {
            continue;
        }
        result.push_str(&content[last_end..start]);
        let _ = write!(result, "[Image: {label}]");
        last_end = end;
    }
    if last_end < content.len() {
        result.push_str(&content[last_end..]);
    }

    Some(result)
}

pub(super) fn image_label_for_path(raw: &str) -> Option<String> {
    let trimmed = raw.trim_matches(|ch: char| matches!(ch, '"' | '\'')).trim();
    if trimmed.is_empty() {
        return None;
    }

    let without_at = trimmed.strip_prefix('@').unwrap_or(trimmed);
    let without_scheme = without_at.strip_prefix("file://").unwrap_or(without_at);
    let unescaped = unescape_whitespace(without_scheme);
    let path = Path::new(unescaped.as_str());
    if !is_image_path(path) {
        return None;
    }

    let label = path.file_name().and_then(|name| name.to_str()).unwrap_or(unescaped.as_str());
    Some(label.to_string())
}

pub(super) fn is_spinner_frame(indicator: &str) -> bool {
    matches!(indicator, "⠋" | "⠙" | "⠹" | "⠸" | "⠼" | "⠴" | "⠦" | "⠧" | "⠇" | "⠏" | "-" | "\\" | "|" | "/" | ".")
}

pub(crate) fn status_requires_shimmer(text: &str) -> bool {
    // Case-insensitive contains without allocating a lowercased String.
    // This function is called up to 3× per TUI tick (4 Hz upkeep when idle,
    // 60 Hz while interacting or animating) from is_running_activity /
    // has_status_spinner / is_shimmer_active, so
    // avoiding the per-call String allocation matters.
    let trimmed = text.trim();
    // Git branch names may contain activity words. Recognize the existing
    // labelled and compact Git formats before interpreting free-form status.
    if is_git_status(trimmed) {
        return false;
    }
    let needles = [
        "running command:",
        "running tool:",
        "running:",
        "running ",
        "executing ",
        "drafting plan",
        "validating plan",
        "persisting plan",
        "preparing approval",
        "approval required",
        "permission required",
        "action required",
        "input required",
        "waiting for approval",
        "waiting for input",
        "blocked",
        "[blocked]",
        "recovery: tools disabled",
        "tools disabled",
        "ctrl+c",
        "/stop to stop",
    ];
    if needles.iter().any(|needle| contains_ignore_ascii_case(trimmed, needle)) {
        return true;
    }
    let Some((indicator, rest)) = trimmed.split_once(' ') else {
        return false;
    };
    if indicator.chars().count() != 1 || rest.trim().is_empty() {
        return false;
    }
    is_spinner_frame(indicator)
}

/// Data structure for input widget rendering
#[derive(Clone, Debug)]
pub struct InputWidgetData {
    pub text: Text<'static>,
    pub cursor_x: u16,
    pub cursor_y: u16,
    pub cursor_should_be_visible: bool,
    pub use_fake_cursor: bool,
    pub background_style: Style,
    pub default_style: Style,
}

pub(super) fn render_fake_cursor(buf: &mut Buffer, cursor_x: u16, cursor_y: u16) {
    if let Some(cell) = buf.cell_mut((cursor_x, cursor_y)) {
        let mut style = cell.style();
        style = style.add_modifier(Modifier::REVERSED);
        cell.set_style(style);
        if cell.symbol().is_empty() {
            cell.set_symbol(" ");
        }
    }
}

pub(super) fn char_index_to_byte_index(content: &str, char_index: usize) -> usize {
    if char_index == 0 {
        return 0;
    }

    content
        .char_indices()
        .nth(char_index)
        .map(|(byte_index, _)| byte_index)
        .unwrap_or(content.len())
}

pub(super) fn byte_index_to_char_index(content: &str, byte_index: usize) -> usize {
    // Clamp to the nearest char boundary so a mid-multi-byte `byte_index`
    // (e.g. from an out-of-sync selection range) never panics the slice.
    let mut safe = byte_index.min(content.len());
    while safe > 0 && !content.is_char_boundary(safe) {
        safe -= 1;
    }
    content[..safe].chars().count()
}

pub(super) fn compact_inline_segment(content: &str) -> String {
    content
        .chars()
        .map(|ch| if ch == '\n' || ch == '\r' { ' ' } else { ch })
        .collect()
}

/// Number of image tokens visible in composer text.
///
/// Counts clipboard `[Image #N]` placeholders, inline `data:image/…` payloads,
/// and image file-path matches. Used with the attachment count to decide when
/// the composer should collapse to a summary instead of showing full text.
pub(super) fn count_input_image_tokens(content: &str) -> usize {
    let placeholder_count = content.matches("[Image #").count();
    let inline_data_count = content.matches("data:image/").count();
    let path_count = IMAGE_PATH_INLINE_REGEX.captures_iter(content).count();
    placeholder_count.saturating_add(inline_data_count).saturating_add(path_count)
}

/// Number of `@file` reference tokens in composer text.
pub(super) fn count_input_file_tokens(content: &str) -> usize {
    tokenize_input(content)
        .iter()
        .filter(|token| token.kind == InputTokenKind::FileReference)
        .count()
}

/// Effective image count for collapse decisions: the larger of visible text
/// tokens and live attachments, so orphaned-attachment payloads still collapse
/// while deleted placeholders do not double-count.
pub(super) fn effective_image_count(content: &str, attachment_count: usize) -> usize {
    count_input_image_tokens(content).max(attachment_count)
}

/// Measured size of composer (or pasted) text for collapse decisions.
///
/// Dimension key: `char_count` is Unicode scalar count, `line_count` is
/// logical `\n` lines, `image_count` covers `[Image #N]` + `data:image/` +
/// image paths (+ attachments via [`InputSizeMetrics::of_content`]),
/// `file_count` is `@file` tokens. Single source of truth so the paste gate,
/// the generic preview gate, and the paste-tracking gate cannot drift apart.
struct InputSizeMetrics {
    char_count: usize,
    line_count: usize,
    image_count: usize,
    file_count: usize,
}

impl InputSizeMetrics {
    fn of_text(text: &str) -> Self {
        Self {
            char_count: text.chars().count(),
            line_count: text.split('\n').count(),
            image_count: count_input_image_tokens(text),
            file_count: count_input_file_tokens(text),
        }
    }

    fn of_content(content: &str, attachment_count: usize) -> Self {
        Self {
            char_count: content.chars().count(),
            line_count: content.split('\n').count(),
            image_count: effective_image_count(content, attachment_count),
            file_count: count_input_file_tokens(content),
        }
    }

    fn should_collapse(&self) -> bool {
        self.line_count >= ui::INLINE_PASTE_COLLAPSE_LINE_THRESHOLD
            || self.char_count >= ui::INLINE_INPUT_COMPACT_CHAR_THRESHOLD
            || self.image_count >= ui::INLINE_INPUT_COMPACT_IMAGE_THRESHOLD
            || self.file_count >= ui::INLINE_INPUT_COMPACT_FILE_TOKEN_THRESHOLD
    }

    fn placeholder(&self) -> String {
        format_large_input_placeholder(self.char_count, self.line_count, self.image_count, self.file_count)
    }
}

/// Summary placeholder for large composer content.
///
/// Dimension key: `char_count` is Unicode scalar count, `line_count` is
/// logical `\n` lines, `image_count` covers `[Image #N]` + `data:image/` +
/// image paths + attachments, `file_count` is `@file` tokens. Keeps the
/// legacy `[Pasted Content N chars]` prefix so existing transcript/status
/// matching keeps working, appending line/image/file details when present.
pub(super) fn format_large_input_placeholder(
    char_count: usize,
    line_count: usize,
    image_count: usize,
    file_count: usize,
) -> String {
    let mut placeholder = format!("[Pasted Content {char_count} chars");
    if line_count > 1 {
        let _ = write!(placeholder, ", {line_count} lines");
    }
    if image_count > 0 {
        let label = if image_count == 1 { "image" } else { "images" };
        let _ = write!(placeholder, ", {image_count} {label}");
    }
    if file_count > 0 {
        let label = if file_count == 1 { "file" } else { "files" };
        let _ = write!(placeholder, ", {file_count} {label}");
    }
    placeholder.push(']');
    placeholder
}

/// Paste-range preview, extended beyond the legacy line-count gate.
///
/// Collapses when the pasted slice itself is large by lines, chars, images,
/// or file tokens. `before` keeps the last head-chars of the previous content
/// and each `after` line keeps its first head-chars, so surrounding context
/// stays visible but bounded while the pasted block becomes one marker.
pub(super) fn compact_paste_range_preview(
    content: &str,
    range: Option<std::ops::Range<usize>>,
) -> Option<CompactInputPreview> {
    let range = range?;
    if range.start >= range.end
        || range.end > content.len()
        || !content.is_char_boundary(range.start)
        || !content.is_char_boundary(range.end)
    {
        return None;
    }

    let pasted = &content[range.clone()];
    let pasted_metrics = InputSizeMetrics::of_text(pasted);
    if !pasted_metrics.should_collapse() {
        return None;
    }

    let head_chars = ui::INLINE_INPUT_COMPACT_PREVIEW_HEAD_CHARS;
    let before_src = &content[..range.start];
    let before = compact_inline_segment(&before_src[last_n_chars_start(before_src, head_chars)..]);
    let after_lines = content[range.end..]
        .split('\n')
        .map(|line| compact_inline_segment(&line[..first_n_chars_end(line, head_chars)]))
        .collect();
    Some(CompactInputPreview {
        before,
        placeholder: pasted_metrics.placeholder(),
        after_lines,
    })
}

/// Generic preview for large composer content without a qualifying paste range.
///
/// Covers typed large inputs, single-line floods (minified JSON/base64), and
/// token floods (many `[Image #N]` / `@file`). For char-large content shows a
/// truncated head, the summary placeholder, and a truncated tail so previous
/// content is summarized rather than rendered in full. For token-only floods
/// (short text but many images/files) shows just the placeholder to avoid
/// echoing the token list twice.
pub(super) fn generic_large_input_preview(content: &str, attachment_count: usize) -> Option<CompactInputPreview> {
    let metrics = InputSizeMetrics::of_content(content, attachment_count);
    if !metrics.should_collapse() {
        return None;
    }

    let head_chars = ui::INLINE_INPUT_COMPACT_PREVIEW_HEAD_CHARS;
    let tail_chars = ui::INLINE_INPUT_COMPACT_PREVIEW_TAIL_CHARS;
    let char_large = metrics.char_count > head_chars.saturating_add(tail_chars);
    let (before, after_lines) = if char_large {
        let before = compact_inline_segment(&content[..first_n_chars_end(content, head_chars)]);
        let tail = &content[last_n_chars_start(content, tail_chars)..];
        (before, vec![compact_inline_segment(tail)])
    } else {
        (String::new(), Vec::new())
    };
    Some(CompactInputPreview {
        before,
        placeholder: metrics.placeholder(),
        after_lines,
    })
}

/// Whether pasted text should be tracked as a collapsible block.
///
/// Mirrors the preview gate so char-large, image-heavy, and file-heavy pastes
/// collapse the same way line-heavy pastes already do.
pub(crate) fn should_track_compact_paste(pasted: &str) -> bool {
    InputSizeMetrics::of_text(pasted).should_collapse()
}

/// End byte index of the first `n` chars (char-boundary safe, no allocation).
pub(super) fn first_n_chars_end(content: &str, n: usize) -> usize {
    content.char_indices().nth(n).map_or(content.len(), |(idx, _)| idx)
}

/// Start byte index of the last `n` chars (char-boundary safe, no allocation).
pub(super) fn last_n_chars_start(content: &str, n: usize) -> usize {
    if n == 0 {
        return content.len();
    }
    match content.char_indices().rev().nth(n) {
        Some((idx, ch)) => idx + ch.len_utf8(),
        None => 0,
    }
}

pub(super) fn display_width_for_char_range(content: &str, char_count: usize) -> u16 {
    content
        .chars()
        .take(char_count)
        .map(|ch| UnicodeWidthChar::width(ch).unwrap_or(0) as u16)
        .fold(0_u16, u16::saturating_add)
}