Fix rustfmt and clippy errors

pull/7/head
Florian Dehau 7 years ago
parent 29db3dd722
commit b2bb24b9d2

@ -85,12 +85,10 @@ fn main() {
});
// Tick
thread::spawn(move || {
loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
}
});
thread::spawn(move || loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
// App
let mut app = App::new();

@ -39,14 +39,12 @@ fn draw(t: &mut Terminal<TermionBackend>, size: &Rect) {
// Wrapping block for a group
// Just draw the block and the group on the same area and build the group
// with at least a margin of 1
Block::default()
.borders(border::ALL)
.render(t, &size);
Block::default().borders(border::ALL).render(t, size);
Group::default()
.direction(Direction::Vertical)
.margin(4)
.sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &size, |t, chunks| {
.render(t, size, |t, chunks| {
Group::default()
.direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)])
@ -59,9 +57,9 @@ fn draw(t: &mut Terminal<TermionBackend>, size: &Rect) {
Block::default()
.title("Styled title")
.title_style(Style::default()
.fg(Color::White)
.bg(Color::Red)
.modifier(Modifier::Bold))
.fg(Color::White)
.bg(Color::Red)
.modifier(Modifier::Bold))
.render(t, &chunks[1]);
});
Group::default()

@ -46,13 +46,13 @@ impl App {
fn advance(&mut self) {
if self.ball.left() < self.playground.left() ||
self.ball.right() > self.playground.right() {
self.dir_x = !self.dir_x;
}
self.ball.right() > self.playground.right() {
self.dir_x = !self.dir_x;
}
if self.ball.top() < self.playground.top() ||
self.ball.bottom() > self.playground.bottom() {
self.dir_y = !self.dir_y;
}
self.ball.bottom() > self.playground.bottom() {
self.dir_y = !self.dir_y;
}
if self.dir_x {
self.ball.x += self.vx;
@ -96,12 +96,10 @@ fn main() {
});
// Tick
thread::spawn(move || {
loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
}
});
thread::spawn(move || loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
// App
let mut app = App::new();
@ -160,52 +158,48 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
.sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &app.size, |t, chunks| {
Canvas::default()
.block(Block::default()
.borders(border::ALL)
.title("World"))
.block(Block::default().borders(border::ALL).title("World"))
.paint(|ctx| {
ctx.draw(&Map {
color: Color::White,
resolution: MapResolution::High,
});
ctx.print(app.x, -app.y, "You are here", Color::Yellow);
})
ctx.draw(&Map {
color: Color::White,
resolution: MapResolution::High,
});
ctx.print(app.x, -app.y, "You are here", Color::Yellow);
})
.x_bounds([-180.0, 180.0])
.y_bounds([-90.0, 90.0])
.render(t, &chunks[0]);
Canvas::default()
.block(Block::default()
.borders(border::ALL)
.title("List"))
.block(Block::default().borders(border::ALL).title("List"))
.paint(|ctx| {
ctx.draw(&Line {
x1: app.ball.left() as f64,
y1: app.ball.top() as f64,
x2: app.ball.right() as f64,
y2: app.ball.top() as f64,
color: Color::Yellow,
});
x1: app.ball.left() as f64,
y1: app.ball.top() as f64,
x2: app.ball.right() as f64,
y2: app.ball.top() as f64,
color: Color::Yellow,
});
ctx.draw(&Line {
x1: app.ball.right() as f64,
y1: app.ball.top() as f64,
x2: app.ball.right() as f64,
y2: app.ball.bottom() as f64,
color: Color::Yellow,
});
x1: app.ball.right() as f64,
y1: app.ball.top() as f64,
x2: app.ball.right() as f64,
y2: app.ball.bottom() as f64,
color: Color::Yellow,
});
ctx.draw(&Line {
x1: app.ball.right() as f64,
y1: app.ball.bottom() as f64,
x2: app.ball.left() as f64,
y2: app.ball.bottom() as f64,
color: Color::Yellow,
});
x1: app.ball.right() as f64,
y1: app.ball.bottom() as f64,
x2: app.ball.left() as f64,
y2: app.ball.bottom() as f64,
color: Color::Yellow,
});
ctx.draw(&Line {
x1: app.ball.left() as f64,
y1: app.ball.bottom() as f64,
x2: app.ball.left() as f64,
y2: app.ball.top() as f64,
color: Color::Yellow,
});
x1: app.ball.left() as f64,
y1: app.ball.bottom() as f64,
x2: app.ball.left() as f64,
y2: app.ball.top() as f64,
color: Color::Yellow,
});
})
.x_bounds([10.0, 110.0])
.y_bounds([10.0, 110.0])

