feat(no_std): make ratatui-macros no-std (#1865)

This commit is contained in:
Jagoda Estera Ślązak 2025-05-20 20:22:48 +02:00 committed by GitHub
parent 7d84d42103
commit b32f78195b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 13 deletions

View File

@ -144,9 +144,11 @@ jobs:
# This makes it easier to debug the exact versions of the dependencies
- run: cargo tree --target x86_64-unknown-none -p ratatui-core
- run: cargo tree --target x86_64-unknown-none -p ratatui-widgets
- run: cargo tree --target x86_64-unknown-none -p ratatui-macros
- run: cargo tree --target x86_64-unknown-none -p ratatui --no-default-features
- run: cargo build --target x86_64-unknown-none -p ratatui-core
- run: cargo build --target x86_64-unknown-none -p ratatui-widgets
- run: cargo build --target x86_64-unknown-none -p ratatui-macros
- run: cargo build --target x86_64-unknown-none -p ratatui --no-default-features
# Check if README.md is up-to-date with the crate's documentation.

View File

@ -1,3 +1,4 @@
#![no_std]
//! `ratatui-macros` is a Rust crate that provides easy-to-use macros for simplifying boilerplate
//! associated with creating UI using [Ratatui].
//!
@ -197,6 +198,12 @@
//! [CI Status]: https://github.com/ratatui/ratatui/actions
//! [Ratatui]: https://github.com/ratatui/ratatui
//! [Layout concepts]: https://ratatui.rs/concepts/layout
extern crate alloc;
#[doc(hidden)]
pub use alloc::{format, vec};
mod layout;
mod line;
mod row;

View File

@ -33,16 +33,17 @@
/// [`span!`]: crate::span
/// [`Line`]: ratatui_core::text::Line
/// [`Span`]: ratatui_core::text::Span
/// [`vec!`]: alloc::vec!
#[macro_export]
macro_rules! line {
() => {
$crate::ratatui_core::text::Line::default()
};
($span:expr; $n:expr) => {
$crate::ratatui_core::text::Line::from(vec![$span.into(); $n])
$crate::ratatui_core::text::Line::from($crate::vec![$span.into(); $n])
};
($($span:expr),+ $(,)?) => {{
$crate::ratatui_core::text::Line::from(vec![
$crate::ratatui_core::text::Line::from($crate::vec![
$(
$span.into(),
)+
@ -52,6 +53,7 @@ macro_rules! line {
#[cfg(test)]
mod tests {
use alloc::vec;
use ratatui_core::text::{Line, Span};
#[test]

View File

@ -46,16 +46,17 @@
/// [`row!`]: crate::row
/// [`Row`]: ratatui_widgets::table::Row
/// [`Cell`]: ratatui_widgets::table::Cell
/// [`vec!`]: alloc::vec!
#[macro_export]
macro_rules! row {
() => {
::ratatui_widgets::table::Row::default()
};
($cell:expr; $n:expr) => {
::ratatui_widgets::table::Row::new(vec![::ratatui_widgets::table::Cell::from($cell); $n])
::ratatui_widgets::table::Row::new($crate::vec![::ratatui_widgets::table::Cell::from($cell); $n])
};
($($cell:expr),+ $(,)?) => {{
::ratatui_widgets::table::Row::new(vec![
::ratatui_widgets::table::Row::new($crate::vec![
$(
::ratatui_widgets::table::Cell::from($cell),
)+
@ -65,7 +66,7 @@ macro_rules! row {
#[cfg(test)]
mod tests {
use alloc::vec;
use ratatui_core::text::Text;
use ratatui_widgets::table::{Cell, Row};

View File

@ -91,28 +91,29 @@
/// [`Color`]: ratatui_core::style::Color
/// [`Span`]: ratatui_core::text::Span
/// [`Style`]: ratatui_core::style::Style
/// [`format!`]: alloc::format!
#[macro_export]
macro_rules! span {
($string:literal) => {
$crate::ratatui_core::text::Span::raw(format!($string))
$crate::ratatui_core::text::Span::raw($crate::format!($string))
};
($string:literal, $($arg:tt)*) => {
$crate::ratatui_core::text::Span::raw(format!($string, $($arg)*))
$crate::ratatui_core::text::Span::raw($crate::format!($string, $($arg)*))
};
($expr:expr) => {
$crate::ratatui_core::text::Span::raw(format!("{}", $expr))
$crate::ratatui_core::text::Span::raw($crate::format!("{}", $expr))
};
($style:expr, $($arg:tt)*) => {
compile_error!("first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semicolon")
};
($style:expr; $string:literal) => {
$crate::ratatui_core::text::Span::styled(format!($string), $style)
$crate::ratatui_core::text::Span::styled($crate::format!($string), $style)
};
($style:expr; $string:literal, $($arg:tt)*) => {
$crate::ratatui_core::text::Span::styled(format!($string, $($arg)*), $style)
$crate::ratatui_core::text::Span::styled($crate::format!($string, $($arg)*), $style)
};
($style:expr; $expr:expr) => {
$crate::ratatui_core::text::Span::styled(format!("{}", $expr), $style)
$crate::ratatui_core::text::Span::styled($crate::format!("{}", $expr), $style)
};
}

View File

@ -35,16 +35,17 @@
/// [`Text`]: ratatui_core::text::Text
/// [`Line`]: ratatui_core::text::Line
/// [`Span`]: ratatui_core::text::Span
/// [`vec!`]: alloc::vec!
#[macro_export]
macro_rules! text {
() => {
$crate::ratatui_core::text::Text::default()
};
($line:expr; $n:expr) => {
$crate::ratatui_core::text::Text::from(vec![$line.into(); $n])
$crate::ratatui_core::text::Text::from($crate::vec![$line.into(); $n])
};
($($line:expr),+ $(,)?) => {{
$crate::ratatui_core::text::Text::from(vec![
$crate::ratatui_core::text::Text::from($crate::vec![
$(
$line.into(),
)+
@ -54,6 +55,7 @@ macro_rules! text {
#[cfg(test)]
mod tests {
use alloc::vec;
use ratatui_core::text::Text;
#[test]