mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 20:53:19 +00:00
67c0ea243b
`ratatui::widgets::block::Title` is deprecated in favor of using `Line` to represent titles. This removes an unnecessary layer of wrapping (string -> Span -> Line -> Title). This struct will be removed in a future release of Ratatui (likely 0.31). For more information see: <https://github.com/ratatui/ratatui/issues/738> To update your code: ```rust Block::new().title(Title::from("foo")); // becomes any of Block::new().title("foo"); Block::new().title(Line::from("foo")); Block::new().title(Title::from("foo").position(Position::TOP)); // becomes any of Block::new().title_top("foo"); Block::new().title_top(Line::from("foo")); Block::new().title(Title::from("foo").position(Position::BOTTOM)); // becomes any of Block::new().title_bottom("foo"); Block::new().title_bottom(Line::from("foo")); ```
55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
use criterion::{criterion_group, BatchSize, Bencher, Criterion};
|
|
use ratatui::{
|
|
buffer::Buffer,
|
|
layout::{Alignment, Rect},
|
|
text::Line,
|
|
widgets::{Block, Padding, Widget},
|
|
};
|
|
|
|
/// Benchmark for rendering a block.
|
|
fn block(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("block");
|
|
|
|
for (width, height) in [
|
|
(100, 50), // vertically split screen
|
|
(200, 50), // 1080p fullscreen with medium font
|
|
(256, 256), // Max sized area
|
|
] {
|
|
let buffer_size = Rect::new(0, 0, width, height);
|
|
|
|
// Render an empty block
|
|
group.bench_with_input(
|
|
format!("render_empty/{width}x{height}"),
|
|
&Block::new(),
|
|
|b, block| render(b, block, buffer_size),
|
|
);
|
|
|
|
// Render with all features
|
|
group.bench_with_input(
|
|
format!("render_all_feature/{width}x{height}"),
|
|
&Block::bordered()
|
|
.padding(Padding::new(5, 5, 2, 2))
|
|
.title("test title")
|
|
.title_bottom(Line::from("bottom left title").alignment(Alignment::Right)),
|
|
|b, block| render(b, block, buffer_size),
|
|
);
|
|
}
|
|
|
|
group.finish();
|
|
}
|
|
|
|
/// render the block into a buffer of the given `size`
|
|
fn render(bencher: &mut Bencher, block: &Block, size: Rect) {
|
|
let mut buffer = Buffer::empty(size);
|
|
// We use `iter_batched` to clone the value in the setup function.
|
|
// See https://github.com/ratatui/ratatui/pull/377.
|
|
bencher.iter_batched(
|
|
|| block.to_owned(),
|
|
|bench_block| {
|
|
bench_block.render(buffer.area, &mut buffer);
|
|
},
|
|
BatchSize::SmallInput,
|
|
);
|
|
}
|
|
|
|
criterion_group!(benches, block);
|