tui: enable bracketed paste, sanitize pasted text
This commit is contained in:
1 parent
5c6c8befa9
commit
57eddb98ca
2 files changed
+21
-1
No files matched your search
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user