2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
2020-05-21 18:17:55 +00:00
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
2020-09-27 22:28:26 +00:00
|
|
|
layout::{Constraint, Direction, Layout, Rect},
|
2023-10-19 11:29:53 +00:00
|
|
|
style::{Color, Modifier, Style, Stylize},
|
2020-09-27 22:28:26 +00:00
|
|
|
symbols,
|
2021-06-16 18:14:16 +00:00
|
|
|
text::Span,
|
2024-05-02 10:09:48 +00:00
|
|
|
widgets::{Block, 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)
|
2023-10-03 23:50:14 +00:00
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
2024-08-06 03:15:14 +00:00
|
|
|
.split(f.area());
|
2018-11-27 01:02:37 +00:00
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
let gauge = Gauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Percentage"))
|
2020-11-29 18:20:08 +00:00
|
|
|
.gauge_style(Style::default().bg(Color::Blue).fg(Color::Red))
|
2020-11-29 18:27:34 +00:00
|
|
|
.use_unicode(true)
|
2019-12-15 20:38:18 +00:00
|
|
|
.percent(43);
|
|
|
|
f.render_widget(gauge, chunks[0]);
|
|
|
|
let gauge = Gauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Ratio"))
|
2020-11-29 18:20:08 +00:00
|
|
|
.gauge_style(Style::default().bg(Color::Blue).fg(Color::Red))
|
2020-11-29 18:27:34 +00:00
|
|
|
.use_unicode(true)
|
2020-11-29 18:20:08 +00:00
|
|
|
.ratio(0.511_313_934_313_1);
|
|
|
|
f.render_widget(gauge, chunks[1]);
|
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2020-11-29 18:20:08 +00:00
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" ┌Percentage────────────────────────┐ ",
|
2023-06-11 14:23:23 +00:00
|
|
|
" │██████████████▋43% │ ",
|
2020-11-29 18:20:08 +00:00
|
|
|
" └──────────────────────────────────┘ ",
|
|
|
|
" ┌Ratio─────────────────────────────┐ ",
|
2023-06-11 14:23:23 +00:00
|
|
|
" │███████████████51% │ ",
|
2020-11-29 18:20:08 +00:00
|
|
|
" └──────────────────────────────────┘ ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]);
|
2023-10-19 11:29:53 +00:00
|
|
|
expected.set_style(Rect::new(3, 3, 34, 1), Style::new().red().on_blue());
|
2020-11-29 18:20:08 +00:00
|
|
|
|
2023-10-19 11:29:53 +00:00
|
|
|
expected.set_style(Rect::new(3, 6, 15, 1), Style::new().red().on_blue());
|
|
|
|
// Note that filled part of the gauge only covers the 5 and the 1, not the % symbol
|
|
|
|
expected.set_style(Rect::new(18, 6, 2, 1), Style::new().blue().on_red());
|
|
|
|
expected.set_style(Rect::new(20, 6, 17, 1), Style::new().red().on_blue());
|
2020-11-29 18:20:08 +00:00
|
|
|
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_gauge_renders_no_unicode() {
|
|
|
|
let backend = TestBackend::new(40, 10);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
2023-10-03 23:50:14 +00:00
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
2024-08-06 03:15:14 +00:00
|
|
|
.split(f.area());
|
2020-11-29 18:20:08 +00:00
|
|
|
|
|
|
|
let gauge = Gauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Percentage"))
|
2020-11-29 18:20:08 +00:00
|
|
|
.percent(43)
|
|
|
|
.use_unicode(false);
|
|
|
|
f.render_widget(gauge, chunks[0]);
|
|
|
|
let gauge = Gauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Ratio"))
|
2020-11-29 18:20:08 +00:00
|
|
|
.ratio(0.211_313_934_313_1)
|
|
|
|
.use_unicode(false);
|
2019-12-15 20:38:18 +00:00
|
|
|
f.render_widget(gauge, chunks[1]);
|
2018-12-07 15:17:33 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer_lines([
|
2018-11-27 01:02:37 +00:00
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" ┌Percentage────────────────────────┐ ",
|
2023-10-19 11:29:53 +00:00
|
|
|
" │███████████████43% │ ",
|
2018-11-27 01:02:37 +00:00
|
|
|
" └──────────────────────────────────┘ ",
|
|
|
|
" ┌Ratio─────────────────────────────┐ ",
|
2023-10-19 11:29:53 +00:00
|
|
|
" │███████ 21% │ ",
|
2018-11-27 01:02:37 +00:00
|
|
|
" └──────────────────────────────────┘ ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]);
|
|
|
|
}
|
2020-09-27 22:28:26 +00:00
|
|
|
|
2021-06-16 18:14:16 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_gauge_applies_styles() {
|
|
|
|
let backend = TestBackend::new(12, 5);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let gauge = Gauge::default()
|
|
|
|
.block(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::bordered().title(Span::styled("Test", Style::default().fg(Color::Red))),
|
2021-06-16 18:14:16 +00:00
|
|
|
)
|
|
|
|
.gauge_style(Style::default().fg(Color::Blue).bg(Color::Red))
|
|
|
|
.percent(43)
|
|
|
|
.label(Span::styled(
|
|
|
|
"43%",
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Green)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
));
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_widget(gauge, f.area());
|
2021-06-16 18:14:16 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2021-06-16 18:14:16 +00:00
|
|
|
"┌Test──────┐",
|
2023-10-19 11:29:53 +00:00
|
|
|
"│████ │",
|
|
|
|
"│███43% │",
|
|
|
|
"│████ │",
|
2021-06-16 18:14:16 +00:00
|
|
|
"└──────────┘",
|
|
|
|
]);
|
|
|
|
// title
|
|
|
|
expected.set_style(Rect::new(1, 0, 4, 1), Style::default().fg(Color::Red));
|
|
|
|
// gauge area
|
|
|
|
expected.set_style(
|
|
|
|
Rect::new(1, 1, 10, 3),
|
|
|
|
Style::default().fg(Color::Blue).bg(Color::Red),
|
|
|
|
);
|
|
|
|
// filled area
|
2023-10-19 11:29:53 +00:00
|
|
|
expected.set_style(
|
|
|
|
Rect::new(1, 1, 4, 3),
|
|
|
|
Style::default().fg(Color::Blue).bg(Color::Red),
|
|
|
|
);
|
2021-06-16 18:14:16 +00:00
|
|
|
// label (foreground and modifier from label style)
|
|
|
|
expected.set_style(
|
|
|
|
Rect::new(4, 2, 1, 1),
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Green)
|
|
|
|
// "4" is in the filled area so background is gauge_style foreground
|
|
|
|
.bg(Color::Blue)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
);
|
|
|
|
expected.set_style(
|
|
|
|
Rect::new(5, 2, 2, 1),
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Green)
|
|
|
|
// "3%" is not in the filled area so background is gauge_style background
|
|
|
|
.bg(Color::Red)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
);
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_gauge_supports_large_labels() {
|
|
|
|
let backend = TestBackend::new(10, 1);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let gauge = Gauge::default()
|
|
|
|
.percent(43)
|
|
|
|
.label("43333333333333333333333333333%");
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_widget(gauge, f.area());
|
2021-06-16 18:14:16 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer_lines(["4333333333"]);
|
2021-06-16 18:14:16 +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()
|
2024-05-24 18:42:52 +00:00
|
|
|
.filled_style(Style::default().fg(Color::Green))
|
|
|
|
.unfilled_style(Style::default().fg(Color::White))
|
2020-09-27 22:28:26 +00:00
|
|
|
.ratio(0.43);
|
|
|
|
f.render_widget(
|
|
|
|
gauge,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 20,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let gauge = LineGauge::default()
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::bordered().title("Gauge 2"))
|
2024-05-24 18:42:52 +00:00
|
|
|
.filled_style(Style::default().fg(Color::Green))
|
2020-09-27 22:28:26 +00:00
|
|
|
.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();
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2020-09-27 22:28:26 +00:00
|
|
|
"43% ────────────────",
|
|
|
|
"┌Gauge 2───────────┐",
|
|
|
|
"│21% ━━━━━━━━━━━━━━│",
|
|
|
|
"└──────────────────┘",
|
|
|
|
]);
|
|
|
|
for col in 4..10 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(col, 0)].set_fg(Color::Green);
|
2020-09-27 22:28:26 +00:00
|
|
|
}
|
|
|
|
for col in 10..20 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(col, 0)].set_fg(Color::White);
|
2020-09-27 22:28:26 +00:00
|
|
|
}
|
|
|
|
for col in 5..7 {
|
feat(buffer): add Buffer::cell, cell_mut and index implementations (#1084)
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)`
should now use index operators, or be transitioned to `buff.cell()` or
`buf.cell_mut()` for safe access that avoids panics by returning
`Option<&Cell>` and `Option<&mut Cell>`.
The new methods accept `Into<Position>` instead of `x` and `y`
coordinates, which makes them more ergonomic to use.
```rust
let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
let cell = buf[(0, 0)];
let cell = buf[Position::new(0, 0)];
let symbol = buf.cell((0, 0)).map(|cell| cell.symbol());
let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol());
buf[(0, 0)].set_symbol("🐀");
buf[Position::new(0, 0)].set_symbol("🐀");
buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀"));
buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀"));
```
The existing `get()` and `get_mut()` methods are marked as deprecated.
These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: https://github.com/ratatui-org/ratatui/issues/1011
---------
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
2024-08-06 07:40:47 +00:00
|
|
|
expected[(col, 2)].set_fg(Color::Green);
|
2020-09-27 22:28:26 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|