diff --git a/src/input.rs b/src/input.rs index bfdef79..43d548b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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 { self.read_unread_bytes(); let pos_semi = self.byte_buf.iter().position(|&b| b == b';'); @@ -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::()?; - let col_num = col.parse::()?; - Ok(CursorPos(row_num - 1, col_num - 1)) - } else { - Err(TuikitError::NoCursorReportResponse) + let row_num = row.parse::()?; + let col_num = col.parse::()?; + + return Ok(CursorPos(row_num - 1, col_num - 1)); + } } + + return Err(TuikitError::NoCursorReportResponse); } fn extended_escape(&mut self, seq2: u8) -> Result {