Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing position cursor report escape sequences #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
28 changes: 16 additions & 12 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ impl KeyBoard {
}
}

// Cursor position report (CPR): Answer is ESC [ y ; x R, where x,y is the cursor location.
fn parse_cursor_report(&mut self) -> Result<Key> {
self.read_unread_bytes();
let pos_semi = self.byte_buf.iter().position(|&b| b == b';');
Expand All @@ -474,21 +475,24 @@ impl KeyBoard {
let pos_semi = pos_semi.unwrap();
let pos_r = pos_r.unwrap();

let remain = self.byte_buf.split_off(pos_r + 1);
let mut col_str = self.byte_buf.split_off(pos_semi + 1);
let mut row_str = std::mem::replace(&mut self.byte_buf, remain);
if pos_r > pos_semi {
let remain = self.byte_buf.split_off(pos_r + 1);
let mut col_str = self.byte_buf.split_off(pos_semi + 1);
let mut row_str = std::mem::replace(&mut self.byte_buf, remain);

row_str.pop(); // remove the ';' character
col_str.pop(); // remove the 'R' character
let row = String::from_utf8(row_str)?;
let col = String::from_utf8(col_str)?;
row_str.pop(); // remove the ';' character
col_str.pop(); // remove the 'R' character
let row = String::from_utf8(row_str)?;
let col = String::from_utf8(col_str)?;

let row_num = row.parse::<u16>()?;
let col_num = col.parse::<u16>()?;
Ok(CursorPos(row_num - 1, col_num - 1))
} else {
Err(TuikitError::NoCursorReportResponse)
let row_num = row.parse::<u16>()?;
let col_num = col.parse::<u16>()?;

return Ok(CursorPos(row_num - 1, col_num - 1));
}
}

return Err(TuikitError::NoCursorReportResponse);
}

fn extended_escape(&mut self, seq2: u8) -> Result<Key> {
Expand Down