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

feat: Add row! macro #52

Merged
merged 4 commits into from
Jun 28, 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
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
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
///
/// * 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;
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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 {
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
() => {
ratatui::widgets::Row::default()
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
};
($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 {
kdheepak marked this conversation as resolved.
Show resolved Hide resolved

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")])
);
kdheepak marked this conversation as resolved.
Show resolved Hide resolved

// 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)),
]),
]
);
}
}
Loading