refactor: update widgets

* all widgets use the consumable builder pattern
* `draw` on terminal expect a closure that take a frame as only arg
This commit is contained in:
Florian Dehau 2018-09-01 14:05:33 +02:00
parent d6016788ef
commit 13e194cd26
26 changed files with 152 additions and 238 deletions

View file

@ -9,6 +9,7 @@ keywords = ["tui", "terminal", "dashboard"]
repository = "https://github.com/fdehau/tui-rs" repository = "https://github.com/fdehau/tui-rs"
license = "MIT" license = "MIT"
exclude = ["docs/*", ".travis.yml"] exclude = ["docs/*", ".travis.yml"]
autoexamples = true
[badges] [badges]
travis-ci = { repository = "fdehau/tui-rs" } travis-ci = { repository = "fdehau/tui-rs" }
@ -31,63 +32,7 @@ rustbox = { version = "0.11.0", optional = true }
stderrlog = "0.3.0" stderrlog = "0.3.0"
rand = "0.4.2" rand = "0.4.2"
[[example]]
name = "barchart"
path = "examples/barchart.rs"
[[example]]
name = "block"
path = "examples/block.rs"
[[example]]
name = "canvas"
path = "examples/canvas.rs"
[[example]]
name = "chart"
path = "examples/chart.rs"
[[example]]
name = "custom_widget"
path = "examples/custom_widget.rs"
[[example]]
name = "demo"
path = "examples/demo.rs"
[[example]]
name = "gauge"
path = "examples/gauge.rs"
[[example]]
name = "list"
path = "examples/list.rs"
[[example]]
name = "paragraph"
path = "examples/paragraph.rs"
[[example]] [[example]]
name = "rustbox" name = "rustbox"
path = "examples/rustbox.rs" path = "examples/rustbox.rs"
required-features = ["rustbox"] required-features = ["rustbox"]
[[example]]
name = "sparkline"
path = "examples/sparkline.rs"
[[example]]
name = "table"
path = "examples/table.rs"
[[example]]
name = "tabs"
path = "examples/tabs.rs"
[[example]]
name = "user_input"
path = "examples/user_input.rs"
[[example]]
name = "layout"
path = "examples/layout.rs"

View file

@ -99,7 +99,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -117,15 +117,14 @@ fn main() {
app.advance(); app.advance();
} }
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)
@ -161,6 +160,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.label_style(Style::default().fg(Color::Cyan).modifier(Modifier::Italic)) .label_style(Style::default().fg(Color::Cyan).modifier(Modifier::Italic))
.render(&mut f, chunks[1]); .render(&mut f, chunks[1]);
} }
} })
t.draw().unwrap();
} }

View file

@ -18,14 +18,14 @@ fn main() {
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
let mut term_size = terminal.size().unwrap(); let mut term_size = terminal.size().unwrap();
draw(&mut terminal, term_size); draw(&mut terminal, term_size).unwrap();
for c in stdin.keys() { for c in stdin.keys() {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
if term_size != size { if term_size != size {
terminal.resize(size).unwrap(); terminal.resize(size).unwrap();
term_size = size; term_size = size;
} }
draw(&mut terminal, term_size); draw(&mut terminal, term_size).unwrap();
let evt = c.unwrap(); let evt = c.unwrap();
if evt == event::Key::Char('q') { if evt == event::Key::Char('q') {
break; break;
@ -34,9 +34,8 @@ fn main() {
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, size: Rect) { fn draw(t: &mut Terminal<MouseBackend>, size: Rect) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
// Wrapping block for a group // Wrapping block for a group
// Just draw the block and the group on the same area and build the group // Just draw the block and the group on the same area and build the group
// with at least a margin of 1 // with at least a margin of 1
@ -81,6 +80,5 @@ fn draw(t: &mut Terminal<MouseBackend>, size: Rect) {
.borders(Borders::LEFT | Borders::RIGHT) .borders(Borders::LEFT | Borders::RIGHT)
.render(&mut f, chunks[1]); .render(&mut f, chunks[1]);
} }
} })
t.draw().unwrap();
} }

View file

@ -107,7 +107,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -141,15 +141,14 @@ fn main() {
app.advance(); app.advance();
} }
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
@ -201,7 +200,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.x_bounds([10.0, 110.0]) .x_bounds([10.0, 110.0])
.y_bounds([10.0, 110.0]) .y_bounds([10.0, 110.0])
.render(&mut f, chunks[1]); .render(&mut f, chunks[1]);
} })
t.draw().unwrap();
} }

View file

@ -97,7 +97,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -115,15 +115,14 @@ fn main() {
app.advance(); app.advance();
} }
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
Chart::default() Chart::default()
.block( .block(
Block::default() Block::default()
@ -164,6 +163,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.data(&app.data2), .data(&app.data2),
]) ])
.render(&mut f, app.size); .render(&mut f, app.size);
} })
t.draw().unwrap();
} }

View file

@ -34,8 +34,9 @@ fn main() {
let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap(); let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap();
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
terminal.clear().unwrap(); terminal.clear().unwrap();
Label::default() terminal
.text("Test") .draw(|mut f| {
.render(&mut terminal.get_frame(), size); Label::default().text("Test").render(&mut f, size);
terminal.draw().unwrap(); })
.unwrap();
} }

View file

@ -194,7 +194,7 @@ fn main() {
let tx = tx.clone(); let tx = tx.clone();
loop { loop {
tx.send(Event::Tick).unwrap(); tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(16)); thread::sleep(time::Duration::from_millis(250));
} }
}); });
@ -268,8 +268,7 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.constraints([Constraint::Length(3), Constraint::Min(0)].as_ref()) .constraints([Constraint::Length(3), Constraint::Min(0)].as_ref())
.split(app.size); .split(app.size);
@ -289,8 +288,7 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
} }
_ => {} _ => {}
}; };
} })
t.draw()
} }
fn draw_first_tab(f: &mut Frame<MouseBackend>, app: &App, area: Rect) { fn draw_first_tab(f: &mut Frame<MouseBackend>, app: &App, area: Rect) {

View file

@ -94,7 +94,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -112,15 +112,14 @@ fn main() {
app.advance(); app.advance();
} }
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)
@ -155,7 +154,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.percent(app.progress4) .percent(app.progress4)
.label(&format!("{}/100", app.progress2)) .label(&format!("{}/100", app.progress2))
.render(&mut f, chunks[3]); .render(&mut f, chunks[3]);
} })
t.draw().unwrap();
} }

View file

@ -61,7 +61,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -76,15 +76,14 @@ fn main() {
break; break;
}, },
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints( .constraints(
@ -104,7 +103,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.title("Block 2") .title("Block 2")
.borders(Borders::ALL) .borders(Borders::ALL)
.render(&mut f, chunks[2]); .render(&mut f, chunks[2]);
} })
t.draw().unwrap();
} }

View file

@ -117,7 +117,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -149,15 +149,14 @@ fn main() {
app.advance(); app.advance();
} }
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
@ -182,13 +181,12 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
"WARNING" => &app.warning_style, "WARNING" => &app.warning_style,
_ => &app.info_style, _ => &app.info_style,
}, },
) )
}); });
List::new(events) List::new(events)
.block(Block::default().borders(Borders::ALL).title("List")) .block(Block::default().borders(Borders::ALL).title("List"))
.start_corner(Corner::BottomLeft) .start_corner(Corner::BottomLeft)
.render(&mut f, chunks[1]); .render(&mut f, chunks[1]);
} }
} })
t.draw().unwrap();
} }

View file

@ -18,7 +18,7 @@ fn main() {
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
let mut term_size = terminal.size().unwrap(); let mut term_size = terminal.size().unwrap();
draw(&mut terminal, term_size); draw(&mut terminal, term_size).unwrap();
for c in stdin.keys() { for c in stdin.keys() {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -27,7 +27,7 @@ fn main() {
term_size = size; term_size = size;
} }
draw(&mut terminal, term_size); draw(&mut terminal, term_size).unwrap();
let evt = c.unwrap(); let evt = c.unwrap();
if evt == event::Key::Char('q') { if evt == event::Key::Char('q') {
break; break;
@ -36,9 +36,8 @@ fn main() {
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, size: Rect) { fn draw(t: &mut Terminal<MouseBackend>, size: Rect) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
Block::default() Block::default()
.style(Style::default().bg(Color::White)) .style(Style::default().bg(Color::White))
.render(&mut f, size); .render(&mut f, size);
@ -80,6 +79,5 @@ fn draw(t: &mut Terminal<MouseBackend>, size: Rect) {
.alignment(Alignment::Right) .alignment(Alignment::Right)
.wrap(true) .wrap(true)
.render(&mut f, chunks[2]); .render(&mut f, chunks[2]);
} })
t.draw().unwrap();
} }

View file

@ -94,7 +94,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -112,15 +112,14 @@ fn main() {
app.advance(); app.advance();
} }
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)
@ -161,6 +160,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.data(&app.data3) .data(&app.data3)
.style(Style::default().fg(Color::Red)) .style(Style::default().fg(Color::Red))
.render(&mut f, chunks[2]); .render(&mut f, chunks[2]);
} })
t.draw().unwrap();
} }

