mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 07:04:17 +00:00
a23ecd9b45
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>
93 lines
4 KiB
Rust
93 lines
4 KiB
Rust
use ratatui::{
|
|
backend::TestBackend,
|
|
buffer::Buffer,
|
|
style::{Color, Style},
|
|
widgets::{Bar, BarChart, BarGroup, Block},
|
|
Terminal,
|
|
};
|
|
|
|
// check that bars fill up correctly up to max value
|
|
#[test]
|
|
fn widgets_barchart_not_full_below_max_value() {
|
|
let backend = TestBackend::new(30, 10);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|f| {
|
|
let barchart = BarChart::default()
|
|
.block(Block::bordered())
|
|
.data(&[("empty", 0), ("half", 50), ("almost", 99), ("full", 100)])
|
|
.max(100)
|
|
.bar_width(7)
|
|
.bar_gap(0);
|
|
f.render_widget(barchart, f.area());
|
|
})
|
|
.unwrap();
|
|
terminal.backend().assert_buffer_lines([
|
|
"┌────────────────────────────┐",
|
|
"│ ▇▇▇▇▇▇▇███████│",
|
|
"│ ██████████████│",
|
|
"│ ██████████████│",
|
|
"│ ▄▄▄▄▄▄▄██████████████│",
|
|
"│ █████████████████████│",
|
|
"│ █████████████████████│",
|
|
"│ ██50█████99█████100██│",
|
|
"│ empty half almost full │",
|
|
"└────────────────────────────┘",
|
|
]);
|
|
}
|
|
|
|
#[test]
|
|
fn widgets_barchart_group() {
|
|
const TERMINAL_HEIGHT: u16 = 11;
|
|
let backend = TestBackend::new(35, TERMINAL_HEIGHT);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|f| {
|
|
let barchart = BarChart::default()
|
|
.block(Block::bordered())
|
|
.data(
|
|
BarGroup::default().label("Mar".into()).bars(&[
|
|
Bar::default()
|
|
.value(10)
|
|
.label("C1".into())
|
|
.style(Style::default().fg(Color::Red))
|
|
.value_style(Style::default().fg(Color::Blue)),
|
|
Bar::default()
|
|
.value(20)
|
|
.style(Style::default().fg(Color::Green))
|
|
.text_value("20M".to_string()),
|
|
]),
|
|
)
|
|
.data(&vec![("C1", 50), ("C2", 40)])
|
|
.data(&[("C1", 60), ("C2", 90)])
|
|
.data(&[("xx", 10), ("xx", 10)])
|
|
.group_gap(2)
|
|
.bar_width(4)
|
|
.bar_gap(1);
|
|
f.render_widget(barchart, f.area());
|
|
})
|
|
.unwrap();
|
|
|
|
let mut expected = Buffer::with_lines([
|
|
"┌─────────────────────────────────┐",
|
|
"│ ████│",
|
|
"│ ████│",
|
|
"│ ▅▅▅▅ ████│",
|
|
"│ ▇▇▇▇ ████ ████│",
|
|
"│ ████ ████ ████ ████│",
|
|
"│ ▄▄▄▄ ████ ████ ████ ████│",
|
|
"│▆10▆ 20M█ █50█ █40█ █60█ █90█│",
|
|
"│ C1 C1 C2 C1 C2 │",
|
|
"│Mar │",
|
|
"└─────────────────────────────────┘",
|
|
]);
|
|
for y in 1..(TERMINAL_HEIGHT - 3) {
|
|
for x in 1..5 {
|
|
expected[(x, y)].set_fg(Color::Red);
|
|
expected[(x + 5, y)].set_fg(Color::Green);
|
|
}
|
|
}
|
|
expected[(2, 7)].set_fg(Color::Blue);
|
|
expected[(3, 7)].set_fg(Color::Blue);
|
|
terminal.backend().assert_buffer(&expected);
|
|
}
|