Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add text! macro #36

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ let line = line!["hello", format!("{name}")];
let line = line!["bye"; 2];
```

## Line
kdheepak marked this conversation as resolved.
Show resolved Hide resolved

The `text!` macro creates a `Text` that contains a sequence of lines. It is similar to the `vec!` macro.

```rust
use ratatui::prelude::*;
use ratatui_macros::{span, line, text};

let name = "world!";
let text = text![span!(Modifier::BOLD; "hello"), line![format!("{name}")]];
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
let text = text!["bye"; 2];
```

## Contributing

Contributions to `ratatui-macros` are welcome! Whether it's submitting a bug report, a feature
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
mod layout;
mod line;
mod span;
mod text;
73 changes: 73 additions & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/// A macro for creating a [`Text`] using vec! syntax.
///
/// `text!` is similar to the [`vec!`] macro, but it returns a [`Text`] instead of a `Vec`.
///
/// # Examples
///
/// * Create a [`Text`] containing a vector of [`Line`]s:
///
/// ```rust
/// # use ratatui::prelude::*;
/// use ratatui_macros::text;
///
/// let text = text!["hello", "world"];
/// let text = text!["hello".red(), "world".red().bold()];
/// ```
///
/// * Create a [`text`] from a given [`Line`] repeated some amount of times:
///
/// ```rust
/// # use ratatui::prelude::*;
/// use ratatui_macros::text;
///
/// let text = text!["hello"; 2];
/// ```
///
/// * Use [`line!`] or [`span!`] macro inside [`text!`] macro.
///
/// ```rust
/// # use ratatui::prelude::*;
/// use ratatui_macros::{line, text, span};
///
/// let text = text![line!["hello", "world"], span!(Modifier::BOLD; "goodbye {}", "world")];
/// ```
///
/// [`Text`]: crate::text::Text
/// [`Line`]: crate::text::Line
/// [`Span`]: crate::text::Span
#[macro_export]
macro_rules! text {
() => {
ratatui::text::Text::default()
};
($line:expr; $n:expr) => {
ratatui::text::Text::from(vec![$line.into(); $n])
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
};
($($line:expr),+ $(,)?) => {{
ratatui::text::Text::from(vec![
$(
$line.into(),
)+
])
}};
}

#[cfg(test)]
mod tests {
use ratatui::prelude::*;

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

// explicit use of span and line
let text = text![crate::span!("hello"), crate::line!["world"]];
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(text, Text::from(vec!["hello".into(), "world".into()]));

// vec count syntax
let text = text!["hello"; 2];
assert_eq!(text, Text::from(vec!["hello".into(), "hello".into()]));
}
}