ratatui_macros/lib.rs
1#![no_std]
2//! `ratatui-macros` is a Rust crate that provides easy-to-use macros for simplifying boilerplate
3//! associated with creating UI using [Ratatui].
4//!
5//! This is an experimental playground for us to explore macros that would be useful to have in
6//! Ratatui proper.
7//!
8//! # Features
9//!
10//! - [Text macros](#text-macros) for easily defining styled [`Text`]s, [`Line`]s, and [`Span`]s.
11//! - [Layout macros](#layout-macros) for defining [`Layout`]s with [`Constraint`]s and directions.
12//! - [Table macros](#table-macros) for creating [`Row`]s and [`Cell`]s.
13//!
14//! # Getting Started
15//!
16//! Add `ratatui-macros` as a dependency in your `Cargo.toml`:
17//!
18//! ```shell
19//! cargo add ratatui-macros
20//! ```
21//!
22//! Then, import the macros in your Rust file:
23//!
24//! ```rust
25//! use ratatui_macros::{constraint, constraints, horizontal, line, row, span, text, vertical};
26//! ```
27//!
28//! # Text Macros
29//!
30//! The [`span!`] macro creates raw or styled [`Span`]s.
31//!
32//! ```rust
33//! # use ratatui_core::style::{Color, Modifier, Style, Stylize};
34//! # use ratatui_macros::span;
35//! let name = "world!";
36//! let raw_greeting = span!("hello {name}");
37//! let styled_greeting = span!(Style::new().green(); "hello {name}");
38//! let colored_greeting = span!(Color::Green; "hello {name}");
39//! let modified_greeting = span!(Modifier::BOLD; "hello {name}");
40//! ```
41//!
42//! The [`line!`] macro creates a [`Line`] that contains a sequence of [`Span`]s. It is similar to
43//! the [`vec!`] macro. Each element is converted into a [`Span`] using [`Into::into`].
44//!
45//! ```rust
46//! # use ratatui_core::style::{Color, Stylize};
47//! # use ratatui_macros::{line, span};
48//! let name = "world!";
49//! let line = line!["hello", format!("{name}")];
50//! let line = line!["hello ", span!(Color::Green; "{name}")];
51//! let line = line!["Name: ".bold(), "Remy".italic()];
52//! let line = line!["bye"; 2];
53//! ```
54//!
55//! The [`text!`] macro creates a [`Text`] that contains a sequence of [`Line`]. It is similar to
56//! the [`vec!`] macro. Each element is converted to a [`Line`] using [`Into::into`].
57//!
58//! ```rust
59//! # use ratatui_core::style::{Modifier, Stylize};
60//! # use ratatui_macros::{span, line, text};
61//! let name = "world!";
62//! let text = text!["hello", format!("{name}")];
63//! let text = text!["bye"; 2];
64//! let name = "Bye!!!";
65//! let text = text![line!["hello", "world".bold()], span!(Modifier::BOLD; "{name}")];
66//! ```
67//!
68//! # Layout Macros
69//!
70//! If you are new to Ratatui, check out the [Layout concepts] article on the Ratatui website before
71//! proceeding.
72//!
73//! The [`constraints!`] macro defines an array of [`Constraint`]s:
74//!
75//! ```rust
76//! # use ratatui_core::layout::Layout;
77//! # use ratatui_macros::constraints;
78//! let layout = Layout::horizontal(constraints![==50, ==30%, >=3, <=1, ==1/2, *=1]);
79//! ```
80//!
81//! The [`constraint!`] macro defines individual [`Constraint`]s:
82//!
83//! ```rust
84//! # use ratatui_core::layout::Layout;
85//! # use ratatui_macros::constraint;
86//! let layout = Layout::horizontal([constraint!(==50)]);
87//! ```
88//!
89//! The [`vertical!`] and [`horizontal!`] macros are a shortcut to defining a [`Layout`]:
90//!
91//! ```rust
92//! # use ratatui_core::layout::Rect;
93//! # use ratatui_macros::{vertical, horizontal};
94//! # let area = Rect { x: 0, y: 0, width: 10, height: 10 };
95//! let [top, main, bottom] = vertical![==1, *=1, >=3].areas(area);
96//! let [left, main, right] = horizontal![>=20, *=1, >=20].areas(main);
97//! ```
98//!
99//! # Table Macros
100//!
101//! The [`row!`] macro creates a [`Row`] for a [`Table`] that contains a sequence of [`Cell`]s. It
102//! is similar to the [`vec!`] macro.
103//!
104//! ```rust
105//! # use ratatui_core::style::{Modifier, Stylize};
106//! # use ratatui_macros::{constraints, line, row, span, text};
107//! # use ratatui_widgets::table::Table;
108//! let rows = [
109//! row!["hello", "world"],
110//! row!["goodbye", "world"],
111//! row![
112//! text!["line 1", line!["Line", "2".bold()]],
113//! span!(Modifier::BOLD; "Cell 2"),
114//! ],
115//! ];
116//! let table = Table::new(rows, constraints![==20, *=1]);
117//! ```
118//!
119//! # Contributing
120//!
121//! Contributions to `ratatui-macros` are welcome! Whether it's submitting a bug report, a feature
122//! request, or a pull request, all forms of contributions are valued and appreciated.
123//!
124//! # Crate Organization
125//!
126//! `ratatui-macros` is part of the Ratatui workspace that was modularized in version 0.30.0.
127//! This crate provides declarative macros to reduce boilerplate when working with
128//! Ratatui.
129//!
130//! **When to use `ratatui-macros`:**
131//!
132//! - You want to reduce boilerplate when creating styled text, layouts, or tables
133//! - You prefer macro-based syntax for creating UI elements
134//! - You need compile-time generation of repetitive UI code
135//!
136//! **When to use the main [`ratatui`] crate:**
137//!
138//! - Building applications (recommended - includes macros when the `macros` feature is enabled)
139//! - You want the convenience of having everything available
140//!
141//! For detailed information about the workspace organization, see [ARCHITECTURE.md].
142//!
143//! [`ratatui`]: https://crates.io/crates/ratatui
144//! [ARCHITECTURE.md]: https://github.com/ratatui/ratatui/blob/main/ARCHITECTURE.md
145//!
146//! [Crates.io badge]: https://img.shields.io/crates/v/ratatui-macros?logo=rust&style=flat-square
147//! [License badge]: https://img.shields.io/crates/l/ratatui-macros
148//! [CI Badge]:
149//! https://img.shields.io/github/actions/workflow/status/ratatui/ratatui/ci.yml?logo=github&style=flat-square
150//! [Docs.rs badge]: https://img.shields.io/docsrs/ratatui-macros?logo=rust&style=flat-square
151//! [Crate Downloads badge]:
152//! https://img.shields.io/crates/d/ratatui-macros?logo=rust&style=flat-square
153//! [ratatui_macros crate]: https://crates.io/crates/ratatui-macros
154//! [API Docs]: https://docs.rs/ratatui-macros
155//! [CI Status]: https://github.com/ratatui/ratatui/actions
156//! [Ratatui]: https://github.com/ratatui/ratatui
157//! [Layout concepts]: https://ratatui.rs/concepts/layout
158//! [`Constraint`]: ratatui_core::layout::Constraint
159//! [`Layout`]: ratatui_core::layout::Layout
160//! [`Span`]: ratatui_core::text::Span
161//! [`Line`]: ratatui_core::text::Line
162//! [`Text`]: ratatui_core::text::Text
163//! [`Row`]: ratatui_widgets::table::Row
164//! [`Cell`]: ratatui_widgets::table::Cell
165//! [`Table`]: ratatui_widgets::table::Table
166
167extern crate alloc;
168
169#[doc(hidden)]
170pub use alloc::{format, vec};
171
172mod layout;
173mod line;
174mod row;
175mod span;
176mod text;
177
178// Re-export the core crate to use the types in macros
179pub use ratatui_core;