ratatui/widgets/borders.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
use std::fmt;
use bitflags::bitflags;
bitflags! {
/// Bitflags that can be composed to set the visible borders essentially on the block widget.
#[derive(Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Borders: u8 {
/// Show no border (default)
const NONE = 0b0000;
/// Show the top border
const TOP = 0b0001;
/// Show the right border
const RIGHT = 0b0010;
/// Show the bottom border
const BOTTOM = 0b0100;
/// Show the left border
const LEFT = 0b1000;
/// Show all borders
const ALL = Self::TOP.bits() | Self::RIGHT.bits() | Self::BOTTOM.bits() | Self::LEFT.bits();
}
}
/// Implement the `Debug` trait for the `Borders` bitflags. This is a manual implementation to
/// display the flags in a more readable way. The default implementation would display the
/// flags as 'Border(0x0)' for `Borders::NONE` for example.
impl fmt::Debug for Borders {
/// Display the Borders bitflags as a list of names. For example, `Borders::NONE` will be
/// displayed as `NONE` and `Borders::ALL` will be displayed as `ALL`. If multiple flags are
/// set, they will be displayed separated by a pipe character.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_empty() {
return write!(f, "NONE");
}
if self.is_all() {
return write!(f, "ALL");
}
let mut first = true;
for (name, border) in self.iter_names() {
if border == Self::NONE {
continue;
}
if first {
write!(f, "{name}")?;
first = false;
} else {
write!(f, " | {name}")?;
}
}
Ok(())
}
}
/// Macro that constructs and returns a combination of the [`Borders`] object from TOP, BOTTOM, LEFT
/// and RIGHT.
///
/// When used with NONE you should consider omitting this completely. For ALL you should consider
/// [`Block::bordered()`](crate::widgets::Block::bordered) instead.
///
/// ## Examples
///
/// ```
/// use ratatui::{
/// border,
/// widgets::{Block, Borders},
/// };
///
/// Block::new()
/// .title("Construct Borders and use them in place")
/// .borders(border!(TOP, BOTTOM));
/// ```
///
/// `border!` can be called with any number of individual sides:
///
/// ```
/// use ratatui::{border, widgets::Borders};
/// let right_open = border!(TOP, LEFT, BOTTOM);
/// assert_eq!(right_open, Borders::TOP | Borders::LEFT | Borders::BOTTOM);
/// ```
///
/// Single borders work but using `Borders::` directly would be simpler.
///
/// ```
/// use ratatui::{border, widgets::Borders};
///
/// assert_eq!(border!(TOP), Borders::TOP);
/// assert_eq!(border!(ALL), Borders::ALL);
/// assert_eq!(border!(), Borders::NONE);
/// ```
#[cfg(feature = "macros")]
#[macro_export]
macro_rules! border {
() => {
Borders::NONE
};
($b:ident) => {
Borders::$b
};
($first:ident,$($other:ident),*) => {
Borders::$first
$(
.union(Borders::$other)
)*
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_borders_debug() {
assert_eq!(format!("{:?}", Borders::empty()), "NONE");
assert_eq!(format!("{:?}", Borders::NONE), "NONE");
assert_eq!(format!("{:?}", Borders::TOP), "TOP");
assert_eq!(format!("{:?}", Borders::BOTTOM), "BOTTOM");
assert_eq!(format!("{:?}", Borders::LEFT), "LEFT");
assert_eq!(format!("{:?}", Borders::RIGHT), "RIGHT");
assert_eq!(format!("{:?}", Borders::ALL), "ALL");
assert_eq!(format!("{:?}", Borders::all()), "ALL");
assert_eq!(
format!("{:?}", Borders::TOP | Borders::BOTTOM),
"TOP | BOTTOM"
);
}
}
#[cfg(all(test, feature = "macros"))]
mod macro_tests {
use super::*;
#[test]
fn can_be_const() {
const NOTHING: Borders = border!();
const JUST_TOP: Borders = border!(TOP);
const TOP_BOTTOM: Borders = border!(TOP, BOTTOM);
const RIGHT_OPEN: Borders = border!(TOP, LEFT, BOTTOM);
assert_eq!(NOTHING, Borders::NONE);
assert_eq!(JUST_TOP, Borders::TOP);
assert_eq!(TOP_BOTTOM, Borders::TOP | Borders::BOTTOM);
assert_eq!(RIGHT_OPEN, Borders::TOP | Borders::LEFT | Borders::BOTTOM);
}
#[test]
fn border_empty() {
let empty = Borders::NONE;
assert_eq!(empty, border!());
}
#[test]
fn border_all() {
let all = Borders::ALL;
assert_eq!(all, border!(ALL));
assert_eq!(all, border!(TOP, BOTTOM, LEFT, RIGHT));
}
#[test]
fn border_left_right() {
let left_right = Borders::from_bits(Borders::LEFT.bits() | Borders::RIGHT.bits());
assert_eq!(left_right, Some(border!(RIGHT, LEFT)));
}
}