Skip to content

Commit

Permalink
Polish get_cursor_position.
Browse files Browse the repository at this point in the history
- Also parse multi-digit cursor positions correctly.
- Clear the terminal after sending the code.
  • Loading branch information
grunweg committed Apr 30, 2022
1 parent e96b5d7 commit 41e8d39
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/common_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {

/// Return the current cursor's position as a tuple `(n, m)`,
/// where `n` is the row and `m` the column of the cursor (both 1-based).
// FIXME: allow a larger range of characters than u8.
// FIXME: clear the terminal after this operation.
pub fn get_cursor_position(mut out: &Term) -> io::Result<(u8, u8)> {
// Send the code ESC6n to the terminal: asking for the current cursor position.
// Send the code "ESC[6n" to the terminal: asking for the current cursor position.
out.write_str("\x1b[6n")?;
// We expect a response ESC[n;mR, where n and m are the row and column of the cursor.
let mut buf = [0u8; 6];
let num_read = io::Read::read(&mut out, &mut buf)?;
let (n, m) = match &buf[..] {
// If we didn't read enough bytes, we certainly didn't get the response we wanted.
_ if num_read < buf.len() => return Err(std::io::Error::new(
io::ErrorKind::Other, format!("invalid terminal response: expected six bytes, only read {}", num_read)
)),
[a, b, n, c, m, d] => {
// The bytes a, b, c and d should be byte string \x1 [ ; R.
if &[*a, *b, *c, *d] != b"\x1b[;R" {
return Err(std::io::Error::new(io::ErrorKind::Other, "invalid terminal response: should be of the form ESC[n;mR"));
} else {
(n, m)
}
}
_ => unreachable!(),
};
Ok((*n, *m))
// We expect a response of the form "ESC[n;mR", where n and m are the row and column of the cursor.
let mut buf = Vec::new();
let num_read = io::Read::read_to_end(&mut out, &mut buf)?;
out.clear_chars(num_read)?;
match &buf[..] {
[b'\x1B', b'[', middle @ .., b'R'] => {
// A valid terminal response means `middle` is valid UTF-8.
// Use string methods to simplify the parsing of input.
let middle = match std::str::from_utf8(middle) {
Ok(m) => m,
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, format!("invalid terminal response: middle part of the output {:?} must be valid UTF-8", buf))),
};
let (nstr, mstr) = match middle.split_once(';') {
Some((nstr, mstr)) => (nstr, mstr),
None => return Err(io::Error::new(io::ErrorKind::Other, format!("invalid terminal response: middle part of the output should be of the form n;m, got {}", middle))),
};
let (n, m) = (nstr.parse::<u8>().unwrap(), mstr.parse::<u8>().unwrap());
Ok((n, m))
},
_ => return Err(io::Error::new(io::ErrorKind::Other, "invalid terminal response: should be of the form ESC[n;mR")),
}
}

pub fn clear_chars(out: &Term, n: usize) -> io::Result<()> {
Expand Down

0 comments on commit 41e8d39

Please sign in to comment.