mirror of
https://github.com/ratatui-org/ratatui
synced 2024-09-20 14:42:06 +00:00
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.
This commit is contained in:
parent
682349c03e
commit
9085c81e76
4 changed files with 71 additions and 70 deletions
|
@ -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]);
|
||||
}
|
||||
})?;
|
||||
|
|
|
@ -131,7 +131,7 @@ pub struct App<'a> {
|
|||
pub show_chart: bool,
|
||||
pub progress: u16,
|
||||
pub sparkline: Signal<RandomSignal>,
|
||||
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)>,
|
||||
|
|
|
@ -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 = "╬";
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue