mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-14 00:47:14 +00:00
Cleanup API
This commit is contained in:
parent
a36e20f217
commit
a5b632aeb0
14 changed files with 28 additions and 110 deletions
|
@ -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;
|
||||
|
|
|
@ -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<W>(&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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<u16> = 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] -
|
||||
|
|
|
@ -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::<Vec<(f64, f64)>>();
|
||||
/// 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<Self::Item> {
|
||||
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<Iterator<Item = (f64, f64)> + 'a> {
|
||||
Box::new(self.into_iter())
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue