Skip to content
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

Closed
sshelll opened this issue Nov 11, 2024 · 3 comments
Closed

Pipe mode support #939

sshelll opened this issue Nov 11, 2024 · 3 comments

Comments

@sshelll
Copy link

sshelll commented Nov 11, 2024

Describe the bug
Not able to read key event through pipe on Mac OS.

To Reproduce
Steps to reproduce the behavior:
run this code:

fn main() {
    use crossterm::{event, terminal};
    terminal::enable_raw_mode().unwrap();
    let _evt = event::read().unwrap();
    terminal::disable_raw_mode().unwrap();
}

with ./main is ok.
with echo 1 | main will cause panic with this info:

called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error: "Failed to initialize input reader" }

Expected behavior
Be able to read events through pipe.

OS

  • MacOS

Terminal/Console

  • Alacritty with Tmux
@sshelll
Copy link
Author

sshelll commented Nov 11, 2024

Actually, crossterm::cursor::position() is also not working through pipe on MacOS -- It'll cause block.
So in my project, I replace this function with my own:

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.
So I opened this issue...🙏

@joshka
Copy link
Collaborator

joshka commented Nov 11, 2024

Take a look at https://github.com/cosmikwolf/piped_cli_interactive for some ideas (via #407)

@sshelll
Copy link
Author

sshelll commented Nov 12, 2024

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 use-dev-tty.

Ps: crossterm = { version = "0.28", features = ["use-dev-tty"] }

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

No branches or pull requests

2 participants