[examples] Run cargo fmt

This commit is contained in:
Florian Dehau 2017-10-30 22:28:37 +01:00
parent 1f285fbac0
commit f24517bc5a
13 changed files with 225 additions and 258 deletions

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
use std::thread; use std::thread;
@ -11,9 +11,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, BarChart}; use tui::widgets::{border, BarChart, Block, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
struct App<'a> { struct App<'a> {
size: Rect, size: Rect,
@ -110,11 +110,9 @@ fn main() {
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => if input == event::Key::Char('q') {
if input == event::Key::Char('q') { break;
break; },
}
}
Event::Tick => { Event::Tick => {
app.advance(); app.advance();
} }
@ -126,7 +124,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
use termion::event; use termion::event;
@ -7,9 +7,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border}; use tui::widgets::{border, Block, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
fn main() { fn main() {
let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap(); let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap();
@ -35,7 +35,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) { fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) {
// 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
@ -56,9 +55,12 @@ fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) {
.render(t, &chunks[0]); .render(t, &chunks[0]);
Block::default() Block::default()
.title("Styled title") .title("Styled title")
.title_style(Style::default().fg(Color::White).bg(Color::Red).modifier( .title_style(
Modifier::Bold, Style::default()
)) .fg(Color::White)
.bg(Color::Red)
.modifier(Modifier::Bold),
)
.render(t, &chunks[1]); .render(t, &chunks[1]);
}); });
Group::default() Group::default()

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
use std::thread; use std::thread;
@ -11,9 +11,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border}; use tui::widgets::{border, Block, Widget};
use tui::widgets::canvas::{Canvas, Map, MapResolution, Line}; use tui::widgets::canvas::{Canvas, Line, Map, MapResolution};
use tui::layout::{Group, Rect, Direction, Size}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::Color; use tui::style::Color;
struct App { struct App {
@ -45,13 +45,11 @@ impl App {
} }
fn advance(&mut self) { fn advance(&mut self) {
if self.ball.left() < self.playground.left() || if self.ball.left() < self.playground.left() || self.ball.right() > self.playground.right()
self.ball.right() > self.playground.right()
{ {
self.dir_x = !self.dir_x; self.dir_x = !self.dir_x;
} }
if self.ball.top() < self.playground.top() || if self.ball.top() < self.playground.top() || self.ball.bottom() > self.playground.bottom()
self.ball.bottom() > self.playground.bottom()
{ {
self.dir_y = !self.dir_y; self.dir_y = !self.dir_y;
} }
@ -121,27 +119,25 @@ fn main() {
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => match input {
match input { event::Key::Char('q') => {
event::Key::Char('q') => { break;
break;
}
event::Key::Down => {
app.y += 1.0;
}
event::Key::Up => {
app.y -= 1.0;
}
event::Key::Right => {
app.x += 1.0;
}
event::Key::Left => {
app.x -= 1.0;
}
_ => {}
} }
} event::Key::Down => {
app.y += 1.0;
}
event::Key::Up => {
app.y -= 1.0;
}
event::Key::Right => {
app.x += 1.0;
}
event::Key::Left => {
app.x -= 1.0;
}
_ => {}
},
Event::Tick => { Event::Tick => {
app.advance(); app.advance();
} }
@ -153,8 +149,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)]) .sizes(&[Size::Percent(50), Size::Percent(50)])

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
mod util; mod util;
use util::*; use util::*;
@ -14,9 +14,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, Chart, Axis, Marker, Dataset}; use tui::widgets::{border, Axis, Block, Chart, Dataset, Marker, Widget};
use tui::layout::Rect; use tui::layout::Rect;
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
struct App { struct App {
size: Rect, size: Rect,
@ -108,11 +108,9 @@ fn main() {
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => if input == event::Key::Char('q') {
if input == event::Key::Char('q') { break;
break; },
}
}
Event::Tick => { Event::Tick => {
app.advance(); app.advance();
} }
@ -124,7 +122,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Chart::default() Chart::default()
.block( .block(
Block::default() Block::default()
@ -138,13 +135,11 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.style(Style::default().fg(Color::Gray)) .style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic)) .labels_style(Style::default().modifier(Modifier::Italic))
.bounds(app.window) .bounds(app.window)
.labels( .labels(&[
&[ &format!("{}", app.window[0]),
&format!("{}", app.window[0]), &format!("{}", (app.window[0] + app.window[1]) / 2.0),
&format!("{}", (app.window[0] + app.window[1]) / 2.0), &format!("{}", app.window[1]),
&format!("{}", app.window[1]), ]),
],
),
) )
.y_axis( .y_axis(
Axis::default() Axis::default()
@ -154,20 +149,18 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
.bounds([-20.0, 20.0]) .bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"]), .labels(&["-20", "0", "20"]),
) )
.datasets( .datasets(&[
&[ Dataset::default()
Dataset::default() .name("data2")
.name("data2") .marker(Marker::Dot)
.marker(Marker::Dot) .style(Style::default().fg(Color::Cyan))
.style(Style::default().fg(Color::Cyan)) .data(&app.data1),
.data(&app.data1), Dataset::default()
Dataset::default() .name("data3")
.name("data3") .marker(Marker::Braille)
.marker(Marker::Braille) .style(Style::default().fg(Color::Yellow))
.style(Style::default().fg(Color::Yellow)) .data(&app.data2),
.data(&app.data2), ])
],
)
.render(t, &app.size); .render(t, &app.size);
t.draw().unwrap(); t.draw().unwrap();

