2023-07-14 08:37:30 +00:00
|
|
|
use std::io;
|
|
|
|
|
2023-07-01 09:14:16 +00:00
|
|
|
use ratatui::{
|
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
|
|
|
layout::Rect,
|
2023-07-14 08:37:30 +00:00
|
|
|
style::{Color, Style, Stylize},
|
2024-05-02 10:09:48 +00:00
|
|
|
widgets::{BarChart, Block, Paragraph},
|
2023-07-01 09:14:16 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
2023-07-14 08:37:30 +00:00
|
|
|
fn barchart_can_be_stylized() {
|
|
|
|
let barchart = BarChart::default()
|
|
|
|
.on_white()
|
|
|
|
.bar_style(Style::new().red())
|
|
|
|
.bar_width(2)
|
|
|
|
.value_style(Style::new().green())
|
|
|
|
.label_style(Style::new().blue())
|
|
|
|
.data(&[("A", 1), ("B", 2), ("C", 3)])
|
|
|
|
.max(3);
|
2023-07-01 09:14:16 +00:00
|
|
|
|
2023-07-14 08:37:30 +00:00
|
|
|
let area = Rect::new(0, 0, 9, 5);
|
|
|
|
let mut terminal = Terminal::new(TestBackend::new(9, 6)).unwrap();
|
2023-07-01 09:14:16 +00:00
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
2023-07-14 08:37:30 +00:00
|
|
|
f.render_widget(barchart, area);
|
2023-07-01 09:14:16 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines([
|
2023-07-14 08:37:30 +00:00
|
|
|
" ██ ",
|
|
|
|
" ▅▅ ██ ",
|
|
|
|
"▂▂ ██ ██ ",
|
|
|
|
"1█ 2█ 3█ ",
|
|
|
|
"A B C ",
|
|
|
|
" ",
|
|
|
|
]);
|
|
|
|
for y in area.y..area.height {
|
|
|
|
// background
|
|
|
|
for x in area.x..area.width {
|
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[(x, y)].set_bg(Color::White);
|
2023-07-14 08:37:30 +00:00
|
|
|
}
|
|
|
|
// bars
|
2024-03-02 09:06:53 +00:00
|
|
|
for x in [0, 1, 3, 4, 6, 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[(x, y)].set_fg(Color::Red);
|
2023-07-14 08:37:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// values
|
|
|
|
for x in 0..3 {
|
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[(x * 3, 3)].set_fg(Color::Green);
|
2023-07-14 08:37:30 +00:00
|
|
|
}
|
|
|
|
// labels
|
|
|
|
for x in 0..3 {
|
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[(x * 3, 4)].set_fg(Color::Blue);
|
|
|
|
expected[(x * 3 + 1, 4)].set_fg(Color::Reset);
|
2023-07-01 09:14:16 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-07-14 08:37:30 +00:00
|
|
|
fn block_can_be_stylized() -> io::Result<()> {
|
2024-05-02 10:09:48 +00:00
|
|
|
let block = Block::bordered()
|
2023-07-14 08:37:30 +00:00
|
|
|
.title("Title".light_blue())
|
|
|
|
.on_cyan()
|
2024-05-02 10:09:48 +00:00
|
|
|
.cyan();
|
2023-07-01 09:14:16 +00:00
|
|
|
|
2023-07-14 08:37:30 +00:00
|
|
|
let area = Rect::new(0, 0, 8, 3);
|
|
|
|
let mut terminal = Terminal::new(TestBackend::new(11, 4))?;
|
|
|
|
terminal.draw(|f| {
|
|
|
|
f.render_widget(block, area);
|
|
|
|
})?;
|
2023-07-01 09:14:16 +00:00
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut expected = Buffer::with_lines([
|
2023-07-14 08:37:30 +00:00
|
|
|
"┌Title─┐ ",
|
|
|
|
"│ │ ",
|
|
|
|
"└──────┘ ",
|
|
|
|
" ",
|
2023-07-01 09:14:16 +00:00
|
|
|
]);
|
2023-07-14 08:37:30 +00:00
|
|
|
for x in area.x..area.width {
|
|
|
|
for y in area.y..area.height {
|
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[(x, y)].set_fg(Color::Cyan).set_bg(Color::Cyan);
|
2023-07-01 09:14:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for x in 1..=5 {
|
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[(x, 0)].set_fg(Color::LightBlue);
|
2023-07-01 09:14:16 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
2023-07-14 08:37:30 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn paragraph_can_be_stylized() -> io::Result<()> {
|
|
|
|
let paragraph = Paragraph::new("Text".cyan());
|
|
|
|
|
|
|
|
let area = Rect::new(0, 0, 10, 1);
|
|
|
|
let mut terminal = Terminal::new(TestBackend::new(10, 1))?;
|
|
|
|
terminal.draw(|f| {
|
|
|
|
f.render_widget(paragraph, area);
|
|
|
|
})?;
|
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines(["Text "]);
|
2023-07-14 08:37:30 +00:00
|
|
|
for x in 0..4 {
|
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[(x, 0)].set_fg(Color::Cyan);
|
2023-07-14 08:37:30 +00:00
|
|
|
}
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
Ok(())
|
2023-07-01 09:14:16 +00:00
|
|
|
}
|