vtcode_tui/core_tui/session/
wrapping.rs1use ratatui::prelude::*;
7use ratatui::widgets::Paragraph;
8use regex::Regex;
9use std::sync::LazyLock;
10use unicode_width::UnicodeWidthStr;
11
12static PRESERVED_TOKEN_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
14 Regex::new(
15 r#"[a-zA-Z][a-zA-Z0-9+.-]*://[^\s<>\[\]{}|^]+|[a-zA-Z0-9][-a-zA-Z0-9]*\.[a-zA-Z]{2,}(/[^\s<>\[\]{}|^]*)?|localhost:\d+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?|`(?:file://|~/|/|\./|\.\./|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^`]+`|"(?:file://|~/|/|\./|\.\./|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^"]+"|'(?:file://|~/|/|\./|\.\./|[A-Za-z]:[\\/]|[A-Za-z0-9._-]+[\\/])[^']+'|(?:\./|\../|~/|/)[^\s<>\[\]{}|^]+|(?:[A-Za-z]:[\\/][^\s<>\[\]{}|^]+)|(?:[A-Za-z0-9._-]+[\\/][^\s<>\[\]{}|^]+)"#,
16 )
17 .unwrap()
18});
19
20pub fn contains_preserved_token(text: &str) -> bool {
22 PRESERVED_TOKEN_PATTERN.is_match(text)
23}
24
25pub fn wrap_line_preserving_urls(line: Line<'static>, max_width: usize) -> Vec<Line<'static>> {
31 if max_width == 0 {
32 return vec![Line::default()];
33 }
34
35 let text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
36
37 if !contains_preserved_token(&text) {
39 return super::text_utils::wrap_line(line, max_width);
40 }
41
42 let urls: Vec<_> = PRESERVED_TOKEN_PATTERN
44 .find_iter(&text)
45 .map(|m| (m.start(), m.end(), m.as_str()))
46 .collect();
47
48 if urls.len() == 1 && urls[0].0 == 0 && urls[0].1 == text.len() && text.width() <= max_width {
50 return vec![line];
51 }
52 wrap_mixed_content(line, &text, max_width, &urls)
56}
57
58fn wrap_mixed_content(
60 line: Line<'static>,
61 text: &str,
62 max_width: usize,
63 urls: &[(usize, usize, &str)],
64) -> Vec<Line<'static>> {
65 use unicode_segmentation::UnicodeSegmentation;
66 use unicode_width::UnicodeWidthStr;
67
68 let mut result = Vec::with_capacity(urls.len() + 1);
69 let mut current_line: Vec<Span<'static>> = Vec::new();
70 let mut current_width = 0usize;
71 let mut text_pos = 0usize;
72
73 let flush_line = |spans: &mut Vec<Span<'static>>, result: &mut Vec<Line<'static>>| {
74 if spans.is_empty() {
75 result.push(Line::default());
76 } else {
77 result.push(Line::from(std::mem::take(spans)));
78 }
79 };
80
81 let default_style = line.spans.first().map(|s| s.style).unwrap_or_default();
83
84 for (url_start, url_end, url_text) in urls {
85 if *url_start > text_pos {
87 let before = &text[text_pos..*url_start];
88
89 for grapheme in UnicodeSegmentation::graphemes(before, true) {
90 let gw = grapheme.width();
91 if current_width + gw > max_width && current_width > 0 {
92 flush_line(&mut current_line, &mut result);
93 current_width = 0;
94 }
95 current_line.push(Span::styled(grapheme.to_string(), default_style));
96 current_width += gw;
97 }
98 }
99
100 let url_width = url_text.width();
102 if current_width > 0 && current_width + url_width > max_width {
103 flush_line(&mut current_line, &mut result);
104 current_width = 0;
105 }
106 current_line.push(Span::styled(url_text.to_string(), default_style));
107 current_width += url_width;
108
109 text_pos = *url_end;
110 }
111
112 if text_pos < text.len() {
114 let remaining = &text[text_pos..];
115
116 for grapheme in UnicodeSegmentation::graphemes(remaining, true) {
117 let gw = grapheme.width();
118 if current_width + gw > max_width && current_width > 0 {
119 flush_line(&mut current_line, &mut result);
120 current_width = 0;
121 }
122 current_line.push(Span::styled(grapheme.to_string(), default_style));
123 current_width += gw;
124 }
125 }
126
127 flush_line(&mut current_line, &mut result);
128 if result.is_empty() {
129 result.push(Line::default());
130 }
131 result
132}
133
134pub fn wrap_lines_preserving_urls(
136 lines: Vec<Line<'static>>,
137 max_width: usize,
138) -> Vec<Line<'static>> {
139 if max_width == 0 {
140 return vec![Line::default()];
141 }
142 lines
143 .into_iter()
144 .flat_map(|line| wrap_line_preserving_urls(line, max_width))
145 .collect()
146}
147
148pub fn calculate_wrapped_height(text: &str, width: u16) -> usize {
150 if width == 0 {
151 return text.lines().count().max(1);
152 }
153 Paragraph::new(text).line_count(width)
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[test]
161 fn test_url_detection() {
162 assert!(contains_preserved_token("https://example.com"));
163 assert!(contains_preserved_token("example.com/path"));
164 assert!(contains_preserved_token("localhost:8080"));
165 assert!(contains_preserved_token("192.168.1.1:8080"));
166 assert!(!contains_preserved_token("not a url"));
167 }
168
169 #[test]
170 fn test_file_path_detection() {
171 assert!(contains_preserved_token("src/main.rs"));
172 assert!(contains_preserved_token("./src/main.rs"));
173 assert!(contains_preserved_token("/tmp/example.txt"));
174 assert!(contains_preserved_token("\"./docs/My Notes.md\""));
175 assert!(contains_preserved_token(
176 "`/Users/example/Library/Application Support/Code/User/settings.json`"
177 ));
178 }
179
180 #[test]
181 fn test_url_only_preserved() {
182 let line = Line::from(Span::raw("https://example.com"));
183 let wrapped = wrap_line_preserving_urls(line, 80);
184 assert_eq!(wrapped.len(), 1);
185 assert!(
186 wrapped[0]
187 .spans
188 .iter()
189 .any(|s| s.content.contains("https://"))
190 );
191 }
192
193 #[test]
194 fn test_mixed_content() {
195 let line = Line::from(Span::raw("See https://example.com for info"));
196 let wrapped = wrap_line_preserving_urls(line, 25);
197 let all_text: String = wrapped
198 .iter()
199 .flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
200 .collect();
201 assert!(all_text.contains("https://example.com"));
202 assert!(all_text.contains("See"));
203 }
204
205 #[test]
206 fn test_quoted_path_with_spaces_is_preserved() {
207 let line = Line::from(Span::raw("Open \"./docs/My Notes.md\" for details"));
208 let wrapped = wrap_line_preserving_urls(line, 18);
209 let all_text: String = wrapped
210 .iter()
211 .flat_map(|line| line.spans.iter().map(|span| span.content.as_ref()))
212 .collect();
213
214 assert!(all_text.contains("\"./docs/My Notes.md\""));
215 }
216
217 #[test]
218 fn test_no_url_delegates() {
219 let line = Line::from(Span::raw("Regular text without URLs"));
220 let wrapped = wrap_line_preserving_urls(line, 10);
221 assert!(!wrapped.is_empty());
222 }
223}