Skip to content
Snippets Groups Projects
Commit bb16b12a authored by Katsu Kawakami's avatar Katsu Kawakami Committed by kkawakam
Browse files

Adding Window-specific isatty function

libc does not define the STDIN_FILENO & STDOUT_FILENO for the windows
platform.  We instead check whether or not stdin and stdout are TTY
by checking the GetConsoleMode output.
parent cb085b6e
No related branches found
No related tags found
No related merge requests found
......@@ -900,8 +900,8 @@ impl<'completer> Editor<'completer> {
// if the number of columns is stored here, we need a SIGWINCH handler...
let editor = Editor {
unsupported_term: tty::is_unsupported_term(),
stdin_isatty: tty::is_a_tty(libc::STDIN_FILENO),
stdout_isatty: tty::is_a_tty(libc::STDOUT_FILENO),
stdin_isatty: tty::is_a_tty(tty::StandardStream::StdIn),
stdout_isatty: tty::is_a_tty(tty::StandardStream::StdOut),
history: History::new(),
completer: None,
kill_ring: KillRing::new(60),
......
......@@ -22,8 +22,12 @@ pub trait Terminal {
fn disable_raw_mode(&self) -> Result<()>;
}
/// Check to see if `fd` is a TTY
pub fn is_a_tty(fd: libc::c_int) -> bool {
unsafe { libc::isatty(fd) != 0 }
/// Enum for Standard Streams
///
/// libc::STDIN_FILENO/STDOUT_FILENO/STDERR_FILENO is not defined for the
/// Windows platform. We will use this enum instead when calling isatty
/// function
pub enum StandardStream {
StdIn,
StdOut,
}
......@@ -5,6 +5,7 @@ use std;
use nix::sys::termios;
use nix::errno::Errno;
use super::Terminal;
use super::StandardStream;
use ::Result;
use ::error;
......@@ -65,6 +66,19 @@ pub fn is_unsupported_term() -> bool {
}
}
/// Return whether or not STDIN, STDOUT or STDERR is a TTY
pub fn is_a_tty(stream: StandardStream) -> bool {
extern crate libc;
let fd = match stream {
StandardStream::StdIn => libc::STDIN_FILENO,
StandardStream::StdOut => libc::STDOUT_FILENO,
};
unsafe { libc::isatty(fd) != 0 }
}
/// Structure that will contain the original termios before enabling RAW mode
pub struct UnixTerminal {
original_termios: Option<termios::Termios>
......@@ -75,7 +89,7 @@ impl Terminal for UnixTerminal {
fn enable_raw_mode(&mut self) -> Result<()> {
use nix::sys::termios::{BRKINT, CS8, ECHO, ICANON, ICRNL, IEXTEN, INPCK, ISIG, ISTRIP, IXON,
OPOST, VMIN, VTIME};
if !super::is_a_tty(libc::STDIN_FILENO) {
if !is_a_tty(StandardStream::StdIn) {
return Err(error::ReadlineError::from_errno(Errno::ENOTTY));
}
let original_termios = try!(termios::tcgetattr(libc::STDIN_FILENO));
......
extern crate kernel32;
extern crate winapi;
use super::StandardStream;
/// Return whether or not STDIN, STDOUT or STDERR is a TTY
fn is_a_tty(stream: StandardStream) -> bool {
let handle = match stream {
StandardStream::StdIn => winapi::winbase::STD_INPUT_HANDLE,
StandardStream::Stdout => winapi::winbase::STD_OUTPUT_HANDLE,
};
unsafe {
let handle = kernel32::GetStdHandle(handle);
let mut out = 0;
kernel32::GetConsoleMode(handle, &mut out) != 0
}
}
/// Checking for an unsupported TERM in windows is a no-op
pub fn is_unsupported_term() -> bool {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment