mirror of
https://github.com/ratatui-org/ratatui
synced 2025-02-16 22:18:51 +00:00
refactor: internally use Position struct (#1256)
This commit is contained in:
parent
a9fe4284ac
commit
e707ff11d1
6 changed files with 36 additions and 34 deletions
|
@ -19,8 +19,7 @@ use crate::{
|
||||||
},
|
},
|
||||||
terminal::{self, Clear},
|
terminal::{self, Clear},
|
||||||
},
|
},
|
||||||
layout::Size,
|
layout::{Position, Rect, Size},
|
||||||
prelude::Rect,
|
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -155,13 +154,13 @@ where
|
||||||
#[cfg(feature = "underline-color")]
|
#[cfg(feature = "underline-color")]
|
||||||
let mut underline_color = Color::Reset;
|
let mut underline_color = Color::Reset;
|
||||||
let mut modifier = Modifier::empty();
|
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 {
|
for (x, y, cell) in content {
|
||||||
// Move the cursor if the previous location was not (x - 1, y)
|
// 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))?;
|
queue!(self.writer, MoveTo(x, y))?;
|
||||||
}
|
}
|
||||||
last_pos = Some((x, y));
|
last_pos = Some(Position { x, y });
|
||||||
if cell.modifier != modifier {
|
if cell.modifier != modifier {
|
||||||
let diff = ModifierDiff {
|
let diff = ModifierDiff {
|
||||||
from: modifier,
|
from: modifier,
|
||||||
|
|
|
@ -12,7 +12,7 @@ use std::{
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{Backend, ClearType, WindowSize},
|
backend::{Backend, ClearType, WindowSize},
|
||||||
buffer::Cell,
|
buffer::Cell,
|
||||||
prelude::Rect,
|
layout::{Position, Rect},
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
termion::{self, color as tcolor, color::Color as _, style as tstyle},
|
termion::{self, color as tcolor, color::Color as _, style as tstyle},
|
||||||
};
|
};
|
||||||
|
@ -176,13 +176,13 @@ where
|
||||||
let mut fg = Color::Reset;
|
let mut fg = Color::Reset;
|
||||||
let mut bg = Color::Reset;
|
let mut bg = Color::Reset;
|
||||||
let mut modifier = Modifier::empty();
|
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 {
|
for (x, y, cell) in content {
|
||||||
// Move the cursor if the previous location was not (x - 1, y)
|
// 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();
|
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 {
|
if cell.modifier != modifier {
|
||||||
write!(
|
write!(
|
||||||
string,
|
string,
|
||||||
|
|
|
@ -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,
|
/// 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()`.
|
/// 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
|
/// The area of the viewport
|
||||||
pub(crate) viewport_area: Rect,
|
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
|
/// `Terminal::show_cursor()`, and `Terminal::set_cursor()`. Pick one of the APIs and stick
|
||||||
/// with it.
|
/// with it.
|
||||||
pub fn set_cursor(&mut self, x: u16, y: u16) {
|
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.
|
/// Gets the buffer that this `Frame` draws into as a mutable reference.
|
||||||
|
|
|
@ -70,7 +70,7 @@ where
|
||||||
last_known_size: Rect,
|
last_known_size: Rect,
|
||||||
/// Last known position of the cursor. Used to find the new area when the viewport is inlined
|
/// Last known position of the cursor. Used to find the new area when the viewport is inlined
|
||||||
/// and the terminal resized.
|
/// and the terminal resized.
|
||||||
last_known_cursor_pos: (u16, u16),
|
last_known_cursor_pos: Position,
|
||||||
/// Number of frames rendered up until current time.
|
/// Number of frames rendered up until current time.
|
||||||
frame_count: usize,
|
frame_count: usize,
|
||||||
}
|
}
|
||||||
|
@ -138,9 +138,9 @@ where
|
||||||
Viewport::Fixed(area) => area,
|
Viewport::Fixed(area) => area,
|
||||||
};
|
};
|
||||||
let (viewport_area, cursor_pos) = match options.viewport {
|
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::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 {
|
Ok(Self {
|
||||||
backend,
|
backend,
|
||||||
|
@ -188,7 +188,7 @@ where
|
||||||
let current_buffer = &self.buffers[self.current];
|
let current_buffer = &self.buffers[self.current];
|
||||||
let updates = previous_buffer.diff(current_buffer);
|
let updates = previous_buffer.diff(current_buffer);
|
||||||
if let Some((col, row, _)) = updates.last() {
|
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())
|
self.backend.draw(updates.into_iter())
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ where
|
||||||
Viewport::Inline(height) => {
|
Viewport::Inline(height) => {
|
||||||
let offset_in_previous_viewport = self
|
let offset_in_previous_viewport = self
|
||||||
.last_known_cursor_pos
|
.last_known_cursor_pos
|
||||||
.1
|
.y
|
||||||
.saturating_sub(self.viewport_area.top());
|
.saturating_sub(self.viewport_area.top());
|
||||||
compute_inline_size(&mut self.backend, height, size, offset_in_previous_viewport)?.0
|
compute_inline_size(&mut self.backend, height, size, offset_in_previous_viewport)?.0
|
||||||
}
|
}
|
||||||
|
@ -383,7 +383,7 @@ where
|
||||||
|
|
||||||
match cursor_position {
|
match cursor_position {
|
||||||
None => self.hide_cursor()?,
|
None => self.hide_cursor()?,
|
||||||
Some((x, y)) => {
|
Some(Position { x, y }) => {
|
||||||
self.show_cursor()?;
|
self.show_cursor()?;
|
||||||
self.set_cursor(x, y)?;
|
self.set_cursor(x, y)?;
|
||||||
}
|
}
|
||||||
|
@ -431,7 +431,7 @@ where
|
||||||
/// Sets the cursor position.
|
/// Sets the cursor position.
|
||||||
pub fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
|
pub fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
|
||||||
self.backend.set_cursor(x, y)?;
|
self.backend.set_cursor(x, y)?;
|
||||||
self.last_known_cursor_pos = (x, y);
|
self.last_known_cursor_pos = Position { x, y };
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,9 +573,9 @@ fn compute_inline_size<B: Backend>(
|
||||||
height: u16,
|
height: u16,
|
||||||
size: Rect,
|
size: Rect,
|
||||||
offset_in_previous_viewport: u16,
|
offset_in_previous_viewport: u16,
|
||||||
) -> io::Result<(Rect, (u16, u16))> {
|
) -> io::Result<(Rect, Position)> {
|
||||||
let pos = backend.get_cursor()?;
|
let pos: Position = backend.get_cursor()?.into();
|
||||||
let mut row = pos.1;
|
let mut row = pos.y;
|
||||||
|
|
||||||
let max_height = size.height.min(height);
|
let max_height = size.height.min(height);
|
||||||
|
|
||||||
|
|
|
@ -408,9 +408,9 @@ impl<'a> Dataset<'a> {
|
||||||
/// labels, legend, ...).
|
/// labels, legend, ...).
|
||||||
struct ChartLayout {
|
struct ChartLayout {
|
||||||
/// Location of the title of the x axis
|
/// 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
|
/// 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
|
/// Location of the first label of the x axis
|
||||||
label_x: Option<u16>,
|
label_x: Option<u16>,
|
||||||
/// Location of the first label of the y axis
|
/// 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 {
|
if let Some(ref title) = self.x_axis.title {
|
||||||
let w = title.width() as u16;
|
let w = title.width() as u16;
|
||||||
if w < graph_area.width && graph_area.height > 2 {
|
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 {
|
if let Some(ref title) = self.y_axis.title {
|
||||||
let w = title.width() as u16;
|
let w = title.width() as u16;
|
||||||
if w + 1 < graph_area.width && graph_area.height > 2 {
|
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);
|
.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 title = self.x_axis.title.as_ref().unwrap();
|
||||||
let width = graph_area
|
let width = graph_area
|
||||||
.right()
|
.right()
|
||||||
|
@ -1050,7 +1050,7 @@ impl WidgetRef for Chart<'_> {
|
||||||
buf.set_line(x, y, title, width);
|
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 title = self.y_axis.title.as_ref().unwrap();
|
||||||
let width = graph_area
|
let width = graph_area
|
||||||
.right()
|
.right()
|
||||||
|
|
|
@ -87,7 +87,7 @@ pub struct Paragraph<'a> {
|
||||||
/// The text to display
|
/// The text to display
|
||||||
text: Text<'a>,
|
text: Text<'a>,
|
||||||
/// Scroll
|
/// Scroll
|
||||||
scroll: (u16, u16),
|
scroll: Position,
|
||||||
/// Alignment of the text
|
/// Alignment of the text
|
||||||
alignment: Alignment,
|
alignment: Alignment,
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,7 @@ impl<'a> Paragraph<'a> {
|
||||||
style: Style::default(),
|
style: Style::default(),
|
||||||
wrap: None,
|
wrap: None,
|
||||||
text: text.into(),
|
text: text.into(),
|
||||||
scroll: (0, 0),
|
scroll: Position::ORIGIN,
|
||||||
alignment: Alignment::Left,
|
alignment: Alignment::Left,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,10 @@ impl<'a> Paragraph<'a> {
|
||||||
/// Scrollable Widgets](https://github.com/ratatui-org/ratatui/issues/174) on GitHub.
|
/// 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"]
|
#[must_use = "method moves the value of self and returns the modified value"]
|
||||||
pub const fn scroll(mut self, offset: (Vertical, Horizontal)) -> Self {
|
pub const fn scroll(mut self, offset: (Vertical, Horizontal)) -> Self {
|
||||||
self.scroll = offset;
|
self.scroll = Position {
|
||||||
|
x: offset.1,
|
||||||
|
y: offset.0,
|
||||||
|
};
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,7 +417,7 @@ impl Paragraph<'_> {
|
||||||
self.render_text(line_composer, text_area, buf);
|
self.render_text(line_composer, text_area, buf);
|
||||||
} else {
|
} else {
|
||||||
let mut line_composer = LineTruncator::new(styled, text_area.width);
|
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);
|
self.render_text(line_composer, text_area, buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -429,7 +432,7 @@ impl<'a> Paragraph<'a> {
|
||||||
alignment: current_line_alignment,
|
alignment: current_line_alignment,
|
||||||
}) = composer.next_line()
|
}) = 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);
|
let mut x = get_line_offset(current_line_width, area.width, current_line_alignment);
|
||||||
for StyledGrapheme { symbol, style } in current_line {
|
for StyledGrapheme { symbol, style } in current_line {
|
||||||
let width = symbol.width();
|
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
|
// 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.get_mut(area.left() + x, area.top() + y - self.scroll.y)
|
||||||
.set_symbol(symbol)
|
.set_symbol(symbol)
|
||||||
.set_style(*style);
|
.set_style(*style);
|
||||||
x += width as u16;
|
x += width as u16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y += 1;
|
y += 1;
|
||||||
if y >= area.height + self.scroll.0 {
|
if y >= area.height + self.scroll.y {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue