From 447e8cf673805f21a8d38ea7faec860deee2e55b Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 30 Jan 2020 21:04:30 +0800 Subject: [PATCH] add term --- term/Cargo.toml | 13 +++++++++++++ term/src/main.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 term/Cargo.toml create mode 100644 term/src/main.rs diff --git a/term/Cargo.toml b/term/Cargo.toml new file mode 100644 index 0000000..9a3da15 --- /dev/null +++ b/term/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "term" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +libc = "0.2.65" +atty = "0.2.13" + + diff --git a/term/src/main.rs b/term/src/main.rs new file mode 100644 index 0000000..eee744b --- /dev/null +++ b/term/src/main.rs @@ -0,0 +1,18 @@ +use atty::Stream; + +// https://crates.io/crates/atty +// https://rosettacode.org/wiki/Check_output_device_is_a_terminal +fn main() { + if atty::is(Stream::Stdout) { + println!("I'm a terminal"); + } else { + println!("I'm not"); + } + + let istty = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) } != 0; + if istty { + println!("stdout is tty"); + } else { + println!("stdout is not tty"); + } +}