View file

@ -84,8 +84,7 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut frame = t.get_frame();
let selected_style = Style::default().fg(Color::Yellow).modifier(Modifier::Bold); let selected_style = Style::default().fg(Color::Yellow).modifier(Modifier::Bold);
let normal_style = Style::default().fg(Color::White); let normal_style = Style::default().fg(Color::White);
let header = ["Header1", "Header2", "Header3"]; let header = ["Header1", "Header2", "Header3"];
@ -104,7 +103,6 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
Table::new(header.into_iter(), rows) Table::new(header.into_iter(), rows)
.block(Block::default().borders(Borders::ALL).title("Table")) .block(Block::default().borders(Borders::ALL).title("Table"))
.widths(&[10, 10, 10]) .widths(&[10, 10, 10])
.render(&mut frame, rects[0]); .render(&mut f, rects[0]);
} })
t.draw()
} }

View file

@ -37,7 +37,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &mut app); draw(&mut terminal, &mut app).unwrap();
// Main loop // Main loop
let stdin = io::stdin(); let stdin = io::stdin();
@ -57,15 +57,14 @@ fn main() {
event::Key::Left => app.tabs.previous(), event::Key::Left => app.tabs.previous(),
_ => {} _ => {}
} }
draw(&mut terminal, &mut app); draw(&mut terminal, &mut app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &mut App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(5) .margin(5)
@ -109,6 +108,5 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &mut App) {
} }
_ => {} _ => {}
} }
} })
t.draw().unwrap();
} }

View file

@ -73,7 +73,7 @@ fn main() {
terminal.clear().unwrap(); terminal.clear().unwrap();
terminal.hide_cursor().unwrap(); terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap(); app.size = terminal.size().unwrap();
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
loop { loop {
let size = terminal.size().unwrap(); let size = terminal.size().unwrap();
@ -100,16 +100,15 @@ fn main() {
_ => {} _ => {}
}, },
} }
draw(&mut terminal, &app); draw(&mut terminal, &app).unwrap();
} }
terminal.show_cursor().unwrap(); terminal.show_cursor().unwrap();
terminal.clear().unwrap(); terminal.clear().unwrap();
} }
fn draw(t: &mut Terminal<AlternateScreenBackend>, app: &App) { fn draw(t: &mut Terminal<AlternateScreenBackend>, app: &App) -> Result<(), io::Error> {
{ t.draw(|mut f| {
let mut f = t.get_frame();
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)
@ -126,7 +125,5 @@ fn draw(t: &mut Terminal<AlternateScreenBackend>, app: &App) {
.map(|(i, m)| Item::Data(format!("{}: {}", i, m))), .map(|(i, m)| Item::Data(format!("{}: {}", i, m))),
).block(Block::default().borders(Borders::ALL).title("Messages")) ).block(Block::default().borders(Borders::ALL).title("Messages"))
.render(&mut f, chunks[1]); .render(&mut f, chunks[1]);
} })
t.draw().unwrap();
} }

View file

@ -84,16 +84,12 @@
//! fn draw(t: &mut Terminal<RawBackend>) -> Result<(), io::Error> { //! fn draw(t: &mut Terminal<RawBackend>) -> Result<(), io::Error> {
//! //!
//! let size = t.size()?; //! let size = t.size()?;
//! //! t.draw(|mut f| {
//! {
//! let mut f = t.get_frame();
//! Block::default() //! Block::default()
//! .title("Block") //! .title("Block")
//! .borders(Borders::ALL) //! .borders(Borders::ALL)
//! .render(&mut f, size); //! .render(&mut f, size);
//! } //! })
//!
//! t.draw()
//! } //! }
//! ``` //! ```
//! //!
@ -126,31 +122,27 @@
//! fn draw(t: &mut Terminal<RawBackend>) -> Result<(), io::Error> { //! fn draw(t: &mut Terminal<RawBackend>) -> Result<(), io::Error> {
//! //!
//! let size = t.size()?; //! let size = t.size()?;
//! //! t.draw(|mut f| {
//! { //! let chunks = Layout::default()
//! let mut f = t.get_frame(); //! .direction(Direction::Vertical)
//! let chunks = Layout::default() //! .margin(1)
//! .direction(Direction::Vertical) //! .constraints(
//! .margin(1) //! [
//! .constraints( //! Constraint::Percentage(10),
//! [ //! Constraint::Percentage(80),
//! Constraint::Percentage(10), //! Constraint::Percentage(10)
//! Constraint::Percentage(80), //! ].as_ref()
//! Constraint::Percentage(10) //! )
//! ].as_ref() //! .split(size);
//! ) //! Block::default()
//! .split(size); //! .title("Block")
//! Block::default() //! .borders(Borders::ALL)
//! .title("Block") //! .render(&mut f, chunks[0]);
//! .borders(Borders::ALL) //! Block::default()
//! .render(&mut f, chunks[0]); //! .title("Block 2")
//! Block::default() //! .borders(Borders::ALL)
//! .title("Block 2") //! .render(&mut f, chunks[2]);
//! .borders(Borders::ALL) //! })
//! .render(&mut f, chunks[2]);
//! }
//!
//! t.draw()
//! } //! }
//! ``` //! ```
//! //!

View file

