From 727e13d8769b9aa506156790c4baaf4fff16dbe3 Mon Sep 17 00:00:00 2001 From: charliedu2000 <68285614+charliedu2000@users.noreply.github.com> Date: Fri, 12 Aug 2022 23:49:24 +0800 Subject: [PATCH] Feature: allow word-break in wrapped paragraphs. --- tests/widgets_paragraph.rs | 70 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/widgets_paragraph.rs b/tests/widgets_paragraph.rs index 13b506d..d3061fc 100644 --- a/tests/widgets_paragraph.rs +++ b/tests/widgets_paragraph.rs @@ -82,6 +82,76 @@ fn widgets_paragraph_can_wrap_its_content() { ); } +#[test] +fn widgets_paragraph_can_wrap_its_content_with_word_break() { + let test_case = |alignment, expected| { + let backend = TestBackend::new(20, 10); + let mut terminal = Terminal::new(backend).unwrap(); + + terminal + .draw(|f| { + let size = f.size(); + let text = vec![Spans::from(SAMPLE_STRING)]; + let paragraph = Paragraph::new(text) + .block(Block::default().borders(Borders::ALL)) + .alignment(alignment) + .wrap(Wrap { + trim: true, + break_words: true, + }); + f.render_widget(paragraph, size); + }) + .unwrap(); + terminal.backend().assert_buffer(&expected); + }; + + test_case( + Alignment::Left, + Buffer::with_lines(vec![ + "┌──────────────────┐", + "│The library is bas│", + "│ed on the principl│", + "│e of immediate ren│", + "│dering with interm│", + "│ediate buffers. Th│", + "│is means that at e│", + "│ach new frame you │", + "│should build all w│", + "└──────────────────┘", + ]), + ); + test_case( + Alignment::Right, + Buffer::with_lines(vec![ + "┌──────────────────┐", + "│The library is bas│", + "│ed on the principl│", + "│e of immediate ren│", + "│dering with interm│", + "│ediate buffers. Th│", + "│is means that at e│", + "│ach new frame you │", + "│should build all w│", + "└──────────────────┘", + ]), + ); + test_case( + Alignment::Center, + Buffer::with_lines(vec![ + "┌──────────────────┐", + "│The library is bas│", + "│ed on the principl│", + "│e of immediate ren│", + "│dering with interm│", + "│ediate buffers. Th│", + "│is means that at e│", + "│ach new frame you │", + "│should build all w│", + "└──────────────────┘", + ]), + ); +} + #[test] fn widgets_paragraph_renders_double_width_graphemes() { let backend = TestBackend::new(10, 10);