Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Use SyncReader & InputEvent::CursorPosition for pos_raw() (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Vojta <[email protected]>
  • Loading branch information
zrzka authored Oct 18, 2019
1 parent 58b5ce9 commit b96407a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
25 changes: 8 additions & 17 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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::<u16>()?;
let cols = String::from_utf8(cols)?.parse::<u16>()?;

Ok((cols - 1, rows - 1))
loop {
if let Some(InputEvent::CursorPosition(x, y)) = reader.next() {
return Ok((x, y));
}
}
}

0 comments on commit b96407a

Please sign in to comment.