18 releases

0.4.13 Aug 14, 2024
0.4.12 Feb 9, 2024
0.4.10 Dec 28, 2023
0.4.9 Jul 6, 2023
0.2.0 Mar 16, 2022

#37 in Command-line interface

Download history 1292515/week @ 2024-08-24 1253579/week @ 2024-08-31 1278697/week @ 2024-09-07 1171393/week @ 2024-09-14 1264383/week @ 2024-09-21 1266008/week @ 2024-09-28 1323881/week @ 2024-10-05 1305860/week @ 2024-10-12 1309785/week @ 2024-10-19 1260209/week @ 2024-10-26 1264341/week @ 2024-11-02 1244165/week @ 2024-11-09 1276827/week @ 2024-11-16 1076538/week @ 2024-11-23 1243215/week @ 2024-11-30 1147852/week @ 2024-12-07

4,968,300 downloads per month
Used in 5,563 crates (103 directly)

MIT license

17KB
272 lines

is-terminal

Test whether a given stream is a terminal

Github Actions CI Status crates.io page docs.rs docs

As of Rust 1.70, most users should use the IsTerminal trait in the Rust standard library instead of this crate.

On Unix platforms, this crate now uses libc, so that the implementation matches what's in std. Users wishing to use the rustix-based implementation can use the rustix-is-terminal crate instead.


is-terminal is a simple utility that answers one question:

Is this a terminal?

A "terminal", also known as a "tty", is an I/O device which may be interactive and may support color and other special features. This crate doesn't provide any of those features; it just answers this one question.

On Unix-family platforms, this is effectively the same as the isatty function for testing whether a given stream is a terminal, though it accepts high-level stream types instead of raw file descriptors.

On Windows, it uses a variety of techniques to determine whether the given stream is a terminal.

This crate is derived from the atty crate with PR #51 bug fix and PR #54 port to windows-sys applied. The only additional difference is that the atty crate only accepts stdin, stdout, or stderr, while this crate accepts any stream. In particular, this crate does not access any stream that is not passed to it, in accordance with I/O safety.

Example

use is_terminal::IsTerminal;

fn main() {
    if std::io::stdout().is_terminal() {
        println!("Stdout is a terminal");
    } else {
        println!("Stdout is not a terminal");
    }
}

Testing

This library is tested on both Unix-family and Windows platforms.

To test it on a platform manually, use the provided stdio example program. When run normally, it prints this:

$ cargo run --example stdio
stdin? true
stdout? true
stderr? true

To test stdin, pipe some text to the program:

$ cat | cargo run --example stdio
stdin? false
stdout? true
stderr? true

To test stdout, pipe the program to something:

$ cargo run --example stdio | cat
stdin? true
stdout? false
stderr? true

To test stderr, pipe the program to something redirecting stderr:

$ cargo run --example stdio 2>&1 | cat
stdin? true
stdout? false
stderr? false

Minimum Supported Rust Version (MSRV)

This crate currently works on the version of Rust on Debian stable, which is currently Rust 1.63. This policy may change in the future, in minor version releases, so users using a fixed version of Rust should pin to a specific version of this crate.

Dependencies

~0–8MB
~61K SLoC