fdehau / tui-rs

Build terminal user interfaces and dashboards using Rust
MIT License
10.84k stars 485 forks source link

Feature: allow word-break in wrapped paragraphs. #653

Closed chldu2000 closed 1 year ago

chldu2000 commented 2 years ago

Description

This PR will allow word-break in wrapped paragraphs.

Testing guidelines

Create a Paragraph in a Block, with the break_word value in its Wrap set to true.

Such as :

const SAMPLE_STRING: &str = "The library is based on the principle of immediate rendering with \
     intermediate buffers. This means that at each new frame you should build all widgets that are \
     supposed to be part of the UI. While providing a great flexibility for rich and \
     interactive UI, this may introduce overhead for highly dynamic content.";

fn ui<B: Backend>(frame: &mut Frame<B>) {
    let size = Rect::new(0, 0, 20, 10);
    let text = vec![Spans::from(SAMPLE_STRING)];
    let paragraph = Paragraph::new(text)
        .block(Block::default().borders(Borders::ALL))
        .alignment(Alignment::Left)
        .wrap(Wrap {
            trim: true,
            break_words: true,
        });
    frame.render_widget(paragraph, size);
}

If set break_words as true, the result will be like:

┌──────────────────┐
│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│
└──────────────────┘

If set break_words as false, the result will be like:

┌──────────────────┐
│The library is    │
│based on the      │
│principle of      │
│immediate         │
│rendering with    │
│intermediate      │
│buffers. This     │
│means that at each│
└──────────────────┘

Checklist

TBS1996 commented 2 years ago

This is great, I'm really missing this feature