From 761ea46c95b7c7699212764a6a4b790bbab66c2f Mon Sep 17 00:00:00 2001 From: sigmaSd Date: Mon, 11 Jan 2021 19:22:23 +0100 Subject: [PATCH] Add movetorow command (#532) --- src/cursor.rs | 20 ++++++++++++++++++++ src/cursor/sys.rs | 2 +- src/cursor/sys/windows.rs | 17 ++++++++++++++++- src/lib.rs | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/cursor.rs b/src/cursor.rs index ee93ed0..f5db525 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -131,6 +131,25 @@ impl Command for MoveToColumn { } } +/// A command that moves the terminal cursor to the given row on the current column. +/// +/// # Notes +/// +/// Commands must be executed/queued for execution otherwise they do nothing. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct MoveToRow(pub u16); + +impl Command for MoveToRow { + fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { + write!(f, csi!("{}d"), self.0) + } + + #[cfg(windows)] + fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> { + sys::move_to_row(self.0) + } +} + /// A command that moves the terminal cursor a given number of rows up. /// /// # Notes @@ -343,6 +362,7 @@ impl Command for DisableBlinking { impl_display!(for MoveTo); impl_display!(for MoveToColumn); +impl_display!(for MoveToRow); impl_display!(for MoveToNextLine); impl_display!(for MoveToPreviousLine); impl_display!(for MoveUp); diff --git a/src/cursor/sys.rs b/src/cursor/sys.rs index 149375d..b63a2bb 100644 --- a/src/cursor/sys.rs +++ b/src/cursor/sys.rs @@ -7,7 +7,7 @@ pub use self::windows::position; #[cfg(windows)] pub(crate) use self::windows::{ move_down, move_left, move_right, move_to, move_to_column, move_to_next_line, - move_to_previous_line, move_up, restore_position, save_position, show_cursor, + move_to_previous_line, move_to_row, move_up, restore_position, save_position, show_cursor, }; #[cfg(windows)] diff --git a/src/cursor/sys/windows.rs b/src/cursor/sys/windows.rs index 1d95d71..0e20e84 100644 --- a/src/cursor/sys/windows.rs +++ b/src/cursor/sys/windows.rs @@ -86,6 +86,12 @@ pub(crate) fn move_to_column(new_column: u16) -> Result<()> { Ok(()) } +pub(crate) fn move_to_row(new_row: u16) -> Result<()> { + let (col, _) = position()?; + move_to(col, new_row)?; + Ok(()) +} + pub(crate) fn move_to_next_line(count: u16) -> Result<()> { let (_, row) = position()?; move_to(0, row + count)?; @@ -209,7 +215,7 @@ impl From for ScreenBufferCursor { mod tests { use super::{ move_down, move_left, move_right, move_to, move_to_column, move_to_next_line, - move_to_previous_line, move_up, position, restore_position, save_position, + move_to_previous_line, move_to_row, move_up, position, restore_position, save_position, }; #[test] @@ -275,6 +281,15 @@ mod tests { assert_eq!(position().unwrap(), (12, 2)); } + #[test] + fn test_move_to_row_winapi() { + move_to(0, 2).unwrap(); + + move_to_row(5).unwrap(); + + assert_eq!(position().unwrap(), (0, 5)); + } + #[test] fn test_move_down_winapi() { move_to(0, 0).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index e69a2c6..41e8828 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ //! [`SavePosition`](cursor/struct.SavePosition.html), [`RestorePosition`](cursor/struct.RestorePosition.html), //! [`MoveUp`](cursor/struct.MoveUp.html), [`MoveDown`](cursor/struct.MoveDown.html), //! [`MoveLeft`](cursor/struct.MoveLeft.html), [`MoveRight`](cursor/struct.MoveRight.html), -//! [`MoveTo`](cursor/struct.MoveTo.html), [`MoveToColumn`](cursor/struct.MoveToColumn.html), +//! [`MoveTo`](cursor/struct.MoveTo.html), [`MoveToColumn`](cursor/struct.MoveToColumn.html),[`MoveToRow`](cursor/struct.MoveToRow.html), //! [`MoveToNextLine`](cursor/struct.MoveToNextLine.html), [`MoveToPreviousLine`](cursor/struct.MoveToPreviousLine.html), //! - Module [`event`](event/index.html) //! - Mouse events - [`EnableMouseCapture`](event/struct.EnableMouseCapture.html),