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, max_width, &urls)
56}
57
58fn wrap_mixed_content(
60 line: Line<'static>,
61 max_width: usize,
62 urls: &[(usize, usize, &str)],
63) -> Vec<Line<'static>> {
64 use unicode_segmentation::UnicodeSegmentation;
65 use unicode_width::UnicodeWidthStr;
66
67 let mut result = Vec::new();
68 let mut current_line: Vec<Span<'static>> = Vec::new();
69 let mut current_width = 0usize;
70 let mut text_pos = 0usize;
71
72 let flush_line = |spans: &mut Vec<Span<'static>>, result: &mut Vec<Line<'static>>| {
73 if spans.is_empty() {
74 result.push(Line::default());
75 } else {
76 result.push(Line::from(std::mem::take(spans)));
77 }
78 };
79
80 let default_style = line.spans.first().map(|s| s.style).unwrap_or_default();
82
83 for (url_start, url_end, url_text) in urls {
84 if *url_start > text_pos {
86 let before = &line
87 .spans
88 .iter()
89 .map(|s| s.content.as_ref())
90 .collect::<String>()[text_pos..*url_start];
91
92 for grapheme in UnicodeSegmentation::graphemes(before, true) {
93 let gw = grapheme.width();
94 if current_width + gw > max_width && current_width > 0 {
95 flush_line(&mut current_line, &mut result);
96 current_width = 0;
97 }
98 current_line.push(Span::styled(grapheme.to_string(), default_style));
99 current_width += gw;
100 }
101 }
102
103 let url_width = url_text.width();
105 if current_width > 0 && current_width + url_width > max_width {
106 flush_line(&mut current_line, &mut result);
107 current_width = 0;
108 }
109 current_line.push(Span::styled(url_text.to_string(), default_style));
110 current_width += url_width;
111
112 text_pos = *url_end;
113 }
114
115 if text_pos < line.spans.iter().map(|s| s.content.len()).sum::<usize>() {
117 let full_text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
118 let remaining = &full_text[text_pos..];
119
120 for grapheme in UnicodeSegmentation::graphemes(remaining, true) {
121 let gw = grapheme.width();
122 if current_width + gw > max_width && current_width > 0 {
123 flush_line(&mut current_line, &mut result);
124 current_width = 0;
125 }
126 current_line.push(Span::styled(grapheme.to_string(), default_style));
127 current_width += gw;
128 }
129 }
130
131 flush_line(&mut current_line, &mut result);
132 if result.is_empty() {
133 result.push(Line::default());
134 }
135 result
136}
137
138pub fn wrap_lines_preserving_urls(
140 lines: Vec<Line<'static>>,
141 max_width: usize,
142) -> Vec<Line<'static>> {
143 if max_width == 0 {
144 return vec![Line::default()];
145 }
146 lines
147 .into_iter()
148 .flat_map(|line| wrap_line_preserving_urls(line, max_width))
149 .collect()
150}
151
152pub fn calculate_wrapped_height(text: &str, width: u16) -> usize {
154 if width == 0 {
155 return text.lines().count().max(1);
156 }
157 Paragraph::new(text).line_count(width)
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_url_detection() {
166 assert!(contains_preserved_token("https://example.com"));
167 assert!(contains_preserved_token("example.com/path"));
168 assert!(contains_preserved_token("localhost:8080"));
169 assert!(contains_preserved_token("192.168.1.1:8080"));
170 assert!(!contains_preserved_token("not a url"));
171 }
172
173 #[test]
174 fn test_file_path_detection() {
175 assert!(contains_preserved_token("src/main.rs"));
176 assert!(contains_preserved_token("./src/main.rs"));
177 assert!(contains_preserved_token("/tmp/example.txt"));
178 assert!(contains_preserved_token("\"./docs/My Notes.md\""));
179 assert!(contains_preserved_token(
180 "`/Users/example/Library/Application Support/Code/User/settings.json`"
181 ));
182 }
183
184 #[test]
185 fn test_url_only_preserved() {
186 let line = Line::from(Span::raw("https://example.com"));
187 let wrapped = wrap_line_preserving_urls(line, 80);
188 assert_eq!(wrapped.len(), 1);
189 assert!(
190 wrapped[0]
191 .spans
192 .iter()
193 .any(|s| s.content.contains("https://"))
194 );
195 }
196
197 #[test]
198 fn test_mixed_content() {
199 let line = Line::from(Span::raw("See https://example.com for info"));
200 let wrapped = wrap_line_preserving_urls(line, 25);
201 let all_text: String = wrapped
202 .iter()
203 .flat_map(|l| l.spans.iter().map(|s| s.content.as_ref()))
204 .collect();
205 assert!(all_text.contains("https://example.com"));
206 assert!(all_text.contains("See"));
207 }
208
209 #[test]
210 fn test_quoted_path_with_spaces_is_preserved() {
211 let line = Line::from(Span::raw("Open \"./docs/My Notes.md\" for details"));
212 let wrapped = wrap_line_preserving_urls(line, 18);
213 let all_text: String = wrapped
214 .iter()
215 .flat_map(|line| line.spans.iter().map(|span| span.content.as_ref()))
216 .collect();
217
218 assert!(all_text.contains("\"./docs/My Notes.md\""));
219 }
220
221 #[test]
222 fn test_no_url_delegates() {
223 let line = Line::from(Span::raw("Regular text without URLs"));
224 let wrapped = wrap_line_preserving_urls(line, 10);
225 assert!(!wrapped.is_empty());
226 }
227}