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"); + } +}