Fix rustfmt and clippy errors

This commit is contained in:
Florian Dehau 2017-05-21 11:13:24 +02:00
parent 29db3dd722
commit b2bb24b9d2
24 changed files with 458 additions and 393 deletions

View file

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

View file

@ -39,14 +39,12 @@ fn draw(t: &mut Terminal<TermionBackend>, 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
Block::default() Block::default().borders(border::ALL).render(t, size);
.borders(border::ALL)
.render(t, &size);
Group::default() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(4) .margin(4)
.sizes(&[Size::Percent(50), Size::Percent(50)]) .sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &size, |t, chunks| { .render(t, size, |t, chunks| {
Group::default() Group::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)]) .sizes(&[Size::Percent(50), Size::Percent(50)])
@ -59,9 +57,9 @@ fn draw(t: &mut Terminal<TermionBackend>, size: &Rect) {
Block::default() Block::default()
.title("Styled title") .title("Styled title")
.title_style(Style::default() .title_style(Style::default()
.fg(Color::White) .fg(Color::White)
.bg(Color::Red) .bg(Color::Red)
.modifier(Modifier::Bold)) .modifier(Modifier::Bold))
.render(t, &chunks[1]); .render(t, &chunks[1]);
}); });
Group::default() Group::default()

View file

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

View file

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

View file

