Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The Paragraph Widget

The paragraph widget displays multi-line text with automatic word wrapping and scrolling support. It's ideal for displaying long text content, logs, or any content that needs to flow across multiple lines.

API

mod paragraph: sig {
    type ScrollPosition = {x: i64, y: i64};

    /// Creates a paragraph widget with text content
    val paragraph: fn(
        ?#scroll: &ScrollPosition,
        ?#alignment: &Alignment,
        ?#wrap: &bool,
        &[string, Text]
    ) -> Widget;
}

Parameters

  • scroll - Record with x and y fields for scroll position
  • alignment - Left, Center, or Right
  • wrap - Enable/disable word wrapping (default: true)

Examples

Basic Usage

use tui;
use tui::paragraph;

paragraph(&"This is a simple paragraph. It will automatically wrap to fit the available width.")

Basic Paragraph

Scrollable Content

use tui;
use tui::paragraph;
use tui::block;
use tui::text;
use tui::input_handler;

let long_text = "I have got a lovely bunch of coconuts. Very long text continues here. More text. Even more text. This is a very long paragraph that will need scrolling to see all of it.";
let scroll_y = 0;

let handle_event = |e: Event| -> [`Stop, `Continue] select e {
    `Key(k) => select k.kind {
        `Press => select k.code {
            k@`Up if scroll_y > 0 => {
                scroll_y <- (k ~ scroll_y) - 1;
                `Stop
            },
            k@`Down if scroll_y < 100 => {
                scroll_y <- (k ~ scroll_y) + 1;
                `Stop
            },
            _ => `Continue
        },
        _ => `Continue
    },
    _ => `Continue
};

input_handler(
    #handle: &handle_event,
    &block(
        #border: &`All,
        #title: &line("Scrollable Text"),
        &paragraph(
            #scroll: &{x: 0, y: scroll_y},
            &long_text
        )
    )
)

Scrollable Paragraph

Live Log Viewer

Display real-time updating content:

use tui;
use tui::paragraph;
use tui::text;

let log_entries = [];
let new_entry = net::subscribe("/local/logs/application")?;

log_entries <- array::window(
    #n: 100,
    new_entry ~ log_entries,
    line(cast<string>(new_entry)?)
);

paragraph(&log_entries)

Log Viewer

Centered Message

use tui;
use tui::paragraph;
use tui::text;

paragraph(
    #alignment: &`Center,
    &[
        line(""),
        line(#style: style(#fg: `Yellow, #add_modifier: [`Bold]), "Welcome"),
        line(""),
        line("Press any key to continue")
    ]
)

Paragraph Centered

Word Wrapping

The paragraph widget automatically wraps long lines to fit the available width. Word boundaries are respected, so words won't be split in the middle unless they're longer than the available width.

See Also

  • text - For creating styled text content
  • scrollbar - For adding scrollbars
  • block - For containing paragraphs with borders
  • list - For line-by-line selectable content