From bf33cc9b7c09ceb715414c8054c7a59dc148f06a Mon Sep 17 00:00:00 2001 From: Mike Grunweg Date: Sat, 30 Apr 2022 11:54:03 +0200 Subject: [PATCH 1/5] Document the current behaviour of clear_last_lines when called with a too large argument. --- src/term.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/term.rs b/src/term.rs index 350a7894..5d41a891 100644 --- a/src/term.rs +++ b/src/term.rs @@ -449,6 +449,10 @@ impl Term { /// Clear the last `n` lines before the current line. /// /// Position the cursor at the beginning of the first line that was cleared. + /// + /// **Caution.** When `n` is larger than the number of lines above the + /// current cursor, the top `n` lines are cleared --- including some lines + /// below the cursor. pub fn clear_last_lines(&self, n: usize) -> io::Result<()> { self.move_cursor_up(n)?; for _ in 0..n { From 6811ca1b79432b0043dc80913215b3dcf67e5f1f Mon Sep 17 00:00:00 2001 From: Mike Grunweg Date: Sat, 30 Apr 2022 10:47:59 +0200 Subject: [PATCH 2/5] wip: Add function to compute the current cursor position. The implementation is a proof-of-concept, and not fully polished yet. --- src/common_term.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/common_term.rs b/src/common_term.rs index 020660a5..6b81acb6 100644 --- a/src/common_term.rs +++ b/src/common_term.rs @@ -38,6 +38,34 @@ pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> { out.write_str(&format!("\x1B[{};{}H", y + 1, x + 1)) } +/// 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. + 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)) +} + pub fn clear_chars(out: &Term, n: usize) -> io::Result<()> { if n > 0 { out.write_str(&format!("\x1b[{}D\x1b[0K", n)) From e96b5d757103b1ffeed6b8bbd27537881b92cfb4 Mon Sep 17 00:00:00 2001 From: Mike Grunweg Date: Fri, 29 Apr 2022 22:02:01 +0200 Subject: [PATCH 3/5] Make sure clear_last_lines never clears below the current cursor. Instead, check the number of rows above the cursor position, and error if passed a too large number. --- src/term.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/term.rs b/src/term.rs index 5d41a891..9cd5e763 100644 --- a/src/term.rs +++ b/src/term.rs @@ -446,14 +446,16 @@ impl Term { clear_line(self) } - /// Clear the last `n` lines before the current line. + /// Clear the last `n` lines before the current line, if possible. /// /// Position the cursor at the beginning of the first line that was cleared. - /// - /// **Caution.** When `n` is larger than the number of lines above the - /// current cursor, the top `n` lines are cleared --- including some lines - /// below the cursor. + /// Error when `n` is larger than the number of lines before the current cursor. pub fn clear_last_lines(&self, n: usize) -> io::Result<()> { + let (current_row, _) = get_cursor_position(self)?; + if usize::from(current_row) < n { + // We cannot move up n lines, only current_row ones. + return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n))); + } self.move_cursor_up(n)?; for _ in 0..n { self.clear_line()?; From 5376c13e347ec3e47cad5680988abae7929de806 Mon Sep 17 00:00:00 2001 From: Mike Grunweg Date: Sun, 8 May 2022 12:15:39 +0200 Subject: [PATCH 4/5] 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. --- src/common_term.rs | 48 +++++++++++++++++++++++++--------------------- src/term.rs | 14 +++++++++----- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/common_term.rs b/src/common_term.rs index 6b81acb6..11d6fe40 100644 --- a/src/common_term.rs +++ b/src/common_term.rs @@ -38,32 +38,36 @@ pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> { out.write_str(&format!("\x1B[{};{}H", y + 1, x + 1)) } +#[cfg(unix)] /// 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. +pub fn get_cursor_position(mut out: &Term) -> io::Result<(u16, u16)> { + // 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]; + // We expect a response of the form "ESC[n;mR", where n and m are the row and column of the cursor. + // n and m are at most 65536, so 4+2*5 bytes should suffice for these purposes. + // TODO: this blocks for user input! + let mut buf = [0u8; 4 + 2*5]; 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)) + out.clear_chars(num_read)?; + // FIXME: re-use ANSI code parser instead of rolling my own. + 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::().unwrap(), mstr.parse::().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<()> { diff --git a/src/term.rs b/src/term.rs index 9cd5e763..747ddb7a 100644 --- a/src/term.rs +++ b/src/term.rs @@ -449,12 +449,16 @@ impl Term { /// Clear the last `n` lines before the current line, if possible. /// /// Position the cursor at the beginning of the first line that was cleared. - /// Error when `n` is larger than the number of lines before the current cursor. + /// On UNIX, error when `n` is larger than the number of lines before the current cursor. + // FIXME: find good behaviour on windows and document it here. pub fn clear_last_lines(&self, n: usize) -> io::Result<()> { - let (current_row, _) = get_cursor_position(self)?; - if usize::from(current_row) < n { - // We cannot move up n lines, only current_row ones. - return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n))); + #[cfg(unix)] + { + let (current_row, _) = get_cursor_position(self)?; + if usize::from(current_row) < n { + // We cannot move up n lines, only current_row ones. + return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n))); + } } self.move_cursor_up(n)?; for _ in 0..n { From 0f7bbacea04305286d37f7eb984fb37ec807556e Mon Sep 17 00:00:00 2001 From: Mike Grunweg Date: Sun, 8 May 2022 12:40:57 +0200 Subject: [PATCH 5/5] Fix formatting; avoid str_split_once feature. --- src/common_term.rs | 17 +++++++++++------ src/term.rs | 5 ++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/common_term.rs b/src/common_term.rs index 11d6fe40..00bb3da8 100644 --- a/src/common_term.rs +++ b/src/common_term.rs @@ -47,7 +47,7 @@ pub fn get_cursor_position(mut out: &Term) -> io::Result<(u16, u16)> { // We expect a response of the form "ESC[n;mR", where n and m are the row and column of the cursor. // n and m are at most 65536, so 4+2*5 bytes should suffice for these purposes. // TODO: this blocks for user input! - let mut buf = [0u8; 4 + 2*5]; + let mut buf = [0u8; 4 + 2 * 5]; let num_read = io::Read::read(&mut out, &mut buf)?; out.clear_chars(num_read)?; // FIXME: re-use ANSI code parser instead of rolling my own. @@ -59,14 +59,19 @@ pub fn get_cursor_position(mut out: &Term) -> io::Result<(u16, u16)> { 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 parts = middle.splitn(2, ';').collect::>(); + let (nstr, mstr) = + match &parts[..] { + [a, b] => (a, b), + _ => 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::().unwrap(), mstr.parse::().unwrap()); Ok((n, m)) - }, - _ => return Err(io::Error::new(io::ErrorKind::Other, "invalid terminal response: should be of the form ESC[n;mR")), + } + _ => Err(io::Error::new( + io::ErrorKind::Other, + "invalid terminal response: should be of the form ESC[n;mR", + )), } } diff --git a/src/term.rs b/src/term.rs index 747ddb7a..3b95314e 100644 --- a/src/term.rs +++ b/src/term.rs @@ -457,7 +457,10 @@ impl Term { let (current_row, _) = get_cursor_position(self)?; if usize::from(current_row) < n { // We cannot move up n lines, only current_row ones. - return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n))); + return Err(io::Error::new( + io::ErrorKind::Other, + format!("can only move up {} lines, not {}", current_row, n), + )); } } self.move_cursor_up(n)?;