Ratatui Macros
ratatui-macros is a Rust crate that provides easy-to-use macros for simplifying boilerplate
associated with creating UI using Ratatui.
This is an experimental playground for us to explore macros that would be useful to have in Ratatui proper.
Features
- Text macros for easily defining styled
Texts,Lines, andSpans. - Layout macros for defining
Layouts withConstraints and directions. - Table macros for creating
Rows andCells.
Getting Started
Add ratatui-macros as a dependency in your Cargo.toml:
cargo add ratatui-macros
Then, import the macros in your Rust file:
use ;
Text Macros
The [span!] macro creates raw or styled Spans.
let name = "world!";
let raw_greeting = span!;
let styled_greeting = span!;
let colored_greeting = span!;
let modified_greeting = span!;
The [line!] macro creates a Line that contains a sequence of Spans. It is similar to
the [vec!] macro. Each element is converted into a Span using [Into::into].
let name = "world!";
let line = line!;
let line = line!;
let line = line!;
let line = line!;
The [text!] macro creates a Text that contains a sequence of Line. It is similar to
the [vec!] macro. Each element is converted to a Line using [Into::into].
let name = "world!";
let text = text!;
let text = text!;
let name = "Bye!!!";
let text = text!;
Layout Macros
If you are new to Ratatui, check out the Layout concepts article on the Ratatui website before proceeding.
The [constraints!] macro defines an array of Constraints:
let layout = horizontal;
The [constraint!] macro defines individual Constraints:
let layout = horizontal;
The [vertical!] and [horizontal!] macros are a shortcut to defining a Layout:
let = vertical!.areas;
let = horizontal!.areas;
Table Macros
The [row!] macro creates a Row for a Table that contains a sequence of Cells. It
is similar to the [vec!] macro.
let rows = ;
let table = new;
Contributing
Contributions to ratatui-macros are welcome! Whether it's submitting a bug report, a feature
request, or a pull request, all forms of contributions are valued and appreciated.
Crate Organization
ratatui-macros is part of the Ratatui workspace that was modularized in version 0.30.0.
This crate provides declarative macros to reduce boilerplate when working with
Ratatui.
When to use ratatui-macros:
- You want to reduce boilerplate when creating styled text, layouts, or tables
- You prefer macro-based syntax for creating UI elements
- You need compile-time generation of repetitive UI code
When to use the main ratatui crate:
- Building applications (recommended - includes macros when the
macrosfeature is enabled) - You want the convenience of having everything available
For detailed information about the workspace organization, see ARCHITECTURE.md.