Is there any way one could read from stdin in non-canonical mode under Linux? Non-canonical input means that calls to read()
on stdin shall return as soon as the user types, which is not the default behaviour, as one can see by trying:
// Create a buffer
let mut buffer :[u8; 1] = [0];
// Loops over the input from stdin, one character a time
while io::stdin().read(&mut buffer).unwrap() > 0 {
println!("{:?}", buffer);
}
This code waits for the user to press return to print the contents of buffer
. The desired behaviour would be for it to print as the user typed. In the documentation for Stdin
(the struct returned by the stdin()
call in the code above), there is no reference to how one could change this default behaviour.
No, not without external crates or unsafe FFI code. You will probably want to use the termios functions. Specifically, see ICANON
and tcsetattr
. The crate nix has bindings for these functions. See here for an example of how to use them in Rust.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With