Skip to content

Commit

Permalink
feat: Add row! macro (#52)
Browse files Browse the repository at this point in the history
This PR adds a `row!` macro similar to the existing `text!` and `line!`
macro.

This will allow code like this:

```rust
let rows = [
    Row::new([
        Cell::from("\u{f002}"),
        Cell::from("Find File"),
        Cell::from(Text::raw("ctrl+f").alignment(ratatui::layout::Alignment::Right)),
    ]),
    Row::new([
        Cell::from("\u{f021a}"),
        Cell::from("Open recent"),
        Cell::from(Text::raw("ctrl+r").alignment(ratatui::layout::Alignment::Right)),
    ]),
    Row::new([
        Cell::from("\u{f0493}"),
        Cell::from("Open config"),
        Cell::from(Text::raw("ctrl+k").alignment(ratatui::layout::Alignment::Right)),
    ]),
];
```

to be written like 

```rust
let rows = [
    row!["\u{f002}", "Find File", text!["ctrl+f"].right_aligned()],
    row!["\u{f021a}", "Open recent", text!["ctrl+r"].right_aligned()],
    row!["\u{f0493}", "Open config", text!["ctrl+k"].right_aligned()],
];
```
  • Loading branch information
kdheepak committed Jun 28, 2024
1 parent ad54cf2 commit c764957
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

mod layout;
mod line;
mod row;
mod span;
mod text;
112 changes: 112 additions & 0 deletions src/row.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/// A macro for creating a [`Row`] using vec! syntax.
///
/// `row!` is similar to the [`vec!`] macro, but it returns a [`Row`] instead of a `Vec`.
///
/// # Examples
///
/// * Create a [`Row`] containing a vector of [`Cell`]s:
///
/// ```rust
/// # use ratatui::prelude::*;
/// use ratatui_macros::row;
///
/// let row = row!["hello", "world"];
/// let row = row!["hello".red(), "world".red().bold()];
/// ```
///
/// * 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_macros::{row, line, text, span};
///
/// let row = row![
/// line!["hello", "world"], span!(Modifier::BOLD; "goodbye {}", "world"),
/// text!["hello", "world"],
/// ];
/// ```
///
/// [`Row`]: crate::widgets::Row
/// [`Cell`]: crate::widgets::Cell
#[macro_export]
macro_rules! row {
() => {
ratatui::widgets::Row::default()
};
($cell:expr; $n:expr) => {
ratatui::widgets::Row::new(vec![ratatui::widgets::Cell::from($cell); $n])
};
($($cell:expr),+ $(,)?) => {{
ratatui::widgets::Row::new(vec![
$(
ratatui::widgets::Cell::from($cell),
)+
])
}};
}

#[cfg(test)]
mod tests {

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

#[test]
fn row() {
// literal
let row = row!["hello", "world"];
assert_eq!(
row,
Row::new(vec![Cell::from("hello"), Cell::from("world")])
);

// explicit use of span and line
let row = row![crate::line!("hello"), crate::span!["world"]];
assert_eq!(
row,
Row::new(vec![Cell::from("hello"), Cell::from("world")])
);

// vec count syntax
let row = row!["hello"; 2];
assert_eq!(
row,
Row::new(vec![Cell::from("hello"), Cell::from("hello")])
);

use crate::text;
let rows = [
row!["Find File", text!["ctrl+f"].right_aligned()],
row!["Open recent", text!["ctrl+r"].right_aligned()],
row!["Open config", text!["ctrl+k"].right_aligned()],
];
assert_eq!(
rows,
[
Row::new([
Cell::from("Find File"),
Cell::from(Text::raw("ctrl+f").alignment(ratatui::layout::Alignment::Right)),
]),
Row::new([
Cell::from("Open recent"),
Cell::from(Text::raw("ctrl+r").alignment(ratatui::layout::Alignment::Right)),
]),
Row::new([
Cell::from("Open config"),
Cell::from(Text::raw("ctrl+k").alignment(ratatui::layout::Alignment::Right)),
]),
]
);
}
}

0 comments on commit c764957

Please sign in to comment.