Add movetorow command (#532)

This commit is contained in:
sigmaSd 2021-01-11 19:22:23 +01:00 committed by GitHub
parent e35cab2408
commit 761ea46c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 3 deletions

View File

@ -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);

View File

@ -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)]

View File

@ -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<Handle> 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();

View File

@ -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),