Skip to content

Commit

Permalink
fix(reflow): allow wrapping at zero width whitespace (ratatui#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
kxxt authored Apr 28, 2024
1 parent da1ade7 commit f4637d4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/widgets/reflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use unicode_width::UnicodeWidthStr;
use crate::{layout::Alignment, text::StyledGrapheme};

const NBSP: &str = "\u{00a0}";
const ZWSP: &str = "\u{200b}";

/// A state machine to pack styled symbols into lines.
/// Cannot implement it as Iterator since it yields slices of the internal buffer (need streaming
Expand Down Expand Up @@ -104,8 +105,8 @@ where

let mut has_seen_non_whitespace = false;
for StyledGrapheme { symbol, style } in line_symbols {
let symbol_whitespace =
symbol.chars().all(&char::is_whitespace) && symbol != NBSP;
let symbol_whitespace = symbol == ZWSP
|| (symbol.chars().all(&char::is_whitespace) && symbol != NBSP);
let symbol_width = symbol.width() as u16;
// Ignore characters wider than the total max width
if symbol_width > self.max_line_width {
Expand Down Expand Up @@ -706,4 +707,12 @@ mod test {
vec![Alignment::Left, Alignment::Right, Alignment::Center]
);
}

#[test]
fn line_composer_zero_width_white_space() {
let width = 3;
let line = "foo\u{200b}bar";
let (word_wrapper, _, _) = run_composer(Composer::WordWrapper { trim: true }, line, width);
assert_eq!(word_wrapper, vec!["foo", "bar"]);
}
}

0 comments on commit f4637d4

Please sign in to comment.