You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.
47
45
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
-
letmut 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() => returnErr(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
-
returnErr(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
+
letmut 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(_) => returnErr(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 => returnErr(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
+
_ => returnErr(io::Error::new(io::ErrorKind::Other,"invalid terminal response: should be of the form ESC[n;mR")),
0 commit comments