Skip to content

Commit

Permalink
Group repeat code to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Apr 13, 2019
1 parent e973520 commit 94a8067
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/utils/tty.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
// Copied from rustc. atty crate did not work as expected
#[cfg(unix)]
pub fn stderr_isatty() -> bool {
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
isatty(libc::STDERR_FILENO)
}

// FIXME: Unfortunately this doesn't detect msys terminals so rustup
// is always colorless there (just like rustc and cargo).
#[cfg(windows)]
pub fn stderr_isatty() -> bool {
type DWORD = u32;
type BOOL = i32;
type HANDLE = *mut u8;
const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
extern "system" {
fn GetStdHandle(which: DWORD) -> HANDLE;
fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: *mut DWORD) -> BOOL;
}
unsafe {
let handle = GetStdHandle(STD_ERROR_HANDLE);
let mut out = 0;
GetConsoleMode(handle, &mut out) != 0
}
isatty(winapi::um::winbase::STD_ERROR_HANDLE)
}

#[cfg(unix)]
pub fn stdout_isatty() -> bool {
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
isatty(libc::STDOUT_FILENO)
}

#[cfg(windows)]
pub fn stdout_isatty() -> bool {
type DWORD = u32;
type BOOL = i32;
type HANDLE = *mut u8;
const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
extern "system" {
fn GetStdHandle(which: DWORD) -> HANDLE;
fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: *mut DWORD) -> BOOL;
}
isatty(winapi::um::winbase::STD_OUTPUT_HANDLE)
}

#[inline]
#[cfg(unix)]
fn isatty(fd: libc::c_int) -> bool {
unsafe { libc::isatty(fd) == 1 }
}

#[inline]
#[cfg(windows)]
fn isatty(fd: winapi::shared::minwindef::DWORD) -> bool {
use winapi::um::{consoleapi::GetConsoleMode, processenv::GetStdHandle};
unsafe {
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
let handle = GetStdHandle(fd);
let mut out = 0;
GetConsoleMode(handle, &mut out) != 0
}
Expand Down

0 comments on commit 94a8067

Please sign in to comment.