mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-28 13:31:14 +00:00
fix: Ignore zero-width symbol on rendering Paragraph
This fixes out-of-bounds crash on rendering `Paragraph` when zero-width character is at end of line. fix #642 Co-authored-by: rhysd <lin90162@yahoo.co.jp>
This commit is contained in:
parent
85eefe1d8b
commit
9534d533e3
@ -176,6 +176,10 @@ impl<'a> Widget for Paragraph<'a> {
|
|||||||
if y >= self.scroll.0 {
|
if y >= self.scroll.0 {
|
||||||
let mut x = get_line_offset(current_line_width, text_area.width, self.alignment);
|
let mut x = get_line_offset(current_line_width, text_area.width, self.alignment);
|
||||||
for StyledGrapheme { symbol, style } in current_line {
|
for StyledGrapheme { symbol, style } in current_line {
|
||||||
|
let width = symbol.width();
|
||||||
|
if width == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
buf.get_mut(text_area.left() + x, text_area.top() + y - self.scroll.0)
|
buf.get_mut(text_area.left() + x, text_area.top() + y - self.scroll.0)
|
||||||
.set_symbol(if symbol.is_empty() {
|
.set_symbol(if symbol.is_empty() {
|
||||||
// If the symbol is empty, the last char which rendered last time will
|
// If the symbol is empty, the last char which rendered last time will
|
||||||
@ -185,7 +189,7 @@ impl<'a> Widget for Paragraph<'a> {
|
|||||||
symbol
|
symbol
|
||||||
})
|
})
|
||||||
.set_style(*style);
|
.set_style(*style);
|
||||||
x += symbol.width() as u16;
|
x += width as u16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y += 1;
|
y += 1;
|
||||||
@ -195,3 +199,16 @@ impl<'a> Widget for Paragraph<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zero_width_char_at_end_of_line() {
|
||||||
|
let line = "foo\0";
|
||||||
|
let paragraph = Paragraph::new(line);
|
||||||
|
let mut buf = Buffer::with_lines(vec![line]);
|
||||||
|
paragraph.render(*buf.area(), &mut buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user