use bitflags::bitflags;
use crate::buffer::Buffer;
use crate::layout::Rect;
use crate::style::Style;
use crate::text::{Alignment, Line};
use crate::widgets::Widget;
bitflags! {
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Borders: u8 {
const NONE = 0b0000;
const TOP = 0b0001;
const BOTTOM = 0b0010;
const LEFT = 0b0100;
const RIGHT = 0b1000;
const ALL = Self::TOP.bits() | Self::BOTTOM.bits() | Self::LEFT.bits() | Self::RIGHT.bits();
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BorderType {
#[default]
Plain,
Rounded,
Double,
Thick,
}
struct BorderSet {
top_left: char,
top_right: char,
bottom_left: char,
bottom_right: char,
horizontal: char,
vertical: char,
}
impl BorderType {
fn glyphs(self) -> BorderSet {
match self {
BorderType::Plain => BorderSet {
top_left: '┌',
top_right: '┐',
bottom_left: '└',
bottom_right: '┘',
horizontal: '─',
vertical: '│',
},
BorderType::Rounded => BorderSet {
top_left: '╭',
top_right: '╮',
bottom_left: '╰',
bottom_right: '╯',
horizontal: '─',
vertical: '│',
},
BorderType::Double => BorderSet {
top_left: '╔',
top_right: '╗',
bottom_left: '╚',
bottom_right: '╝',
horizontal: '═',
vertical: '║',
},
BorderType::Thick => BorderSet {
top_left: '┏',
top_right: '┓',
bottom_left: '┗',
bottom_right: '┛',
horizontal: '━',
vertical: '┃',
},
}
}
}
#[derive(Debug, Default, Clone)]
pub struct Block<'a> {
title: Option<Line<'a>>,
title_alignment: Alignment,
borders: Borders,
border_style: Style,
border_type: BorderType,
style: Style,
}
impl<'a> Block<'a> {
pub fn default() -> Block<'a> {
Block {
title: None,
title_alignment: Alignment::Left,
borders: Borders::NONE,
border_style: Style::default(),
border_type: BorderType::Plain,
style: Style::default(),
}
}
pub fn bordered() -> Block<'a> {
Block::default().borders(Borders::ALL)
}
pub fn title<T>(mut self, title: T) -> Block<'a>
where
T: Into<Line<'a>>,
{
self.title = Some(title.into());
self
}
pub fn title_alignment(mut self, alignment: Alignment) -> Block<'a> {
self.title_alignment = alignment;
self
}
pub fn borders(mut self, borders: Borders) -> Block<'a> {
self.borders = borders;
self
}
pub fn border_style(mut self, style: Style) -> Block<'a> {
self.border_style = style;
self
}
pub fn border_type(mut self, border_type: BorderType) -> Block<'a> {
self.border_type = border_type;
self
}
pub fn style(mut self, style: Style) -> Block<'a> {
self.style = style;
self
}
pub fn inner(&self, area: Rect) -> Rect {
let mut inner = area;
if self.borders.contains(Borders::TOP) {
inner.y = inner.y.saturating_add(1);
inner.height = inner.height.saturating_sub(1);
}
if self.borders.contains(Borders::BOTTOM) {
inner.height = inner.height.saturating_sub(1);
}
if self.borders.contains(Borders::LEFT) {
inner.x = inner.x.saturating_add(1);
inner.width = inner.width.saturating_sub(1);
}
if self.borders.contains(Borders::RIGHT) {
inner.width = inner.width.saturating_sub(1);
}
inner
}
}
impl<'a> Widget for Block<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
if area.area() == 0 {
return;
}
buf.set_style(area, self.style);
let glyphs = self.border_type.glyphs();
let b = self.borders;
if b.contains(Borders::TOP) {
for x in area.left()..area.right() {
buf.get_mut(x, area.top())
.set_char(glyphs.horizontal)
.set_style(self.border_style);
}
}
if b.contains(Borders::BOTTOM) && area.height > 0 {
let y = area.bottom() - 1;
for x in area.left()..area.right() {
buf.get_mut(x, y)
.set_char(glyphs.horizontal)
.set_style(self.border_style);
}
}
if b.contains(Borders::LEFT) {
for y in area.top()..area.bottom() {
buf.get_mut(area.left(), y)
.set_char(glyphs.vertical)
.set_style(self.border_style);
}
}
if b.contains(Borders::RIGHT) && area.width > 0 {
let x = area.right() - 1;
for y in area.top()..area.bottom() {
buf.get_mut(x, y)
.set_char(glyphs.vertical)
.set_style(self.border_style);
}
}
if b.contains(Borders::TOP) && b.contains(Borders::LEFT) {
buf.get_mut(area.left(), area.top())
.set_char(glyphs.top_left)
.set_style(self.border_style);
}
if b.contains(Borders::TOP) && b.contains(Borders::RIGHT) && area.width > 0 {
buf.get_mut(area.right() - 1, area.top())
.set_char(glyphs.top_right)
.set_style(self.border_style);
}
if b.contains(Borders::BOTTOM) && b.contains(Borders::LEFT) && area.height > 0 {
buf.get_mut(area.left(), area.bottom() - 1)
.set_char(glyphs.bottom_left)
.set_style(self.border_style);
}
if b.contains(Borders::BOTTOM) && b.contains(Borders::RIGHT) && area.width > 0 && area.height > 0
{
buf.get_mut(area.right() - 1, area.bottom() - 1)
.set_char(glyphs.bottom_right)
.set_style(self.border_style);
}
if let Some(title) = self.title {
if b.contains(Borders::TOP) {
let width = title.width() as u16;
let available = area.width.saturating_sub(2);
let x = match self.title_alignment {
Alignment::Left => area.left() + 1,
Alignment::Center => {
area.left() + 1 + available.saturating_sub(width) / 2
}
Alignment::Right => area.right().saturating_sub(1 + width),
};
let mut cx = x;
for span in &title.spans {
cx = buf.set_stringn(
cx,
area.top(),
&span.content,
available as usize,
span.style,
);
}
}
}
}
}