Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit d0f75eb

Browse files
authored
feat: allow span macro to accept a bare expression (#61)
This makes it possible to pass an expression directly to the `span!` macro, which will be formatted into the display representation of the expression using the `format!` macro. ```rust let number = 123; let test = "test"; span!(number); span!(test); ```
1 parent 7eeb6af commit d0f75eb

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/span.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/// A macro for creating a [`Span`] using formatting syntax.
22
///
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.
46
///
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()`).
710
///
811
/// # Examples
912
///
@@ -13,6 +16,10 @@
1316
///
1417
/// let content = "content";
1518
///
19+
/// // expression
20+
/// let span = span!(content);
21+
///
22+
/// // format string
1623
/// let span = span!("test content");
1724
/// let span = span!("test {}", "content");
1825
/// let span = span!("{} {}", "test", "content");
@@ -24,6 +31,11 @@
2431
/// let span = span!("test {:04}", 123);
2532
///
2633
/// let style = Style::new().green();
34+
///
35+
/// // styled expression
36+
/// let span = span!(style; content);
37+
///
38+
/// // styled format string
2739
/// let span = span!(style; "test content");
2840
/// let span = span!(style; "test {}", "content");
2941
/// let span = span!(style; "{} {}", "test", "content");
@@ -42,8 +54,8 @@
4254
///
4355
/// # Note
4456
///
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.
4759
///
4860
/// For example, the following will fail to compile:
4961
///
@@ -89,11 +101,20 @@ macro_rules! span {
89101
($string:literal, $($arg:tt)*) => {
90102
::ratatui::text::Span::raw(format!($string, $($arg)*))
91103
};
104+
($expr:expr) => {
105+
::ratatui::text::Span::raw(format!("{}", $expr))
106+
};
92107
($style:expr, $($arg:tt)*) => {
93108
compile_error!("first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semicolon")
94109
};
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)
97118
};
98119
}
99120

@@ -146,6 +167,14 @@ mod tests {
146167
// a number with a format specifier
147168
let span = span!("test {number:04}");
148169
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"));
149178
}
150179

151180
#[test]
@@ -202,5 +231,13 @@ mod tests {
202231

203232
let span = span!(Modifier::BOLD; "test {content}");
204233
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));
205242
}
206243
}

0 commit comments

Comments
 (0)