@ -186,12 +186,12 @@ fn main() {
}); });
thread::spawn(move || { thread::spawn(move || {
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(200)); thread::sleep(time::Duration::from_millis(200));
} }
}); });
let backend = TermionBackend::new().unwrap(); let backend = TermionBackend::new().unwrap();
let mut terminal = Terminal::new(backend).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) .direction(Direction::Vertical)
.sizes(&[Size::Fixed(7), Size::Min(7), Size::Fixed(7)]) .sizes(&[Size::Fixed(7), Size::Min(7), Size::Fixed(7)])
.render(t, area, |t, chunks| { .render(t, area, |t, chunks| {
Block::default() draw_gauges(t, app, &chunks[0]);
.borders(border::ALL) draw_charts(t, app, &chunks[1]);
.title("Graphs") 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]); .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() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(1) .sizes(&[Size::Percent(50), Size::Percent(50)])
.sizes(&[Size::Fixed(2), Size::Fixed(3)])
.render(t, &chunks[0], |t, chunks| { .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() Group::default()
.direction(Direction::Vertical) .direction(Direction::Horizontal)
.sizes(&[Size::Percent(50), Size::Percent(50)]) .sizes(&[Size::Percent(50), Size::Percent(50)])
.render(t, &chunks[0], |t, chunks| { .render(t, &chunks[0], |t, chunks| {
Group::default() SelectableList::default()
.direction(Direction::Horizontal) .block(Block::default().borders(border::ALL).title("List"))
.sizes(&[Size::Percent(50), Size::Percent(50)]) .items(&app.items)
.render(t, &chunks[0], |t, chunks| { .select(app.selected)
SelectableList::default() .highlight_style(Style::default()
.block(Block::default() .fg(Color::Yellow)
.borders(border::ALL) .modifier(Modifier::Bold))
.title("List")) .highlight_symbol(">")
.items(&app.items) .render(t, &chunks[0]);
.select(app.selected) let info_style = Style::default().fg(Color::White);
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold)) let warning_style = Style::default().fg(Color::Yellow);
.highlight_symbol(">") let error_style = Style::default().fg(Color::Magenta);
.render(t, &chunks[0]); let critical_style = Style::default().fg(Color::Red);
let info_style = Style::default().fg(Color::White); List::default()
let warning_style = Style::default().fg(Color::Yellow); .block(Block::default().borders(border::ALL).title("List"))
let error_style = Style::default().fg(Color::Magenta); .items(&app.events
let critical_style = Style::default().fg(Color::Red); .iter()
List::default() .map(|&(evt, level)| {
.block(Block::default() (format!("{}: {}", level, evt),
.borders(border::ALL) match level {
.title("List")) "ERROR" => &error_style,
.items(&app.events.iter().map(|&(evt, level)| (format!("{}: {}", level, evt), match level { "CRITICAL" => &critical_style,
"ERROR" => &error_style, "WARNING" => &warning_style,
"CRITICAL" => &critical_style, _ => &info_style,
"WARNING" => &warning_style, })
_ => &info_style, })
})).collect::<Vec<(String, &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))
.render(t, &chunks[1]); .render(t, &chunks[1]);
}); });
if app.show_chart { BarChart::default()
Chart::default() .block(Block::default().borders(border::ALL).title("Bar chart"))
.block(Block::default() .data(&app.data4)
.title("Chart") .bar_width(3)
.title_style(Style::default() .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) .fg(Color::Cyan)
.modifier(Modifier::Bold)) .modifier(Modifier::Bold))
.borders(border::ALL)) .borders(border::ALL))
.x_axis(Axis::default() .x_axis(Axis::default()
.title("X Axis") .title("X Axis")
.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(&[&format!("{}", app.window[0]), .labels(&[&format!("{}", app.window[0]),
&format!("{}", (app.window[0] + app.window[1]) / 2.0), &format!("{}",
&format!("{}", app.window[1])])) (app.window[0] + app.window[1]) / 2.0),
.y_axis(Axis::default() &format!("{}", app.window[1])]))
.y_axis(Axis::default()
.title("Y Axis") .title("Y Axis")
.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([-20.0, 20.0]) .bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"])) .labels(&["-20", "0", "20"]))
.datasets(&[Dataset::default() .datasets(&[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]);
} }
});
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]);
}); });
} }
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) { fn draw_second_tab(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
Group::default() Group::default()
.direction(Direction::Horizontal) .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 up_style = Style::default().fg(Color::Green);
let failure_style = Style::default().fg(Color::Red); let failure_style = Style::default().fg(Color::Red);
Table::default() Table::default()
.block(Block::default() .block(Block::default().title("Servers").borders(border::ALL))
.title("Servers")
.borders(border::ALL))
.header(&["Server", "Location", "Status"]) .header(&["Server", "Location", "Status"])
.header_style(Style::default().fg(Color::Yellow)) .header_style(Style::default().fg(Color::Yellow))
.widths(&[15, 15, 10]) .widths(&[15, 15, 10])
.rows(&app.servers .rows(&app.servers
.iter() .iter()
.map(|s| { .map(|s| {
(vec![s.name, s.location, s.status], (vec![s.name, s.location, s.status],
if s.status == "Up" { if s.status == "Up" {
&up_style &up_style
} else { } else {
&failure_style &failure_style
}) })
}) })
.collect::<Vec<(Vec<&str>, &Style)>>()) .collect::<Vec<(Vec<&str>, &Style)>>())
.render(t, &chunks[0]); .render(t, &chunks[0]);
Canvas::default() Canvas::default()
.block(Block::default().title("World").borders(border::ALL)) .block(Block::default().title("World").borders(border::ALL))
.paint(|ctx| { .paint(|ctx| {
ctx.draw(&Map { ctx.draw(&Map {
color: Color::White, color: Color::White,
resolution: MapResolution::High, resolution: MapResolution::High,
}); });
ctx.layer(); ctx.layer();
for (i, s1) in app.servers.iter().enumerate() { for (i, s1) in app.servers.iter().enumerate() {
for s2 in &app.servers[i + 1..] { for s2 in &app.servers[i + 1..] {
ctx.draw(&Line { ctx.draw(&Line {
x1: s1.coords.1, x1: s1.coords.1,
y1: s1.coords.0, y1: s1.coords.0,
y2: s2.coords.0, y2: s2.coords.0,
x2: s2.coords.1, x2: s2.coords.1,
color: Color::Yellow, color: Color::Yellow,
}); });
} }
} }
for server in &app.servers { for server in &app.servers {

View file

@ -82,12 +82,10 @@ fn main() {
}); });
// Tick // Tick
thread::spawn(move || { thread::spawn(move || loop {
loop { clock_tx.send(Event::Tick).unwrap();
clock_tx.send(Event::Tick).unwrap(); thread::sleep(time::Duration::from_millis(500));
thread::sleep(time::Duration::from_millis(500)); });
}
});
// App // App
let mut app = App::new(); let mut app = App::new();
@ -127,7 +125,10 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &App) {
Group::default() Group::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(2) .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| { .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

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

View file

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

View file

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

View file

@ -82,12 +82,10 @@ fn main() {
}); });
// Tick // Tick
thread::spawn(move || { thread::spawn(move || loop {
loop { clock_tx.send(Event::Tick).unwrap();
clock_tx.send(Event::Tick).unwrap(); thread::sleep(time::Duration::from_millis(500));
thread::sleep(time::Duration::from_millis(500)); });
}
});
// App // App
let mut app = App::new(); 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)]) .sizes(&[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(border::LEFT | border::RIGHT)) .block(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(border::LEFT | border::RIGHT)) .block(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(border::LEFT | border::RIGHT)) .block(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

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

View file

@ -83,16 +83,28 @@ fn draw(t: &mut Terminal<TermionBackend>, app: &mut App) {
.render(t, &chunks[0]); .render(t, &chunks[0]);
match app.tabs.selection { match app.tabs.selection {
0 => { 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 => { 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 => { 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 => { 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]);
} }
_ => {} _ => {}
} }

View file

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

View file

@ -95,11 +95,11 @@ impl Backend for TermionBackend {
fn size(&self) -> Result<Rect, io::Error> { fn size(&self) -> Result<Rect, io::Error> {
let terminal = try!(termion::terminal_size()); let terminal = try!(termion::terminal_size());
Ok(Rect { Ok(Rect {
x: 0, x: 0,
y: 0, y: 0,
width: terminal.0, width: terminal.0,
height: terminal.1, height: terminal.1,
}) })
} }
fn flush(&mut self) -> Result<(), io::Error> { fn flush(&mut self) -> Result<(), io::Error> {
@ -109,23 +109,33 @@ impl Backend for TermionBackend {
} }
macro_rules! termion_fg { 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 { 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 { 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 { 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 { macro_rules! termion_modifier {
($style:ident) => (format!("{}", termion::style::$style)); ($style:ident) => (
format!("{}", termion::style::$style)
);
} }
impl Color { impl Color {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -98,7 +98,9 @@ impl<'a> Widget for Gauge<'a> {
// Fix colors // Fix colors
for x in gauge_area.left()..end { 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);
} }
} }
} }

View file

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

View file

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

View file

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

View file

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