|
1 | 1 | /// A macro for creating a [`Span`] using formatting syntax.
|
2 | 2 | ///
|
3 |
| -/// `span!` is similar to the [`format!`] macro, but it returns a [`Span`] instead of a `String`. |
| 3 | +/// `span!` is similar to the [`format!`] macro, but it returns a [`Span`] instead of a `String`. In |
| 4 | +/// addition, it also accepts an expression for the first argument, which will be converted to a |
| 5 | +/// string using the [`format!`] macro. |
4 | 6 | ///
|
5 |
| -/// If semicolon follows the first argument, then the first argument is a [`Style`] and a styled [`Span`] will be created. |
6 |
| -/// Otherwise, the [`Span`] will be created as a raw span (i.e. with style set to `Style::default()`). |
| 7 | +/// If semicolon follows the first argument, then the first argument is a [`Style`] and a styled |
| 8 | +/// [`Span`] will be created. Otherwise, the [`Span`] will be created as a raw span (i.e. with style |
| 9 | +/// set to `Style::default()`). |
7 | 10 | ///
|
8 | 11 | /// # Examples
|
9 | 12 | ///
|
|
13 | 16 | ///
|
14 | 17 | /// let content = "content";
|
15 | 18 | ///
|
| 19 | +/// // expression |
| 20 | +/// let span = span!(content); |
| 21 | +/// |
| 22 | +/// // format string |
16 | 23 | /// let span = span!("test content");
|
17 | 24 | /// let span = span!("test {}", "content");
|
18 | 25 | /// let span = span!("{} {}", "test", "content");
|
|
24 | 31 | /// let span = span!("test {:04}", 123);
|
25 | 32 | ///
|
26 | 33 | /// let style = Style::new().green();
|
| 34 | +/// |
| 35 | +/// // styled expression |
| 36 | +/// let span = span!(style; content); |
| 37 | +/// |
| 38 | +/// // styled format string |
27 | 39 | /// let span = span!(style; "test content");
|
28 | 40 | /// let span = span!(style; "test {}", "content");
|
29 | 41 | /// let span = span!(style; "{} {}", "test", "content");
|
|
42 | 54 | ///
|
43 | 55 | /// # Note
|
44 | 56 | ///
|
45 |
| -/// The first parameter must be a formatting specifier followed by a comma OR |
46 |
| -/// anything that can be converted into a [`Style`] followed by a semicolon. |
| 57 | +/// The first parameter must be a formatting specifier followed by a comma OR anything that can be |
| 58 | +/// converted into a [`Style`] followed by a semicolon. |
47 | 59 | ///
|
48 | 60 | /// For example, the following will fail to compile:
|
49 | 61 | ///
|
@@ -89,11 +101,20 @@ macro_rules! span {
|
89 | 101 | ($string:literal, $($arg:tt)*) => {
|
90 | 102 | ::ratatui::text::Span::raw(format!($string, $($arg)*))
|
91 | 103 | };
|
| 104 | + ($expr:expr) => { |
| 105 | + ::ratatui::text::Span::raw(format!("{}", $expr)) |
| 106 | + }; |
92 | 107 | ($style:expr, $($arg:tt)*) => {
|
93 | 108 | compile_error!("first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semicolon")
|
94 | 109 | };
|
95 |
| - ($style:expr; $($arg:tt)*) => { |
96 |
| - ::ratatui::text::Span::styled(format!($($arg)*), $style) |
| 110 | + ($style:expr; $string:literal) => { |
| 111 | + ::ratatui::text::Span::styled(format!($string), $style) |
| 112 | + }; |
| 113 | + ($style:expr; $string:literal, $($arg:tt)*) => { |
| 114 | + ::ratatui::text::Span::styled(format!($string, $($arg)*), $style) |
| 115 | + }; |
| 116 | + ($style:expr; $expr:expr) => { |
| 117 | + ::ratatui::text::Span::styled(format!("{}", $expr), $style) |
97 | 118 | };
|
98 | 119 | }
|
99 | 120 |
|
@@ -146,6 +167,14 @@ mod tests {
|
146 | 167 | // a number with a format specifier
|
147 | 168 | let span = span!("test {number:04}");
|
148 | 169 | assert_eq!(span, Span::raw("test 0123"));
|
| 170 | + |
| 171 | + // directly pass a number expression |
| 172 | + let span = span!(number); |
| 173 | + assert_eq!(span, Span::raw("123")); |
| 174 | + |
| 175 | + // directly pass a string expression |
| 176 | + let span = span!(test); |
| 177 | + assert_eq!(span, Span::raw("test")); |
149 | 178 | }
|
150 | 179 |
|
151 | 180 | #[test]
|
@@ -202,5 +231,13 @@ mod tests {
|
202 | 231 |
|
203 | 232 | let span = span!(Modifier::BOLD; "test {content}");
|
204 | 233 | assert_eq!(span, Span::styled("test content", Style::new().bold()));
|
| 234 | + |
| 235 | + // directly pass a number expression |
| 236 | + let span = span!(STYLE; number); |
| 237 | + assert_eq!(span, Span::styled("123", STYLE)); |
| 238 | + |
| 239 | + // directly pass a string expression |
| 240 | + let span = span!(STYLE; test); |
| 241 | + assert_eq!(span, Span::styled("test", STYLE)); |
205 | 242 | }
|
206 | 243 | }
|
0 commit comments