tui: enable bracketed paste, sanitize pasted text

This commit is contained in:
hermes committed 2026-10-14 09:08:59 +00:00
1 parent 5c6c8befa9
commit 57eddb98ca
2 files changed
+21 -1

No files matched your search

+16
View File
@@ -20,6 +20,11 @@ impl Editor {
pub fn insert(&mut self, s: &str) {
// normalize pasted CRLF
let s = s.replace("\r\n", "\n").replace('\r', "\n");
// drop ESC etc. so pasted escape sequences cannot act on the terminal
let s: String = s
.chars()
.filter(|c| !c.is_control() || matches!(c, '\n' | '\t'))
.collect();
self.buf.insert_str(self.cur, &s);
self.cur += s.len();
}
@@ -278,3 +283,14 @@ mod tests {
assert_eq!((e.buf.as_str(), e.cur), ("look at @src/main.rs ", 21));
}
}
#[cfg(test)]
mod paste_tests {
use super::*;
#[test]
fn paste_multiline_keeps_text_and_strips_controls() {
let mut e = Editor::default();
e.insert("a\r\nb\x1b[31m\tc\x07");
assert_eq!(e.buf, "a\nb[31m\tc");
}
}
+5 -1
View File
@@ -416,12 +416,15 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
std::io::stdout(),
crossterm::event::PopKeyboardEnhancementFlags
);
let _ = crossterm::execute!(std::io::stdout(), crossterm::event::DisableBracketedPaste);
prev(i)
}));
}
if mouse {
let _ = crossterm::execute!(std::io::stdout(), crossterm::event::EnableMouseCapture);
}
// one Paste event per paste instead of a key event (and redraw) per char; newlines no longer submit
let _ = crossterm::execute!(std::io::stdout(), crossterm::event::EnableBracketedPaste);
let mut keys = EventStream::new();
let mut tick = tokio::time::interval(std::time::Duration::from_millis(50));
let mut lens_tick = tokio::time::interval(std::time::Duration::from_secs(2));
@@ -485,7 +488,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
}
}
TEvent::Mouse(m) => on_mouse(&mut app, m),
TEvent::Paste(s) => app.ed.insert(&s),
TEvent::Paste(s) if app.perm.is_none() && app.choose.is_none() && app.ask.is_none() => app.ed.insert(&s),
TEvent::Resize(..) => app.sel = None,
_ => {}
}
@@ -574,6 +577,7 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
}
}
}
let _ = crossterm::execute!(std::io::stdout(), crossterm::event::DisableBracketedPaste);
if mouse {
let _ = crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture);
}