@ -85,12 +85,10 @@ fn main() {
});
// Tick
thread::spawn(move || {
loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
}
});
thread::spawn(move || loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
// App
let mut app = App::new();
@ -129,25 +127,23 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
Chart::default()
.block(Block::default()
.title("Chart")
.title_style(Style::default()
.fg(Color::Cyan)
.modifier(Modifier::Bold))
.borders(border::ALL))
.title("Chart")
.title_style(Style::default().fg(Color::Cyan).modifier(Modifier::Bold))
.borders(border::ALL))
.x_axis(Axis::default()
.title("X Axis")
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds(app.window)
.labels(&[&format!("{}", app.window[0]),
&format!("{}", (app.window[0] + app.window[1]) / 2.0),
&format!("{}", app.window[1])]))
.title("X Axis")
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds(app.window)
.labels(&[&format!("{}", app.window[0]),
&format!("{}", (app.window[0] + app.window[1]) / 2.0),
&format!("{}", app.window[1])]))
.y_axis(Axis::default()
.title("Y Axis")
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"]))
.title("Y Axis")
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"]))
.datasets(&[Dataset::default()
.name("data2")
.marker(Marker::Dot)

@ -186,12 +186,12 @@ fn main() {
});
thread::spawn(move || {
let tx = tx.clone();
loop {
tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(200));
}
});
let tx = tx.clone();
loop {
tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(200));
}
});
let backend = TermionBackend::new().unwrap();
let mut terminal = Terminal::new(backend).unwrap();
@ -297,133 +297,157 @@ fn draw_first_tab(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
.direction(Direction::Vertical)
.sizes(&[Size::Fixed(7), Size::Min(7), Size::Fixed(7)])
.render(t, area, |t, chunks| {
Block::default()
.borders(border::ALL)
.title("Graphs")
draw_gauges(t, app, &chunks[0]);
draw_charts(t, app, &chunks[1]);
draw_text(t, &chunks[2]);
});
}
fn draw_gauges(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
Block::default()
.borders(border::ALL)
.title("Graphs")
.render(t, area);
Group::default()
.direction(Direction::Vertical)
.margin(1)
.sizes(&[Size::Fixed(2), Size::Fixed(3)])
.render(t, area, |t, chunks| {
Gauge::default()
.block(Block::default().title("Gauge:"))
.style(Style::default()
.fg(Color::Magenta)
.bg(Color::Black)
.modifier(Modifier::Italic))
.label(&format!("{} / 100", app.progress))
.percent(app.progress)
.render(t, &chunks[0]);
Sparkline::default()
.block(Block::default().title("Sparkline:"))
.style(Style::default().fg(Color::Green))
.data(&app.data)
.render(t, &chunks[1]);
});
}
fn draw_charts(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
let sizes = if app.show_chart {
vec![Size::Percent(50), Size::Percent(50)]
} else {
vec![Size::Percent(100)]
};
Group::default()
.direction(Direction::Horizontal)
.sizes(&sizes)
.render(t, area, |t, chunks| {
Group::default()
.direction(Direction::Vertical)
.margin(1)
.sizes(&[Size::Fixed(2), Size::Fixed(3)])
.sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &chunks[0], |t, chunks| {
Gauge::default()
.block(Block::default().title("Gauge:"))
.style(Style::default().fg(Color::Magenta).bg(Color::Black).modifier(Modifier::Italic))
.label(&format!("{} / 100", app.progress))
.percent(app.progress)
.render(t, &chunks[0]);
Sparkline::default()
.block(Block::default().title("Sparkline:"))
.style(Style::default().fg(Color::Green))
.data(&app.data)
.render(t, &chunks[1]);
});
let sizes = if app.show_chart {
vec![Size::Percent(50), Size::Percent(50)]
} else {
vec![Size::Percent(100)]
};
Group::default()
.direction(Direction::Horizontal)
.sizes(&sizes)
.render(t, &chunks[1], |t, chunks| {
Group::default()
.direction(Direction::Vertical)
.direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &chunks[0], |t, chunks| {
Group::default()
.direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &chunks[0], |t, chunks| {
SelectableList::default()
.block(Block::default()
.borders(border::ALL)
.title("List"))
.items(&app.items)
.select(app.selected)
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold))
.highlight_symbol(">")
.render(t, &chunks[0]);
let info_style = Style::default().fg(Color::White);
let warning_style = Style::default().fg(Color::Yellow);
let error_style = Style::default().fg(Color::Magenta);
let critical_style = Style::default().fg(Color::Red);
List::default()
.block(Block::default()
.borders(border::ALL)
.title("List"))
.items(&app.events.iter().map(|&(evt, level)| (format!("{}: {}", level, evt), match level {
"ERROR" => &error_style,
"CRITICAL" => &critical_style,
"WARNING" => &warning_style,
_ => &info_style,
})).collect::<Vec<(String, &Style)>>())
.render(t, &chunks[1]);
});
BarChart::default()
.block(Block::default()
.borders(border::ALL)
.title("Bar chart"))
.data(&app.data4)
.bar_width(3)
.bar_gap(2)
.value_style(Style::default().fg(Color::Black).bg(Color::Green).modifier(Modifier::Italic))
.label_style(Style::default().fg(Color::Yellow))
.style(Style::default().fg(Color::Green))
SelectableList::default()
.block(Block::default().borders(border::ALL).title("List"))
.items(&app.items)
.select(app.selected)
.highlight_style(Style::default()
.fg(Color::Yellow)
.modifier(Modifier::Bold))
.highlight_symbol(">")
.render(t, &chunks[0]);
let info_style = Style::default().fg(Color::White);
let warning_style = Style::default().fg(Color::Yellow);
let error_style = Style::default().fg(Color::Magenta);
let critical_style = Style::default().fg(Color::Red);
List::default()
.block(Block::default().borders(border::ALL).title("List"))
.items(&app.events
.iter()
.map(|&(evt, level)| {
(format!("{}: {}", level, evt),
match level {
"ERROR" => &error_style,
"CRITICAL" => &critical_style,
"WARNING" => &warning_style,
_ => &info_style,
})
})
.collect::<Vec<(String, &Style)>>())
.render(t, &chunks[1]);
});
if app.show_chart {
Chart::default()
.block(Block::default()
.title("Chart")
.title_style(Style::default()
BarChart::default()
.block(Block::default().borders(border::ALL).title("Bar chart"))
.data(&app.data4)
.bar_width(3)
.bar_gap(2)
.value_style(Style::default()
.fg(Color::Black)
.bg(Color::Green)
.modifier(Modifier::Italic))
.label_style(Style::default().fg(Color::Yellow))
.style(Style::default().fg(Color::Green))
.render(t, &chunks[1]);
});
if app.show_chart {
Chart::default()
.block(Block::default()
.title("Chart")
.title_style(Style::default()
.fg(Color::Cyan)
.modifier(Modifier::Bold))
.borders(border::ALL))
.x_axis(Axis::default()
.borders(border::ALL))
.x_axis(Axis::default()
.title("X Axis")
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds(app.window)
.labels(&[&format!("{}", app.window[0]),
&format!("{}", (app.window[0] + app.window[1]) / 2.0),
&format!("{}", app.window[1])]))
.y_axis(Axis::default()
&format!("{}",
(app.window[0] + app.window[1]) / 2.0),
&format!("{}", app.window[1])]))
.y_axis(Axis::default()
.title("Y Axis")
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"]))
.datasets(&[Dataset::default()
.name("data2")
.marker(Marker::Dot)
.style(Style::default().fg(Color::Cyan))
.data(&app.data2),
Dataset::default()
.name("data3")
.marker(Marker::Braille)
.style(Style::default().fg(Color::Yellow))
.data(&app.data3)])
.render(t, &chunks[1]);
}
});
Paragraph::default()
.block(Block::default()
.borders(border::ALL)
.title("Footer")
.title_style(Style::default().fg(Color::Magenta).modifier(Modifier::Bold)))
.wrap(true)
.text("This is a paragraph with several lines.\nYou can change the color.\nUse \
\\{fg=[color];bg=[color];mod=[modifier] [text]} to highlight the text with a color. For example, {fg=red \
u}{fg=green n}{fg=yellow d}{fg=magenta e}{fg=cyan r} {fg=gray t}{fg=light_gray h}{fg=light_red \
e} {fg=light_green r}{fg=light_yellow a}{fg=light_magenta i}{fg=light_cyan n}{fg=white \
b}{fg=red o}{fg=green w}.\nOh, and if you didn't {mod=italic notice} you can {mod=bold automatically} \
{mod=invert wrap} your {mod=underline text} =).\nOne more thing is that it should display unicode \
characters properly: , ٩(-̮̮̃-̃)۶ ٩(̮̮̃̃)۶ ٩(̯͡͡)۶ ٩(-̮̮̃̃).")
.render(t, &chunks[2]);
.datasets(&[Dataset::default()
.name("data2")
.marker(Marker::Dot)
.style(Style::default().fg(Color::Cyan))
.data(&app.data2),
Dataset::default()
.name("data3")
.marker(Marker::Braille)
.style(Style::default().fg(Color::Yellow))
.data(&app.data3)])
.render(t, &chunks[1]);
}
});
}
fn draw_text(t: &mut Terminal<TermionBackend>, area: &Rect) {
Paragraph::default()
.block(Block::default()
.borders(border::ALL)
.title("Footer")
.title_style(Style::default().fg(Color::Magenta).modifier(Modifier::Bold)))
.wrap(true)
.text("This is a paragraph with several lines.\nYou can change the color.\nUse \
\\{fg=[color];bg=[color];mod=[modifier] [text]} to highlight the text with a color. \
For example, {fg=red u}{fg=green n}{fg=yellow d}{fg=magenta e}{fg=cyan r} \
{fg=gray t}{fg=light_gray h}{fg=light_red e} {fg=light_green r}{fg=light_yellow a} \
{fg=light_magenta i}{fg=light_cyan n}{fg=white b}{fg=red o}{fg=green w}.\n\
Oh, and if you didn't {mod=italic notice} you can {mod=bold automatically} \
{mod=invert wrap} your {mod=underline text} =).\nOne more thing is that \
it should display unicode characters properly: , ٩(-̮̮̃-̃)۶ ٩(̮̮̃̃)۶ ٩(̯͡͡)۶ \
٩(-̮̮̃̃).")
.render(t, area);
}
fn draw_second_tab(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
Group::default()
.direction(Direction::Horizontal)
@ -432,42 +456,40 @@ fn draw_second_tab(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
let up_style = Style::default().fg(Color::Green);
let failure_style = Style::default().fg(Color::Red);
Table::default()
.block(Block::default()
.title("Servers")
.borders(border::ALL))
.block(Block::default().title("Servers").borders(border::ALL))
.header(&["Server", "Location", "Status"])
.header_style(Style::default().fg(Color::Yellow))
.widths(&[15, 15, 10])
.rows(&app.servers
.iter()
.map(|s| {
(vec![s.name, s.location, s.status],
if s.status == "Up" {
&up_style
} else {
&failure_style
})
})
.collect::<Vec<(Vec<&str>, &Style)>>())
.iter()
.map(|s| {
(vec![s.name, s.location, s.status],
if s.status == "Up" {
&up_style
} else {
&failure_style
})
})
.collect::<Vec<(Vec<&str>, &Style)>>())
.render(t, &chunks[0]);
Canvas::default()
.block(Block::default().title("World").borders(border::ALL))
.paint(|ctx| {
ctx.draw(&Map {
color: Color::White,
resolution: MapResolution::High,
});
color: Color::White,
resolution: MapResolution::High,
});
ctx.layer();
for (i, s1) in app.servers.iter().enumerate() {
for s2 in &app.servers[i + 1..] {
ctx.draw(&Line {
x1: s1.coords.1,
y1: s1.coords.0,
y2: s2.coords.0,
x2: s2.coords.1,
color: Color::Yellow,
});
x1: s1.coords.1,
y1: s1.coords.0,
y2: s2.coords.0,
x2: s2.coords.1,
color: Color::Yellow,
});
}
}
for server in &app.servers {

@ -82,12 +82,10 @@ fn main() {
});
// Tick
thread::spawn(move || {
loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
}
});
thread::spawn(move || loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
// App
let mut app = App::new();
@ -127,7 +125,10 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
Group::default()
.direction(Direction::Vertical)
.margin(2)
.sizes(&[Size::Percent(25), Size::Percent(25), Size::Percent(25), Size::Percent(25)])
.sizes(&[Size::Percent(25),
Size::Percent(25),
Size::Percent(25),
Size::Percent(25)])
.render(t, &app.size, |t, chunks| {
Gauge::default()
.block(Block::default().title("Gauge1").borders(border::ALL))

@ -102,12 +102,10 @@ fn main() {
});
// Tick
thread::spawn(move || {
loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
}
});
thread::spawn(move || loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
// App
let mut app = App::new();
@ -165,30 +163,26 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
.sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &app.size, |t, chunks| {
SelectableList::default()
.block(Block::default()
.borders(border::ALL)
.title("List"))
.block(Block::default().borders(border::ALL).title("List"))
.items(&app.items)
.select(app.selected)
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold))
.highlight_symbol(">")
.render(t, &chunks[0]);
List::default()
.block(Block::default()
.borders(border::ALL)
.title("List"))
.block(Block::default().borders(border::ALL).title("List"))
.items(&app.events
.iter()
.map(|&(evt, level)| {
(format!("{}: {}", level, evt),
match level {
"ERROR" => &app.error_style,
"CRITICAL" => &app.critical_style,
"WARNING" => &app.warning_style,
_ => &app.info_style,
})
})
.collect::<Vec<(String, &Style)>>())
.iter()
.map(|&(evt, level)| {
(format!("{}: {}", level, evt),
match level {
"ERROR" => &app.error_style,
"CRITICAL" => &app.critical_style,
"WARNING" => &app.warning_style,
_ => &app.info_style,
})
})
.collect::<Vec<(String, &Style)>>())
.render(t, &chunks[1]);
});

