refactor: internally use Position struct (#1256)

This commit is contained in:
EdJoPaTo 2024-08-02 19:55:41 +02:00 committed by GitHub
parent a9fe4284ac
commit e707ff11d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 36 additions and 34 deletions

View file

@ -19,8 +19,7 @@ use crate::{
},
terminal::{self, Clear},
},
layout::Size,
prelude::Rect,
layout::{Position, Rect, Size},
style::{Color, Modifier, Style},
};
@ -155,13 +154,13 @@ where
#[cfg(feature = "underline-color")]
let mut underline_color = Color::Reset;
let mut modifier = Modifier::empty();
let mut last_pos: Option<(u16, u16)> = None;
let mut last_pos: Option<Position> = None;
for (x, y, cell) in content {
// Move the cursor if the previous location was not (x - 1, y)
if !matches!(last_pos, Some(p) if x == p.0 + 1 && y == p.1) {
if !matches!(last_pos, Some(p) if x == p.x + 1 && y == p.y) {
queue!(self.writer, MoveTo(x, y))?;
}
last_pos = Some((x, y));
last_pos = Some(Position { x, y });
if cell.modifier != modifier {
let diff = ModifierDiff {
from: modifier,

View file

@ -12,7 +12,7 @@ use std::{
use crate::{
backend::{Backend, ClearType, WindowSize},
buffer::Cell,
prelude::Rect,
layout::{Position, Rect},
style::{Color, Modifier, Style},
termion::{self, color as tcolor, color::Color as _, style as tstyle},
};
@ -176,13 +176,13 @@ where
let mut fg = Color::Reset;
let mut bg = Color::Reset;
let mut modifier = Modifier::empty();
let mut last_pos: Option<(u16, u16)> = None;
let mut last_pos: Option<Position> = None;
for (x, y, cell) in content {
// Move the cursor if the previous location was not (x - 1, y)
if !matches!(last_pos, Some(p) if x == p.0 + 1 && y == p.1) {
if !matches!(last_pos, Some(p) if x == p.x + 1 && y == p.y) {
write!(string, "{}", termion::cursor::Goto(x + 1, y + 1)).unwrap();
}
last_pos = Some((x, y));
last_pos = Some(Position { x, y });
if cell.modifier != modifier {
write!(
string,

View file

@ -16,7 +16,7 @@ pub struct Frame<'a> {
///
/// If `None`, the cursor is hidden and its position is controlled by the backend. If `Some((x,
/// y))`, the cursor is shown and placed at `(x, y)` after the call to `Terminal::draw()`.
pub(crate) cursor_position: Option<(u16, u16)>,
pub(crate) cursor_position: Option<Position>,
/// The area of the viewport
pub(crate) viewport_area: Rect,
@ -167,7 +167,7 @@ impl Frame<'_> {
/// `Terminal::show_cursor()`, and `Terminal::set_cursor()`. Pick one of the APIs and stick
/// with it.
pub fn set_cursor(&mut self, x: u16, y: u16) {
self.cursor_position = Some((x, y));
self.cursor_position = Some(Position { x, y });
}
/// Gets the buffer that this `Frame` draws into as a mutable reference.

View file

@ -70,7 +70,7 @@ where
last_known_size: Rect,
/// Last known position of the cursor. Used to find the new area when the viewport is inlined
/// and the terminal resized.
last_known_cursor_pos: (u16, u16),
last_known_cursor_pos: Position,
/// Number of frames rendered up until current time.
frame_count: usize,
}
@ -138,9 +138,9 @@ where
Viewport::Fixed(area) => area,
};
let (viewport_area, cursor_pos) = match options.viewport {
Viewport::Fullscreen => (size, (0, 0)),
Viewport::Fullscreen => (size, Position::ORIGIN),
Viewport::Inline(height) => compute_inline_size(&mut backend, height, size, 0)?,
Viewport::Fixed(area) => (area, (area.left(), area.top())),
Viewport::Fixed(area) => (area, area.as_position()),
};
Ok(Self {
backend,
@ -188,7 +188,7 @@ where
let current_buffer = &self.buffers[self.current];
let updates = previous_buffer.diff(current_buffer);
if let Some((col, row, _)) = updates.last() {
self.last_known_cursor_pos = (*col, *row);
self.last_known_cursor_pos = Position { x: *col, y: *row };
}
self.backend.draw(updates.into_iter())
}
@ -203,7 +203,7 @@ where
Viewport::Inline(height) => {
let offset_in_previous_viewport = self
.last_known_cursor_pos
.1
.y
.saturating_sub(self.viewport_area.top());
compute_inline_size(&mut self.backend, height, size, offset_in_previous_viewport)?.0
}
@ -383,7 +383,7 @@ where
match cursor_position {
None => self.hide_cursor()?,
Some((x, y)) => {
Some(Position { x, y }) => {
self.show_cursor()?;
self.set_cursor(x, y)?;
}
@ -431,7 +431,7 @@ where
/// Sets the cursor position.
pub fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
self.backend.set_cursor(x, y)?;
self.last_known_cursor_pos = (x, y);
self.last_known_cursor_pos = Position { x, y };
Ok(())
}
@ -573,9 +573,9 @@ fn compute_inline_size<B: Backend>(
height: u16,
size: Rect,
offset_in_previous_viewport: u16,
) -> io::Result<(Rect, (u16, u16))> {
let pos = backend.get_cursor()?;
let mut row = pos.1;
) -> io::Result<(Rect, Position)> {
let pos: Position = backend.get_cursor()?.into();
let mut row = pos.y;
let max_height = size.height.min(height);

View file

@ -408,9 +408,9 @@ impl<'a> Dataset<'a> {
/// labels, legend, ...).
struct ChartLayout {
/// Location of the title of the x axis
title_x: Option<(u16, u16)>,
title_x: Option<Position>,
/// Location of the title of the y axis
title_y: Option<(u16, u16)>,
title_y: Option<Position>,
/// Location of the first label of the x axis
label_x: Option<u16>,
/// Location of the first label of the y axis
@ -740,7 +740,7 @@ impl<'a> Chart<'a> {
if let Some(ref title) = self.x_axis.title {
let w = title.width() as u16;
if w < graph_area.width && graph_area.height > 2 {
title_x = Some((x + graph_area.width - w, y));
title_x = Some(Position::new(x + graph_area.width - w, y));
}
}
@ -748,7 +748,7 @@ impl<'a> Chart<'a> {
if let Some(ref title) = self.y_axis.title {
let w = title.width() as u16;
if w + 1 < graph_area.width && graph_area.height > 2 {
title_y = Some((x, area.top()));
title_y = Some(Position::new(x, area.top()));
}
}
@ -1032,7 +1032,7 @@ impl WidgetRef for Chart<'_> {
.render(graph_area, buf);
}
if let Some((x, y)) = layout.title_x {
if let Some(Position { x, y }) = layout.title_x {
let title = self.x_axis.title.as_ref().unwrap();
let width = graph_area
.right()
@ -1050,7 +1050,7 @@ impl WidgetRef for Chart<'_> {
buf.set_line(x, y, title, width);
}
if let Some((x, y)) = layout.title_y {
if let Some(Position { x, y }) = layout.title_y {
let title = self.y_axis.title.as_ref().unwrap();
let width = graph_area
.right()

View file

@ -87,7 +87,7 @@ pub struct Paragraph<'a> {
/// The text to display
text: Text<'a>,
/// Scroll
scroll: (u16, u16),
scroll: Position,
/// Alignment of the text
alignment: Alignment,
}
@ -155,7 +155,7 @@ impl<'a> Paragraph<'a> {
style: Style::default(),
wrap: None,
text: text.into(),
scroll: (0, 0),
scroll: Position::ORIGIN,
alignment: Alignment::Left,
}
}
@ -223,7 +223,10 @@ impl<'a> Paragraph<'a> {
/// Scrollable Widgets](https://github.com/ratatui-org/ratatui/issues/174) on GitHub.
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn scroll(mut self, offset: (Vertical, Horizontal)) -> Self {
self.scroll = offset;
self.scroll = Position {
x: offset.1,
y: offset.0,
};
self
}
@ -414,7 +417,7 @@ impl Paragraph<'_> {
self.render_text(line_composer, text_area, buf);
} else {
let mut line_composer = LineTruncator::new(styled, text_area.width);
line_composer.set_horizontal_offset(self.scroll.1);
line_composer.set_horizontal_offset(self.scroll.x);
self.render_text(line_composer, text_area, buf);
}
}
@ -429,7 +432,7 @@ impl<'a> Paragraph<'a> {
alignment: current_line_alignment,
}) = composer.next_line()
{
if y >= self.scroll.0 {
if y >= self.scroll.y {
let mut x = get_line_offset(current_line_width, area.width, current_line_alignment);
for StyledGrapheme { symbol, style } in current_line {
let width = symbol.width();
@ -439,14 +442,14 @@ impl<'a> Paragraph<'a> {
// If the symbol is empty, the last char which rendered last time will
// leave on the line. It's a quick fix.
let symbol = if symbol.is_empty() { " " } else { symbol };
buf.get_mut(area.left() + x, area.top() + y - self.scroll.0)
buf.get_mut(area.left() + x, area.top() + y - self.scroll.y)
.set_symbol(symbol)
.set_style(*style);
x += width as u16;
}
}
y += 1;
if y >= area.height + self.scroll.0 {
if y >= area.height + self.scroll.y {
break;
}
}