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(border): add border! macro for easy bitflag manipulation #11

Merged
merged 12 commits into from
May 21, 2023
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rust-version = "1.65.0"
default = ["crossterm"]
all-widgets = ["widget-calendar"]
widget-calendar = ["time"]
macros = []

[package.metadata.docs.rs]
all-features = true
Expand Down
6 changes: 3 additions & 3 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ args = [
]

[tasks.test-crossterm]
env = { TUI_FEATURES = "serde,crossterm,all-widgets" }
env = { TUI_FEATURES = "serde,crossterm,all-widgets,macros" }
run_task = "test"

[tasks.test-termion]
env = { TUI_FEATURES = "serde,termion,all-widgets" }
env = { TUI_FEATURES = "serde,termion,all-widgets,macros" }
run_task = "test"

[tasks.test-termwiz]
env = { TUI_FEATURES = "serde,termwiz,all-widgets" }
env = { TUI_FEATURES = "serde,termwiz,all-widgets,macros" }
run_task = "test"

[tasks.test]
Expand Down
38 changes: 38 additions & 0 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,41 @@ pub trait StatefulWidget {
type State;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State);
}

/// Macro that constructs and returns a [`Borders`] object from TOP, BOTTOM, LEFT, RIGHT, NONE, and ALL.
/// Internally it creates an empty `Borders` object and then inserts each bit flag specified
/// into it using `Borders::insert()`.
///
/// ## Examples
///
///```
/// # use tui::widgets::{Block, Borders};
/// # use tui::style::{Style, Color};
/// # use tui::border;
///
/// Block::default()
/// //Construct a `Borders` object and use it in place
/// .borders(border!(TOP, BOTTOM));
///
/// //`border!` can be called with any order of individual sides
/// let bottom_first = border!(BOTTOM, LEFT, TOP);
/// //with the ALL keyword which works as expected
/// let all = border!(ALL);
/// //or with nothing to return a `Borders::NONE' bitflag.
/// let none = border!(NONE);
///
///```
#[cfg(feature = "macros")]
#[macro_export]
macro_rules! border {
( $($b:tt), +) => {{
let mut border = Borders::empty();
$(
border.insert(Borders::$b);
)*
border
}};
() =>{
Borders::NONE
}
}
19 changes: 19 additions & 0 deletions tests/border_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![cfg(feature = "macros")]
use ratatui::{border, widgets::Borders};

#[test]
fn border_empty_test() {
let empty = Borders::NONE;
assert_eq!(empty, border!());
}
#[test]
fn border_all_test() {
let all = Borders::ALL;
assert_eq!(all, border!(ALL));
assert_eq!(all, border!(TOP, BOTTOM, LEFT, RIGHT));
}
#[test]
fn border_left_right_test() {
let left_right = Borders::from_bits(Borders::LEFT.bits() | Borders::RIGHT.bits());
assert_eq!(left_right, Some(border!(RIGHT, LEFT)));
}