summaryrefslogtreecommitdiffstats
path: root/tui-react/src
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-05 19:06:51 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-05 19:08:13 +0530
commit42fb0cccb10ce1084267b63b07a5a0a8bf84de99 (patch)
tree53699272d0b881f1914034914efcf8a48a3c9bea /tui-react/src
parentf59b32d344875bbfc584f259c2c2e74dbb254b08 (diff)
The block is now not needed anymore - we can just own simple props
Diffstat (limited to 'tui-react/src')
-rw-r--r--tui-react/src/block.rs165
-rw-r--r--tui-react/src/lib.rs2
-rw-r--r--tui-react/src/list.rs16
3 files changed, 7 insertions, 176 deletions
diff --git a/tui-react/src/block.rs b/tui-react/src/block.rs
deleted file mode 100644
index 9225f84..0000000
--- a/tui-react/src/block.rs
+++ /dev/null
@@ -1,165 +0,0 @@
-//! Derived from TUI-rs, license: MIT, Copyright (c) 2016 Florian Dehau
-use std::borrow::Borrow;
-use std::marker::PhantomData;
-use tui::{
- buffer::Buffer, layout::Rect, style::Color, style::Style, symbols::line, widgets::Borders,
-};
-
-pub fn fill_background(area: Rect, buf: &mut Buffer, color: Color) {
- for y in area.top()..area.bottom() {
- for x in area.left()..area.right() {
- buf.get_mut(x, y).set_bg(color);
- }
- }
-}
-
-#[derive(Clone, Copy, Default)]
-pub struct Block<'a, T>(PhantomData<&'a T>);
-
-pub struct BlockProps<'a> {
- /// Optional title place on the upper left of the block
- pub title: Option<&'a str>,
- /// Title style
- pub title_style: Style,
- /// Visible borders
- pub borders: Borders,
- /// Border style
- pub border_style: Style,
- /// Widget style
- pub style: Style,
-}
-
-impl<'a> Default for BlockProps<'a> {
- fn default() -> BlockProps<'a> {
- BlockProps {
- title: None,
- title_style: Default::default(),
- borders: Borders::NONE,
- border_style: Default::default(),
- style: Default::default(),
- }
- }
-}
-
-impl<'a> BlockProps<'a> {
- /// Compute the inner area of a block based on its border visibility rules.
- pub fn inner(&self, area: Rect) -> Rect {
- if area.width < 2 || area.height < 2 {
- return Rect::default();
- }
- let mut inner = area;
- if self.borders.intersects(Borders::LEFT) {
- inner.x += 1;
- inner.width -= 1;
- }
- if self.borders.intersects(Borders::TOP) || self.title.is_some() {
- inner.y += 1;
- inner.height -= 1;
- }
- if self.borders.intersects(Borders::RIGHT) {
- inner.width -= 1;
- }
- if self.borders.intersects(Borders::BOTTOM) {
- inner.height -= 1;
- }
- inner
- }
-
- pub fn render(&self, area: Rect, buf: &mut Buffer) {
- Block::<()>::default().render(self, area, buf);
- }
-}
-
-impl<'a, T> Block<'a, T> {
- fn render(&self, props: impl Borrow<BlockProps<'a>>, area: Rect, buf: &mut Buffer) {
- if area.width < 2 || area.height < 2 {
- return;
- }
- let BlockProps {
- title,
- title_style,
- borders,
- border_style,
- style,
- } = props.borrow();
-
- fill_background(area, buf, style.bg);
-
- // Sides
- if borders.intersects(Borders::LEFT) {
- for y in area.top()..area.bottom() {
- buf.get_mut(area.left(), y)
- .set_symbol(line::VERTICAL)
- .set_style(*border_style);
- }
- }
- if borders.intersects(Borders::TOP) {
- for x in area.left()..area.right() {
- buf.get_mut(x, area.top())
- .set_symbol(line::HORIZONTAL)
- .set_style(*border_style);
- }
- }
- if borders.intersects(Borders::RIGHT) {
- let x = area.right() - 1;
- for y in area.top()..area.bottom() {
- buf.get_mut(x, y)
- .set_symbol(line::VERTICAL)
- .set_style(*border_style);
- }
- }
- if borders.intersects(Borders::BOTTOM) {
- let y = area.bottom() - 1;
- for x in area.left()..area.right() {
- buf.get_mut(x, y)
- .set_symbol(line::HORIZONTAL)
- .set_style(*border_style);
- }
- }
-
- // Corners
- if borders.contains(Borders::LEFT | Borders::TOP) {
- buf.get_mut(area.left(), area.top())
- .set_symbol(line::TOP_LEFT)
- .set_style(*border_style);
- }
- if borders.contains(Borders::RIGHT | Borders::TOP) {
- buf.get_mut(area.right() - 1, area.top())
- .set_symbol(line::TOP_RIGHT)
- .set_style(*border_style);
- }
- if borders.contains(Borders::LEFT | Borders::BOTTOM) {
- buf.get_mut(area.left(), area.bottom() - 1)
- .set_symbol(line::BOTTOM_LEFT)
- .set_style(*border_style);
- }
- if borders.contains(Borders::RIGHT | Borders::BOTTOM) {
- buf.get_mut(area.right() - 1, area.bottom() - 1)
- .set_symbol(line::BOTTOM_RIGHT)
- .set_style(*border_style);
- }
-
- if area.width > 2 {
- if let Some(title) = title {
- let lx = if borders.intersects(Borders::LEFT) {
- 1
- } else {
- 0
- };
- let rx = if borders.intersects(Borders::RIGHT) {
- 1
- } else {
- 0
- };
- let width = area.width - lx - rx;
- buf.set_stringn(
- area.left() + lx,
- area.top(),
- title,
- width as usize,
- *title_style,
- );
- }
- }
- }
-}
diff --git a/tui-react/src/lib.rs b/tui-react/src/lib.rs
index e8af82b..e15b68e 100644
--- a/tui-react/src/lib.rs
+++ b/tui-react/src/lib.rs
@@ -1,7 +1,5 @@
-mod block;
mod list;
mod terminal;
-pub use block::*;
pub use list::*;
pub use terminal::*;
diff --git a/tui-react/src/list.rs b/tui-react/src/list.rs
index e61162e..d9510ff 100644
--- a/tui-react/src/list.rs
+++ b/tui-react/src/list.rs
@@ -1,10 +1,8 @@
-use super::BlockProps;
-use std::borrow::Borrow;
use std::iter::repeat;
use tui::{
buffer::Buffer,
layout::Rect,
- widgets::{Paragraph, Text, Widget},
+ widgets::{Block, Paragraph, Text, Widget},
};
pub fn fill_background_to_right(mut s: String, entire_width: u16) -> String {
@@ -38,14 +36,14 @@ impl ReactList {
#[derive(Default)]
pub struct ReactListProps<'b> {
- pub block: Option<BlockProps<'b>>,
+ pub block: Option<Block<'b>>,
pub entry_in_view: Option<usize>,
}
impl ReactList {
pub fn render<'a, 't>(
&mut self,
- props: impl Borrow<ReactListProps<'a>>,
+ props: ReactListProps<'a>,
items: impl IntoIterator<Item = Vec<Text<'t>>>,
area: Rect,
buf: &mut Buffer,
@@ -53,16 +51,16 @@ impl ReactList {
let ReactListProps {
block,
entry_in_view,
- } = props.borrow();
+ } = props;
let list_area = match block {
- Some(b) => {
- b.render(area, buf);
+ Some(mut b) => {
+ b.draw(area, buf);
b.inner(area)
}
None => area,
};
- self.offset = self.list_offset_for(*entry_in_view, list_area.height as usize);
+ self.offset = self.list_offset_for(entry_in_view, list_area.height as usize);
if list_area.width < 1 || list_area.height < 1 {
return;