From 8913e2ce1f40d451ddb4527f08ec75f198d5063c Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Fri, 28 Jun 2024 09:16:42 -0400 Subject: [PATCH] feat: Use `::ratatui` instead of `ratatui` (#54) This PR uses `::ratatui` to ensure it doesn't clash with a module named `ratatui` in an app, per comment https://github.com/ratatui-org/ratatui-macros/pull/52#discussion_r1656670148 This PR also refactors some tests. --- src/layout.rs | 16 ++++++++-------- src/line.rs | 39 +++++++++++++++++++++++++++++++-------- src/row.rs | 49 +++++++++++++++++++++++++++++++++++++++++-------- src/span.rs | 6 +++--- 4 files changed, 83 insertions(+), 27 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index 7b3c424..b446031 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -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) }; } @@ -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)+ )) }; } @@ -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)+ )) }; } diff --git a/src/line.rs b/src/line.rs index 57ddeaf..cab1780 100644 --- a/src/line.rs +++ b/src/line.rs @@ -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(), )+ @@ -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()])); + } } diff --git a/src/row.rs b/src/row.rs index a090d99..d6c7a11 100644 --- a/src/row.rs +++ b/src/row.rs @@ -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 @@ -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), )+ ]) }}; @@ -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()], diff --git a/src/span.rs b/src/span.rs index ba01669..dccfae4 100644 --- a/src/span.rs +++ b/src/span.rs @@ -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) }; }