Skip to content

Commit 5376c13

Browse files
committed
Polish get_cursor_position.
- Also parse multi-digit cursor positions correctly. - Clear the terminal after sending the code. - accept an u16, as terminal can be up to 65536 chars wide or long - just read a limited buffer, not an entire line; I can use a fixed-size buffer because of the above. - note two current FIXMEs in the code. - restrict this code to UNIX systems for now.
1 parent e96b5d7 commit 5376c13

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/common_term.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,36 @@ pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
3838
out.write_str(&format!("\x1B[{};{}H", y + 1, x + 1))
3939
}
4040

41+
#[cfg(unix)]
4142
/// Return the current cursor's position as a tuple `(n, m)`,
4243
/// 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.
45-
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+
pub fn get_cursor_position(mut out: &Term) -> io::Result<(u16, u16)> {
45+
// Send the code "ESC[6n" to the terminal: asking for the current cursor position.
4746
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];
47+
// We expect a response of the form "ESC[n;mR", where n and m are the row and column of the cursor.
48+
// n and m are at most 65536, so 4+2*5 bytes should suffice for these purposes.
49+
// TODO: this blocks for user input!
50+
let mut buf = [0u8; 4 + 2*5];
5051
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))
52+
out.clear_chars(num_read)?;
53+
// FIXME: re-use ANSI code parser instead of rolling my own.
54+
match &buf[..] {
55+
[b'\x1B', b'[', middle @ .., b'R'] => {
56+
// A valid terminal response means `middle` is valid UTF-8.
57+
// Use string methods to simplify the parsing of input.
58+
let middle = match std::str::from_utf8(middle) {
59+
Ok(m) => m,
60+
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, format!("invalid terminal response: middle part of the output {:?} must be valid UTF-8", buf))),
61+
};
62+
let (nstr, mstr) = match middle.split_once(';') {
63+
Some((nstr, mstr)) => (nstr, mstr),
64+
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))),
65+
};
66+
let (n, m) = (nstr.parse::<u16>().unwrap(), mstr.parse::<u16>().unwrap());
67+
Ok((n, m))
68+
},
69+
_ => return Err(io::Error::new(io::ErrorKind::Other, "invalid terminal response: should be of the form ESC[n;mR")),
70+
}
6771
}
6872

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

src/term.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,16 @@ impl Term {
449449
/// Clear the last `n` lines before the current line, if possible.
450450
///
451451
/// Position the cursor at the beginning of the first line that was cleared.
452-
/// Error when `n` is larger than the number of lines before the current cursor.
452+
/// On UNIX, error when `n` is larger than the number of lines before the current cursor.
453+
// FIXME: find good behaviour on windows and document it here.
453454
pub fn clear_last_lines(&self, n: usize) -> io::Result<()> {
454-
let (current_row, _) = get_cursor_position(self)?;
455-
if usize::from(current_row) < n {
456-
// We cannot move up n lines, only current_row ones.
457-
return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n)));
455+
#[cfg(unix)]
456+
{
457+
let (current_row, _) = get_cursor_position(self)?;
458+
if usize::from(current_row) < n {
459+
// We cannot move up n lines, only current_row ones.
460+
return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n)));
461+
}
458462
}
459463
self.move_cursor_up(n)?;
460464
for _ in 0..n {

0 commit comments

Comments
 (0)