diff --git a/crossterm_cursor/src/cursor.rs b/crossterm_cursor/src/cursor.rs index 8a76028..0478d1a 100644 --- a/crossterm_cursor/src/cursor.rs +++ b/crossterm_cursor/src/cursor.rs @@ -1,5 +1,6 @@ //! 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 @@ -25,11 +26,13 @@ mod winapi_cursor; ///! 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. 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<()>; - /// 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); - /// Move cursor n times up + /// Move cursor `n` times up fn move_up(&self, count: u16) -> Result<()>; /// Move the cursor `n` times to the right. fn move_right(&self, count: u16) -> Result<()>; @@ -37,7 +40,8 @@ trait ITerminalCursor: Sync + Send { fn move_down(&self, count: u16) -> Result<()>; /// Move the cursor `n` times left. 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<()>; /// Return to saved cursor position fn reset_position(&self) -> Result<()>; diff --git a/crossterm_cursor/src/cursor/winapi_cursor.rs b/crossterm_cursor/src/cursor/winapi_cursor.rs index d95f9a9..cba5115 100644 --- a/crossterm_cursor/src/cursor/winapi_cursor.rs +++ b/crossterm_cursor/src/cursor/winapi_cursor.rs @@ -26,7 +26,7 @@ impl ITerminalCursor for WinApiCursor { fn pos(&self) -> (u16, u16) { 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<()> { diff --git a/crossterm_cursor/src/sys/unix.rs b/crossterm_cursor/src/sys/unix.rs index a9ad956..59bd047 100644 --- a/crossterm_cursor/src/sys/unix.rs +++ b/crossterm_cursor/src/sys/unix.rs @@ -9,17 +9,9 @@ use crossterm_utils::{ #[cfg(unix)] pub fn get_cursor_position() -> (u16, u16) { if unsafe { RAW_MODE_ENABLED } { - if let Ok(pos) = pos_raw() { - pos - } else { - (0, 0) - } + pos_raw().unwrap_or((0, 0)) } else { - if let Ok(pos) = pos() { - pos - } else { - (0, 0) - } + pos().unwrap_or((0, 0)) } }