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 Block Widget

The block widget is a container that wraps other widgets with optional borders, titles, and styling. It's one of the most commonly used widgets for creating structured layouts and visually separating different sections of your TUI.

API

mod block: sig {
    type Borders = [`All, `None, `Top, `Bottom, `Left, `Right];

    /// Creates a block widget that wraps content with borders and styling
    val block: fn(
        ?#border: &Borders,
        ?#border_style: &Style,
        ?#title: &Line,
        ?#title_bottom: &Line,
        ?#style: &Style,
        ?#size: &Size,
        &Widget
    ) -> Widget;
}

Parameters

  • border - Border style: All, None, Top, Bottom, Left, or Right
  • border_style - Style for the border
  • title - Line displayed at the top of the block
  • title_bottom - Line displayed at the bottom of the block
  • style - Style for the block's interior
  • size (output) - Rendered size of the block

Examples

Basic Usage

use tui;
use tui::block;
use tui::paragraph;

let content = paragraph(&"Hello, World!");

block(
    #border: &`All,
    #title: &line("My Block"),
    &content
)

Basic Block

Focus Indication

Use dynamic styling to show which block has focus:

use tui;
use tui::block;
use tui::paragraph;

let focused_block = 0;
let content = paragraph(&"Content here");

block(
    #border: &`All,
    #border_style: &style(
        #fg: select focused_block {
            0 => `Red,
            _ => `Yellow
        }
    ),
    #title: &line("Block 1"),
    &content
)

Styled Block

Dynamic Titles

Titles can contain reactive values that update automatically:

use tui;
use tui::block;
use tui::paragraph;

let count = 0;
let timer = time::timer(duration:1.s, true);
count <- timer ~ (count + 1);

let content = paragraph(&"Content here");

block(
    #border: &`All,
    #title: &line("Counter: [count]"),
    &content
)

Block With Dynamic Title

See Also

  • layout - For arranging multiple blocks
  • paragraph - Common content for blocks
  • text - For creating styled text content