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

Use SyncReader & InputEvent::CursorPosition for pos_raw() #10

Merged
merged 3 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "zrzka/unix-async-events", 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));
}
}
}