mirror of
https://github.com/ratatui/ratatui.git
synced 2025-10-02 15:25:54 +00:00
fix(buffer): dont render control characters (#1226)
This commit is contained in:
parent
379dab9cdb
commit
7e1bab049b
@ -192,7 +192,7 @@ impl Buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Print at most the first n characters of a string if enough space is available
|
/// Print at most the first n characters of a string if enough space is available
|
||||||
/// until the end of the line.
|
/// until the end of the line. Skips zero-width graphemes and control characters.
|
||||||
///
|
///
|
||||||
/// Use [`Buffer::set_string`] when the maximum amount of characters can be printed.
|
/// Use [`Buffer::set_string`] when the maximum amount of characters can be printed.
|
||||||
pub fn set_stringn<T, S>(
|
pub fn set_stringn<T, S>(
|
||||||
@ -210,6 +210,7 @@ impl Buffer {
|
|||||||
let max_width = max_width.try_into().unwrap_or(u16::MAX);
|
let max_width = max_width.try_into().unwrap_or(u16::MAX);
|
||||||
let mut remaining_width = self.area.right().saturating_sub(x).min(max_width);
|
let mut remaining_width = self.area.right().saturating_sub(x).min(max_width);
|
||||||
let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true)
|
let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true)
|
||||||
|
.filter(|symbol| !symbol.contains(|char: char| char.is_control()))
|
||||||
.map(|symbol| (symbol, symbol.width() as u16))
|
.map(|symbol| (symbol, symbol.width() as u16))
|
||||||
.filter(|(_symbol, width)| *width > 0)
|
.filter(|(_symbol, width)| *width > 0)
|
||||||
.map_while(|(symbol, width)| {
|
.map_while(|(symbol, width)| {
|
||||||
@ -931,4 +932,35 @@ mod tests {
|
|||||||
buf.set_string(0, 1, "bar", Style::new().blue());
|
buf.set_string(0, 1, "bar", Style::new().blue());
|
||||||
assert_eq!(buf, Buffer::with_lines(["foo".red(), "bar".blue()]));
|
assert_eq!(buf, Buffer::with_lines(["foo".red(), "bar".blue()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn control_sequence_rendered_full() {
|
||||||
|
let text = "I \x1b[0;36mwas\x1b[0m here!";
|
||||||
|
|
||||||
|
let mut buffer = Buffer::filled(Rect::new(0, 0, 25, 3), Cell::new("x"));
|
||||||
|
buffer.set_string(1, 1, text, Style::new());
|
||||||
|
|
||||||
|
let expected = Buffer::with_lines([
|
||||||
|
"xxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||||
|
"xI [0;36mwas[0m here!xxxx",
|
||||||
|
"xxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||||
|
]);
|
||||||
|
assert_eq!(buffer, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn control_sequence_rendered_partially() {
|
||||||
|
let text = "I \x1b[0;36mwas\x1b[0m here!";
|
||||||
|
|
||||||
|
let mut buffer = Buffer::filled(Rect::new(0, 0, 11, 3), Cell::new("x"));
|
||||||
|
buffer.set_string(1, 1, text, Style::new());
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
let expected = Buffer::with_lines([
|
||||||
|
"xxxxxxxxxxx",
|
||||||
|
"xI [0;36mwa",
|
||||||
|
"xxxxxxxxxxx",
|
||||||
|
]);
|
||||||
|
assert_eq!(buffer, expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user