raw_tty
This crate can be used for generally interacting with a tty's mode safely, but was created originally to solve the problem of using raw mode with /dev/tty while reading stdin for data.
Description from the termion
crate:
Managing raw mode.
Raw mode is a particular state a TTY can have. It signifies that:
- No line buffering (the input is given byte-by-byte).
- The input is not written out, instead it has to be done manually by the programmer.
- The output is not canonicalized (for example,
\n
means "go one line down", not "line break").
It is essential to design terminal programs.
```rust use raw_tty::IntoRawMode; use std::io::{Write, stdin, stdout};
fn main() { let stdin = stdin().intorawmode().unwrap(); let mut stdout = stdout();
write!(stdout, "Hey there.").unwrap();
} ```
```rust use raw_tty::IntoRawMode; use std::io::{self, Read, Write, stdin, stdout}; use std::fs;
fn main() -> io::Result<()> { let mut tty = fs::OpenOptions::new().read(true).write(true).open("/dev/tty")?; // Can use the ttyinput for keys while also reading stdin for data. let mut ttyinput = tty.tryclone()?.intorawmode(); let mut buffer = String::new(); stdin().readto_string(&mut buffer)?;
write!(tty, "Hey there.")
} ```
```rust use raw_tty::GuardMode; use std::io::{self, Write, stdin, stdout};
fn testintorawmode() -> io::Result<()> { let mut stdin = stdin().guardmode()?; stdin.setrawmode()?; let mut out = stdout();
out.write_all(b"this is a test, muahhahahah\r\n")?;
drop(out);
Ok(())
}
fn main() -> io::Result<()> { let mut stdout = stdout().guardmode()?; stdout.modifymode(|ios| /* do stuff with termios here */ ios)?;
// Have to use &* since TtyModeGuard only implements
// deref, unlike RawReader which implements read specifically.
// Otherwise, it wouldn't be recognized as `Write`able.
write!(&mut *stdout, "Hey there.")
}
```