-
Notifications
You must be signed in to change notification settings - Fork 14.3k
tui: sanitize terminal controls in user messages #31494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d6748df
1a08407
32f6fd5
9c252be
cacebe6
dd46385
cc6f7fa
fb46ef3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,20 @@ pub(crate) struct UserHistoryCell { | |
| pub remote_image_urls: Vec<String>, | ||
| } | ||
|
|
||
| /// Remove CSI sequences and control characters, preserving tabs and newlines. | ||
| pub(crate) fn sanitize_user_text(text: &str) -> String { | ||
| let mut sanitized = String::with_capacity(text.len()); | ||
| let mut chars = text.chars().peekable(); | ||
| while let Some(ch) = chars.next() { | ||
| if ch == '\x1b' && chars.next_if_eq(&'[').is_some() { | ||
| let _ = chars.find(|ch| ('@'..='~').contains(ch)); | ||
| } else if matches!(ch, '\n' | '\t') || !ch.is_control() { | ||
| sanitized.push(ch); | ||
| } | ||
| } | ||
| sanitized | ||
| } | ||
|
|
||
| /// Build logical lines for a user message with styled text elements. | ||
| /// | ||
| /// This preserves explicit newlines while interleaving element spans and skips | ||
|
|
@@ -93,6 +107,12 @@ fn trim_trailing_blank_lines(mut lines: Vec<Line<'static>>) -> Vec<Line<'static> | |
|
|
||
| impl HistoryCell for UserHistoryCell { | ||
| fn display_lines(&self, width: u16) -> Vec<Line<'static>> { | ||
| let message = sanitize_user_text(&self.message); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, good catch. Fixed. |
||
| let text_elements = if message == self.message { | ||
| self.text_elements.as_slice() | ||
| } else { | ||
| &[] | ||
| }; | ||
| let wrap_width = width | ||
| .saturating_sub( | ||
| LIVE_PREFIX_COLS + 1, /* keep a one-column right margin for wrapping */ | ||
|
|
@@ -117,10 +137,10 @@ impl HistoryCell for UserHistoryCell { | |
| )) | ||
| }; | ||
|
|
||
| let wrapped_message = if self.message.is_empty() && self.text_elements.is_empty() { | ||
| let wrapped_message = if message.is_empty() && text_elements.is_empty() { | ||
| None | ||
| } else if self.text_elements.is_empty() { | ||
| let message_without_trailing_newlines = self.message.trim_end_matches(['\r', '\n']); | ||
| } else if text_elements.is_empty() { | ||
| let message_without_trailing_newlines = message.trim_end_matches(['\r', '\n']); | ||
| let wrapped = adaptive_wrap_lines( | ||
| message_without_trailing_newlines | ||
| .split('\n') | ||
|
|
@@ -133,8 +153,8 @@ impl HistoryCell for UserHistoryCell { | |
| (!wrapped.is_empty()).then_some(wrapped) | ||
| } else { | ||
| let raw_lines = build_user_message_lines_with_elements( | ||
| &self.message, | ||
| &self.text_elements, | ||
| &message, | ||
| text_elements, | ||
| style, | ||
| element_style, | ||
| ); | ||
|
|
@@ -177,7 +197,8 @@ impl HistoryCell for UserHistoryCell { | |
| } | ||
|
|
||
| fn raw_lines(&self) -> Vec<Line<'static>> { | ||
| let mut lines = raw_lines_from_source(self.message.trim_end_matches(['\r', '\n'])); | ||
| let message = sanitize_user_text(&self.message); | ||
| let mut lines = raw_lines_from_source(message.trim_end_matches(['\r', '\n'])); | ||
| if !self.remote_image_urls.is_empty() { | ||
| if !lines.is_empty() { | ||
| lines.push(Line::from("")); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,6 @@ | ||
| --- | ||
| source: tui/src/history_cell.rs | ||
| source: tui/src/history_cell/tests.rs | ||
| expression: rendered | ||
| --- | ||
|
|
||
| › one two | ||
| three | ||
| four five | ||
| six seven | ||
| › _count_ro | ||
| ws |
Uh oh!
There was an error while loading. Please reload this page.