1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2024-12-12 19:18:24 +02:00
ripgrep/src/atty.rs

42 lines
905 B
Rust
Raw Normal View History

2016-09-05 23:36:41 +02:00
/*!
2016-09-10 06:05:20 +02:00
This atty module contains functions for detecting whether ripgrep is being fed
from (or to) a terminal. Windows and Unix do this differently, so implement
both here.
2016-09-05 23:36:41 +02:00
*/
#[cfg(unix)]
2016-09-10 06:05:20 +02:00
pub fn on_stdin() -> bool {
use libc;
2016-09-05 23:36:41 +02:00
0 < unsafe { libc::isatty(libc::STDIN_FILENO) }
}
#[cfg(unix)]
2016-09-10 06:05:20 +02:00
pub fn on_stdout() -> bool {
use libc;
2016-09-05 23:36:41 +02:00
0 < unsafe { libc::isatty(libc::STDOUT_FILENO) }
}
#[cfg(windows)]
2016-09-10 06:05:20 +02:00
pub fn on_stdin() -> bool {
2016-09-05 23:36:41 +02:00
use kernel32;
use winapi;
unsafe {
let fd = winapi::winbase::STD_INPUT_HANDLE;
let mut out = 0;
kernel32::GetConsoleMode(kernel32::GetStdHandle(fd), &mut out) != 0
}
}
#[cfg(windows)]
2016-09-10 06:05:20 +02:00
pub fn on_stdout() -> bool {
2016-09-05 23:36:41 +02:00
use kernel32;
use winapi;
unsafe {
let fd = winapi::winbase::STD_OUTPUT_HANDLE;
let mut out = 0;
2016-09-06 03:02:08 +02:00
kernel32::GetConsoleMode(kernel32::GetStdHandle(fd), &mut out) != 0
2016-09-05 23:36:41 +02:00
}
}