chore: deprecate Buffer::get and get_mut

Replace with indexer (Buffer[])
This commit is contained in:
Josh McKinney 2024-05-21 09:36:23 -07:00
parent 71e8bd299c
commit ad40261c71
No known key found for this signature in database
GPG key ID: 722287396A903BC5
30 changed files with 123 additions and 135 deletions

View file

@ -39,7 +39,7 @@ use crossterm::{
ExecutableCommand, ExecutableCommand,
}; };
use palette::{convert::FromColorUnclamped, Okhsv, Srgb}; use palette::{convert::FromColorUnclamped, Okhsv, Srgb};
use ratatui::prelude::*; use ratatui::{layout::Position, prelude::*};
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct App { struct App {
@ -217,7 +217,7 @@ impl Widget for &mut ColorsWidget {
// pixel below it // pixel below it
let fg = colors[yi * 2][xi]; let fg = colors[yi * 2][xi];
let bg = colors[yi * 2 + 1][xi]; let bg = colors[yi * 2 + 1][xi];
buf.get_mut(x, y).set_char('▀').set_fg(fg).set_bg(bg); buf[Position::new(x, y)].set_char('▀').set_fg(fg).set_bg(bg);
} }
} }
self.frame_count += 1; self.frame_count += 1;

View file

@ -238,7 +238,7 @@ impl App {
for (i, cell) in visible_content.enumerate() { for (i, cell) in visible_content.enumerate() {
let x = i as u16 % area.width; let x = i as u16 % area.width;
let y = i as u16 / area.width; let y = i as u16 / area.width;
*buf.get_mut(area.x + x, area.y + y) = cell; buf[(area.x + x, area.y + y)] = cell;
} }
if scrollbar_needed { if scrollbar_needed {

View file

@ -245,7 +245,7 @@ fn render_glyph(glyph: [u8; 8], area: Rect, buf: &mut Buffer, pixel_size: PixelS
.clone() .clone()
.zip(area.left()..area.right()) .zip(area.left()..area.right())
{ {
let cell = buf.get_mut(x, y); let cell = &mut buf[(x, y)];
let symbol_character = match pixel_size { let symbol_character = match pixel_size {
PixelSize::Full => match glyph[row] & (1 << col) { PixelSize::Full => match glyph[row] & (1 << col) {
0 => ' ', 0 => ' ',

View file

@ -19,7 +19,7 @@ impl Widget for RgbSwatch {
let hue = xi as f32 * 360.0 / f32::from(area.width); let hue = xi as f32 * 360.0 / f32::from(area.width);
let fg = color_from_oklab(hue, Okhsv::max_saturation(), value_fg); let fg = color_from_oklab(hue, Okhsv::max_saturation(), value_fg);
let bg = color_from_oklab(hue, Okhsv::max_saturation(), value_bg); let bg = color_from_oklab(hue, Okhsv::max_saturation(), value_bg);
buf.get_mut(x, y).set_char('▀').set_fg(fg).set_bg(bg); buf[(x, y)].set_char('▀').set_fg(fg).set_bg(bg);
} }
} }
} }

View file

@ -45,7 +45,7 @@ fn drip(frame_count: usize, area: Rect, buf: &mut Buffer) {
for _ in 0..pixel_count { for _ in 0..pixel_count {
let src_x = rng.gen_range(0..area.width); let src_x = rng.gen_range(0..area.width);
let src_y = rng.gen_range(1..area.height - 2); let src_y = rng.gen_range(1..area.height - 2);
let src = buf.get_mut(src_x, src_y).clone(); let src = buf[(src_x, src_y)].clone();
// 1% of the time, move a blank or pixel (10:1) to the top line of the screen // 1% of the time, move a blank or pixel (10:1) to the top line of the screen
if rng.gen_ratio(1, 100) { if rng.gen_ratio(1, 100) {
let dest_x = rng let dest_x = rng
@ -53,7 +53,7 @@ fn drip(frame_count: usize, area: Rect, buf: &mut Buffer) {
.clamp(area.left(), area.right() - 1); .clamp(area.left(), area.right() - 1);
let dest_y = area.top() + 1; let dest_y = area.top() + 1;
let dest = buf.get_mut(dest_x, dest_y); let dest = &mut buf[(dest_x, dest_y)];
// copy the cell to the new location about 1/10 of the time blank out the cell the rest // copy the cell to the new location about 1/10 of the time blank out the cell the rest
// of the time. This has the effect of gradually removing the pixels from the screen. // of the time. This has the effect of gradually removing the pixels from the screen.
if rng.gen_ratio(1, 10) { if rng.gen_ratio(1, 10) {
@ -66,8 +66,7 @@ fn drip(frame_count: usize, area: Rect, buf: &mut Buffer) {
let dest_x = src_x; let dest_x = src_x;
let dest_y = src_y.saturating_add(1).min(area.bottom() - 2); let dest_y = src_y.saturating_add(1).min(area.bottom() - 2);
// copy the cell to the new location // copy the cell to the new location
let dest = buf.get_mut(dest_x, dest_y); buf[(dest_x, dest_y)] = src;
*dest = src;
} }
} }
} }
@ -98,8 +97,8 @@ fn text(frame_count: usize, area: Rect, buf: &mut Buffer) {
for row in area.rows() { for row in area.rows() {
for col in row.columns() { for col in row.columns() {
let cell = buf.get_mut(col.x, col.y); let cell = &mut buf[(col.x, col.y)];
let mask_cell = mask_buf.get(col.x, col.y); let mask_cell = &mut mask_buf[(col.x, col.y)];
cell.set_symbol(mask_cell.symbol()); cell.set_symbol(mask_cell.symbol());
// blend the mask cell color with the cell color // blend the mask cell color with the cell color

View file

@ -116,7 +116,7 @@ pub fn render_logo(selected_row: usize, area: Rect, buf: &mut Buffer) {
for (x, (ch1, ch2)) in line1.chars().zip(line2.chars()).enumerate() { for (x, (ch1, ch2)) in line1.chars().zip(line2.chars()).enumerate() {
let x = area.left() + x as u16; let x = area.left() + x as u16;
let y = area.top() + y as u16; let y = area.top() + y as u16;
let cell = buf.get_mut(x, y); let cell = &mut buf[(x, y)];
let rat_color = THEME.logo.rat; let rat_color = THEME.logo.rat;
let term_color = THEME.logo.term; let term_color = THEME.logo.term;
match (ch1, ch2) { match (ch1, ch2) {

View file

@ -334,7 +334,7 @@ impl App {
for (i, cell) in visible_content.enumerate() { for (i, cell) in visible_content.enumerate() {
let x = i as u16 % area.width; let x = i as u16 % area.width;
let y = i as u16 / area.width; let y = i as u16 / area.width;
*buf.get_mut(area.x + x, area.y + y) = cell; buf[(area.x + x, area.y + y)] = cell;
} }
if scrollbar_needed { if scrollbar_needed {

View file

@ -139,8 +139,7 @@ impl Backend for TestBackend {
I: Iterator<Item = (u16, u16, &'a Cell)>, I: Iterator<Item = (u16, u16, &'a Cell)>,
{ {
for (x, y, c) in content { for (x, y, c) in content {
let cell = self.buffer.get_mut(x, y); self.buffer[(x, y)] = c.clone();
*cell = c.clone();
} }
Ok(()) Ok(())
} }

View file

@ -18,7 +18,7 @@ macro_rules! assert_buffer_eq {
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(|(i, (x, y, cell))| { .map(|(i, (x, y, cell))| {
let expected_cell = expected.get(x, y); let expected_cell = &expected[(x, y)];
format!("{i}: at ({x}, {y})\n expected: {expected_cell:?}\n actual: {cell:?}") format!("{i}: at ({x}, {y})\n expected: {expected_cell:?}\n actual: {cell:?}")
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()

View file

@ -23,8 +23,8 @@ use crate::{buffer::Cell, layout::Position, prelude::*};
/// width: 10, /// width: 10,
/// height: 5, /// height: 5,
/// }); /// });
/// buf.get_mut(0, 2).set_symbol("x"); /// buf[(0, 2)].set_symbol("x");
/// assert_eq!(buf.get(0, 2).symbol(), "x"); /// assert_eq!(buf[(0, 2)].symbol(), "x");
/// ///
/// buf.set_string( /// buf.set_string(
/// 3, /// 3,
@ -32,13 +32,13 @@ use crate::{buffer::Cell, layout::Position, prelude::*};
/// "string", /// "string",
/// Style::default().fg(Color::Red).bg(Color::White), /// Style::default().fg(Color::Red).bg(Color::White),
/// ); /// );
/// let cell = buf.get(5, 0); /// let cell = &buf[(5, 0)];
/// assert_eq!(cell.symbol(), "r"); /// assert_eq!(cell.symbol(), "r");
/// assert_eq!(cell.fg, Color::Red); /// assert_eq!(cell.fg, Color::Red);
/// assert_eq!(cell.bg, Color::White); /// assert_eq!(cell.bg, Color::White);
/// ///
/// buf.get_mut(5, 0).set_char('x'); /// buf[(5, 0)].set_char('x');
/// assert_eq!(buf.get(5, 0).symbol(), "x"); /// assert_eq!(buf[(5, 0)].symbol(), "x");
/// ``` /// ```
#[derive(Default, Clone, Eq, PartialEq, Hash)] #[derive(Default, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@ -94,8 +94,8 @@ impl Buffer {
/// Returns a reference to Cell at the given coordinates /// Returns a reference to Cell at the given coordinates
/// ///
/// Callers should generally use the Index trait (`buf[(x, y)]`) or the [`Buffer::cell`] method /// Callers should generally use the [`ops::Index`] trait ([`Buffer[Position]`]) or the
/// instead of this method. /// [`Buffer::cell`] method instead of this method.
/// ///
/// Note that conventionally methods named `get` usually return `Option<&T>`, but this method /// Note that conventionally methods named `get` usually return `Option<&T>`, but this method
/// panics instead. This is kept for backwards compatibility. See `get_opt` for a safe /// panics instead. This is kept for backwards compatibility. See `get_opt` for a safe
@ -105,6 +105,7 @@ impl Buffer {
/// ///
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[track_caller] #[track_caller]
#[deprecated(note = "Use Buffer[] or Buffer::cell instead")]
pub fn get(&self, x: u16, y: u16) -> &Cell { pub fn get(&self, x: u16, y: u16) -> &Cell {
let i = self.index_of(x, y); let i = self.index_of(x, y);
&self.content[i] &self.content[i]
@ -112,7 +113,7 @@ impl Buffer {
/// Returns a mutable reference to Cell at the given coordinates /// Returns a mutable reference to Cell at the given coordinates
/// ///
/// Callers should generally use the `IndexMut` trait (`&mut buf[(x, y)]`) or the /// Callers should generally use the [`ops::IndexMut`] trait (`&mut Buffer[Position]`) or the
/// [`Buffer::cell_mut`] method instead of this method. /// [`Buffer::cell_mut`] method instead of this method.
/// ///
/// Note that conventionally methods named `get_mut` usually return `Option<&mut T>`, but this /// Note that conventionally methods named `get_mut` usually return `Option<&mut T>`, but this
@ -123,6 +124,7 @@ impl Buffer {
/// ///
/// Panics if the index is out of bounds. /// Panics if the index is out of bounds.
#[track_caller] #[track_caller]
#[deprecated(note = "Use Buffer[] or Buffer::cell_mut instead")]
pub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell { pub fn get_mut(&mut self, x: u16, y: u16) -> &mut Cell {
let i = self.index_of(x, y); let i = self.index_of(x, y);
&mut self.content[i] &mut self.content[i]
@ -162,11 +164,12 @@ impl Buffer {
/// ```rust /// ```rust
/// # use ratatui::{prelude::*, buffer::Cell, layout::Position}; /// # use ratatui::{prelude::*, buffer::Cell, layout::Position};
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); /// let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10));
/// /// if let Some(cell) = buffer.cell_mut(Position::new(0, 0)) {
/// assert_eq!(buffer.cell(Position::new(0, 0)), Some(&mut Cell::default())); /// *cell = Cell::default();
/// assert_eq!(buffer.cell(Position::new(10, 10)), None); /// }
/// assert_eq!(buffer.cell((0, 0)), Some(&mut Cell::default())); /// if let Some(cell) = buffer.cell_mut((0, 0)) {
/// assert_eq!(buffer.cell((10, 10)), None); /// *cell = Cell::default();
/// }
/// ``` /// ```
pub fn cell_mut<P: Into<Position>>(&mut self, pos: P) -> Option<&mut Cell> { pub fn cell_mut<P: Into<Position>>(&mut self, pos: P) -> Option<&mut Cell> {
let pos = pos.into(); let pos = pos.into();
@ -297,12 +300,12 @@ impl Buffer {
}); });
let style = style.into(); let style = style.into();
for (symbol, width) in graphemes { for (symbol, width) in graphemes {
self.get_mut(x, y).set_symbol(symbol).set_style(style); self[(x, y)].set_symbol(symbol).set_style(style);
let next_symbol = x + width; let next_symbol = x + width;
x += 1; x += 1;
// Reset following cells if multi-width (they would be hidden by the grapheme), // Reset following cells if multi-width (they would be hidden by the grapheme),
while x < next_symbol { while x < next_symbol {
self.get_mut(x, y).reset(); self[(x, y)].reset();
x += 1; x += 1;
} }
} }
@ -345,7 +348,7 @@ impl Buffer {
let area = self.area.intersection(area); let area = self.area.intersection(area);
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {
for x in area.left()..area.right() { for x in area.left()..area.right() {
self.get_mut(x, y).set_style(style); self[(x, y)].set_style(style);
} }
} }
} }
@ -465,8 +468,8 @@ impl<P: Into<Position>> ops::Index<P> for Buffer {
/// ``` /// ```
/// # use ratatui::{prelude::*, buffer::Cell, layout::Position}; /// # use ratatui::{prelude::*, buffer::Cell, layout::Position};
/// let buf = Buffer::empty(Rect::new(0, 0, 10, 10)); /// let buf = Buffer::empty(Rect::new(0, 0, 10, 10));
/// let cell = buf[(0, 0)]; /// let cell = &buf[(0, 0)];
/// let cell = buf[Position::new(0, 0)]; /// let cell = &buf[Position::new(0, 0)];
/// ``` /// ```
fn index(&self, pos: P) -> &Self::Output { fn index(&self, pos: P) -> &Self::Output {
let pos = pos.into(); let pos = pos.into();

View file

@ -291,7 +291,7 @@ impl Rect {
/// # use ratatui::prelude::*; /// # use ratatui::prelude::*;
/// fn render(area: Rect, buf: &mut Buffer) { /// fn render(area: Rect, buf: &mut Buffer) {
/// for position in area.positions() { /// for position in area.positions() {
/// buf.get_mut(position.x, position.y).set_symbol("x"); /// buf[(position.x, position.y)].set_symbol("x");
/// } /// }
/// } /// }
/// ``` /// ```

View file

@ -179,7 +179,7 @@ impl fmt::Debug for Modifier {
/// ]; /// ];
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1)); /// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
/// for style in &styles { /// for style in &styles {
/// buffer.get_mut(0, 0).set_style(*style); /// buffer[(0, 0)].set_style(*style);
/// } /// }
/// assert_eq!( /// assert_eq!(
/// Style { /// Style {
@ -190,7 +190,7 @@ impl fmt::Debug for Modifier {
/// add_modifier: Modifier::BOLD | Modifier::UNDERLINED, /// add_modifier: Modifier::BOLD | Modifier::UNDERLINED,
/// sub_modifier: Modifier::empty(), /// sub_modifier: Modifier::empty(),
/// }, /// },
/// buffer.get(0, 0).style(), /// buffer[(0, 0)].style(),
/// ); /// );
/// ``` /// ```
/// ///
@ -208,7 +208,7 @@ impl fmt::Debug for Modifier {
/// ]; /// ];
/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1)); /// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
/// for style in &styles { /// for style in &styles {
/// buffer.get_mut(0, 0).set_style(*style); /// buffer[(0, 0)].set_style(*style);
/// } /// }
/// assert_eq!( /// assert_eq!(
/// Style { /// Style {
@ -219,7 +219,7 @@ impl fmt::Debug for Modifier {
/// add_modifier: Modifier::empty(), /// add_modifier: Modifier::empty(),
/// sub_modifier: Modifier::empty(), /// sub_modifier: Modifier::empty(),
/// }, /// },
/// buffer.get(0, 0).style(), /// buffer[(0, 0)].style(),
/// ); /// );
/// ``` /// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
@ -600,9 +600,9 @@ mod tests {
let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1)); let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1));
for m in mods { for m in mods {
buffer.get_mut(0, 0).set_style(Style::reset()); buffer[(0, 0)].set_style(Style::reset());
buffer.get_mut(0, 0).set_style(Style::new().add_modifier(m)); buffer[(0, 0)].set_style(Style::new().add_modifier(m));
let style = buffer.get(0, 0).style(); let style = buffer[(0, 0)].style();
assert!(style.add_modifier.contains(m)); assert!(style.add_modifier.contains(m));
assert!(!style.sub_modifier.contains(m)); assert!(!style.sub_modifier.contains(m));
} }

View file

@ -377,18 +377,16 @@ impl WidgetRef for Span<'_> {
if next_x > max_x { if next_x > max_x {
break; break;
} }
buf.get_mut(current_x, y) buf[(current_x, y)].set_symbol(g.symbol).set_style(g.style);
.set_symbol(g.symbol)
.set_style(g.style);
// multi-width graphemes must clear the cells of characters that are hidden by the // multi-width graphemes must clear the cells of characters that are hidden by the
// grapheme, otherwise the hidden characters will be re-rendered if the grapheme is // grapheme, otherwise the hidden characters will be re-rendered if the grapheme is
// overwritten. // overwritten.
for i in (current_x + 1)..next_x { for i in (current_x + 1)..next_x {
buf.get_mut(i, y).reset(); buf[(i, y)].reset();
// it may seem odd that the style of the hidden cells are not set to the style of // it may seem odd that the style of the hidden cells are not set to the style of
// the grapheme, but this is how the existing buffer.set_span() method works. // the grapheme, but this is how the existing buffer.set_span() method works.
// buf.get_mut(i, y).set_style(g.style); // buf[(i, y)].set_style(g.style);
} }
current_x = next_x; current_x = next_x;
} }

View file

@ -431,7 +431,7 @@ impl BarChart<'_> {
} else { } else {
self.bar_set.empty self.bar_set.empty
}; };
buf.get_mut(bars_area.left() + x, bar_y) buf[(bars_area.left() + x, bar_y)]
.set_symbol(symbol) .set_symbol(symbol)
.set_style(bar_style); .set_style(bar_style);
} }
@ -507,7 +507,7 @@ impl BarChart<'_> {
let bar_style = self.bar_style.patch(bar.style); let bar_style = self.bar_style.patch(bar.style);
for x in 0..self.bar_width { for x in 0..self.bar_width {
buf.get_mut(bar_x + x, area.top() + j) buf[(bar_x + x, area.top() + j)]
.set_symbol(symbol) .set_symbol(symbol)
.set_style(bar_style); .set_style(bar_style);
} }
@ -698,7 +698,7 @@ mod tests {
"f b ", "f b ",
]); ]);
for (x, y) in iproduct!([0, 2], [0, 1]) { for (x, y) in iproduct!([0, 2], [0, 1]) {
expected.get_mut(x, y).set_fg(Color::Red); expected[(x, y)].set_fg(Color::Red);
} }
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -790,8 +790,8 @@ mod tests {
"█1█ █2█ ", "█1█ █2█ ",
"foo bar ", "foo bar ",
]); ]);
expected.get_mut(1, 1).set_fg(Color::Red); expected[(1, 1)].set_fg(Color::Red);
expected.get_mut(5, 1).set_fg(Color::Red); expected[(5, 1)].set_fg(Color::Red);
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -808,8 +808,8 @@ mod tests {
"1 2 ", "1 2 ",
"f b ", "f b ",
]); ]);
expected.get_mut(0, 2).set_fg(Color::Red); expected[(0, 2)].set_fg(Color::Red);
expected.get_mut(2, 2).set_fg(Color::Red); expected[(2, 2)].set_fg(Color::Red);
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -827,7 +827,7 @@ mod tests {
"f b ", "f b ",
]); ]);
for (x, y) in iproduct!(0..10, 0..3) { for (x, y) in iproduct!(0..10, 0..3) {
expected.get_mut(x, y).set_fg(Color::Red); expected[(x, y)].set_fg(Color::Red);
} }
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -958,9 +958,9 @@ mod tests {
let mut expected = Buffer::with_lines(["label", "5████"]); let mut expected = Buffer::with_lines(["label", "5████"]);
// first line has a yellow foreground. first cell contains italic "5" // first line has a yellow foreground. first cell contains italic "5"
expected.get_mut(0, 1).modifier.insert(Modifier::ITALIC); expected[(0, 1)].modifier.insert(Modifier::ITALIC);
for x in 0..5 { for x in 0..5 {
expected.get_mut(x, 1).set_fg(Color::Yellow); expected[(x, 1)].set_fg(Color::Yellow);
} }
let expected_color = if let Some(color) = bar_color { let expected_color = if let Some(color) = bar_color {
@ -972,13 +972,13 @@ mod tests {
// second line contains the word "label". Since the bar value is 2, // second line contains the word "label". Since the bar value is 2,
// then the first 2 characters of "label" are italic red. // then the first 2 characters of "label" are italic red.
// the rest is white (using the Bar's style). // the rest is white (using the Bar's style).
let cell = expected.get_mut(0, 0).set_fg(Color::Red); let cell = expected[(0, 0)].set_fg(Color::Red);
cell.modifier.insert(Modifier::ITALIC); cell.modifier.insert(Modifier::ITALIC);
let cell = expected.get_mut(1, 0).set_fg(Color::Red); let cell = expected[(1, 0)].set_fg(Color::Red);
cell.modifier.insert(Modifier::ITALIC); cell.modifier.insert(Modifier::ITALIC);
expected.get_mut(2, 0).set_fg(expected_color); expected[(2, 0)].set_fg(expected_color);
expected.get_mut(3, 0).set_fg(expected_color); expected[(3, 0)].set_fg(expected_color);
expected.get_mut(4, 0).set_fg(expected_color); expected[(4, 0)].set_fg(expected_color);
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -1031,9 +1031,9 @@ mod tests {
// bold: because of BarChart::label_style // bold: because of BarChart::label_style
// red: is included with the label itself // red: is included with the label itself
let mut expected = Buffer::with_lines(["2████", "G1 "]); let mut expected = Buffer::with_lines(["2████", "G1 "]);
let cell = expected.get_mut(0, 1).set_fg(Color::Red); let cell = expected[(0, 1)].set_fg(Color::Red);
cell.modifier.insert(Modifier::BOLD); cell.modifier.insert(Modifier::BOLD);
let cell = expected.get_mut(1, 1).set_fg(Color::Red); let cell = expected[(1, 1)].set_fg(Color::Red);
cell.modifier.insert(Modifier::BOLD); cell.modifier.insert(Modifier::BOLD);
assert_eq!(buffer, expected); assert_eq!(buffer, expected);

View file

@ -628,7 +628,7 @@ impl Block<'_> {
fn render_left_side(&self, area: Rect, buf: &mut Buffer) { fn render_left_side(&self, area: Rect, buf: &mut Buffer) {
if self.borders.contains(Borders::LEFT) { if self.borders.contains(Borders::LEFT) {
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {
buf.get_mut(area.left(), y) buf[(area.left(), y)]
.set_symbol(self.border_set.vertical_left) .set_symbol(self.border_set.vertical_left)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -638,7 +638,7 @@ impl Block<'_> {
fn render_top_side(&self, area: Rect, buf: &mut Buffer) { fn render_top_side(&self, area: Rect, buf: &mut Buffer) {
if self.borders.contains(Borders::TOP) { if self.borders.contains(Borders::TOP) {
for x in area.left()..area.right() { for x in area.left()..area.right() {
buf.get_mut(x, area.top()) buf[(x, area.top())]
.set_symbol(self.border_set.horizontal_top) .set_symbol(self.border_set.horizontal_top)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -649,7 +649,7 @@ impl Block<'_> {
if self.borders.contains(Borders::RIGHT) { if self.borders.contains(Borders::RIGHT) {
let x = area.right() - 1; let x = area.right() - 1;
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {
buf.get_mut(x, y) buf[(x, y)]
.set_symbol(self.border_set.vertical_right) .set_symbol(self.border_set.vertical_right)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -660,7 +660,7 @@ impl Block<'_> {
if self.borders.contains(Borders::BOTTOM) { if self.borders.contains(Borders::BOTTOM) {
let y = area.bottom() - 1; let y = area.bottom() - 1;
for x in area.left()..area.right() { for x in area.left()..area.right() {
buf.get_mut(x, y) buf[(x, y)]
.set_symbol(self.border_set.horizontal_bottom) .set_symbol(self.border_set.horizontal_bottom)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -669,7 +669,7 @@ impl Block<'_> {
fn render_bottom_right_corner(&self, buf: &mut Buffer, area: Rect) { fn render_bottom_right_corner(&self, buf: &mut Buffer, area: Rect) {
if self.borders.contains(Borders::RIGHT | Borders::BOTTOM) { if self.borders.contains(Borders::RIGHT | Borders::BOTTOM) {
buf.get_mut(area.right() - 1, area.bottom() - 1) buf[(area.right() - 1, area.bottom() - 1)]
.set_symbol(self.border_set.bottom_right) .set_symbol(self.border_set.bottom_right)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -677,7 +677,7 @@ impl Block<'_> {
fn render_top_right_corner(&self, buf: &mut Buffer, area: Rect) { fn render_top_right_corner(&self, buf: &mut Buffer, area: Rect) {
if self.borders.contains(Borders::RIGHT | Borders::TOP) { if self.borders.contains(Borders::RIGHT | Borders::TOP) {
buf.get_mut(area.right() - 1, area.top()) buf[(area.right() - 1, area.top())]
.set_symbol(self.border_set.top_right) .set_symbol(self.border_set.top_right)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -685,7 +685,7 @@ impl Block<'_> {
fn render_bottom_left_corner(&self, buf: &mut Buffer, area: Rect) { fn render_bottom_left_corner(&self, buf: &mut Buffer, area: Rect) {
if self.borders.contains(Borders::LEFT | Borders::BOTTOM) { if self.borders.contains(Borders::LEFT | Borders::BOTTOM) {
buf.get_mut(area.left(), area.bottom() - 1) buf[(area.left(), area.bottom() - 1)]
.set_symbol(self.border_set.bottom_left) .set_symbol(self.border_set.bottom_left)
.set_style(self.border_style); .set_style(self.border_style);
} }
@ -693,7 +693,7 @@ impl Block<'_> {
fn render_top_left_corner(&self, buf: &mut Buffer, area: Rect) { fn render_top_left_corner(&self, buf: &mut Buffer, area: Rect) {
if self.borders.contains(Borders::LEFT | Borders::TOP) { if self.borders.contains(Borders::LEFT | Borders::TOP) {
buf.get_mut(area.left(), area.top()) buf[(area.left(), area.top())]
.set_symbol(self.border_set.top_left) .set_symbol(self.border_set.top_left)
.set_style(self.border_style); .set_style(self.border_style);
} }

View file

@ -771,7 +771,7 @@ where
(index % width) as u16 + canvas_area.left(), (index % width) as u16 + canvas_area.left(),
(index / width) as u16 + canvas_area.top(), (index / width) as u16 + canvas_area.top(),
); );
let cell = buf.get_mut(x, y).set_char(ch); let cell = buf[(x, y)].set_char(ch);
if colors.0 != Color::Reset { if colors.0 != Color::Reset {
cell.set_fg(colors.0); cell.set_fg(colors.0);
} }

View file

@ -957,14 +957,14 @@ impl WidgetRef for Chart<'_> {
// Sample the style of the entire widget. This sample will be used to reset the style of // Sample the style of the entire widget. This sample will be used to reset the style of
// the cells that are part of the components put on top of the grah area (i.e legend and // the cells that are part of the components put on top of the grah area (i.e legend and
// axis names). // axis names).
let original_style = buf.get(area.left(), area.top()).style(); let original_style = buf[(area.left(), area.top())].style();
self.render_x_labels(buf, &layout, chart_area, graph_area); self.render_x_labels(buf, &layout, chart_area, graph_area);
self.render_y_labels(buf, &layout, chart_area, graph_area); self.render_y_labels(buf, &layout, chart_area, graph_area);
if let Some(y) = layout.axis_x { if let Some(y) = layout.axis_x {
for x in graph_area.left()..graph_area.right() { for x in graph_area.left()..graph_area.right() {
buf.get_mut(x, y) buf[(x, y)]
.set_symbol(symbols::line::HORIZONTAL) .set_symbol(symbols::line::HORIZONTAL)
.set_style(self.x_axis.style); .set_style(self.x_axis.style);
} }
@ -972,7 +972,7 @@ impl WidgetRef for Chart<'_> {
if let Some(x) = layout.axis_y { if let Some(x) = layout.axis_y {
for y in graph_area.top()..graph_area.bottom() { for y in graph_area.top()..graph_area.bottom() {
buf.get_mut(x, y) buf[(x, y)]
.set_symbol(symbols::line::VERTICAL) .set_symbol(symbols::line::VERTICAL)
.set_style(self.y_axis.style); .set_style(self.y_axis.style);
} }
@ -980,7 +980,7 @@ impl WidgetRef for Chart<'_> {
if let Some(y) = layout.axis_x { if let Some(y) = layout.axis_x {
if let Some(x) = layout.axis_y { if let Some(x) = layout.axis_y {
buf.get_mut(x, y) buf[(x, y)]
.set_symbol(symbols::line::BOTTOM_LEFT) .set_symbol(symbols::line::BOTTOM_LEFT)
.set_style(self.x_axis.style); .set_style(self.x_axis.style);
} }

View file

@ -34,7 +34,7 @@ impl WidgetRef for Clear {
fn render_ref(&self, area: Rect, buf: &mut Buffer) { fn render_ref(&self, area: Rect, buf: &mut Buffer) {
for x in area.left()..area.right() { for x in area.left()..area.right() {
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {
buf.get_mut(x, y).reset(); buf[(x, y)].reset();
} }
} }
} }

View file

@ -195,23 +195,23 @@ impl Gauge<'_> {
for y in gauge_area.top()..gauge_area.bottom() { for y in gauge_area.top()..gauge_area.bottom() {
// render the filled area (left to end) // render the filled area (left to end)
for x in gauge_area.left()..end { for x in gauge_area.left()..end {
let cell = buf.get_mut(x, y);
// Use full block for the filled part of the gauge and spaces for the part that is // Use full block for the filled part of the gauge and spaces for the part that is
// covered by the label. Note that the background and foreground colors are swapped // covered by the label. Note that the background and foreground colors are swapped
// for the label part, otherwise the gauge will be inverted // for the label part, otherwise the gauge will be inverted
if x < label_col || x > label_col + clamped_label_width || y != label_row { if x < label_col || x > label_col + clamped_label_width || y != label_row {
cell.set_symbol(symbols::block::FULL) buf[(x, y)]
.set_symbol(symbols::block::FULL)
.set_fg(self.gauge_style.fg.unwrap_or(Color::Reset)) .set_fg(self.gauge_style.fg.unwrap_or(Color::Reset))
.set_bg(self.gauge_style.bg.unwrap_or(Color::Reset)); .set_bg(self.gauge_style.bg.unwrap_or(Color::Reset));
} else { } else {
cell.set_symbol(" ") buf[(x, y)]
.set_symbol(" ")
.set_fg(self.gauge_style.bg.unwrap_or(Color::Reset)) .set_fg(self.gauge_style.bg.unwrap_or(Color::Reset))
.set_bg(self.gauge_style.fg.unwrap_or(Color::Reset)); .set_bg(self.gauge_style.fg.unwrap_or(Color::Reset));
} }
} }
if self.use_unicode && self.ratio < 1.0 { if self.use_unicode && self.ratio < 1.0 {
buf.get_mut(end, y) buf[(end, y)].set_symbol(get_unicode_block(filled_width % 1.0));
.set_symbol(get_unicode_block(filled_width % 1.0));
} }
} }
// render the label // render the label
@ -377,7 +377,7 @@ impl WidgetRef for LineGauge<'_> {
let end = start let end = start
+ (f64::from(gauge_area.right().saturating_sub(start)) * self.ratio).floor() as u16; + (f64::from(gauge_area.right().saturating_sub(start)) * self.ratio).floor() as u16;
for col in start..end { for col in start..end {
buf.get_mut(col, row) buf[(col, row)]
.set_symbol(self.line_set.horizontal) .set_symbol(self.line_set.horizontal)
.set_style(Style { .set_style(Style {
fg: self.gauge_style.fg, fg: self.gauge_style.fg,
@ -389,7 +389,7 @@ impl WidgetRef for LineGauge<'_> {
}); });
} }
for col in end..gauge_area.right() { for col in end..gauge_area.right() {
buf.get_mut(col, row) buf[(col, row)]
.set_symbol(self.line_set.horizontal) .set_symbol(self.line_set.horizontal)
.set_style(Style { .set_style(Style {
fg: self.gauge_style.bg, fg: self.gauge_style.bg,

View file

@ -376,7 +376,7 @@ impl<'a> Paragraph<'a> {
// If the symbol is empty, the last char which rendered last time will // If the symbol is empty, the last char which rendered last time will
// leave on the line. It's a quick fix. // leave on the line. It's a quick fix.
let symbol = if symbol.is_empty() { " " } else { symbol }; let symbol = if symbol.is_empty() { " " } else { symbol };
buf.get_mut(area.left() + x, area.top() + y - self.scroll.0) buf[(area.left() + x, area.top() + y - self.scroll.0)]
.set_symbol(symbol) .set_symbol(symbol)
.set_style(*style); .set_style(*style);
x += width as u16; x += width as u16;

View file

@ -206,7 +206,7 @@ impl Sparkline<'_> {
RenderDirection::LeftToRight => spark_area.left() + i as u16, RenderDirection::LeftToRight => spark_area.left() + i as u16,
RenderDirection::RightToLeft => spark_area.right() - i as u16 - 1, RenderDirection::RightToLeft => spark_area.right() - i as u16 - 1,
}; };
buf.get_mut(x, spark_area.top() + j) buf[(x, spark_area.top() + j)]
.set_symbol(symbol) .set_symbol(symbol)
.set_style(self.style); .set_style(self.style);

View file

@ -39,21 +39,21 @@ fn barchart_can_be_stylized() {
for y in area.y..area.height { for y in area.y..area.height {
// background // background
for x in area.x..area.width { for x in area.x..area.width {
expected.get_mut(x, y).set_bg(Color::White); expected[(x, y)].set_bg(Color::White);
} }
// bars // bars
for x in [0, 1, 3, 4, 6, 7] { for x in [0, 1, 3, 4, 6, 7] {
expected.get_mut(x, y).set_fg(Color::Red); expected[(x, y)].set_fg(Color::Red);
} }
} }
// values // values
for x in 0..3 { for x in 0..3 {
expected.get_mut(x * 3, 3).set_fg(Color::Green); expected[(x * 3, 3)].set_fg(Color::Green);
} }
// labels // labels
for x in 0..3 { for x in 0..3 {
expected.get_mut(x * 3, 4).set_fg(Color::Blue); expected[(x * 3, 4)].set_fg(Color::Blue);
expected.get_mut(x * 3 + 1, 4).set_fg(Color::Reset); expected[(x * 3 + 1, 4)].set_fg(Color::Reset);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }
@ -80,14 +80,11 @@ fn block_can_be_stylized() -> io::Result<()> {
]); ]);
for x in area.x..area.width { for x in area.x..area.width {
for y in area.y..area.height { for y in area.y..area.height {
expected expected[(x, y)].set_fg(Color::Cyan).set_bg(Color::Cyan);
.get_mut(x, y)
.set_fg(Color::Cyan)
.set_bg(Color::Cyan);
} }
} }
for x in 1..=5 { for x in 1..=5 {
expected.get_mut(x, 0).set_fg(Color::LightBlue); expected[(x, 0)].set_fg(Color::LightBlue);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
Ok(()) Ok(())
@ -105,7 +102,7 @@ fn paragraph_can_be_stylized() -> io::Result<()> {
let mut expected = Buffer::with_lines(["Text "]); let mut expected = Buffer::with_lines(["Text "]);
for x in 0..4 { for x in 0..4 {
expected.get_mut(x, 0).set_fg(Color::Cyan); expected[(x, 0)].set_fg(Color::Cyan);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
Ok(()) Ok(())

View file

@ -36,14 +36,14 @@ fn terminal_draw_returns_the_completed_frame() -> Result<(), Box<dyn Error>> {
let paragraph = Paragraph::new("Test"); let paragraph = Paragraph::new("Test");
f.render_widget(paragraph, f.size()); f.render_widget(paragraph, f.size());
})?; })?;
assert_eq!(frame.buffer.get(0, 0).symbol(), "T"); assert_eq!(frame.buffer[(0, 0)].symbol(), "T");
assert_eq!(frame.area, Rect::new(0, 0, 10, 10)); assert_eq!(frame.area, Rect::new(0, 0, 10, 10));
terminal.backend_mut().resize(8, 8); terminal.backend_mut().resize(8, 8);
let frame = terminal.draw(|f| { let frame = terminal.draw(|f| {
let paragraph = Paragraph::new("test"); let paragraph = Paragraph::new("test");
f.render_widget(paragraph, f.size()); f.render_widget(paragraph, f.size());
})?; })?;
assert_eq!(frame.buffer.get(0, 0).symbol(), "t"); assert_eq!(frame.buffer[(0, 0)].symbol(), "t");
assert_eq!(frame.area, Rect::new(0, 0, 8, 8)); assert_eq!(frame.area, Rect::new(0, 0, 8, 8));
Ok(()) Ok(())
} }

View file

@ -85,11 +85,11 @@ fn widgets_barchart_group() {
]); ]);
for y in 1..(TERMINAL_HEIGHT - 3) { for y in 1..(TERMINAL_HEIGHT - 3) {
for x in 1..5 { for x in 1..5 {
expected.get_mut(x, y).set_fg(Color::Red); expected[(x, y)].set_fg(Color::Red);
expected.get_mut(x + 5, y).set_fg(Color::Green); expected[(x + 5, y)].set_fg(Color::Green);
} }
} }
expected.get_mut(2, 7).set_fg(Color::Blue); expected[(2, 7)].set_fg(Color::Blue);
expected.get_mut(3, 7).set_fg(Color::Blue); expected[(3, 7)].set_fg(Color::Blue);
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }

View file

@ -34,7 +34,7 @@ fn widgets_block_renders() {
" ", " ",
]); ]);
for x in 1..=5 { for x in 1..=5 {
expected.get_mut(x, 0).set_fg(Color::LightBlue); expected[(x, 0)].set_fg(Color::LightBlue);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }

View file

@ -32,11 +32,11 @@ fn widgets_canvas_draw_labels() {
let mut expected = Buffer::with_lines(["", "", "", "", "test "]); let mut expected = Buffer::with_lines(["", "", "", "", "test "]);
for row in 0..5 { for row in 0..5 {
for col in 0..5 { for col in 0..5 {
expected.get_mut(col, row).set_bg(Color::Yellow); expected[(col, row)].set_bg(Color::Yellow);
} }
} }
for col in 0..4 { for col in 0..4 {
expected.get_mut(col, 4).set_fg(Color::Blue); expected[(col, 4)].set_fg(Color::Blue);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }

View file

@ -470,7 +470,7 @@ fn widgets_chart_can_have_a_legend() {
// Set expected background color // Set expected background color
for row in 0..30 { for row in 0..30 {
for col in 0..60 { for col in 0..60 {
expected.get_mut(col, row).set_bg(Color::White); expected[(col, row)].set_bg(Color::White);
} }
} }
@ -532,10 +532,10 @@ fn widgets_chart_can_have_a_legend() {
(57, 2), (57, 2),
]; ];
for (col, row) in line1 { for (col, row) in line1 {
expected.get_mut(col, row).set_fg(Color::Blue); expected[(col, row)].set_fg(Color::Blue);
} }
for (col, row) in legend1 { for (col, row) in legend1 {
expected.get_mut(col, row).set_fg(Color::Blue); expected[(col, row)].set_fg(Color::Blue);
} }
// Set expected colors of the second dataset // Set expected colors of the second dataset
@ -603,16 +603,16 @@ fn widgets_chart_can_have_a_legend() {
(57, 3), (57, 3),
]; ];
for (col, row) in line2 { for (col, row) in line2 {
expected.get_mut(col, row).set_fg(Color::Green); expected[(col, row)].set_fg(Color::Green);
} }
for (col, row) in legend2 { for (col, row) in legend2 {
expected.get_mut(col, row).set_fg(Color::Green); expected[(col, row)].set_fg(Color::Green);
} }
// Set expected colors of the x axis // Set expected colors of the x axis
let x_axis_title = vec![(53, 26), (54, 26), (55, 26), (56, 26), (57, 26), (58, 26)]; let x_axis_title = vec![(53, 26), (54, 26), (55, 26), (56, 26), (57, 26), (58, 26)];
for (col, row) in x_axis_title { for (col, row) in x_axis_title {
expected.get_mut(col, row).set_fg(Color::Yellow); expected[(col, row)].set_fg(Color::Yellow);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }

View file

@ -214,13 +214,13 @@ fn widgets_line_gauge_renders() {
"└──────────────────┘", "└──────────────────┘",
]); ]);
for col in 4..10 { for col in 4..10 {
expected.get_mut(col, 0).set_fg(Color::Green); expected[(col, 0)].set_fg(Color::Green);
} }
for col in 10..20 { for col in 10..20 {
expected.get_mut(col, 0).set_fg(Color::White); expected[(col, 0)].set_fg(Color::White);
} }
for col in 5..7 { for col in 5..7 {
expected.get_mut(col, 2).set_fg(Color::Green); expected[(col, 2)].set_fg(Color::Green);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }

View file

@ -53,7 +53,7 @@ fn widgets_list_should_highlight_the_selected_item() {
" Item 3 ", " Item 3 ",
]); ]);
for x in 0..10 { for x in 0..10 {
expected.get_mut(x, 1).set_bg(Color::Yellow); expected[(x, 1)].set_bg(Color::Yellow);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }
@ -88,7 +88,7 @@ fn widgets_list_should_highlight_the_selected_item_wide_symbol() {
" Item 3 ", " Item 3 ",
]); ]);
for x in 0..10 { for x in 0..10 {
expected.get_mut(x, 1).set_bg(Color::Yellow); expected[(x, 1)].set_bg(Color::Yellow);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }
@ -222,8 +222,8 @@ fn widgets_list_should_display_multiline_items() {
" Item 3c", " Item 3c",
]); ]);
for x in 0..10 { for x in 0..10 {
expected.get_mut(x, 2).set_bg(Color::Yellow); expected[(x, 2)].set_bg(Color::Yellow);
expected.get_mut(x, 3).set_bg(Color::Yellow); expected[(x, 3)].set_bg(Color::Yellow);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }
@ -258,8 +258,8 @@ fn widgets_list_should_repeat_highlight_symbol() {
" Item 3c", " Item 3c",
]); ]);
for x in 0..10 { for x in 0..10 {
expected.get_mut(x, 2).set_bg(Color::Yellow); expected[(x, 2)].set_bg(Color::Yellow);
expected.get_mut(x, 3).set_bg(Color::Yellow); expected[(x, 3)].set_bg(Color::Yellow);
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }

View file

@ -680,7 +680,7 @@ fn widgets_table_can_have_elements_styled_individually() {
]); ]);
// First row = row color + highlight style // First row = row color + highlight style
for col in 1..=28 { for col in 1..=28 {
expected.get_mut(col, 2).set_style( expected[(col, 2)].set_style(
Style::default() Style::default()
.fg(Color::Green) .fg(Color::Green)
.add_modifier(Modifier::BOLD), .add_modifier(Modifier::BOLD),
@ -689,26 +689,18 @@ fn widgets_table_can_have_elements_styled_individually() {
// Second row: // Second row:
// 1. row color // 1. row color
for col in 1..=28 { for col in 1..=28 {
expected expected[(col, 3)].set_style(Style::default().fg(Color::LightGreen));
.get_mut(col, 3)
.set_style(Style::default().fg(Color::LightGreen));
} }
// 2. cell color // 2. cell color
for col in 11..=16 { for col in 11..=16 {
expected expected[(col, 3)].set_style(Style::default().fg(Color::Yellow));
.get_mut(col, 3)
.set_style(Style::default().fg(Color::Yellow));
} }
for col in 18..=23 { for col in 18..=23 {
expected expected[(col, 3)].set_style(Style::default().fg(Color::Red));
.get_mut(col, 3)
.set_style(Style::default().fg(Color::Red));
} }
// 3. text color // 3. text color
for col in 21..=22 { for col in 21..=22 {
expected expected[(col, 3)].set_style(Style::default().fg(Color::Blue));
.get_mut(col, 3)
.set_style(Style::default().fg(Color::Blue));
} }
terminal.backend().assert_buffer(&expected); terminal.backend().assert_buffer(&expected);
} }