Skip to content

Use in conjunction with console input #15

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

Closed
MasonRemaley opened this issue Feb 18, 2017 · 2 comments
Closed

Use in conjunction with console input #15

MasonRemaley opened this issue Feb 18, 2017 · 2 comments

Comments

@MasonRemaley
Copy link

MasonRemaley commented Feb 18, 2017

Should it be possible to use this library in a package that also reads input from the console? I wrote the following test program that I expected to wait for me to press control c, then prompt me for input, and then echo that input back to me:

extern crate ctrlc;

use std::sync::mpsc;
use std::io;

fn main() {
    println!("Waiting for Ctrl-C...");
    let (tx, rx) = mpsc::channel();
    ctrlc::set_handler(move || tx.send(()).unwrap());
    rx.recv().unwrap();

    println!("Waiting for input...");
    let mut line = String::new();
    println!("{:?}", io::stdin().read_line(&mut line));
    println!("Line: {}", line);
}

On macOS 10.12.1 and rust-ctrlc v2.* (due to #14), the call to read_line fails:

Err(Error { repr: Os { code: 5, message: "Input/output error" } })

On Windows 10, after pressing Ctrl-C, it prints "Waiting for input..." and then immediately exits without printing anything else/any errors. If I add a panic to the last line just to make sure it's not something funky with the standard output, it never makes it there.

@Detegr
Copy link
Owner

Detegr commented Feb 18, 2017

Are you running the executable with cargo run? There's a known issue #11 that Cargo sets up its own signal handlers and there might be some side effects due to that.

I'm also getting Input/output error if I run with cargo run but running the executable directly seems to work (at least on my Linux machine):

$ target/debug/ctrlctest
Waiting for Ctrl-C...
^CWaiting for input...
hello world
Ok(12)
Line: hello world

@MasonRemaley
Copy link
Author

Oh yup that's it, running the executable directly did the trick!

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