ITerminalCursor::pos() - sync platforms behaviour (#227)

This commit is contained in:
Zrzka 2019-09-17 17:55:30 +02:00 committed by Timon
parent 38a22537d9
commit 24b9d6f455
3 changed files with 12 additions and 16 deletions

View File

@ -1,5 +1,6 @@
//! A module that contains all the actions related to cursor movement in the terminal. //! A module that contains all the actions related to cursor movement in the terminal.
//! Like: moving the cursor position; saving and resetting the cursor position; hiding showing and control the blinking of the cursor. //! Like: moving the cursor position; saving and resetting the cursor position; hiding showing and control
//! the blinking of the cursor.
//! //!
//! Note that positions of the cursor are 0 -based witch means that the coordinates (cells) starts counting from 0 //! Note that positions of the cursor are 0 -based witch means that the coordinates (cells) starts counting from 0
@ -25,11 +26,13 @@ mod winapi_cursor;
///! This trait is implemented for `WinApi` (Windows specific) and `ANSI` (Unix specific), ///! This trait is implemented for `WinApi` (Windows specific) and `ANSI` (Unix specific),
///! so that cursor related actions can be performed on both UNIX and Windows systems. ///! so that cursor related actions can be performed on both UNIX and Windows systems.
trait ITerminalCursor: Sync + Send { trait ITerminalCursor: Sync + Send {
/// Goto some location (x,y) in the context. /// Goto location (`x`, `y`) in the current terminal window.
fn goto(&self, x: u16, y: u16) -> Result<()>; fn goto(&self, x: u16, y: u16) -> Result<()>;
/// Get the location (x,y) of the current cursor in the context /// Get the cursor location `(x, y)` in the current terminal window.
///
/// `(0, 0)` is returned in case of an error.
fn pos(&self) -> (u16, u16); fn pos(&self) -> (u16, u16);
/// Move cursor n times up /// Move cursor `n` times up
fn move_up(&self, count: u16) -> Result<()>; fn move_up(&self, count: u16) -> Result<()>;
/// Move the cursor `n` times to the right. /// Move the cursor `n` times to the right.
fn move_right(&self, count: u16) -> Result<()>; fn move_right(&self, count: u16) -> Result<()>;
@ -37,7 +40,8 @@ trait ITerminalCursor: Sync + Send {
fn move_down(&self, count: u16) -> Result<()>; fn move_down(&self, count: u16) -> Result<()>;
/// Move the cursor `n` times left. /// Move the cursor `n` times left.
fn move_left(&self, count: u16) -> Result<()>; fn move_left(&self, count: u16) -> Result<()>;
/// Save cursor position so that its saved position can be recalled later. Note that this position is stored program based not per instance of the cursor struct. /// Save cursor position so that its saved position can be recalled later. Note that this position
/// is stored program based not per instance of the cursor struct.
fn save_position(&self) -> Result<()>; fn save_position(&self) -> Result<()>;
/// Return to saved cursor position /// Return to saved cursor position
fn reset_position(&self) -> Result<()>; fn reset_position(&self) -> Result<()>;

View File

@ -26,7 +26,7 @@ impl ITerminalCursor for WinApiCursor {
fn pos(&self) -> (u16, u16) { fn pos(&self) -> (u16, u16) {
let cursor = Cursor::new().unwrap(); let cursor = Cursor::new().unwrap();
cursor.position().unwrap().into() cursor.position().map(Into::into).unwrap_or((0, 0))
} }
fn move_up(&self, count: u16) -> Result<()> { fn move_up(&self, count: u16) -> Result<()> {

View File

@ -9,17 +9,9 @@ use crossterm_utils::{
#[cfg(unix)] #[cfg(unix)]
pub fn get_cursor_position() -> (u16, u16) { pub fn get_cursor_position() -> (u16, u16) {
if unsafe { RAW_MODE_ENABLED } { if unsafe { RAW_MODE_ENABLED } {
if let Ok(pos) = pos_raw() { pos_raw().unwrap_or((0, 0))
pos
} else {
(0, 0)
}
} else { } else {
if let Ok(pos) = pos() { pos().unwrap_or((0, 0))
pos
} else {
(0, 0)
}
} }
} }