View File

@ -1,8 +1,8 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
mod util; mod util;
@ -17,11 +17,11 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, SelectableList, List, Item, Gauge, Sparkline, Paragraph, border, use tui::widgets::{border, Axis, BarChart, Block, Chart, Dataset, Gauge, Item, List, Marker,
Chart, Axis, Dataset, BarChart, Marker, Tabs, Table, Row}; Paragraph, Row, SelectableList, Sparkline, Table, Tabs, Widget};
use tui::widgets::canvas::{Canvas, Map, MapResolution, Line}; use tui::widgets::canvas::{Canvas, Line, Map, MapResolution};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
use util::*; use util::*;
@ -57,8 +57,6 @@ enum Event {
} }
fn main() { fn main() {
for argument in env::args() { for argument in env::args() {
if argument == "--log" { if argument == "--log" {
setup_log("demo.log"); setup_log("demo.log");
@ -236,33 +234,29 @@ fn main() {
draw(&mut terminal, &app).unwrap(); draw(&mut terminal, &app).unwrap();
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => match input {
match input { event::Key::Char('q') => {
event::Key::Char('q') => { break;
break;
}
event::Key::Up => {
if app.selected > 0 {
app.selected -= 1
};
}
event::Key::Down => {
if app.selected < app.items.len() - 1 {
app.selected += 1;
}
}
event::Key::Left => {
app.tabs.previous();
}
event::Key::Right => {
app.tabs.next();
}
event::Key::Char('t') => {
app.show_chart = !app.show_chart;
}
_ => {}
} }
} event::Key::Up => {
if app.selected > 0 {
app.selected -= 1
};
}
event::Key::Down => if app.selected < app.items.len() - 1 {
app.selected += 1;
},
event::Key::Left => {
app.tabs.previous();
}
event::Key::Right => {
app.tabs.next();
}
event::Key::Char('t') => {
app.show_chart = !app.show_chart;
}
_ => {}
},
Event::Tick => { Event::Tick => {
app.progress += 5; app.progress += 5;
if app.progress > 100 { if app.progress > 100 {
@ -296,7 +290,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> { fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
Group::default() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.sizes(&[Size::Fixed(3), Size::Min(0)]) .sizes(&[Size::Fixed(3), Size::Min(0)])
@ -334,7 +327,6 @@ fn draw_first_tab(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
} }
fn draw_gauges(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) { fn draw_gauges(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
Block::default() Block::default()
.borders(border::ALL) .borders(border::ALL)
.title("Graphs") .title("Graphs")
@ -438,13 +430,11 @@ fn draw_charts(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
.style(Style::default().fg(Color::Gray)) .style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic)) .labels_style(Style::default().modifier(Modifier::Italic))
.bounds(app.window) .bounds(app.window)
.labels( .labels(&[
&[ &format!("{}", app.window[0]),
&format!("{}", app.window[0]), &format!("{}", (app.window[0] + app.window[1]) / 2.0),
&format!("{}", (app.window[0] + app.window[1]) / 2.0), &format!("{}", app.window[1]),
&format!("{}", app.window[1]), ]),
],
),
) )
.y_axis( .y_axis(
Axis::default() Axis::default()
@ -454,20 +444,18 @@ fn draw_charts(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
.bounds([-20.0, 20.0]) .bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"]), .labels(&["-20", "0", "20"]),
) )
.datasets( .datasets(&[
&[ Dataset::default()
Dataset::default() .name("data2")
.name("data2") .marker(Marker::Dot)
.marker(Marker::Dot) .style(Style::default().fg(Color::Cyan))
.style(Style::default().fg(Color::Cyan)) .data(&app.data2),
.data(&app.data2), Dataset::default()
Dataset::default() .name("data3")
.name("data3") .marker(Marker::Braille)
.marker(Marker::Braille) .style(Style::default().fg(Color::Yellow))
.style(Style::default().fg(Color::Yellow)) .data(&app.data3),
.data(&app.data3), ])
],
)
.render(t, &chunks[1]); .render(t, &chunks[1]);
} }
}); });
@ -475,20 +463,24 @@ fn draw_charts(t: &mut Terminal<MouseBackend>, app: &App, area: &Rect) {
fn draw_text(t: &mut Terminal<MouseBackend>, area: &Rect) { fn draw_text(t: &mut Terminal<MouseBackend>, area: &Rect) {
Paragraph::default() Paragraph::default()
.block(Block::default() .block(
.borders(border::ALL) Block::default()
.title("Footer") .borders(border::ALL)
.title_style(Style::default().fg(Color::Magenta).modifier(Modifier::Bold))) .title("Footer")
.title_style(Style::default().fg(Color::Magenta).modifier(Modifier::Bold)),
)
.wrap(true) .wrap(true)
.text("This is a paragraph with several lines.\nYou can change the color.\nUse \ .text(
\\{fg=[color];bg=[color];mod=[modifier] [text]} to highlight the text with a color. \ "This is a paragraph with several lines.\nYou can change the color.\nUse \
For example, {fg=red u}{fg=green n}{fg=yellow d}{fg=magenta e}{fg=cyan r} \ \\{fg=[color];bg=[color];mod=[modifier] [text]} to highlight the text with a color. \
{fg=gray t}{fg=light_gray h}{fg=light_red e} {fg=light_green r}{fg=light_yellow a} \ For example, {fg=red u}{fg=green n}{fg=yellow d}{fg=magenta e}{fg=cyan r} \
{fg=light_magenta i}{fg=light_cyan n}{fg=white b}{fg=red o}{fg=green w}.\n\ {fg=gray t}{fg=light_gray h}{fg=light_red e} {fg=light_green r}{fg=light_yellow a} \
Oh, and if you didn't {mod=italic notice} you can {mod=bold automatically} \ {fg=light_magenta i}{fg=light_cyan n}{fg=white b}{fg=red o}{fg=green w}.\n\
{mod=invert wrap} your {mod=underline text} =).\nOne more thing is that \ Oh, and if you didn't {mod=italic notice} you can {mod=bold automatically} \
it should display unicode characters properly: , ٩(-̮̮̃-̃)۶ ٩(̮̮̃̃)۶ ٩(̯͡͡)۶ \ {mod=invert wrap} your {mod=underline text} =).\nOne more thing is that \
٩(-̮̮̃̃).") it should display unicode characters properly: , ٩(-̮̮̃-̃)۶ ٩(̮̮̃̃)۶ ٩(̯͡͡)۶ \
٩(-̮̮̃̃).",
)
.render(t, area); .render(t, area);
} }

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
use std::thread; use std::thread;
@ -11,9 +11,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, Gauge}; use tui::widgets::{border, Block, Gauge, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
struct App { struct App {
size: Rect, size: Rect,
@ -105,11 +105,9 @@ fn main() {
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => if input == event::Key::Char('q') {
if input == event::Key::Char('q') { break;
break; },
}
}
Event::Tick => { Event::Tick => {
app.advance(); app.advance();
} }
@ -121,18 +119,15 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)
.sizes( .sizes(&[
&[ Size::Percent(25),
Size::Percent(25), Size::Percent(25),
Size::Percent(25), Size::Percent(25),
Size::Percent(25), Size::Percent(25),
Size::Percent(25), ])
],
)
.render(t, &app.size, |t, chunks| { .render(t, &app.size, |t, chunks| {
Gauge::default() Gauge::default()
.block(Block::default().title("Gauge1").borders(border::ALL)) .block(Block::default().title("Gauge1").borders(border::ALL))

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
use std::thread; use std::thread;
@ -11,9 +11,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, SelectableList, List, Item}; use tui::widgets::{border, Block, Item, List, SelectableList, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
struct App<'a> { struct App<'a> {
size: Rect, size: Rect,
@ -149,27 +149,23 @@ fn main() {
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => match input {
match input { event::Key::Char('q') => {
event::Key::Char('q') => { break;
break;
}
event::Key::Down => {
app.selected += 1;
if app.selected > app.items.len() - 1 {
app.selected = 0;
}
}
event::Key::Up => {
if app.selected > 0 {
app.selected -= 1;
} else {
app.selected = app.items.len() - 1;
}
}
_ => {}
} }
} event::Key::Down => {
app.selected += 1;
if app.selected > app.items.len() - 1 {
app.selected = 0;
}
}
event::Key::Up => if app.selected > 0 {
app.selected -= 1;
} else {
app.selected = app.items.len() - 1;
},
_ => {}
},
Event::Tick => { Event::Tick => {
app.advance(); app.advance();
} }
@ -181,7 +177,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)]) .sizes(&[Size::Percent(50), Size::Percent(50)])

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
use termion::event; use termion::event;
@ -7,9 +7,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, Paragraph}; use tui::widgets::{Block, Paragraph, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color}; use tui::style::{Color, Style};
fn main() { fn main() {
let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap(); let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap();
@ -37,7 +37,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) { fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) {
Block::default() Block::default()
.style(Style::default().bg(Color::White)) .style(Style::default().bg(Color::White))
.render(t, size); .render(t, size);
@ -54,10 +53,10 @@ fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) {
Paragraph::default() Paragraph::default()
.text( .text(
"This is a line\n{fg=red This is a line}\n{bg=red This is a \ "This is a line\n{fg=red This is a line}\n{bg=red This is a \
line}\n{mod=italic This is a line}\n{mod=bold This is a \ line}\n{mod=italic This is a line}\n{mod=bold This is a \
line}\n{mod=crossed_out This is a line}\n{mod=invert This is a \ line}\n{mod=crossed_out This is a line}\n{mod=invert This is a \
line}\n{mod=underline This is a \ line}\n{mod=underline This is a \
line}\n{bg=green;fg=yellow;mod=italic This is a line}\n", line}\n{bg=green;fg=yellow;mod=italic This is a line}\n",
) )
.render(t, &chunks[0]); .render(t, &chunks[0]);
}); });

View File

@ -1,14 +1,14 @@
extern crate tui;
extern crate rustbox; extern crate rustbox;
extern crate tui;
use std::error::Error; use std::error::Error;
use rustbox::Key; use rustbox::Key;
use tui::Terminal; use tui::Terminal;
use tui::backend::RustboxBackend; use tui::backend::RustboxBackend;
use tui::widgets::{Widget, Block, border, Paragraph}; use tui::widgets::{border, Block, Paragraph, Widget};
use tui::layout::{Group, Direction, Size}; use tui::layout::{Direction, Group, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
fn main() { fn main() {
let mut terminal = Terminal::new(RustboxBackend::new().unwrap()).unwrap(); let mut terminal = Terminal::new(RustboxBackend::new().unwrap()).unwrap();
@ -17,11 +17,9 @@ fn main() {
draw(&mut terminal); draw(&mut terminal);
loop { loop {
match terminal.backend().rustbox().poll_event(false) { match terminal.backend().rustbox().poll_event(false) {
Ok(rustbox::Event::KeyEvent(key)) => { Ok(rustbox::Event::KeyEvent(key)) => if key == Key::Char('q') {
if key == Key::Char('q') { break;
break; },
}
}
Err(e) => panic!("{}", e.description()), Err(e) => panic!("{}", e.description()),
_ => {} _ => {}
}; };
@ -31,7 +29,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<RustboxBackend>) { fn draw(t: &mut Terminal<RustboxBackend>) {
let size = t.size().unwrap(); let size = t.size().unwrap();
Group::default() Group::default()

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
mod util; mod util;
use util::*; use util::*;
@ -14,9 +14,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, Sparkline}; use tui::widgets::{border, Block, Sparkline, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color}; use tui::style::{Color, Style};
struct App { struct App {
size: Rect, size: Rect,
@ -105,11 +105,9 @@ fn main() {
let evt = rx.recv().unwrap(); let evt = rx.recv().unwrap();
match evt { match evt {
Event::Input(input) => { Event::Input(input) => if input == event::Key::Char('q') {
if input == event::Key::Char('q') { break;
break; },
}
}
Event::Tick => { Event::Tick => {
app.advance(); app.advance();
} }
@ -121,33 +119,41 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .margin(2)
.sizes( .sizes(&[
&[Size::Fixed(3), Size::Fixed(3), Size::Fixed(7), Size::Min(0)], Size::Fixed(3),
) Size::Fixed(3),
Size::Fixed(7),
Size::Min(0),
])
.render(t, &app.size, |t, chunks| { .render(t, &app.size, |t, chunks| {
Sparkline::default() Sparkline::default()
.block(Block::default().title("Data1").borders( .block(
border::LEFT | border::RIGHT, Block::default()
)) .title("Data1")
.borders(border::LEFT | border::RIGHT),
)
.data(&app.data1) .data(&app.data1)
.style(Style::default().fg(Color::Yellow)) .style(Style::default().fg(Color::Yellow))
.render(t, &chunks[0]); .render(t, &chunks[0]);
Sparkline::default() Sparkline::default()
.block(Block::default().title("Data2").borders( .block(
border::LEFT | border::RIGHT, Block::default()
)) .title("Data2")
.borders(border::LEFT | border::RIGHT),
)
.data(&app.data2) .data(&app.data2)
.style(Style::default().bg(Color::Green)) .style(Style::default().bg(Color::Green))
.render(t, &chunks[1]); .render(t, &chunks[1]);
// Multiline // Multiline
Sparkline::default() Sparkline::default()
.block(Block::default().title("Data3").borders( .block(
border::LEFT | border::RIGHT, Block::default()
)) .title("Data3")
.borders(border::LEFT | border::RIGHT),
)
.data(&app.data3) .data(&app.data3)
.style(Style::default().fg(Color::Red)) .style(Style::default().fg(Color::Red))
.render(t, &chunks[2]); .render(t, &chunks[2]);

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
use std::io; use std::io;
@ -8,9 +8,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, Table, Row}; use tui::widgets::{border, Block, Row, Table, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color, Modifier}; use tui::style::{Color, Modifier, Style};
struct App<'a> { struct App<'a> {
size: Rect, size: Rect,
@ -70,13 +70,11 @@ fn main() {
app.selected = 0; app.selected = 0;
} }
} }
event::Key::Up => { event::Key::Up => if app.selected > 0 {
if app.selected > 0 { app.selected -= 1;
app.selected -= 1; } else {
} else { app.selected = app.items.len() - 1;
app.selected = app.items.len() - 1; },
}
}
_ => {} _ => {}
}; };
draw(&mut terminal, &app); draw(&mut terminal, &app);
@ -87,7 +85,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &App) { fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.sizes(&[Size::Percent(100)]) .sizes(&[Size::Percent(100)])
@ -97,13 +94,14 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
let normal_style = Style::default().fg(Color::White); let normal_style = Style::default().fg(Color::White);
Table::new( Table::new(
["Header1", "Header2", "Header3"].into_iter(), ["Header1", "Header2", "Header3"].into_iter(),
app.items.iter().enumerate().map(|(i, item)| if i == app.items
app.selected .iter()
{ .enumerate()
Row::StyledData(item.into_iter(), &selected_style) .map(|(i, item)| if i == app.selected {
} else { Row::StyledData(item.into_iter(), &selected_style)
Row::StyledData(item.into_iter(), &normal_style) } else {
}), Row::StyledData(item.into_iter(), &normal_style)
}),
).block(Block::default().borders(border::ALL).title("Table")) ).block(Block::default().borders(border::ALL).title("Table"))
.widths(&[10, 10, 10]) .widths(&[10, 10, 10])
.render(t, &chunks[0]); .render(t, &chunks[0]);

View File

@ -1,5 +1,5 @@
extern crate tui;
extern crate termion; extern crate termion;
extern crate tui;
mod util; mod util;
use util::*; use util::*;
@ -10,9 +10,9 @@ use termion::input::TermRead;
use tui::Terminal; use tui::Terminal;
use tui::backend::MouseBackend; use tui::backend::MouseBackend;
use tui::widgets::{Widget, Block, border, Tabs}; use tui::widgets::{border, Block, Tabs, Widget};
use tui::layout::{Group, Direction, Size, Rect}; use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Style, Color}; use tui::style::{Color, Style};
struct App<'a> { struct App<'a> {
size: Rect, size: Rect,
@ -64,7 +64,6 @@ fn main() {
} }
fn draw(t: &mut Terminal<MouseBackend>, app: &mut App) { fn draw(t: &mut Terminal<MouseBackend>, app: &mut App) {
Block::default() Block::default()
.style(Style::default().bg(Color::White)) .style(Style::default().bg(Color::White))
.render(t, &app.size); .render(t, &app.size);

View File

@ -1,8 +1,8 @@
#![allow(dead_code)] #![allow(dead_code)]
extern crate rand;
extern crate log4rs; extern crate log4rs;
extern crate log; extern crate log;
extern crate rand;
use self::rand::distributions::{IndependentSample, Range}; use self::rand::distributions::{IndependentSample, Range};
@ -15,7 +15,7 @@ pub fn setup_log(file_name: &str) {
let log = FileAppender::builder() let log = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new( .encoder(Box::new(PatternEncoder::new(
"{l} / {d(%H:%M:%S)} / \ "{l} / {d(%H:%M:%S)} / \
{M}:{L}{n}{m}{n}{n}", {M}:{L}{n}{m}{n}{n}",
))) )))
.build(file_name) .build(file_name)
.unwrap(); .unwrap();