Skip to content

Commit

Permalink
feat: Use ::ratatui instead of ratatui (#54)
Browse files Browse the repository at this point in the history
This PR uses `::ratatui` to ensure it doesn't clash with a module named
`ratatui` in an app, per comment
#52 (comment)

This PR also refactors some tests.
  • Loading branch information
kdheepak authored Jun 28, 2024
1 parent c764957 commit 8913e2c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 27 deletions.
16 changes: 8 additions & 8 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
#[macro_export]
macro_rules! constraint {
(== $token:tt %) => {
ratatui::layout::Constraint::Percentage($token)
::ratatui::layout::Constraint::Percentage($token)
};
(>= $expr:expr) => {
ratatui::layout::Constraint::Min($expr)
::ratatui::layout::Constraint::Min($expr)
};
(<= $expr:expr) => {
ratatui::layout::Constraint::Max($expr)
::ratatui::layout::Constraint::Max($expr)
};
(== $num:tt / $denom:tt) => {
ratatui::layout::Constraint::Ratio($num as u32, $denom as u32)
::ratatui::layout::Constraint::Ratio($num as u32, $denom as u32)
};
(== $expr:expr) => {
ratatui::layout::Constraint::Length($expr)
::ratatui::layout::Constraint::Length($expr)
};
(*= $expr:expr) => {
ratatui::layout::Constraint::Fill($expr)
::ratatui::layout::Constraint::Fill($expr)
};
}

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)+ ))
::ratatui::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)+ ))
::ratatui::layout::Layout::horizontal($crate::constraints!( $($constraint)+ ))
};
}
39 changes: 31 additions & 8 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
#[macro_export]
macro_rules! line {
() => {
ratatui::text::Line::default()
::ratatui::text::Line::default()
};
($span:expr; $n:expr) => {
ratatui::text::Line::from(vec![$span.into(); $n])
::ratatui::text::Line::from(vec![$span.into(); $n])
};
($($span:expr),+ $(,)?) => {{
ratatui::text::Line::from(vec![
::ratatui::text::Line::from(vec![
$(
$span.into(),
)+
Expand All @@ -56,21 +56,44 @@ mod tests {
use ratatui::prelude::*;

#[test]
fn line() {
// literal
fn line_literal() {
let line = line!["hello", "world"];
assert_eq!(line, Line::from(vec!["hello".into(), "world".into()]));
}

// raw instead line
#[test]
fn line_raw_instead_of_literal() {
let line = line![Span::raw("hello"), "world"];
assert_eq!(line, Line::from(vec!["hello".into(), "world".into()]));
}

// vec count syntax
#[test]
fn line_vec_count_syntax() {
let line = line!["hello"; 2];
assert_eq!(line, Line::from(vec!["hello".into(), "hello".into()]));
}

// vec count syntax with span
#[test]
fn line_vec_count_syntax_with_span() {
let line = line![crate::span!("hello"); 2];
assert_eq!(line, Line::from(vec!["hello".into(), "hello".into()]));
}

#[test]
fn line_empty() {
let line = line![];
assert_eq!(line, Line::default());
}

#[test]
fn line_single_span() {
let line = line![Span::raw("foo")];
assert_eq!(line, Line::from(vec!["foo".into()]));
}

#[test]
fn line_repeated_span() {
let line = line![Span::raw("foo"); 2];
assert_eq!(line, Line::from(vec!["foo".into(), "foo".into()]));
}
}
49 changes: 41 additions & 8 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
/// let row = row!["hello".red(), "world".red().bold()];
/// ```
///
/// * 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
Expand All @@ -40,15 +49,15 @@
#[macro_export]
macro_rules! row {
() => {
ratatui::widgets::Row::default()
::ratatui::widgets::Row::default()
};
($cell:expr; $n:expr) => {
ratatui::widgets::Row::new(vec![ratatui::widgets::Cell::from($cell); $n])
::ratatui::widgets::Row::new(vec![::ratatui::widgets::Cell::from($cell); $n])
};
($($cell:expr),+ $(,)?) => {{
ratatui::widgets::Row::new(vec![
::ratatui::widgets::Row::new(vec![
$(
ratatui::widgets::Cell::from($cell),
::ratatui::widgets::Cell::from($cell),
)+
])
}};
Expand All @@ -63,28 +72,52 @@ mod tests {
};

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

#[test]
fn row_empty() {
let row = row![];
assert_eq!(row, Row::default());
}

// explicit use of span and line
#[test]
fn row_single_cell() {
let row = row![Cell::from("foo")];
assert_eq!(row, Row::new(vec![Cell::from("foo")]));
}

#[test]
fn row_repeated_cell() {
let row = row![Cell::from("foo"); 2];
assert_eq!(row, Row::new(vec![Cell::from("foo"), Cell::from("foo")]));
}

#[test]
fn row_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
#[test]
fn row_vec_count_syntax() {
let row = row!["hello"; 2];
assert_eq!(
row,
Row::new(vec![Cell::from("hello"), Cell::from("hello")])
);
}

#[test]
fn multiple_rows() {
use crate::text;
let rows = [
row!["Find File", text!["ctrl+f"].right_aligned()],
Expand Down
6 changes: 3 additions & 3 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@
#[macro_export]
macro_rules! span {
($string:literal) => {
ratatui::text::Span::raw(format!($string))
::ratatui::text::Span::raw(format!($string))
};
($string:literal, $($arg:tt)*) => {
ratatui::text::Span::raw(format!($string, $($arg)*))
::ratatui::text::Span::raw(format!($string, $($arg)*))
};
($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; $($arg:tt)*) => {
ratatui::text::Span::styled(format!($($arg)*), $style)
::ratatui::text::Span::styled(format!($($arg)*), $style)
};
}

Expand Down

0 comments on commit 8913e2c

Please sign in to comment.