mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 07:04:17 +00:00
perf(buffer)!: filled moves the cell to be filled (#1148)
This commit is contained in:
parent
df4b706674
commit
4ce67fc84e
5 changed files with 41 additions and 25 deletions
|
@ -12,6 +12,7 @@ This is a quick summary of the sections below:
|
||||||
|
|
||||||
- [Unreleased](#unreleased)
|
- [Unreleased](#unreleased)
|
||||||
- `Rect::inner` takes `Margin` directly instead of reference
|
- `Rect::inner` takes `Margin` directly instead of reference
|
||||||
|
- `Buffer::filled` takes `Cell` directly instead of reference
|
||||||
- `Stylize::bg()` now accepts `Into<Color>`
|
- `Stylize::bg()` now accepts `Into<Color>`
|
||||||
- Removed deprecated `List::start_corner`
|
- Removed deprecated `List::start_corner`
|
||||||
- [v0.26.0](#v0260)
|
- [v0.26.0](#v0260)
|
||||||
|
@ -67,6 +68,17 @@ This is a quick summary of the sections below:
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `Buffer::filled` takes `Cell` directly instead of reference ([#1148])
|
||||||
|
|
||||||
|
[#1148]: https://github.com/ratatui-org/ratatui/pull/1148
|
||||||
|
|
||||||
|
`Buffer::filled` moves the `Cell` instead of taking a reference.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
-Buffer::filled(area, &Cell::new("X"));
|
||||||
|
+Buffer::filled(area, Cell::new("X"));
|
||||||
|
```
|
||||||
|
|
||||||
### `Stylize::bg()` now accepts `Into<Color>` ([#1103])
|
### `Stylize::bg()` now accepts `Into<Color>` ([#1103])
|
||||||
|
|
||||||
[#1103]: https://github.com/ratatui-org/ratatui/pull/1103
|
[#1103]: https://github.com/ratatui-org/ratatui/pull/1103
|
||||||
|
|
|
@ -54,14 +54,14 @@ impl Buffer {
|
||||||
/// Returns a Buffer with all cells set to the default one
|
/// Returns a Buffer with all cells set to the default one
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn empty(area: Rect) -> Self {
|
pub fn empty(area: Rect) -> Self {
|
||||||
Self::filled(area, &Cell::EMPTY)
|
Self::filled(area, Cell::EMPTY)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a Buffer with all cells initialized with the attributes of the given Cell
|
/// Returns a Buffer with all cells initialized with the attributes of the given Cell
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn filled(area: Rect, cell: &Cell) -> Self {
|
pub fn filled(area: Rect, cell: Cell) -> Self {
|
||||||
let size = area.area() as usize;
|
let size = area.area() as usize;
|
||||||
let content = vec![cell.clone(); size];
|
let content = vec![cell; size];
|
||||||
Self { area, content }
|
Self { area, content }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -750,7 +750,7 @@ mod tests {
|
||||||
fn diff_empty_filled() {
|
fn diff_empty_filled() {
|
||||||
let area = Rect::new(0, 0, 40, 40);
|
let area = Rect::new(0, 0, 40, 40);
|
||||||
let prev = Buffer::empty(area);
|
let prev = Buffer::empty(area);
|
||||||
let next = Buffer::filled(area, &Cell::new("a"));
|
let next = Buffer::filled(area, Cell::new("a"));
|
||||||
let diff = prev.diff(&next);
|
let diff = prev.diff(&next);
|
||||||
assert_eq!(diff.len(), 40 * 40);
|
assert_eq!(diff.len(), 40 * 40);
|
||||||
}
|
}
|
||||||
|
@ -758,8 +758,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn diff_filled_filled() {
|
fn diff_filled_filled() {
|
||||||
let area = Rect::new(0, 0, 40, 40);
|
let area = Rect::new(0, 0, 40, 40);
|
||||||
let prev = Buffer::filled(area, &Cell::new("a"));
|
let prev = Buffer::filled(area, Cell::new("a"));
|
||||||
let next = Buffer::filled(area, &Cell::new("a"));
|
let next = Buffer::filled(area, Cell::new("a"));
|
||||||
let diff = prev.diff(&next);
|
let diff = prev.diff(&next);
|
||||||
assert_eq!(diff, []);
|
assert_eq!(diff, []);
|
||||||
}
|
}
|
||||||
|
@ -853,8 +853,8 @@ mod tests {
|
||||||
Lines: IntoIterator,
|
Lines: IntoIterator,
|
||||||
Lines::Item: Into<Line<'line>>,
|
Lines::Item: Into<Line<'line>>,
|
||||||
{
|
{
|
||||||
let mut one = Buffer::filled(one, &Cell::new("1"));
|
let mut one = Buffer::filled(one, Cell::new("1"));
|
||||||
let two = Buffer::filled(two, &Cell::new("2"));
|
let two = Buffer::filled(two, Cell::new("2"));
|
||||||
one.merge(&two);
|
one.merge(&two);
|
||||||
assert_eq!(one, Buffer::with_lines(expected));
|
assert_eq!(one, Buffer::with_lines(expected));
|
||||||
}
|
}
|
||||||
|
@ -868,7 +868,7 @@ mod tests {
|
||||||
width: 2,
|
width: 2,
|
||||||
height: 2,
|
height: 2,
|
||||||
},
|
},
|
||||||
&Cell::new("1"),
|
Cell::new("1"),
|
||||||
);
|
);
|
||||||
let two = Buffer::filled(
|
let two = Buffer::filled(
|
||||||
Rect {
|
Rect {
|
||||||
|
@ -877,7 +877,7 @@ mod tests {
|
||||||
width: 3,
|
width: 3,
|
||||||
height: 4,
|
height: 4,
|
||||||
},
|
},
|
||||||
&Cell::new("2"),
|
Cell::new("2"),
|
||||||
);
|
);
|
||||||
one.merge(&two);
|
one.merge(&two);
|
||||||
let mut expected = Buffer::with_lines(["222 ", "222 ", "2221", "2221"]);
|
let mut expected = Buffer::with_lines(["222 ", "222 ", "2221", "2221"]);
|
||||||
|
@ -893,25 +893,29 @@ mod tests {
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[case(false, true, [false, false, true, true, true, true])]
|
#[case(false, true, [false, false, true, true, true, true])]
|
||||||
#[case(true, false, [true, true, false, false, false, false])]
|
#[case(true, false, [true, true, false, false, false, false])]
|
||||||
fn merge_skip(#[case] one: bool, #[case] two: bool, #[case] expected: [bool; 6]) {
|
fn merge_skip(#[case] skip_one: bool, #[case] skip_two: bool, #[case] expected: [bool; 6]) {
|
||||||
let mut one = Buffer::filled(
|
let mut one = {
|
||||||
Rect {
|
let area = Rect {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
width: 2,
|
width: 2,
|
||||||
height: 2,
|
height: 2,
|
||||||
},
|
};
|
||||||
Cell::new("1").set_skip(one),
|
let mut cell = Cell::new("1");
|
||||||
);
|
cell.skip = skip_one;
|
||||||
let two = Buffer::filled(
|
Buffer::filled(area, cell)
|
||||||
Rect {
|
};
|
||||||
|
let two = {
|
||||||
|
let area = Rect {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 1,
|
y: 1,
|
||||||
width: 2,
|
width: 2,
|
||||||
height: 2,
|
height: 2,
|
||||||
},
|
};
|
||||||
Cell::new("2").set_skip(two),
|
let mut cell = Cell::new("2");
|
||||||
);
|
cell.skip = skip_two;
|
||||||
|
Buffer::filled(area, cell)
|
||||||
|
};
|
||||||
one.merge(&two);
|
one.merge(&two);
|
||||||
let skipped = one.content().iter().map(|c| c.skip).collect::<Vec<_>>();
|
let skipped = one.content().iter().map(|c| c.skip).collect::<Vec<_>>();
|
||||||
assert_eq!(skipped, expected);
|
assert_eq!(skipped, expected);
|
||||||
|
|
|
@ -1169,7 +1169,7 @@ mod tests {
|
||||||
fn render_truncates_away_from_0x0(#[case] alignment: Alignment, #[case] expected: &str) {
|
fn render_truncates_away_from_0x0(#[case] alignment: Alignment, #[case] expected: &str) {
|
||||||
let line = Line::from(vec![Span::raw("a🦀b"), Span::raw("c🦀d")]).alignment(alignment);
|
let line = Line::from(vec![Span::raw("a🦀b"), Span::raw("c🦀d")]).alignment(alignment);
|
||||||
// Fill buffer with stuff to ensure the output is indeed padded
|
// Fill buffer with stuff to ensure the output is indeed padded
|
||||||
let mut buf = Buffer::filled(Rect::new(0, 0, 10, 1), &Cell::new("X"));
|
let mut buf = Buffer::filled(Rect::new(0, 0, 10, 1), Cell::new("X"));
|
||||||
let area = Rect::new(2, 0, 6, 1);
|
let area = Rect::new(2, 0, 6, 1);
|
||||||
line.render_ref(area, &mut buf);
|
line.render_ref(area, &mut buf);
|
||||||
assert_eq!(buf, Buffer::with_lines([expected]));
|
assert_eq!(buf, Buffer::with_lines([expected]));
|
||||||
|
@ -1188,7 +1188,7 @@ mod tests {
|
||||||
let line = Line::from(vec![Span::raw("a🦀b"), Span::raw("c🦀d")]).right_aligned();
|
let line = Line::from(vec![Span::raw("a🦀b"), Span::raw("c🦀d")]).right_aligned();
|
||||||
let area = Rect::new(0, 0, buf_width, 1);
|
let area = Rect::new(0, 0, buf_width, 1);
|
||||||
// Fill buffer with stuff to ensure the output is indeed padded
|
// Fill buffer with stuff to ensure the output is indeed padded
|
||||||
let mut buf = Buffer::filled(area, &Cell::new("X"));
|
let mut buf = Buffer::filled(area, Cell::new("X"));
|
||||||
line.render_ref(buf.area, &mut buf);
|
line.render_ref(buf.area, &mut buf);
|
||||||
assert_eq!(buf, Buffer::with_lines([expected]));
|
assert_eq!(buf, Buffer::with_lines([expected]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -817,7 +817,7 @@ mod tests {
|
||||||
// results in the expected output
|
// results in the expected output
|
||||||
fn test_marker(marker: Marker, expected: &str) {
|
fn test_marker(marker: Marker, expected: &str) {
|
||||||
let area = Rect::new(0, 0, 5, 5);
|
let area = Rect::new(0, 0, 5, 5);
|
||||||
let mut buf = Buffer::filled(area, &Cell::new("x"));
|
let mut buf = Buffer::filled(area, Cell::new("x"));
|
||||||
let horizontal_line = Line {
|
let horizontal_line = Line {
|
||||||
x1: 0.0,
|
x1: 0.0,
|
||||||
y1: 0.0,
|
y1: 0.0,
|
||||||
|
|
|
@ -239,7 +239,7 @@ mod tests {
|
||||||
// filled with x symbols to make it easier to assert on the result
|
// filled with x symbols to make it easier to assert on the result
|
||||||
fn render(widget: Sparkline, width: u16) -> Buffer {
|
fn render(widget: Sparkline, width: u16) -> Buffer {
|
||||||
let area = Rect::new(0, 0, width, 1);
|
let area = Rect::new(0, 0, width, 1);
|
||||||
let mut buffer = Buffer::filled(area, &Cell::new("x"));
|
let mut buffer = Buffer::filled(area, Cell::new("x"));
|
||||||
widget.render(area, &mut buffer);
|
widget.render(area, &mut buffer);
|
||||||
buffer
|
buffer
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue