2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
2021-10-17 16:44:00 +00:00
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
|
|
|
style::{Color, Style},
|
|
|
|
text::Span,
|
|
|
|
widgets::canvas::Canvas,
|
|
|
|
Terminal,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_canvas_draw_labels() {
|
|
|
|
let backend = TestBackend::new(5, 5);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
let label = String::from("test");
|
|
|
|
let canvas = Canvas::default()
|
|
|
|
.background_color(Color::Yellow)
|
|
|
|
.x_bounds([0.0, 5.0])
|
|
|
|
.y_bounds([0.0, 5.0])
|
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.print(
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
Span::styled(label.clone(), Style::default().fg(Color::Blue)),
|
|
|
|
);
|
|
|
|
});
|
2024-08-06 03:15:14 +00:00
|
|
|
f.render_widget(canvas, f.area());
|
2021-10-17 16:44:00 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2024-05-14 01:13:46 +00:00
|
|
|
let mut expected = Buffer::with_lines(["", "", "", "", "test "]);
|
2021-10-17 16:44:00 +00:00
|
|
|
for row in 0..5 {
|
|
|
|
for col in 0..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[(col, row)].set_bg(Color::Yellow);
|
2021-10-17 16:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for col 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[(col, 4)].set_fg(Color::Blue);
|
2021-10-17 16:44:00 +00:00
|
|
|
}
|
2023-05-22 03:45:37 +00:00
|
|
|
terminal.backend().assert_buffer(&expected);
|
2021-10-17 16:44:00 +00:00
|
|
|
}
|