@ -102,7 +102,12 @@ where
} }
/// Flushes the current internal state and prepares the interface for the next draw call /// Flushes the current internal state and prepares the interface for the next draw call
pub fn draw(&mut self) -> Result<(), io::Error> { pub fn draw<F>(&mut self, f: F) -> Result<(), io::Error>
where
F: FnOnce(Frame<B>),
{
f(self.get_frame());
// Draw to stdout // Draw to stdout
self.flush()?; self.flush()?;

View file

@ -67,7 +67,7 @@ impl<'a> Default for BarChart<'a> {
} }
impl<'a> BarChart<'a> { impl<'a> BarChart<'a> {
pub fn data(&'a mut self, data: &'a [(&'a str, u64)]) -> &mut BarChart<'a> { pub fn data(mut self, data: &'a [(&'a str, u64)]) -> BarChart<'a> {
self.data = data; self.data = data;
self.values = Vec::with_capacity(self.data.len()); self.values = Vec::with_capacity(self.data.len());
for &(_, v) in self.data { for &(_, v) in self.data {
@ -76,32 +76,32 @@ impl<'a> BarChart<'a> {
self self
} }
pub fn block(&'a mut self, block: Block<'a>) -> &mut BarChart<'a> { pub fn block(mut self, block: Block<'a>) -> BarChart<'a> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn max(&'a mut self, max: u64) -> &mut BarChart<'a> { pub fn max(mut self, max: u64) -> BarChart<'a> {
self.max = Some(max); self.max = Some(max);
self self
} }
pub fn bar_width(&'a mut self, width: u16) -> &mut BarChart<'a> { pub fn bar_width(mut self, width: u16) -> BarChart<'a> {
self.bar_width = width; self.bar_width = width;
self self
} }
pub fn bar_gap(&'a mut self, gap: u16) -> &mut BarChart<'a> { pub fn bar_gap(mut self, gap: u16) -> BarChart<'a> {
self.bar_gap = gap; self.bar_gap = gap;
self self
} }
pub fn value_style(&'a mut self, style: Style) -> &mut BarChart<'a> { pub fn value_style(mut self, style: Style) -> BarChart<'a> {
self.value_style = style; self.value_style = style;
self self
} }
pub fn label_style(&'a mut self, style: Style) -> &mut BarChart<'a> { pub fn label_style(mut self, style: Style) -> BarChart<'a> {
self.label_style = style; self.label_style = style;
self self
} }
pub fn style(&'a mut self, style: Style) -> &mut BarChart<'a> { pub fn style(mut self, style: Style) -> BarChart<'a> {
self.style = style; self.style = style;
self self
} }

View file

@ -194,26 +194,26 @@ impl<'a, F> Canvas<'a, F>
where where
F: Fn(&mut Context), F: Fn(&mut Context),
{ {
pub fn block(&mut self, block: Block<'a>) -> &mut Canvas<'a, F> { pub fn block(mut self, block: Block<'a>) -> Canvas<'a, F> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn x_bounds(&mut self, bounds: [f64; 2]) -> &mut Canvas<'a, F> { pub fn x_bounds(mut self, bounds: [f64; 2]) -> Canvas<'a, F> {
self.x_bounds = bounds; self.x_bounds = bounds;
self self
} }
pub fn y_bounds(&mut self, bounds: [f64; 2]) -> &mut Canvas<'a, F> { pub fn y_bounds(mut self, bounds: [f64; 2]) -> Canvas<'a, F> {
self.y_bounds = bounds; self.y_bounds = bounds;
self self
} }
/// Store the closure that will be used to draw to the Canvas /// Store the closure that will be used to draw to the Canvas
pub fn paint(&mut self, f: F) -> &mut Canvas<'a, F> { pub fn paint(mut self, f: F) -> Canvas<'a, F> {
self.painter = Some(f); self.painter = Some(f);
self self
} }
pub fn background_color(&'a mut self, color: Color) -> &mut Canvas<'a, F> { pub fn background_color(mut self, color: Color) -> Canvas<'a, F> {
self.background_color = color; self.background_color = color;
self self
} }

View file

@ -195,6 +195,7 @@ impl Default for ChartLayout {
/// .style(Style::default().fg(Color::Magenta)) /// .style(Style::default().fg(Color::Magenta))
/// .data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)])]); /// .data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)])]);
/// # } /// # }
/// ```
pub struct Chart<'a, LX, LY> pub struct Chart<'a, LX, LY>
where where
LX: AsRef<str> + 'a, LX: AsRef<str> + 'a,
@ -233,27 +234,27 @@ where
LX: AsRef<str>, LX: AsRef<str>,
LY: AsRef<str>, LY: AsRef<str>,
{ {
pub fn block(&'a mut self, block: Block<'a>) -> &mut Chart<'a, LX, LY> { pub fn block(mut self, block: Block<'a>) -> Chart<'a, LX, LY> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn style(&mut self, style: Style) -> &mut Chart<'a, LX, LY> { pub fn style(mut self, style: Style) -> Chart<'a, LX, LY> {
self.style = style; self.style = style;
self self
} }
pub fn x_axis(&mut self, axis: Axis<'a, LX>) -> &mut Chart<'a, LX, LY> { pub fn x_axis(mut self, axis: Axis<'a, LX>) -> Chart<'a, LX, LY> {
self.x_axis = axis; self.x_axis = axis;
self self
} }
pub fn y_axis(&mut self, axis: Axis<'a, LY>) -> &mut Chart<'a, LX, LY> { pub fn y_axis(mut self, axis: Axis<'a, LY>) -> Chart<'a, LX, LY> {
self.y_axis = axis; self.y_axis = axis;
self self
} }
pub fn datasets(&mut self, datasets: &'a [Dataset<'a>]) -> &mut Chart<'a, LX, LY> { pub fn datasets(mut self, datasets: &'a [Dataset<'a>]) -> Chart<'a, LX, LY> {
self.datasets = datasets; self.datasets = datasets;
self self
} }

View file

@ -39,22 +39,22 @@ impl<'a> Default for Gauge<'a> {
} }
impl<'a> Gauge<'a> { impl<'a> Gauge<'a> {
pub fn block(&mut self, block: Block<'a>) -> &mut Gauge<'a> { pub fn block(mut self, block: Block<'a>) -> Gauge<'a> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn percent(&mut self, percent: u16) -> &mut Gauge<'a> { pub fn percent(mut self, percent: u16) -> Gauge<'a> {
self.percent = percent; self.percent = percent;
self self
} }
pub fn label(&mut self, string: &'a str) -> &mut Gauge<'a> { pub fn label(mut self, string: &'a str) -> Gauge<'a> {
self.label = Some(string); self.label = Some(string);
self self
} }
pub fn style(&mut self, style: Style) -> &mut Gauge<'a> { pub fn style(mut self, style: Style) -> Gauge<'a> {
self.style = style; self.style = style;
self self
} }

View file

@ -51,12 +51,12 @@ where
} }
} }
pub fn block(&'b mut self, block: Block<'b>) -> &mut List<'b, 'i, L, D> { pub fn block(mut self, block: Block<'b>) -> List<'b, 'i, L, D> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn items<I>(&'b mut self, items: I) -> &mut List<'b, 'i, L, D> pub fn items<I>(mut self, items: I) -> List<'b, 'i, L, D>
where where
I: IntoIterator<Item = Item<'i, D>, IntoIter = L>, I: IntoIterator<Item = Item<'i, D>, IntoIter = L>,
{ {
@ -64,12 +64,12 @@ where
self self
} }
pub fn style(&'b mut self, style: Style) -> &mut List<'b, 'i, L, D> { pub fn style(mut self, style: Style) -> List<'b, 'i, L, D> {
self.style = style; self.style = style;
self self
} }
pub fn start_corner(&'b mut self, corner: Corner) -> &mut List<'b, 'i, L, D> { pub fn start_corner(mut self, corner: Corner) -> List<'b, 'i, L, D> {
self.start_corner = corner; self.start_corner = corner;
self self
} }
@ -171,12 +171,12 @@ impl<'b> Default for SelectableList<'b> {
} }
impl<'b> SelectableList<'b> { impl<'b> SelectableList<'b> {
pub fn block(&'b mut self, block: Block<'b>) -> &mut SelectableList<'b> { pub fn block(mut self, block: Block<'b>) -> SelectableList<'b> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn items<I>(&'b mut self, items: &'b [I]) -> &mut SelectableList<'b> pub fn items<I>(mut self, items: &'b [I]) -> SelectableList<'b>
where where
I: AsRef<str> + 'b, I: AsRef<str> + 'b,
{ {
@ -184,22 +184,22 @@ impl<'b> SelectableList<'b> {
self self
} }
pub fn style(&'b mut self, style: Style) -> &mut SelectableList<'b> { pub fn style(mut self, style: Style) -> SelectableList<'b> {
self.style = style; self.style = style;
self self
} }
pub fn highlight_symbol(&'b mut self, highlight_symbol: &'b str) -> &mut SelectableList<'b> { pub fn highlight_symbol(mut self, highlight_symbol: &'b str) -> SelectableList<'b> {
self.highlight_symbol = Some(highlight_symbol); self.highlight_symbol = Some(highlight_symbol);
self self
} }
pub fn highlight_style(&'b mut self, highlight_style: Style) -> &mut SelectableList<'b> { pub fn highlight_style(mut self, highlight_style: Style) -> SelectableList<'b> {
self.highlight_style = highlight_style; self.highlight_style = highlight_style;
self self
} }
pub fn select(&'b mut self, index: usize) -> &'b mut SelectableList<'b> { pub fn select(mut self, index: usize) -> SelectableList<'b> {
self.selected = Some(index); self.selected = Some(index);
self self
} }

View file

@ -70,32 +70,32 @@ where
} }
} }
pub fn block(&'a mut self, block: Block<'a>) -> &mut Paragraph<'a, 't, T> { pub fn block(mut self, block: Block<'a>) -> Paragraph<'a, 't, T> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn style(&mut self, style: Style) -> &mut Paragraph<'a, 't, T> { pub fn style(mut self, style: Style) -> Paragraph<'a, 't, T> {
self.style = style; self.style = style;
self self
} }
pub fn wrap(&mut self, flag: bool) -> &mut Paragraph<'a, 't, T> { pub fn wrap(mut self, flag: bool) -> Paragraph<'a, 't, T> {
self.wrapping = flag; self.wrapping = flag;
self self
} }
pub fn raw(&mut self, flag: bool) -> &mut Paragraph<'a, 't, T> { pub fn raw(mut self, flag: bool) -> Paragraph<'a, 't, T> {
self.raw = flag; self.raw = flag;
self self
} }
pub fn scroll(&mut self, offset: u16) -> &mut Paragraph<'a, 't, T> { pub fn scroll(mut self, offset: u16) -> Paragraph<'a, 't, T> {
self.scroll = offset; self.scroll = offset;
self self
} }
pub fn alignment(&mut self, alignment: Alignment) -> &mut Paragraph<'a, 't, T> { pub fn alignment(mut self, alignment: Alignment) -> Paragraph<'a, 't, T> {
self.alignment = alignment; self.alignment = alignment;
self self
} }

View file

@ -46,22 +46,22 @@ impl<'a> Default for Sparkline<'a> {
} }
impl<'a> Sparkline<'a> { impl<'a> Sparkline<'a> {
pub fn block(&mut self, block: Block<'a>) -> &mut Sparkline<'a> { pub fn block(mut self, block: Block<'a>) -> Sparkline<'a> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn style(&mut self, style: Style) -> &mut Sparkline<'a> { pub fn style(mut self, style: Style) -> Sparkline<'a> {
self.style = style; self.style = style;
self self
} }
pub fn data(&mut self, data: &'a [u64]) -> &mut Sparkline<'a> { pub fn data(mut self, data: &'a [u64]) -> Sparkline<'a> {
self.data = data; self.data = data;
self self
} }
pub fn max(&mut self, max: u64) -> &mut Sparkline<'a> { pub fn max(mut self, max: u64) -> Sparkline<'a> {
self.max = Some(max); self.max = Some(max);
self self
} }

View file

@ -106,12 +106,12 @@ where
column_spacing: 1, column_spacing: 1,
} }
} }
pub fn block(&'a mut self, block: Block<'a>) -> &mut Table<'a, 'i, T, H, I, D, R> { pub fn block(mut self, block: Block<'a>) -> Table<'a, 'i, T, H, I, D, R> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn header<II>(&mut self, header: II) -> &mut Table<'a, 'i, T, H, I, D, R> pub fn header<II>(mut self, header: II) -> Table<'a, 'i, T, H, I, D, R>
where where
II: IntoIterator<Item = T, IntoIter = H>, II: IntoIterator<Item = T, IntoIter = H>,
{ {
@ -119,17 +119,17 @@ where
self self
} }
pub fn header_style(&mut self, style: Style) -> &mut Table<'a, 'i, T, H, I, D, R> { pub fn header_style(mut self, style: Style) -> Table<'a, 'i, T, H, I, D, R> {
self.header_style = style; self.header_style = style;
self self
} }
pub fn widths(&mut self, widths: &'a [u16]) -> &mut Table<'a, 'i, T, H, I, D, R> { pub fn widths(mut self, widths: &'a [u16]) -> Table<'a, 'i, T, H, I, D, R> {
self.widths = widths; self.widths = widths;
self self
} }
pub fn rows<II>(&mut self, rows: II) -> &mut Table<'a, 'i, T, H, I, D, R> pub fn rows<II>(mut self, rows: II) -> Table<'a, 'i, T, H, I, D, R>
where where
II: IntoIterator<Item = Row<'i, D, I>, IntoIter = R>, II: IntoIterator<Item = Row<'i, D, I>, IntoIter = R>,
{ {
@ -137,12 +137,12 @@ where
self self
} }
pub fn style(&mut self, style: Style) -> &mut Table<'a, 'i, T, H, I, D, R> { pub fn style(mut self, style: Style) -> Table<'a, 'i, T, H, I, D, R> {
self.style = style; self.style = style;
self self
} }
pub fn column_spacing(&mut self, spacing: u16) -> &mut Table<'a, 'i, T, H, I, D, R> { pub fn column_spacing(mut self, spacing: u16) -> Table<'a, 'i, T, H, I, D, R> {
self.column_spacing = spacing; self.column_spacing = spacing;
self self
} }

View file

@ -57,27 +57,27 @@ impl<'a, T> Tabs<'a, T>
where where
T: AsRef<str>, T: AsRef<str>,
{ {
pub fn block(&mut self, block: Block<'a>) -> &mut Tabs<'a, T> { pub fn block(mut self, block: Block<'a>) -> Tabs<'a, T> {
self.block = Some(block); self.block = Some(block);
self self
} }
pub fn titles(&mut self, titles: &'a [T]) -> &mut Tabs<'a, T> { pub fn titles(mut self, titles: &'a [T]) -> Tabs<'a, T> {
self.titles = titles; self.titles = titles;
self self
} }
pub fn select(&mut self, selected: usize) -> &mut Tabs<'a, T> { pub fn select(mut self, selected: usize) -> Tabs<'a, T> {
self.selected = selected; self.selected = selected;
self self
} }
pub fn style(&mut self, style: Style) -> &mut Tabs<'a, T> { pub fn style(mut self, style: Style) -> Tabs<'a, T> {
self.style = style; self.style = style;
self self
} }
pub fn highlight_style(&mut self, style: Style) -> &mut Tabs<'a, T> { pub fn highlight_style(mut self, style: Style) -> Tabs<'a, T> {
self.highlight_style = style; self.highlight_style = style;
self self
} }