@ -40,13 +40,13 @@ fn draw(t: &mut Terminal<TermionBackend>, size: &Rect) {
Block::default()
.style(Style::default().bg(Color::White))
.render(t, &size);
.render(t, size);
Group::default()
.direction(Direction::Vertical)
.margin(5)
.sizes(&[Size::Percent(100)])
.render(t, &size, |t, chunks| {
.render(t, size, |t, chunks| {
Group::default()
.direction(Direction::Horizontal)
.sizes(&[Size::Percent(100)])

@ -40,10 +40,12 @@ fn draw(t: &mut Terminal<RustboxBackend>) {
.render(t, &size, |t, chunks| {
Paragraph::default()
.block(Block::default()
.title("Rustbox backend")
.title_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold))
.borders(border::ALL)
.border_style(Style::default().fg(Color::Magenta)))
.title("Rustbox backend")
.title_style(Style::default()
.fg(Color::Yellow)
.modifier(Modifier::Bold))
.borders(border::ALL)
.border_style(Style::default().fg(Color::Magenta)))
.text("It {yellow works}!")
.render(t, &chunks[0]);
});

@ -82,12 +82,10 @@ fn main() {
});
// Tick
thread::spawn(move || {
loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
}
});
thread::spawn(move || loop {
clock_tx.send(Event::Tick).unwrap();
thread::sleep(time::Duration::from_millis(500));
});
// App
let mut app = App::new();
@ -130,18 +128,24 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
.sizes(&[Size::Fixed(3), Size::Fixed(3), Size::Fixed(7), Size::Min(0)])
.render(t, &app.size, |t, chunks| {
Sparkline::default()
.block(Block::default().title("Data1").borders(border::LEFT | border::RIGHT))
.block(Block::default()
.title("Data1")
.borders(border::LEFT | border::RIGHT))
.data(&app.data1)
.style(Style::default().fg(Color::Yellow))
.render(t, &chunks[0]);
Sparkline::default()
.block(Block::default().title("Data2").borders(border::LEFT | border::RIGHT))
.block(Block::default()
.title("Data2")
.borders(border::LEFT | border::RIGHT))
.data(&app.data2)
.style(Style::default().bg(Color::Green))
.render(t, &chunks[1]);
// Multiline
Sparkline::default()
.block(Block::default().title("Data3").borders(border::LEFT | border::RIGHT))
.block(Block::default()
.title("Data3")
.borders(border::LEFT | border::RIGHT))
.data(&app.data3)
.style(Style::default().fg(Color::Red))
.render(t, &chunks[2]);

@ -93,23 +93,21 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
let selected_style = Style::default().fg(Color::Yellow).modifier(Modifier::Bold);
let normal_style = Style::default().fg(Color::White);
Table::default()
.block(Block::default()
.borders(border::ALL)
.title("Table"))
.block(Block::default().borders(border::ALL).title("Table"))
.header(&["Header1", "Header2", "Header3"])
.widths(&[10, 10, 10])
.rows(&app.items
.iter()
.enumerate()
.map(|(i, item)| {
(item,
if i == app.selected {
&selected_style
} else {
&normal_style
})
})
.collect::<Vec<(&Vec<&str>, &Style)>>())
.iter()
.enumerate()
.map(|(i, item)| {
(item,
if i == app.selected {
&selected_style
} else {
&normal_style
})
})
.collect::<Vec<(&Vec<&str>, &Style)>>())
.render(t, &chunks[0]);
});

