Document why stdout is required for into_raw_mode
Created by: joshhansen
It appears that, counterintuitively, it's necessary to open a raw mode stdout in order to read from stdin keypress by keypress. Consider this working example:
extern crate termion;
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use std::io::{Write, stdout, stdin};
fn main() {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
for c in stdin.keys() {
match c.unwrap() {
Key::Char('q') => break,
_ => {},
}
}
}
Here it's possible to exit the program simply by pushing 'q'. But now if we comment out the line that opens stdout in raw mode:
extern crate termion;
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use std::io::{Write, stdout, stdin};
fn main() {
let stdin = stdin();
// let mut stdout = stdout().into_raw_mode().unwrap();
for c in stdin.keys() {
match c.unwrap() {
Key::Char('q') => break,
_ => {},
}
}
}
then exiting the program requires pressing 'q' and then pushing 'Enter'.
This is counterintuitive. I wouldn't expect the use of stdout to be necessary in order to read keys from stdin.
It's not enough simply to instantiate the raw stdout---it also needs to remain in scope while the keys are being read from stdin.
This is with termion 1.0.4, rustc 1.10.0, on Linux (Fedora 20).