From a5b632aeb0b3d81f2f820115c73c44a9dee296ad Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Wed, 2 Nov 2016 19:16:53 +0100 Subject: [PATCH] Cleanup API --- src/lib.rs | 4 +- src/terminal.rs | 6 +-- src/widgets/barchart.rs | 4 +- src/widgets/block.rs | 2 +- src/widgets/canvas/mod.rs | 14 ++---- src/widgets/canvas/rectangle.rs | 76 --------------------------------- src/widgets/chart.rs | 6 +-- src/widgets/gauge.rs | 4 +- src/widgets/list.rs | 4 +- src/widgets/mod.rs | 2 +- src/widgets/sparkline.rs | 4 +- src/widgets/table.rs | 4 +- src/widgets/tabs.rs | 4 +- src/widgets/text.rs | 4 +- 14 files changed, 28 insertions(+), 110 deletions(-) delete mode 100644 src/widgets/canvas/rectangle.rs diff --git a/src/lib.rs b/src/lib.rs index f9cefb7f..013b1205 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ extern crate cassowary; extern crate unicode_segmentation; extern crate unicode_width; -mod buffer; +pub mod buffer; mod util; pub mod symbols; pub mod terminal; @@ -15,4 +15,4 @@ pub mod widgets; pub mod style; pub mod layout; -pub use self::terminal::Terminal; \ No newline at end of file +pub use self::terminal::Terminal; diff --git a/src/terminal.rs b/src/terminal.rs index 5ffacfad..f887d123 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -65,7 +65,7 @@ impl Terminal { entry.chunks.clone() } - pub fn draw(&mut self) { + pub fn flush(&mut self) { let width = self.buffers[self.current].area.width; let mut string = String::with_capacity(self.buffers[self.current].content.len()); let mut fg = Color::Reset; @@ -114,7 +114,7 @@ impl Terminal { pub fn render(&mut self, widget: &W, area: &Rect) where W: Widget { - widget.buffer(area, &mut self.buffers[self.current]); + widget.draw(area, &mut self.buffers[self.current]); } pub fn resize(&mut self, area: Rect) { @@ -128,7 +128,7 @@ impl Terminal { pub fn finish(&mut self) { // Draw to stdout - self.draw(); + self.flush(); // Clean layout cache let to_remove = self.layout_cache diff --git a/src/widgets/barchart.rs b/src/widgets/barchart.rs index 00719f8f..c6a62b6e 100644 --- a/src/widgets/barchart.rs +++ b/src/widgets/barchart.rs @@ -84,10 +84,10 @@ impl<'a> BarChart<'a> { } impl<'a> Widget for BarChart<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let chart_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, diff --git a/src/widgets/block.rs b/src/widgets/block.rs index 04132b31..0d9da2cf 100644 --- a/src/widgets/block.rs +++ b/src/widgets/block.rs @@ -75,7 +75,7 @@ impl<'a> Block<'a> { } impl<'a> Widget for Block<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { if area.width < 2 || area.height < 2 { return; diff --git a/src/widgets/canvas/mod.rs b/src/widgets/canvas/mod.rs index 0c2e53cc..379d3f32 100644 --- a/src/widgets/canvas/mod.rs +++ b/src/widgets/canvas/mod.rs @@ -1,12 +1,10 @@ mod points; mod line; -mod rectangle; mod map; mod world; pub use self::points::Points; pub use self::line::Line; -pub use self::rectangle::Rectangle; pub use self::map::{Map, MapResolution}; use style::Color; @@ -69,10 +67,10 @@ impl<'a> Canvas<'a> { } impl<'a> Widget for Canvas<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let canvas_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, @@ -81,11 +79,6 @@ impl<'a> Widget for Canvas<'a> { let width = canvas_area.width as usize; let height = canvas_area.height as usize; - let mut x_bounds = self.x_bounds; - x_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap()); - let mut y_bounds = self.y_bounds; - y_bounds.sort_by(|a, b| a.partial_cmp(b).unwrap()); - for layer in self.layers { let mut grid: Vec = vec![BRAILLE_OFFSET; width * height]; @@ -93,7 +86,8 @@ impl<'a> Widget for Canvas<'a> { for shape in layer.iter() { for (x, y) in shape.points().filter(|&(x, y)| { - !(x < x_bounds[0] || x > x_bounds[1] || y < y_bounds[0] || y > y_bounds[1]) + !(x < self.x_bounds[0] || x > self.x_bounds[1] || y < self.y_bounds[0] || + y > self.y_bounds[1]) }) { let dy = ((self.y_bounds[1] - y) * (canvas_area.height - 1) as f64 * 4.0 / (self.y_bounds[1] - diff --git a/src/widgets/canvas/rectangle.rs b/src/widgets/canvas/rectangle.rs deleted file mode 100644 index 3dc41338..00000000 --- a/src/widgets/canvas/rectangle.rs +++ /dev/null @@ -1,76 +0,0 @@ -use super::Shape; -use style::Color; - -/// # Examples -/// -/// ``` -/// use tui::style::Color; -/// use tui::widgets::canvas::{Shape, Rectangle}; -/// -/// let rectangle = Rectangle { x: 4.0, y: 4.0, width: 4.0, height: 4.0, color: Color::Red }; -/// let points = rectangle.points().collect::>(); -/// assert_eq!(&points, &[(4.0, 4.0), (5.0, 4.0), (6.0, 4.0), (7.0, 4.0), (4.0, 5.0), (7.0, 5.0), -/// (4.0, 6.0), (7.0, 6.0), (4.0, 7.0), (5.0, 7.0), (6.0, 7.0), (7.0, 7.0)]); -/// ``` -pub struct Rectangle { - pub x: f64, - pub y: f64, - pub width: f64, - pub height: f64, - pub color: Color, -} - -pub struct RectangleIterator<'a> { - rect: &'a Rectangle, - x: f64, - y: f64, - right: f64, - bottom: f64, -} - -impl<'a> Iterator for RectangleIterator<'a> { - type Item = (f64, f64); - fn next(&mut self) -> Option { - if self.y < self.bottom { - let pos = (self.x, self.y); - let dx = if self.y == self.rect.y || self.y == self.bottom - 1.0 { - 1.0 - } else { - self.rect.width - 1.0 - }; - self.x += dx; - if self.x >= self.right { - self.x = self.rect.x; - self.y += 1.0; - } - Some(pos) - } else { - None - } - } -} - -impl<'a> IntoIterator for &'a Rectangle { - type Item = (f64, f64); - type IntoIter = RectangleIterator<'a>; - fn into_iter(self) -> Self::IntoIter { - let right = self.x + self.width; - let bottom = self.y + self.height; - RectangleIterator { - rect: self, - x: self.x, - y: self.y, - right: right, - bottom: bottom, - } - } -} - -impl<'a> Shape<'a> for Rectangle { - fn color(&self) -> Color { - self.color - } - fn points(&'a self) -> Box + 'a> { - Box::new(self.into_iter()) - } -} diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index dda25c59..b5602126 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -226,10 +226,10 @@ impl<'a> Chart<'a> { } impl<'a> Widget for Chart<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let chart_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, @@ -348,7 +348,7 @@ impl<'a> Widget for Chart<'a> { coords: dataset.data, color: dataset.color, }]]) - .buffer(&graph_area, buf); + .draw(&graph_area, buf); } } } diff --git a/src/widgets/gauge.rs b/src/widgets/gauge.rs index 7a12cc7d..cdee8323 100644 --- a/src/widgets/gauge.rs +++ b/src/widgets/gauge.rs @@ -60,10 +60,10 @@ impl<'a> Gauge<'a> { } impl<'a> Widget for Gauge<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let gauge_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, diff --git a/src/widgets/list.rs b/src/widgets/list.rs index 1f356e6b..41db3663 100644 --- a/src/widgets/list.rs +++ b/src/widgets/list.rs @@ -69,11 +69,11 @@ impl<'a> List<'a> { } impl<'a> Widget for List<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let list_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index e652b79e..7807fa89 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -38,7 +38,7 @@ pub mod border { } pub trait Widget { - fn buffer(&self, area: &Rect, buf: &mut Buffer); + fn draw(&self, area: &Rect, buf: &mut Buffer); fn background(&self, area: &Rect, buf: &mut Buffer, color: Color) { for y in area.top()..area.bottom() { for x in area.left()..area.right() { diff --git a/src/widgets/sparkline.rs b/src/widgets/sparkline.rs index c6fce3d8..c34081b7 100644 --- a/src/widgets/sparkline.rs +++ b/src/widgets/sparkline.rs @@ -55,10 +55,10 @@ impl<'a> Sparkline<'a> { } impl<'a> Widget for Sparkline<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let spark_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, diff --git a/src/widgets/table.rs b/src/widgets/table.rs index 45da7659..9385467f 100644 --- a/src/widgets/table.rs +++ b/src/widgets/table.rs @@ -76,12 +76,12 @@ impl<'a> Table<'a> { } impl<'a> Widget for Table<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { // Render block if necessary and get the drawing area let table_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs index 493cf78c..d0f30132 100644 --- a/src/widgets/tabs.rs +++ b/src/widgets/tabs.rs @@ -61,11 +61,11 @@ impl<'a> Tabs<'a> { } impl<'a> Widget for Tabs<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let tabs_area = match self.block { Some(b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area, diff --git a/src/widgets/text.rs b/src/widgets/text.rs index 85072a7c..31ed59f6 100644 --- a/src/widgets/text.rs +++ b/src/widgets/text.rs @@ -151,10 +151,10 @@ impl<'a> Iterator for Parser<'a> { } impl<'a> Widget for Text<'a> { - fn buffer(&self, area: &Rect, buf: &mut Buffer) { + fn draw(&self, area: &Rect, buf: &mut Buffer) { let text_area = match self.block { Some(ref b) => { - b.buffer(area, buf); + b.draw(area, buf); b.inner(area) } None => *area,