From f842e04c4b5579342c1e74b41e867d02e1727f3d Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 11 Jan 2024 18:48:07 -0800 Subject: Don't read the wrong value in cursor_pos. It seems like sometimes a slow terminal like lxterminal can take a little while to respond with the cursor position, so the read can time out. If it does, a later call to cursor_pos could read it in. To prevent this, we can call read before even querying the position. If there are any previous pending status responses, this should hopefully read them in, and we can abort early without getting confused. If there is no buffered data on stdin, then it's hopefully safe to write the status query and read the response. --- src/term.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/term.rs b/src/term.rs index 33d9d7c..d77a8f6 100644 --- a/src/term.rs +++ b/src/term.rs @@ -109,6 +109,12 @@ pub fn read_key(stdin: &mut BufReader) -> Result> { // cursor_pos *WILL* fail if the user is typing on the keyboard. Be sure to handle // errors appropriately. pub fn cursor_pos(stdin: &mut BufReader) -> Result<(u16, u16), Box> { + let buf = stdin.fill_buf()?; + if buf.len() > 0 { + // If there is any buffered input, the below call to fill_buf will just return the buffered + // data and not actually read the terminal response. + return Err(Box::from("interrupted")); + } write!(stdout(), "\x1b[6n")?; stdout().flush()?; let buf = stdin.fill_buf()?; -- cgit v1.3.1