Skip to content
This repository was archived by the owner on Aug 6, 2023. It is now read-only.

border! macro for the easy and pretty creation of Borders bitflags. #484

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ bitflags! {
}
}

/// 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);
///
///```
#[macro_export]
macro_rules! border {
( $($b:tt), +) => {{
let mut border = Borders::empty();
$(
border.insert(Borders::$b);
)*
border
}};
() =>{
Borders::NONE
}
}

/// Base requirements for a Widget
pub trait Widget {
/// Draws the current state of the widget in the given buffer. That the only method required to
Expand Down
18 changes: 18 additions & 0 deletions tests/border_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use tui::{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)));
}