@ -83,16 +83,28 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &mut App) {
.render(t, &chunks[0]);
match app.tabs.selection {
0 => {
Block::default().title("Inner 0").borders(border::ALL).render(t, &chunks[1]);
Block::default()
.title("Inner 0")
.borders(border::ALL)
.render(t, &chunks[1]);
}
1 => {
Block::default().title("Inner 1").borders(border::ALL).render(t, &chunks[1]);
Block::default()
.title("Inner 1")
.borders(border::ALL)
.render(t, &chunks[1]);
}
2 => {
Block::default().title("Inner 2").borders(border::ALL).render(t, &chunks[1]);
Block::default()
.title("Inner 2")
.borders(border::ALL)
.render(t, &chunks[1]);
}
3 => {
Block::default().title("Inner 3").borders(border::ALL).render(t, &chunks[1]);
Block::default()
.title("Inner 3")
.borders(border::ALL)
.render(t, &chunks[1]);
}
_ => {}
}

@ -33,12 +33,13 @@ impl Backend for RustboxBackend {
let mut inst = 0;
for (x, y, cell) in content {
inst += 1;
self.rustbox.print(x as usize,
y as usize,
cell.style.modifier.into(),
cell.style.fg.into(),
cell.style.bg.into(),
&cell.symbol);
self.rustbox
.print(x as usize,
y as usize,
cell.style.modifier.into(),
cell.style.fg.into(),
cell.style.bg.into(),
&cell.symbol);
}
debug!("{} instructions outputed", inst);
Ok(())
@ -54,12 +55,12 @@ impl Backend for RustboxBackend {
Ok(())
}
fn size(&self) -> Result<Rect, io::Error> {
Ok((Rect {
x: 0,
y: 0,
width: self.rustbox.width() as u16,
height: self.rustbox.height() as u16,
}))
Ok(Rect {
x: 0,
y: 0,
width: self.rustbox.width() as u16,
height: self.rustbox.height() as u16,
})
}
fn flush(&mut self) -> Result<(), io::Error> {
self.rustbox.present();
@ -68,7 +69,7 @@ impl Backend for RustboxBackend {
}
fn rgb_to_byte(r: u8, g: u8, b: u8) -> u16 {
((((r & 255 & 0xC0) + ((g & 255 & 0xE0) >> 2) + ((b & 0xE0) >> 5))) & 0xFF) as u16
(((r & 255 & 0xC0) + ((g & 255 & 0xE0) >> 2) + ((b & 0xE0) >> 5)) & 0xFF) as u16
}
impl Into<rustbox::Color> for Color {

@ -95,11 +95,11 @@ impl Backend for TermionBackend {
fn size(&self) -> Result<Rect, io::Error> {
let terminal = try!(termion::terminal_size());
Ok(Rect {
x: 0,
y: 0,
width: terminal.0,
height: terminal.1,
})
x: 0,
y: 0,
width: terminal.0,
height: terminal.1,
})
}
fn flush(&mut self) -> Result<(), io::Error> {
@ -109,23 +109,33 @@ impl Backend for TermionBackend {
}
macro_rules! termion_fg {
($color:ident) => (format!("{}", termion::color::Fg(termion::color::$color)));
($color:ident) => (
format!("{}", termion::color::Fg(termion::color::$color))
);
}
macro_rules! termion_fg_rgb {
($r:expr, $g:expr, $b:expr) => (format!("{}", termion::color::Fg(termion::color::Rgb($r, $g, $b))));
($r:expr, $g:expr, $b:expr) => (
format!("{}", termion::color::Fg(termion::color::Rgb($r, $g, $b)))
);
}
macro_rules! termion_bg {
($color:ident) => (format!("{}", termion::color::Bg(termion::color::$color)));
($color:ident) => (
format!("{}", termion::color::Bg(termion::color::$color))
);
}
macro_rules! termion_bg_rgb {
($r:expr, $g:expr, $b:expr) => (format!("{}", termion::color::Bg(termion::color::Rgb($r, $g, $b))));
($r:expr, $g:expr, $b:expr) => (
format!("{}", termion::color::Bg(termion::color::Rgb($r, $g, $b)))
);
}
macro_rules! termion_modifier {
($style:ident) => (format!("{}", termion::style::$style));
($style:ident) => (
format!("{}", termion::style::$style)
);
}
impl Color {

@ -139,7 +139,10 @@ pub enum Size {
pub fn split(area: &Rect, dir: &Direction, margin: u16, sizes: &[Size]) -> Vec<Rect> {
let mut solver = Solver::new();
let mut vars: HashMap<Variable, (usize, usize)> = HashMap::new();
let elements = sizes.iter().map(|_| Element::new()).collect::<Vec<Element>>();
let elements = sizes
.iter()
.map(|_| Element::new())
.collect::<Vec<Element>>();
let mut results = sizes.iter().map(|_| Rect::default()).collect::<Vec<Rect>>();
let dest_area = area.inner(margin);
for (i, e) in elements.iter().enumerate() {
@ -157,15 +160,23 @@ pub fn split(area: &Rect, dir: &Direction, margin: u16, sizes: &[Size]) -> Vec<R
}
if let Some(first) = elements.first() {
constraints.push(match *dir {
Direction::Horizontal => first.left() | EQ(REQUIRED) | dest_area.left() as f64,
Direction::Vertical => first.top() | EQ(REQUIRED) | dest_area.top() as f64,
});
Direction::Horizontal => {
first.left() | EQ(REQUIRED) | dest_area.left() as f64
}
Direction::Vertical => {
first.top() | EQ(REQUIRED) | dest_area.top() as f64
}
});
}
if let Some(last) = elements.last() {
constraints.push(match *dir {
Direction::Horizontal => last.right() | EQ(REQUIRED) | dest_area.right() as f64,
Direction::Vertical => last.bottom() | EQ(REQUIRED) | dest_area.bottom() as f64,
});
Direction::Horizontal => {
last.right() | EQ(REQUIRED) | dest_area.right() as f64
}
Direction::Vertical => {
last.bottom() | EQ(REQUIRED) | dest_area.bottom() as f64
}
});
}
match *dir {
Direction::Horizontal => {
@ -176,13 +187,14 @@ pub fn split(area: &Rect, dir: &Direction, margin: u16, sizes: &[Size]) -> Vec<R
constraints.push(elements[i].y | EQ(REQUIRED) | dest_area.y as f64);
constraints.push(elements[i].height | EQ(REQUIRED) | dest_area.height as f64);
constraints.push(match *size {
Size::Fixed(v) => elements[i].width | EQ(WEAK) | v as f64,
Size::Percent(v) => {
elements[i].width | EQ(WEAK) | ((v * dest_area.width) as f64 / 100.0)
}
Size::Min(v) => elements[i].width | GE(WEAK) | v as f64,
Size::Max(v) => elements[i].width | LE(WEAK) | v as f64,
});
Size::Fixed(v) => elements[i].width | EQ(WEAK) | v as f64,
Size::Percent(v) => {
elements[i].width | EQ(WEAK) |
((v * dest_area.width) as f64 / 100.0)
}
Size::Min(v) => elements[i].width | GE(WEAK) | v as f64,
Size::Max(v) => elements[i].width | LE(WEAK) | v as f64,
});
}
}
Direction::Vertical => {
@ -193,13 +205,14 @@ pub fn split(area: &Rect, dir: &Direction, margin: u16, sizes: &[Size]) -> Vec<R
constraints.push(elements[i].x | EQ(REQUIRED) | dest_area.x as f64);
constraints.push(elements[i].width | EQ(REQUIRED) | dest_area.width as f64);
constraints.push(match *size {
Size::Fixed(v) => elements[i].height | EQ(WEAK) | v as f64,
Size::Percent(v) => {
elements[i].height | EQ(WEAK) | ((v * dest_area.height) as f64 / 100.0)
}
Size::Min(v) => elements[i].height | GE(WEAK) | v as f64,
Size::Max(v) => elements[i].height | LE(WEAK) | v as f64,
});
Size::Fixed(v) => elements[i].height | EQ(WEAK) | v as f64,
Size::Percent(v) => {
elements[i].height | EQ(WEAK) |
((v * dest_area.height) as f64 / 100.0)
}
Size::Min(v) => elements[i].height | GE(WEAK) | v as f64,
Size::Max(v) => elements[i].height | LE(WEAK) | v as f64,
});
}
}
}

@ -35,11 +35,11 @@ impl<B> Terminal<B>
pub fn new(backend: B) -> Result<Terminal<B>, io::Error> {
let size = try!(backend.size());
Ok(Terminal {
backend: backend,
layout_cache: HashMap::new(),
buffers: [Buffer::empty(size), Buffer::empty(size)],
current: 0,
})
backend: backend,
layout_cache: HashMap::new(),
buffers: [Buffer::empty(size), Buffer::empty(size)],
current: 0,
})
}
pub fn backend(&self) -> &B {
@ -80,13 +80,13 @@ impl<B> Terminal<B>
.zip(self.buffers[1 - self.current].content.iter())
.enumerate()
.filter_map(|(i, (c, p))| if c != p {
let i = i as u16;
let x = i % width;
let y = i / width;
Some((x, y, c))
} else {
None
});
let i = i as u16;
let x = i % width;
let y = i / width;
Some((x, y, c))
} else {
None
});
self.backend.draw(content)
}
@ -123,7 +123,7 @@ impl<B> Terminal<B>
self.layout_cache.insert(key, value);
}
for (_, e) in &mut self.layout_cache {
for e in self.layout_cache.values_mut() {
e.hot = false;
}

@ -123,7 +123,8 @@ impl<'a> Widget for BarChart<'a> {
self.background(&chart_area, buf, self.style.bg);
let max = self.max.unwrap_or(self.data.iter().fold(0, |acc, &(_, v)| max(v, acc)));
let max = self.max
.unwrap_or_else(|| self.data.iter().fold(0, |acc, &(_, v)| max(v, acc)));
let max_index = min((chart_area.width / (self.bar_width + self.bar_gap)) as usize,
self.data.len());
let mut data = self.data
@ -146,8 +147,7 @@ impl<'a> Widget for BarChart<'a> {
};
for x in 0..self.bar_width {
buf.get_mut(chart_area.left() + i as u16 * (self.bar_width + self.bar_gap) +
x,
buf.get_mut(chart_area.left() + i as u16 * (self.bar_width + self.bar_gap) + x,
chart_area.top() + j)
.set_symbol(symbol)
.set_style(self.style);

@ -12,8 +12,10 @@ use buffer::Buffer;
use widgets::{Block, Widget};
use layout::Rect;
pub const DOTS: [[u16; 2]; 4] =
[[0x0001, 0x0008], [0x0002, 0x0010], [0x0004, 0x0020], [0x0040, 0x0080]];
pub const DOTS: [[u16; 2]; 4] = [[0x0001, 0x0008],
[0x0002, 0x0010],
[0x0004, 0x0020],
[0x0040, 0x0080]];
pub const BRAILLE_OFFSET: u16 = 0x2800;
pub const BRAILLE_BLANK: char = '';
@ -90,8 +92,9 @@ impl<'a> Context<'a> {
let right = self.x_bounds[1];
let bottom = self.y_bounds[0];
let top = self.y_bounds[1];
for (x, y) in shape.points()
.filter(|&(x, y)| !(x < left || x > right || y < bottom || y > top)) {
for (x, y) in shape
.points()
.filter(|&(x, y)| !(x < left || x > right || y < bottom || y > top)) {
let dy = ((top - y) * (self.height - 1) as f64 * 4.0 / (top - bottom)) as usize;
let dx = ((x - left) * (self.width - 1) as f64 * 2.0 / (right - left)) as usize;
let index = dy / 4 * self.width as usize + dx / 2;
@ -109,12 +112,13 @@ impl<'a> Context<'a> {
/// Print a string on the canvas at the given position
pub fn print(&mut self, x: f64, y: f64, text: &'a str, color: Color) {
self.labels.push(Label {
x: x,
y: y,
text: text,
color: color,
});
self.labels
.push(Label {
x: x,
y: y,
text: text,
color: color,
});
}
/// Push the last layer if necessary
@ -248,10 +252,11 @@ impl<'a, F> Widget for Canvas<'a, F>
// Retreive painted points for each layer
for layer in ctx.layers {
for (i, (ch, color)) in layer.string
.chars()
.zip(layer.colors.into_iter())
.enumerate() {
for (i, (ch, color)) in layer
.string
.chars()
.zip(layer.colors.into_iter())
.enumerate() {
if ch != BRAILLE_BLANK {
let (x, y) = (i % width, i / width);
buf.get_mut(x as u16 + canvas_area.left(), y as u16 + canvas_area.top())
@ -264,10 +269,13 @@ impl<'a, F> Widget for Canvas<'a, F>
// Finally draw the labels
let style = Style::default().bg(self.background_color);
for label in ctx.labels.iter().filter(|l| {
!(l.x < self.x_bounds[0] || l.x > self.x_bounds[1] || l.y < self.y_bounds[0] ||
l.y > self.y_bounds[1])
}) {
for label in ctx.labels
.iter()
.filter(|l| {
!(l.x < self.x_bounds[0] || l.x > self.x_bounds[1] ||
l.y < self.y_bounds[0] ||
l.y > self.y_bounds[1])
}) {
let dy = ((self.y_bounds[1] - label.y) * (canvas_area.height - 1) as f64 /
(self.y_bounds[1] - self.y_bounds[0])) as u16;
let dx = ((label.x - self.x_bounds[0]) * (canvas_area.width - 1) as f64 /

@ -389,17 +389,20 @@ impl<'a> Widget for Chart<'a> {
for dataset in self.datasets {
match dataset.marker {
Marker::Dot => {
for &(x, y) in dataset.data.iter().filter(|&&(x, y)| {
!(x < self.x_axis.bounds[0] || x > self.x_axis.bounds[1] ||
y < self.y_axis.bounds[0] ||
y > self.y_axis.bounds[1])
}) {
for &(x, y) in dataset
.data
.iter()
.filter(|&&(x, y)| {
!(x < self.x_axis.bounds[0] || x > self.x_axis.bounds[1] ||
y < self.y_axis.bounds[0] ||
y > self.y_axis.bounds[1])
}) {
let dy = ((self.y_axis.bounds[1] - y) * (graph_area.height - 1) as f64 /
(self.y_axis.bounds[1] -
self.y_axis.bounds[0])) as u16;
(self.y_axis.bounds[1] - self.y_axis.bounds[0])) as
u16;
let dx = ((x - self.x_axis.bounds[0]) * (graph_area.width - 1) as f64 /
(self.x_axis.bounds[1] -
self.x_axis.bounds[0])) as u16;
(self.x_axis.bounds[1] - self.x_axis.bounds[0])) as
u16;
buf.get_mut(graph_area.left() + dx, graph_area.top() + dy)
.set_symbol(symbols::DOT)
@ -413,11 +416,11 @@ impl<'a> Widget for Chart<'a> {
.x_bounds(self.x_axis.bounds)
.y_bounds(self.y_axis.bounds)
.paint(|ctx| {
ctx.draw(&Points {
coords: dataset.data,
color: dataset.style.fg,
});
})
ctx.draw(&Points {
coords: dataset.data,
color: dataset.style.fg,
});
})
.draw(&graph_area, buf);
}
}

@ -98,7 +98,9 @@ impl<'a> Widget for Gauge<'a> {
// Fix colors
for x in gauge_area.left()..end {
buf.get_mut(x, y).set_fg(self.style.bg).set_bg(self.style.fg);
buf.get_mut(x, y)
.set_fg(self.style.bg)
.set_bg(self.style.fg);
}
}
}

@ -34,8 +34,10 @@ impl<'a> List<'a> {
pub fn items<I>(&'a mut self, items: &'a [(I, &'a Style)]) -> &mut List<'a>
where I: AsRef<str> + 'a
{
self.items =
items.iter().map(|&(ref i, s)| (i.as_ref(), s)).collect::<Vec<(&'a str, &'a Style)>>();
self.items = items
.iter()
.map(|&(ref i, s)| (i.as_ref(), s))
.collect::<Vec<(&'a str, &'a Style)>>();
self
}
@ -62,7 +64,7 @@ impl<'a> Widget for List<'a> {
self.background(&list_area, buf, self.style.bg);
let max_index = min(self.items.len(), list_area.height as usize);
for (i, &(ref item, style)) in self.items.iter().enumerate().take(max_index) {
for (i, &(item, style)) in self.items.iter().enumerate().take(max_index) {
buf.set_stringn(list_area.left(),
list_area.top() + i as u16,
item.as_ref(),
@ -169,7 +171,9 @@ impl<'a> Widget for SelectableList<'a> {
None => (0, &self.style),
};
let highlight_symbol = self.highlight_symbol.unwrap_or("");
let blank_symbol = iter::repeat(" ").take(highlight_symbol.width()).collect::<String>();
let blank_symbol = iter::repeat(" ")
.take(highlight_symbol.width())
.collect::<String>();
// Make sure the list show the selected item
let offset = if selected >= list_height {
selected - list_height + 1
@ -181,10 +185,10 @@ impl<'a> Widget for SelectableList<'a> {
.cloned()
.enumerate()
.map(|(i, item)| if i == selected {
(format!("{} {}", highlight_symbol, item), highlight_style)
} else {
(format!("{} {}", blank_symbol, item), &self.style)
})
(format!("{} {}", highlight_symbol, item), highlight_style)
} else {
(format!("{} {}", blank_symbol, item), &self.style)
})
.skip(offset as usize)
.collect::<Vec<(String, &Style)>>();

@ -193,14 +193,12 @@ impl<'a, T> Iterator for Parser<'a, T>
if self.escaping {
self.escaping = false;
Some((s, self.style))
} else if self.mark {
Some((s, self.style))
} else {
if self.mark {
Some((s, self.style))
} else {
self.style = self.base_style;
self.mark = true;
self.next()
}
self.style = self.base_style;
self.mark = true;
self.next()
}
} else if s == "}" && self.mark {
self.reset();

@ -88,8 +88,12 @@ impl<'a> Table<'a> {
{
self.rows = rows.iter()
.map(|&(ref r, style)| {
(r.as_ref().iter().map(|i| i.as_ref()).collect::<Vec<&'a str>>(), style)
})
(r.as_ref()
.iter()
.map(|i| i.as_ref())
.collect::<Vec<&'a str>>(),
style)
})
.collect::<Vec<(Vec<&'a str>, &'a Style)>>();
self
}

@ -92,11 +92,14 @@ impl<'a> Widget for Tabs<'a> {
self.background(&tabs_area, buf, self.style.bg);
let mut x = tabs_area.left();
for (title, style) in self.titles.iter().enumerate().map(|(i, t)| if i == self.selected {
(t, &self.highlight_style)
} else {
(t, &self.style)
}) {
for (title, style) in self.titles
.iter()
.enumerate()
.map(|(i, t)| if i == self.selected {
(t, &self.highlight_style)
} else {
(t, &self.style)
}) {
x += 1;
if x > tabs_area.right() {
break;

Loading…
Cancel
Save