Skip to content

Commit 41e8d39

Browse files
committed
Polish get_cursor_position.
- Also parse multi-digit cursor positions correctly. - Clear the terminal after sending the code.
1 parent e96b5d7 commit 41e8d39

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/common_term.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,30 @@ pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
4040

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

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

0 commit comments

Comments
 (0)