aboutsummaryrefslogtreecommitdiffstats
path: root/src/term.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-01-11 18:48:07 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-01-11 18:48:07 -0800
commitf842e04c4b5579342c1e74b41e867d02e1727f3d (patch)
tree1b1f17332332fd6a6dde0f11aad85f683f4b8799 /src/term.rs
parent781478d73dd3dfe42596e1dd370339ca6ad30e31 (diff)
downloadeditor-f842e04c4b5579342c1e74b41e867d02e1727f3d.tar.zst
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.
Diffstat (limited to 'src/term.rs')
-rw-r--r--src/term.rs6
1 files changed, 6 insertions, 0 deletions
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<Stdin>) -> Result<Key, Box<dyn Error>> {
// 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<Stdin>) -> Result<(u16, u16), Box<dyn Error>> {
+ 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()?;