-
-
Notifications
You must be signed in to change notification settings - Fork 128
feat: add get cursor position #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Dave Grantham <[email protected]>
There was a problem hiding this 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]>
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' => { |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()
?
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]