-
Notifications
You must be signed in to change notification settings - Fork 285
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
Pipe mode support #939
Comments
Actually, fn get_cursor_position() -> io::Result<(u16, u16)> {
use std::fs::OpenOptions;
use std::io::{self, Read, Write};
// open tty
let mut tty = OpenOptions::new().read(true).write(true).open("/dev/tty")?;
// ask for cursor position
write!(tty, "\x1b[6n")?;
tty.flush()?;
// read resp
// resp fmt: \x1b[{row};{col}R
let mut response = String::new();
let mut buffer = [0; 1];
while tty.read(&mut buffer)? == 1 {
response.push(buffer[0] as char);
if buffer[0] == b'R' {
break;
}
}
if let Some(caps) = response
.strip_prefix("\x1b[")
.and_then(|s| s.strip_suffix("R"))
{
let mut parts = caps.split(';');
if let (Some(row), Some(col)) = (parts.next(), parts.next()) {
let row = row.parse().unwrap_or(0);
let col = col.parse().unwrap_or(0);
return Ok((row, col));
}
}
Err(io::Error::new(
io::ErrorKind::Other,
"Failed to parse cursor position",
))
} However, refactoring and implementing those codes of reading events would be much more complicated in my project. |
Take a look at https://github.com/cosmikwolf/piped_cli_interactive for some ideas (via #407) |
Thanks a lot, there're no more errors after adding the feature Ps: |
Describe the bug
Not able to read key event through pipe on Mac OS.
To Reproduce
Steps to reproduce the behavior:
run this code:
with
./main
is ok.with
echo 1 | main
will cause panic with this info:Expected behavior
Be able to read events through pipe.
OS
Terminal/Console
The text was updated successfully, but these errors were encountered: