Relative windows cursor position (#350)

This commit is contained in:
Timon 2019-12-17 13:28:43 -06:00 committed by GitHub
parent 6fdc819e55
commit 43c977dc4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -1,3 +1,7 @@
# Version 0.14.1
- Made windows cursor position relative to the window instead absolute to the screen buffer windows.
- Fix windows bug with `queue` macro were it consumed a type and required an type to be `Copy`.
# Version 0.14 # Version 0.14
- Replace the `input` module with brand new `event` module - Replace the `input` module with brand new `event` module

View File

@ -16,12 +16,32 @@ lazy_static! {
static ref SAVED_CURSOR_POS: Mutex<Option<(i16, i16)>> = Mutex::new(None); static ref SAVED_CURSOR_POS: Mutex<Option<(i16, i16)>> = Mutex::new(None);
} }
// The 'y' position of the cursor is not relative to the window but absolute to screen buffer.
// We can calculate the relative cursor position by subtracting the top position of the terminal window from the y position.
// This results in an 1-based coord zo subtract 1 to make cursor position 0-based.
pub fn parse_relative_y(y: i16) -> Result<i16> {
let window = ScreenBuffer::current()?.info()?;
let window_size = window.terminal_window();
let screen_size = window.terminal_size();
if y <= screen_size.height {
Ok(y)
} else {
Ok(y - window_size.top)
}
}
/// Returns the cursor position (column, row). /// Returns the cursor position (column, row).
/// ///
/// The top left cell is represented `0,0`. /// The top left cell is represented `0,0`.
pub fn position() -> Result<(u16, u16)> { pub fn position() -> Result<(u16, u16)> {
let cursor = ScreenBufferCursor::output()?; let cursor = ScreenBufferCursor::output()?;
Ok(cursor.position()?.into()) let mut position = cursor.position()?;
// if position.y != 0 {
position.y = parse_relative_y(position.y)?;
// }
Ok(position.into())
} }
pub(crate) fn show_cursor(show_cursor: bool) -> Result<()> { pub(crate) fn show_cursor(show_cursor: bool) -> Result<()> {