1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/// 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 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_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")])
        );
    }

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

    #[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")])
        );
    }

    #[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()],
            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)),
                ]),
            ]
        );
    }
}