From 8572ee6eb82cf96aff63a2053910fc0008b6f772 Mon Sep 17 00:00:00 2001 From: ticki Date: Fri, 29 Jul 2016 19:49:29 +0200 Subject: [PATCH] Get the tty device, get_tty --- Cargo.toml | 2 +- README.md | 1 + src/lib.rs | 2 +- src/tty.rs | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ad625ae..599eb4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "termion" -version = "1.0.3" +version = "1.0.4" authors = ["Ticki "] description = "A bindless library for manipulating terminals." repository = "https://github.com/ticki/termion" diff --git a/README.md b/README.md index a34bcd6..9a4f5c3 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ version of Termion. - Cursor movement. - Text formatting. - Console size. +- TTY-only stream. - Control sequences. - Termios control. - Password input. diff --git a/src/lib.rs b/src/lib.rs index 2c6ff43..668209f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ mod size; pub use size::terminal_size; mod tty; -pub use tty::is_tty; +pub use tty::{is_tty, get_tty}; #[macro_use] mod macros; diff --git a/src/tty.rs b/src/tty.rs index 17b4177..960f474 100644 --- a/src/tty.rs +++ b/src/tty.rs @@ -1,3 +1,4 @@ +use std::{fs, io}; use std::os::unix::io::AsRawFd; /// Is this stream an TTY? @@ -13,3 +14,19 @@ pub fn is_tty(stream: T) -> bool { pub fn is_tty(_stream: T) -> bool { unimplemented!(); } + +/// Get the TTY device. +/// +/// This allows for getting stdio representing _only_ the TTY, and not other streams. +#[cfg(target_os = "redox")] +pub fn get_tty() -> io::Result { + fs::OpenOptions::new().read(true).write(true).open(env::var("TTY")?) +} + +/// Get the TTY device. +/// +/// This allows for getting stdio representing _only_ the TTY, and not other streams. +#[cfg(not(target_os = "redox"))] +pub fn get_tty() -> io::Result { + fs::OpenOptions::new().read(true).write(true).open("/dev/tty") +}