Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use modularized ratatui crates #75

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 140 additions & 196 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ratatui-macros"
version = "0.6.0"
version = "0.7.0-alpha.0"
edition = "2021"
authors = ["The Ratatui Developers"]
description = "Macros for Ratatui"
Expand All @@ -13,8 +13,8 @@ categories = ["command-line-interface"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# requires alpha version of ratatui for the Into<Style> changes
ratatui = "0.29.0"
ratatui-core = "0.1.0-alpha.0"
ratatui-widgets = "0.3.0-alpha.0"

[dev-dependencies]
trybuild = { version = "1.0.101", features = ["diff"] }
Expand Down
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ If you are new to Ratatui, check out the [Layout concepts] article on the Ratatu
Use the `constraints!` macro to define layout constraints:

```rust
use ratatui::prelude::*;
# use ratatui_core::layout::Constraint;
use ratatui_macros::constraints;

assert_eq!(
Expand All @@ -62,7 +62,7 @@ assert_eq!(
```

```rust
use ratatui::prelude::*;
# use ratatui_core::layout::Constraint;
use ratatui_macros::constraints;

assert_eq!(
Expand All @@ -79,7 +79,7 @@ assert_eq!(
Use the `constraint!` macro to define individual constraints:

```rust
use ratatui::prelude::*;
# use ratatui_core::layout::Constraint;
use ratatui_macros::constraint;

assert_eq!(
Expand All @@ -91,7 +91,7 @@ assert_eq!(
Create vertical and horizontal layouts using the `vertical!` and `horizontal!` macros:

```rust
use ratatui::prelude::*;
# use ratatui_core::layout::Rect;
use ratatui_macros::{vertical, horizontal};

let area = Rect { x: 0, y: 0, width: 10, height: 10 };
Expand All @@ -114,7 +114,7 @@ The `span!` macro create raw and styled `Span`s. They each take a format string
`;` followed by the format string and arguments.

```rust
use ratatui::prelude::*;
# use ratatui_core::style::{Color, Modifier, Style, Stylize};
use ratatui_macros::span;

let name = "world!";
Expand All @@ -130,7 +130,6 @@ The `line!` macro creates a `Line` that contains a sequence of spans. It is simi
macro.

```rust
use ratatui::prelude::*;
use ratatui_macros::line;

let name = "world!";
Expand All @@ -144,7 +143,6 @@ The `text!` macro creates a `Text` that contains a sequence of lines. It is simi
macro.

```rust
use ratatui::prelude::*;
use ratatui_macros::{span, line, text};

let name = "world!";
Expand All @@ -155,7 +153,7 @@ let text = text!["bye"; 2];
It is even possible to use `span!` and `line!` in the `text!` macro:

```rust
use ratatui::prelude::*;
# use ratatui_core::style::{Modifier, Stylize};
use ratatui_macros::{span, line, text};
let name = "Bye!!!";
let text = text![line!["hello", "world".bold()], span!(Modifier::BOLD; "{name}")];
Expand All @@ -167,7 +165,6 @@ The `row!` macro creates a `Row` that contains a sequence of `Cell`. It is simil
macro. A `Row` represents a sequence of `Cell`s in a single row of a table.

```rust
use ratatui::prelude::*;
use ratatui_macros::row;

let rows = [
Expand All @@ -179,7 +176,7 @@ let rows = [
It is even possible to use `span!`, `line!` and `text!` in the `row!` macro:

```rust
use ratatui::prelude::*;
# use ratatui_core::style::{Modifier, Stylize};
use ratatui_macros::{span, line, text, row};
let name = "Bye!!!";
let text = row![text![line!["hello", "world".bold()]], span!(Modifier::BOLD; "{name}")];
Expand Down
20 changes: 10 additions & 10 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// # Examples
///
/// ```
/// # use ratatui::prelude::*;
/// # use ratatui_core::layout::Constraint;
/// use ratatui_macros::constraint;
/// assert_eq!(constraint!(>= 3 + 4), Constraint::Min(7));
/// assert_eq!(constraint!(<= 3 + 4), Constraint::Max(7));
Expand All @@ -18,22 +18,22 @@
#[macro_export]
macro_rules! constraint {
(== $token:tt %) => {
::ratatui::layout::Constraint::Percentage($token)
$crate::ratatui_core::layout::Constraint::Percentage($token)
};
(>= $expr:expr) => {
::ratatui::layout::Constraint::Min($expr)
$crate::ratatui_core::layout::Constraint::Min($expr)
};
(<= $expr:expr) => {
::ratatui::layout::Constraint::Max($expr)
$crate::ratatui_core::layout::Constraint::Max($expr)
};
(== $num:tt / $denom:tt) => {
::ratatui::layout::Constraint::Ratio($num as u32, $denom as u32)
$crate::ratatui_core::layout::Constraint::Ratio($num as u32, $denom as u32)
};
(== $expr:expr) => {
::ratatui::layout::Constraint::Length($expr)
$crate::ratatui_core::layout::Constraint::Length($expr)
};
(*= $expr:expr) => {
::ratatui::layout::Constraint::Fill($expr)
$crate::ratatui_core::layout::Constraint::Fill($expr)
};
}

Expand All @@ -53,7 +53,7 @@ macro_rules! constraint {
/// ```
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::layout::Constraint;
/// # use ratatui_macros::constraints;
/// assert_eq!(
/// constraints![==50, ==30%, >=3, <=1, ==1/2, *=1],
Expand Down Expand Up @@ -171,7 +171,7 @@ macro_rules! constraints {
#[macro_export]
macro_rules! vertical {
($( $constraint:tt )+) => {
::ratatui::layout::Layout::vertical($crate::constraints!( $($constraint)+ ))
$crate::ratatui_core::layout::Layout::vertical($crate::constraints!( $($constraint)+ ))
};
}

Expand All @@ -192,6 +192,6 @@ macro_rules! vertical {
#[macro_export]
macro_rules! horizontal {
($( $constraint:tt )+) => {
::ratatui::layout::Layout::horizontal($crate::constraints!( $($constraint)+ ))
$crate::ratatui_core::layout::Layout::horizontal($crate::constraints!( $($constraint)+ ))
};
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ mod line;
mod row;
mod span;
mod text;

// Re-export the core crate to use the types in macros
pub use ratatui_core;
13 changes: 6 additions & 7 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// * Create a [`Line`] containing a vector of [`Span`]s:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::style::Stylize;
/// use ratatui_macros::line;
///
/// let line = line!["hello", "world"];
Expand All @@ -17,15 +17,14 @@
/// * Create a [`Line`] from a given [`Span`] repeated some amount of times:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_macros::line;
/// let line = line!["hello"; 2];
/// ```
///
/// * Use [`span!`] macro inside [`line!`] macro for formatting.
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::style::Modifier;
/// use ratatui_macros::{line, span};
///
/// let line = line![span!("hello {}", "world"), span!(Modifier::BOLD; "goodbye {}", "world")];
Expand All @@ -36,13 +35,13 @@
#[macro_export]
macro_rules! line {
() => {
::ratatui::text::Line::default()
$crate::ratatui_core::text::Line::default()
};
($span:expr; $n:expr) => {
::ratatui::text::Line::from(vec![$span.into(); $n])
$crate::ratatui_core::text::Line::from(vec![$span.into(); $n])
};
($($span:expr),+ $(,)?) => {{
::ratatui::text::Line::from(vec![
$crate::ratatui_core::text::Line::from(vec![
$(
$span.into(),
)+
Expand All @@ -52,7 +51,7 @@ macro_rules! line {

#[cfg(test)]
mod tests {
use ratatui::prelude::*;
use ratatui_core::text::{Line, Span};

#[test]
fn line_literal() {
Expand Down
26 changes: 11 additions & 15 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// * Create a [`Row`] containing a vector of [`Cell`]s:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::style::Stylize;
/// use ratatui_macros::row;
///
/// let row = row!["hello", "world"];
Expand All @@ -17,23 +17,21 @@
/// * Create an empty [`Row`]:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_macros::row;
/// let empty_row = row![];
/// ```
///
/// * Create a [`Row`] from a given [`Cell`] repeated some amount of times:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_macros::row;
/// let row = row!["hello"; 2];
/// ```
///
/// * Use [`text!`], [`line!`] or [`span!`] macro inside [`row!`] macro.
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::style::{Modifier};
/// use ratatui_macros::{row, line, text, span};
///
/// let row = row![
Expand All @@ -47,15 +45,15 @@
#[macro_export]
macro_rules! row {
() => {
::ratatui::widgets::Row::default()
::ratatui_widgets::table::Row::default()
};
($cell:expr; $n:expr) => {
::ratatui::widgets::Row::new(vec![::ratatui::widgets::Cell::from($cell); $n])
::ratatui_widgets::table::Row::new(vec![::ratatui_widgets::table::Cell::from($cell); $n])
};
($($cell:expr),+ $(,)?) => {{
::ratatui::widgets::Row::new(vec![
::ratatui_widgets::table::Row::new(vec![
$(
::ratatui::widgets::Cell::from($cell),
::ratatui_widgets::table::Cell::from($cell),
)+
])
}};
Expand All @@ -64,10 +62,8 @@ macro_rules! row {
#[cfg(test)]
mod tests {

use ratatui::{
text::Text,
widgets::{Cell, Row},
};
use ratatui_core::text::Text;
use ratatui_widgets::table::{Cell, Row};

#[test]
fn row_literal() {
Expand Down Expand Up @@ -127,15 +123,15 @@ mod tests {
[
Row::new([
Cell::from("Find File"),
Cell::from(Text::raw("ctrl+f").alignment(ratatui::layout::Alignment::Right)),
Cell::from(Text::raw("ctrl+f").right_aligned()),
]),
Row::new([
Cell::from("Open recent"),
Cell::from(Text::raw("ctrl+r").alignment(ratatui::layout::Alignment::Right)),
Cell::from(Text::raw("ctrl+r").right_aligned()),
]),
Row::new([
Cell::from("Open config"),
Cell::from(Text::raw("ctrl+k").alignment(ratatui::layout::Alignment::Right)),
Cell::from(Text::raw("ctrl+k").right_aligned()),
]),
]
);
Expand Down
22 changes: 12 additions & 10 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// # Examples
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::style::{Color, Modifier, Style, Stylize};
/// use ratatui_macros::span;
///
/// let content = "content";
Expand Down Expand Up @@ -68,7 +68,7 @@
/// But this will work:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_core::style::{Modifier};
/// # use ratatui_macros::span;
/// let span = span!(Modifier::BOLD; "hello world");
/// ```
Expand All @@ -84,7 +84,6 @@
/// But this will work:
///
/// ```rust
/// # use ratatui::prelude::*;
/// # use ratatui_macros::span;
/// let span = span!("hello {}", "world");
/// ```
Expand All @@ -96,31 +95,34 @@
#[macro_export]
macro_rules! span {
($string:literal) => {
::ratatui::text::Span::raw(format!($string))
$crate::ratatui_core::text::Span::raw(format!($string))
};
($string:literal, $($arg:tt)*) => {
::ratatui::text::Span::raw(format!($string, $($arg)*))
$crate::ratatui_core::text::Span::raw(format!($string, $($arg)*))
};
($expr:expr) => {
::ratatui::text::Span::raw(format!("{}", $expr))
$crate::ratatui_core::text::Span::raw(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) => {
::ratatui::text::Span::styled(format!($string), $style)
$crate::ratatui_core::text::Span::styled(format!($string), $style)
};
($style:expr; $string:literal, $($arg:tt)*) => {
::ratatui::text::Span::styled(format!($string, $($arg)*), $style)
$crate::ratatui_core::text::Span::styled(format!($string, $($arg)*), $style)
};
($style:expr; $expr:expr) => {
::ratatui::text::Span::styled(format!("{}", $expr), $style)
$crate::ratatui_core::text::Span::styled(format!("{}", $expr), $style)
};
}

#[cfg(test)]
mod tests {
use ratatui::prelude::*;
use ratatui_core::{
style::{Color, Modifier, Style, Stylize},
text::Span,
};

#[test]
fn raw() {
Expand Down
Loading