2020-05-21 18:17:55 +00:00
|
|
|
use tui::{
|
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
2020-09-27 22:28:26 +00:00
|
|
|
layout::{Constraint, Direction, Layout, Rect},
|
|
|
|
style::{Color, Style},
|
|
|
|
symbols,
|
|
|
|
widgets::{Block, Borders, Gauge, LineGauge},
|
2020-05-21 18:17:55 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2018-11-27 01:02:37 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_gauge_renders() {
|
2018-11-27 01:02:37 +00:00
|
|
|
let backend = TestBackend::new(40, 10);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
2020-06-15 20:57:23 +00:00
|
|
|
.draw(|f| {
|
2018-11-27 01:02:37 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-12-07 20:20:27 +00:00
|
|
|
.split(f.size());
|
2018-11-27 01:02:37 +00:00
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
let gauge = Gauge::default()
|
2018-11-27 01:02:37 +00:00
|
|
|
.block(Block::default().title("Percentage").borders(Borders::ALL))
|
2019-12-15 20:38:18 +00:00
|
|
|
.percent(43);
|
|
|
|
f.render_widget(gauge, chunks[0]);
|
|
|
|
let gauge = Gauge::default()
|
2018-11-27 01:02:37 +00:00
|
|
|
.block(Block::default().title("Ratio").borders(Borders::ALL))
|
2019-12-15 20:38:18 +00:00
|
|
|
.ratio(0.211_313_934_313_1);
|
|
|
|
f.render_widget(gauge, chunks[1]);
|
2018-12-07 15:17:33 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-11-27 01:02:37 +00:00
|
|
|
let expected = Buffer::with_lines(vec![
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" ┌Percentage────────────────────────┐ ",
|
|
|
|
" │ 43% │ ",
|
|
|
|
" └──────────────────────────────────┘ ",
|
|
|
|
" ┌Ratio─────────────────────────────┐ ",
|
|
|
|
" │ 21% │ ",
|
|
|
|
" └──────────────────────────────────┘ ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]);
|
2020-05-21 18:17:55 +00:00
|
|
|
terminal.backend().assert_buffer(&expected);
|
2018-11-27 01:02:37 +00:00
|
|
|
}
|
2020-09-27 22:28:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_line_gauge_renders() {
|
|
|
|
let backend = TestBackend::new(20, 4);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let gauge = LineGauge::default()
|
|
|
|
.gauge_style(Style::default().fg(Color::Green).bg(Color::White))
|
|
|
|
.ratio(0.43);
|
|
|
|
f.render_widget(
|
|
|
|
gauge,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 20,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let gauge = LineGauge::default()
|
|
|
|
.block(Block::default().title("Gauge 2").borders(Borders::ALL))
|
|
|
|
.gauge_style(Style::default().fg(Color::Green))
|
|
|
|
.line_set(symbols::line::THICK)
|
|
|
|
.ratio(0.211_313_934_313_1);
|
|
|
|
f.render_widget(
|
|
|
|
gauge,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 1,
|
|
|
|
width: 20,
|
|
|
|
height: 3,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let mut expected = Buffer::with_lines(vec![
|
|
|
|
"43% ────────────────",
|
|
|
|
"┌Gauge 2───────────┐",
|
|
|
|
"│21% ━━━━━━━━━━━━━━│",
|
|
|
|
"└──────────────────┘",
|
|
|
|
]);
|
|
|
|
for col in 4..10 {
|
|
|
|
expected.get_mut(col, 0).set_fg(Color::Green);
|
|
|
|
}
|
|
|
|
for col in 10..20 {
|
|
|
|
expected.get_mut(col, 0).set_fg(Color::White);
|
|
|
|
}
|
|
|
|
for col in 5..7 {
|
|
|
|
expected.get_mut(col, 2).set_fg(Color::Green);
|
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|