//! Shared border drawing primitives. //! //! `Border` owns both the glyph set and style used for box edges, corners, and //! divider junctions. Widgets use it to draw consistent chrome while keeping //! their child content and divider fill cells independently styled. use crate::layout::Rect; use crate::{Buffer, Color, Line, Paint, Style}; /// Border glyph set. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct BorderChars { /// Top-left corner. pub top_left: char, /// Top-right corner. pub top_right: char, /// Bottom-left corner. pub bottom_left: char, /// Bottom-right corner. pub bottom_right: char, /// Vertical edge. pub vertical: char, /// Horizontal edge. pub horizontal: char, /// Left divider junction. pub divider_left: char, /// Right divider junction. pub divider_right: char, } impl BorderChars { /// Rounded border glyphs. pub const ROUNDED: Self = Self { top_left: '╭', top_right: '╮', bottom_left: '╰', bottom_right: '╯', vertical: '│', horizontal: '─', divider_left: '├', divider_right: '┤', }; } impl Default for BorderChars { fn default() -> Self { Self::ROUNDED } } /// A border style. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Border { /// Border style. pub style: Style, /// Border characters. pub chars: BorderChars, } impl Border { /// Creates a rounded border from a foreground color. pub const fn new(color: Color) -> Self { Self::with_style(Style::new(color)) } /// Creates a rounded border from a style. pub const fn with_style(style: Style) -> Self { Self { style, chars: BorderChars::ROUNDED, } } /// Returns a top border line with `cols` inner columns. pub fn top(self, cols: usize) -> Line { self.horizontal( self.chars.top_left, self.chars.horizontal, self.chars.top_right, cols, ) } /// Returns a divider line with `cols` inner columns. pub fn divider(self, cols: usize, sigil: char) -> Line { Line::default() .item(self.paint(self.chars.divider_left)) .item(self.paint(std::iter::repeat_n(sigil, cols).collect::())) .item(self.paint(self.chars.divider_right)) } /// Returns a bottom border line with `cols` inner columns. pub fn bottom(self, cols: usize) -> Line { self.horizontal( self.chars.bottom_left, self.chars.horizontal, self.chars.bottom_right, cols, ) } /// Returns a left border segment with padding. pub fn left(self, padding: usize) -> Paint { self.paint(format!("{}{}", self.chars.vertical, " ".repeat(padding))) } /// Returns a right border segment with padding. pub fn right(self, padding: usize) -> Paint { self.paint(format!("{}{}", " ".repeat(padding), self.chars.vertical)) } /// Draws the border into a buffer. pub fn draw(self, area: Rect, buf: &mut Buffer) { if area.is_empty() { return; } if area.cols == 1 && area.rows == 1 { self.set(buf, area.x, area.y, self.chars.top_left); return; } if area.rows == 1 { let right = area.right().saturating_sub(1); self.set(buf, area.x, area.y, self.chars.top_left); for x in area.x + 1..right { self.set(buf, x, area.y, self.chars.horizontal); } self.set(buf, right, area.y, self.chars.top_right); return; } if area.cols == 1 { let bottom = area.bottom().saturating_sub(1); self.set(buf, area.x, area.y, self.chars.top_left); for y in area.y + 1..bottom { self.set(buf, area.x, y, self.chars.vertical); } self.set(buf, area.x, bottom, self.chars.bottom_left); return; } let right = area.right().saturating_sub(1); let bottom = area.bottom().saturating_sub(1); self.set(buf, area.x, area.y, self.chars.top_left); self.set(buf, right, area.y, self.chars.top_right); self.set(buf, area.x, bottom, self.chars.bottom_left); self.set(buf, right, bottom, self.chars.bottom_right); for x in area.x + 1..right { self.set(buf, x, area.y, self.chars.horizontal); self.set(buf, x, bottom, self.chars.horizontal); } for y in area.y + 1..bottom { self.set(buf, area.x, y, self.chars.vertical); self.set(buf, right, y, self.chars.vertical); } } fn horizontal(self, left: char, fill: char, right: char, cols: usize) -> Line { Line::default() .item(self.paint(left)) .item(self.paint(std::iter::repeat_n(fill, cols).collect::())) .item(self.paint(right)) } fn paint(self, item: T) -> Paint { Paint::new(item).with_style(self.style) } fn set(self, buf: &mut Buffer, x: usize, y: usize, glyph: char) { buf.set_char(x, y, glyph, self.style); } } #[cfg(test)] mod tests { use super::*; use crate::Buffer; #[test] fn border_lines_use_shared_glyphs_and_style() { let border = Border::new(Color::Red); let line = border.top(3); assert_eq!(line.to_string(), "╭───╮"); assert!( line.labels() .iter() .all(|label| { label.paint.style().fg_color() == Color::Red }) ); assert_eq!(border.divider(2, '┄').to_string(), "├┄┄┤"); assert_eq!(border.bottom(1).to_string(), "╰─╯"); } #[test] fn border_draws_degenerate_and_rectangular_buffers() { let border = Border::new(Color::Unset); let mut one = Buffer::empty(Rect::new(0, 0, 1, 1)); let mut row = Buffer::empty(Rect::new(0, 0, 4, 1)); let mut column = Buffer::empty(Rect::new(0, 0, 1, 3)); let mut rect = Buffer::empty(Rect::new(0, 0, 4, 3)); border.draw(one.area(), &mut one); border.draw(row.area(), &mut row); border.draw(column.area(), &mut column); border.draw(rect.area(), &mut rect); assert_eq!(one.lines(), vec!["╭"]); assert_eq!(row.lines(), vec!["╭──╮"]); assert_eq!(column.lines(), vec!["╭", "│", "╰"]); assert_eq!(rect.lines(), vec!["╭──╮", "│ │", "╰──╯"]); } }