From b96407a7309107d39e697f31df0206b263c8d86b Mon Sep 17 00:00:00 2001 From: Zrzka Date: Fri, 18 Oct 2019 11:40:52 +0200 Subject: [PATCH] Use SyncReader & InputEvent::CursorPosition for pos_raw() (#10) Signed-off-by: Robert Vojta --- Cargo.toml | 1 + src/sys/unix.rs | 25 ++++++++----------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 70e1782..77c592c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ crossterm_winapi = { version = "0.2.1" } [dependencies] crossterm_utils = { git = "https://github.com/crossterm-rs/crossterm-utils.git", branch = "master", version = "0.3.1" } +crossterm_input = { git = "https://github.com/crossterm-rs/crossterm-input.git", branch = "master", version = "0.4.1" } lazy_static = "1.4" diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 724febf..af0ab6b 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -1,5 +1,6 @@ -use std::io::{self, BufRead, Write}; +use std::io::{self, Write}; +use crossterm_input::{InputEvent, TerminalInput}; use crossterm_utils::{ csi, sys::unix::{disable_raw_mode, enable_raw_mode, is_raw_mode_enabled}, @@ -34,26 +35,16 @@ fn pos_raw() -> Result<(u16, u16)> { // Where is the cursor? // Use `ESC [ 6 n`. let mut stdout = io::stdout(); - let stdin = io::stdin(); // Write command stdout.write_all(b"\x1B[6n")?; stdout.flush()?; - stdin.lock().read_until(b'[', &mut vec![])?; + let mut reader = TerminalInput::new().read_sync(); - let mut rows = vec![]; - stdin.lock().read_until(b';', &mut rows)?; - - let mut cols = vec![]; - stdin.lock().read_until(b'R', &mut cols)?; - - // remove delimiter - rows.pop(); - cols.pop(); - - let rows = String::from_utf8(rows)?.parse::()?; - let cols = String::from_utf8(cols)?.parse::()?; - - Ok((cols - 1, rows - 1)) + loop { + if let Some(InputEvent::CursorPosition(x, y)) = reader.next() { + return Ok((x, y)); + } + } }