Skip to content

Conversation

dhuseby
Copy link

@dhuseby dhuseby commented Aug 22, 2025

This adds the ability to get the current cursor position using the ansi cursor
position query sequence. Sometimes it is useful to know where you are in the
terminal when your program first starts up, or when you are doing some complex
terminal manipulation.

Signed-off-by: Dave Grantham [email protected]

Signed-off-by: Dave Grantham <[email protected]>
Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's some initial feedback. It doesn't look like this needs a semver-incompatible version bump. It does look like it needs either a Windows implementation or a Unix-only guard.

Signed-off-by: Dave Grantham <[email protected]>
@dhuseby
Copy link
Author

dhuseby commented Aug 26, 2025

reverted back to 0.16.0 as well.

@@ -258,6 +258,44 @@ fn read_single_key_impl(fd: RawFd) -> Result<Key, io::Error> {
'H' => Ok(Key::Home),
'F' => Ok(Key::End),
'Z' => Ok(Key::BackTab),
'0'..='9' => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to keep this inside read_single_key_impl() when it effectively is only expected inside a call to read_single_key()?

(Part of this is is the very deeply nested indentation level making this code harder to follow.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could refactor this to reduce the indentation at several levels. I was trying to keep my edits minimal. If it were me I would refactor the block to be something like:

loop {
  match read_single_char(fd)? {
    Some('\x1b') => break match read_single_char(fd)? {
      Some('[') => match read_single_char(fd)? {
        Some(c) => match c {
          'A' => Ok(Key::ArrowUp),
          //...
          '0'..='9' => read_cursor_pos(c),
          _ => read_tilde_seq(c),
        }
        _ => Ok(Key::UnknownEscSeq(vec![c])),
      }
      Some(c) => Ok(Key::UnknownEscSeq(vec![c])),
      _ => Ok(Key::Escape)
    }
    Some(c) => read_non_escape_seq(c),
    None => {
      // there is no subsequent byte ready to be read, block and wait for
      // input negative timeout means that it will block indefinitely
      match select_or_poll_term_fd(fd, -1) {
        Ok(_) => continue,
        Err(_) => break Err(io::Error::last_os_error()),
      }
    }
  }
}

fn read_cursor_pos(c: char) -> Result<Key, io::Error> {
  //...
}

fn read_tilde_seq(c: char) -> Result<Key, io::Error> {
  //...
}

fn read_non_escape_seq(c: char) -> Result<Key, io::Error> {
  //...
}

Do you think moving the match arm bodies to functions is easier to read?

Copy link
Member

@djc djc Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might prefer that but let's not do it in this PR.

For this PR, do you think we need the specific read_cursor_pos() stuff inside the generic read_single_key_impl(), or can we get away with just having it in cursor_position()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants