From 9085c81e768b488ea8e63e9dc6704f439d9b27a4 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Sun, 19 Jan 2020 15:44:03 +0100 Subject: [PATCH] refactor: clean up border type for blocks * Merge line symbols in a single module. * Replace set_border_type with border_type to match other builder methods. * Remove unecessary branching. --- examples/block.rs | 4 +- examples/demo/app.rs | 2 +- src/symbols.rs | 48 +++++++++++++----------- src/widgets/block.rs | 87 +++++++++++++++++++++----------------------- 4 files changed, 71 insertions(+), 70 deletions(-) diff --git a/examples/block.rs b/examples/block.rs index c5a702c..2dc65d1 100644 --- a/examples/block.rs +++ b/examples/block.rs @@ -35,7 +35,7 @@ fn main() -> Result<(), failure::Error> { Block::default() .borders(Borders::ALL) .title("Main block with round corners") - .set_border_type(BorderType::Rounded) + .border_type(BorderType::Rounded) .render(&mut f, size); let chunks = Layout::default() .direction(Direction::Vertical) @@ -75,7 +75,7 @@ fn main() -> Result<(), failure::Error> { .title("With styled and double borders") .border_style(Style::default().fg(Color::Cyan)) .borders(Borders::LEFT | Borders::RIGHT) - .set_border_type(BorderType::Double) + .border_type(BorderType::Double) .render(&mut f, chunks[1]); } })?; diff --git a/examples/demo/app.rs b/examples/demo/app.rs index 40f3704..6ffba07 100644 --- a/examples/demo/app.rs +++ b/examples/demo/app.rs @@ -131,7 +131,7 @@ pub struct App<'a> { pub show_chart: bool, pub progress: u16, pub sparkline: Signal, - pub tasks: ListState<(&'a str)>, + pub tasks: ListState<&'a str>, pub logs: ListState<(&'a str, &'a str)>, pub signals: Signals, pub barchart: Vec<(&'a str, u64)>, diff --git a/src/symbols.rs b/src/symbols.rs index 25c7cda..ccd72d6 100644 --- a/src/symbols.rs +++ b/src/symbols.rs @@ -21,38 +21,42 @@ pub mod bar { } pub mod line { - pub const TOP_RIGHT: &str = "┐"; pub const VERTICAL: &str = "│"; + pub const DOUBLE_VERTICAL: &str = "║"; + pub const HORIZONTAL: &str = "─"; + pub const DOUBLE_HORIZONTAL: &str = "═"; + + pub const TOP_RIGHT: &str = "┐"; + pub const ROUNDED_TOP_RIGHT: &str = "╮"; + pub const DOUBLE_TOP_RIGHT: &str = "╗"; + pub const TOP_LEFT: &str = "┌"; + pub const ROUNDED_TOP_LEFT: &str = "╭"; + pub const DOUBLE_TOP_LEFT: &str = "╔"; + pub const BOTTOM_RIGHT: &str = "┘"; + pub const ROUNDED_BOTTOM_RIGHT: &str = "╯"; + pub const DOUBLE_BOTTOM_RIGHT: &str = "╝"; + pub const BOTTOM_LEFT: &str = "└"; + pub const ROUNDED_BOTTOM_LEFT: &str = "╰"; + pub const DOUBLE_BOTTOM_LEFT: &str = "╚"; + pub const VERTICAL_LEFT: &str = "┤"; + pub const DOUBLE_VERTICAL_LEFT: &str = "╣"; + pub const VERTICAL_RIGHT: &str = "├"; + pub const DOUBLE_VERTICAL_RIGHT: &str = "╠"; + pub const HORIZONTAL_DOWN: &str = "┬"; + pub const DOUBLE_HORIZONTAL_DOWN: &str = "╦"; + pub const HORIZONTAL_UP: &str = "┴"; + pub const DOUBLE_HORIZONTAL_UP: &str = "╩"; + pub const CROSS: &str = "┼"; + pub const DOUBLE_CROSS: &str = "╬"; } pub const DOT: &str = "•"; - -pub mod rounded { - pub const TOP_RIGHT: &str = "╮"; - pub const TOP_LEFT: &str = "╭"; - pub const BOTTOM_RIGHT: &str = "╯"; - pub const BOTTOM_LEFT: &str = "╰"; -} - -pub mod double { - pub const VERTICAL: &str = "║"; - pub const HORIZONTAL: &str = "═"; - pub const TOP_RIGHT: &str = "╗"; - pub const TOP_LEFT: &str = "╔"; - pub const BOTTOM_RIGHT: &str = "╝"; - pub const BOTTOM_LEFT: &str = "╚"; - pub const VERTICAL_LEFT: &str = "╣"; - pub const VERTICAL_RIGHT: &str = "╠"; - pub const HORIZONTAL_DOWN: &str = "╦"; - pub const HORIZONTAL_UP: &str = "╩"; - pub const CROSS: &str = "╬"; -} diff --git a/src/widgets/block.rs b/src/widgets/block.rs index 341280a..b73163b 100644 --- a/src/widgets/block.rs +++ b/src/widgets/block.rs @@ -1,16 +1,23 @@ use crate::buffer::Buffer; use crate::layout::Rect; use crate::style::Style; -use crate::symbols::{double, line, rounded}; +use crate::symbols::line; use crate::widgets::{Borders, Widget}; +#[derive(Debug, Copy, Clone)] +pub enum BorderType { + Plain, + Rounded, + Double, +} + /// Base widget to be used with all upper level ones. It may be used to display a box border around /// the widget and/or add a title. /// /// # Examples /// /// ``` -/// # use tui::widgets::{Block, Borders}; +/// # use tui::widgets::{Block, BorderType, Borders}; /// # use tui::style::{Style, Color}; /// # fn main() { /// Block::default() @@ -18,6 +25,7 @@ use crate::widgets::{Borders, Widget}; /// .title_style(Style::default().fg(Color::Red)) /// .borders(Borders::LEFT | Borders::RIGHT) /// .border_style(Style::default().fg(Color::White)) +/// .border_type(BorderType::Rounded) /// .style(Style::default().bg(Color::Black)); /// # } /// ``` @@ -29,10 +37,10 @@ pub struct Block<'a> { title_style: Style, /// Visible borders borders: Borders, - /// Border style (meaning colors) + /// Border style border_style: Style, - /// Border type (meaning one of single lines w/sharp corners, single lines w/rounded corners, - /// or double lines w/sharp corners) + /// Type of the border. The default is plain lines but one can choose to have rounded corners + /// or doubled lines instead. border_type: BorderType, /// Widget style style: Style, @@ -51,13 +59,6 @@ impl<'a> Default for Block<'a> { } } -#[derive(Debug, Copy, Clone)] -pub enum BorderType { - Plain, - Rounded, - Double, -} - impl<'a> Block<'a> { pub fn title(mut self, title: &'a str) -> Block<'a> { self.title = Some(title); @@ -84,7 +85,7 @@ impl<'a> Block<'a> { self } - pub fn set_border_type(mut self, border_type: BorderType) -> Block<'a> { + pub fn border_type(mut self, border_type: BorderType) -> Block<'a> { self.border_type = border_type; self } @@ -123,52 +124,48 @@ impl<'a> Widget for Block<'a> { // Sides if self.borders.intersects(Borders::LEFT) { + let symbol = match self.border_type { + BorderType::Double => line::DOUBLE_VERTICAL, + _ => line::VERTICAL, + }; for y in area.top()..area.bottom() { buf.get_mut(area.left(), y) - .set_symbol({ - match self.border_type { - BorderType::Double => double::VERTICAL, - _ => line::VERTICAL, - } - }) + .set_symbol(symbol) .set_style(self.border_style); } } if self.borders.intersects(Borders::TOP) { + let symbol = match self.border_type { + BorderType::Double => line::DOUBLE_HORIZONTAL, + _ => line::HORIZONTAL, + }; for x in area.left()..area.right() { buf.get_mut(x, area.top()) - .set_symbol({ - match self.border_type { - BorderType::Double => double::HORIZONTAL, - _ => line::HORIZONTAL, - } - }) + .set_symbol(symbol) .set_style(self.border_style); } } if self.borders.intersects(Borders::RIGHT) { let x = area.right() - 1; + let symbol = match self.border_type { + BorderType::Double => line::DOUBLE_VERTICAL, + _ => line::VERTICAL, + }; for y in area.top()..area.bottom() { buf.get_mut(x, y) - .set_symbol({ - match self.border_type { - BorderType::Double => double::VERTICAL, - _ => line::VERTICAL, - } - }) + .set_symbol(symbol) .set_style(self.border_style); } } if self.borders.intersects(Borders::BOTTOM) { let y = area.bottom() - 1; + let symbol = match self.border_type { + BorderType::Double => line::DOUBLE_HORIZONTAL, + _ => line::HORIZONTAL, + }; for x in area.left()..area.right() { buf.get_mut(x, y) - .set_symbol({ - match self.border_type { - BorderType::Double => double::HORIZONTAL, - _ => line::HORIZONTAL, - } - }) + .set_symbol(symbol) .set_style(self.border_style); } } @@ -178,8 +175,8 @@ impl<'a> Widget for Block<'a> { buf.get_mut(area.left(), area.top()) .set_symbol({ match self.border_type { - BorderType::Double => double::TOP_LEFT, - BorderType::Rounded => rounded::TOP_LEFT, + BorderType::Double => line::DOUBLE_TOP_LEFT, + BorderType::Rounded => line::ROUNDED_TOP_LEFT, _ => line::TOP_LEFT, } }) @@ -189,8 +186,8 @@ impl<'a> Widget for Block<'a> { buf.get_mut(area.right() - 1, area.top()) .set_symbol({ match self.border_type { - BorderType::Double => double::TOP_RIGHT, - BorderType::Rounded => rounded::TOP_RIGHT, + BorderType::Double => line::DOUBLE_TOP_RIGHT, + BorderType::Rounded => line::ROUNDED_TOP_RIGHT, _ => line::TOP_RIGHT, } }) @@ -200,8 +197,8 @@ impl<'a> Widget for Block<'a> { buf.get_mut(area.left(), area.bottom() - 1) .set_symbol({ match self.border_type { - BorderType::Double => double::BOTTOM_LEFT, - BorderType::Rounded => rounded::BOTTOM_LEFT, + BorderType::Double => line::DOUBLE_BOTTOM_LEFT, + BorderType::Rounded => line::ROUNDED_BOTTOM_LEFT, _ => line::BOTTOM_LEFT, } }) @@ -211,8 +208,8 @@ impl<'a> Widget for Block<'a> { buf.get_mut(area.right() - 1, area.bottom() - 1) .set_symbol({ match self.border_type { - BorderType::Double => double::BOTTOM_RIGHT, - BorderType::Rounded => rounded::BOTTOM_RIGHT, + BorderType::Double => line::DOUBLE_BOTTOM_RIGHT, + BorderType::Rounded => line::ROUNDED_BOTTOM_RIGHT, _ => line::BOTTOM_RIGHT, } })