1d0b88b7eSopenharmony_ci<div align="center">
2d0b88b7eSopenharmony_ci  <h1><code>is-terminal</code></h1>
3d0b88b7eSopenharmony_ci
4d0b88b7eSopenharmony_ci  <p>
5d0b88b7eSopenharmony_ci    <strong>Test whether a given stream is a terminal</strong>
6d0b88b7eSopenharmony_ci  </p>
7d0b88b7eSopenharmony_ci
8d0b88b7eSopenharmony_ci  <p>
9d0b88b7eSopenharmony_ci    <a href="https://github.com/sunfishcode/is-terminal/actions?query=workflow%3ACI"><img src="https://github.com/sunfishcode/is-terminal/workflows/CI/badge.svg" alt="Github Actions CI Status" /></a>
10d0b88b7eSopenharmony_ci    <a href="https://crates.io/crates/is-terminal"><img src="https://img.shields.io/crates/v/is-terminal.svg" alt="crates.io page" /></a>
11d0b88b7eSopenharmony_ci    <a href="https://docs.rs/is-terminal"><img src="https://docs.rs/is-terminal/badge.svg" alt="docs.rs docs" /></a>
12d0b88b7eSopenharmony_ci  </p>
13d0b88b7eSopenharmony_ci</div>
14d0b88b7eSopenharmony_ci
15d0b88b7eSopenharmony_ciis-terminal is a simple utility that answers one question:
16d0b88b7eSopenharmony_ci
17d0b88b7eSopenharmony_ci> Is this a terminal?
18d0b88b7eSopenharmony_ci
19d0b88b7eSopenharmony_ciA "terminal", also known as a "tty", is an I/O device which may be interactive
20d0b88b7eSopenharmony_ciand may support color and other special features. This crate doesn't provide
21d0b88b7eSopenharmony_ciany of those features; it just answers this one question.
22d0b88b7eSopenharmony_ci
23d0b88b7eSopenharmony_ciOn Unix-family platforms, this is effectively the same as the [`isatty`]
24d0b88b7eSopenharmony_cifunction for testing whether a given stream is a terminal, though it accepts
25d0b88b7eSopenharmony_cihigh-level stream types instead of raw file descriptors.
26d0b88b7eSopenharmony_ci
27d0b88b7eSopenharmony_ciOn Windows, it uses a variety of techniques to determine whether the given
28d0b88b7eSopenharmony_cistream is a terminal.
29d0b88b7eSopenharmony_ci
30d0b88b7eSopenharmony_ciThis crate is derived from [the atty crate] with [PR \#51] bug fix and
31d0b88b7eSopenharmony_ci[PR \#54] port to windows-sys applied. The only additional difference is that
32d0b88b7eSopenharmony_cithe atty crate only accepts stdin, stdout, or stderr, while this crate accepts
33d0b88b7eSopenharmony_ciany stream. In particular, this crate does not access any stream that is not
34d0b88b7eSopenharmony_cipassed to it, in accordance with [I/O safety].
35d0b88b7eSopenharmony_ci
36d0b88b7eSopenharmony_ci[PR \#51]: https://github.com/softprops/atty/pull/51
37d0b88b7eSopenharmony_ci[PR \#54]: https://github.com/softprops/atty/pull/54
38d0b88b7eSopenharmony_ci
39d0b88b7eSopenharmony_ci## Example
40d0b88b7eSopenharmony_ci
41d0b88b7eSopenharmony_ci```rust
42d0b88b7eSopenharmony_ciuse is_terminal::IsTerminal;
43d0b88b7eSopenharmony_ci
44d0b88b7eSopenharmony_cifn main() {
45d0b88b7eSopenharmony_ci    if std::io::stdout().is_terminal() {
46d0b88b7eSopenharmony_ci        println!("Stdout is a terminal");
47d0b88b7eSopenharmony_ci    } else {
48d0b88b7eSopenharmony_ci        println!("Stdout is not a terminal");
49d0b88b7eSopenharmony_ci    }
50d0b88b7eSopenharmony_ci}
51d0b88b7eSopenharmony_ci```
52d0b88b7eSopenharmony_ci
53d0b88b7eSopenharmony_ci## Testing
54d0b88b7eSopenharmony_ci
55d0b88b7eSopenharmony_ciThis library is tested on both Unix-family and Windows platforms.
56d0b88b7eSopenharmony_ci
57d0b88b7eSopenharmony_ciTo test it on a platform manually, use the provided `stdio` example program.
58d0b88b7eSopenharmony_ciWhen run normally, it prints this:
59d0b88b7eSopenharmony_ci
60d0b88b7eSopenharmony_ci```bash
61d0b88b7eSopenharmony_ci$ cargo run --example stdio
62d0b88b7eSopenharmony_cistdin? true
63d0b88b7eSopenharmony_cistdout? true
64d0b88b7eSopenharmony_cistderr? true
65d0b88b7eSopenharmony_ci```
66d0b88b7eSopenharmony_ci
67d0b88b7eSopenharmony_ciTo test stdin, pipe some text to the program:
68d0b88b7eSopenharmony_ci
69d0b88b7eSopenharmony_ci```bash
70d0b88b7eSopenharmony_ci$ cat | cargo run --example stdio
71d0b88b7eSopenharmony_cistdin? false
72d0b88b7eSopenharmony_cistdout? true
73d0b88b7eSopenharmony_cistderr? true
74d0b88b7eSopenharmony_ci```
75d0b88b7eSopenharmony_ci
76d0b88b7eSopenharmony_ciTo test stdout, pipe the program to something:
77d0b88b7eSopenharmony_ci
78d0b88b7eSopenharmony_ci```bash
79d0b88b7eSopenharmony_ci$ cargo run --example stdio | cat
80d0b88b7eSopenharmony_cistdin? true
81d0b88b7eSopenharmony_cistdout? false
82d0b88b7eSopenharmony_cistderr? true
83d0b88b7eSopenharmony_ci```
84d0b88b7eSopenharmony_ci
85d0b88b7eSopenharmony_ciTo test stderr, pipe the program to something redirecting stderr:
86d0b88b7eSopenharmony_ci
87d0b88b7eSopenharmony_ci```bash
88d0b88b7eSopenharmony_ci$ cargo run --example stdio 2>&1 | cat
89d0b88b7eSopenharmony_cistdin? true
90d0b88b7eSopenharmony_cistdout? false
91d0b88b7eSopenharmony_cistderr? false
92d0b88b7eSopenharmony_ci```
93d0b88b7eSopenharmony_ci
94d0b88b7eSopenharmony_ci# Minimum Supported Rust Version (MSRV)
95d0b88b7eSopenharmony_ci
96d0b88b7eSopenharmony_ciThis crate currently works on the version of [Rust on Debian stable], which is
97d0b88b7eSopenharmony_cicurrently Rust 1.48. This policy may change in the future, in minor version
98d0b88b7eSopenharmony_cireleases, so users using a fixed version of Rust should pin to a specific
99d0b88b7eSopenharmony_civersion of this crate.
100d0b88b7eSopenharmony_ci
101d0b88b7eSopenharmony_ci[`isatty`]: https://man7.org/linux/man-pages/man3/isatty.3.html
102d0b88b7eSopenharmony_ci[the atty crate]: https://crates.io/crates/atty
103d0b88b7eSopenharmony_ci[I/O safety]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md
104d0b88b7eSopenharmony_ci[Rust on Debian stable]: https://packages.debian.org/stable/rust/rustc
105