Test whether a given stream is a terminal

Overview

is-terminal

Test whether a given stream is a terminal

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

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 applied. The main externally visible differences are 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.48. 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.

You might also like...
Small and simple CLI app to generate .editorconfig based on a given settings.

add-editorconfig Small and simple CLI app to generate .editorconfig based on a given settings. Usage # Will create an .editorconfig in the current dir

Command line utility to remove duplicates from the given input.

Command line utility to remove duplicates from the given input. Note that huniq does not sort the input, it just removes duplicates.

Write a simple CLI script, that when given a 64-byte encoded string

Write a simple CLI script, that when given a 64-byte encoded string, it finds a suitable 4-byte prefix so that, a SHA256 hash of the prefix combined with the original string of bytes, has two last bytes as 0xca, 0xfe. Script should expect the original content of the string to be passed in hexadecimal format and should return two lines, first being the SHA256 string found and second 4-byte prefix used (in hexadecimal format).

Fetch and extract HTML's title and description by given link.

extd Fetch and extract HTML's title and description by given link. Usage in Cargo.toml: [dependencies] extd = "0.1.4" Example use extd::extract_td; f

Given a set of kmers (fasta format) and a set of sequences (fasta format), this tool will extract the sequences containing the kmers.

Kmer2sequences Description Given a set of kmers (fasta / fastq [.gz] format) and a set of sequences (fasta / fastq [.gz] format), this tool will extra

Stream-based visual programming language for systems observability
Stream-based visual programming language for systems observability

Stream-based visual programming language for systems observability. Metalens allows to build observability programs in a reactive and visual way. On L

Scol (Stream Colorizer)

Scol (Stream Colorizer) Scol is a simple command-line program that reads lines from standard input and colorizes them based on a regular expression pa

A terminal ASCII media player. View images, gifs, videos, webcam, YouTube, etc.. directly in the terminal as ASCII art.
A terminal ASCII media player. View images, gifs, videos, webcam, YouTube, etc.. directly in the terminal as ASCII art.

Terminal Media Player View images, videos (files or YouTube links), webcam, etc directly in the terminal as ASCII. All images you see below are just m

ask.sh: AI terminal assistant that can read and write your terminal directly!
ask.sh: AI terminal assistant that can read and write your terminal directly!

ask.sh: AI terminal assistant that read from & write to your terminal ask.sh is an AI terminal assistant based on OpenAI APIs such as GPT-3.5/4! What'

Comments
  • stub wasm32-u-u target ?

    stub wasm32-u-u target ?

    Re: https://github.com/clap-rs/clap/issues/4510

    Considering is_terminal is seen as direct migration from atty -

    When people are migrating off atty they will get this error under wasm32-u-u if there is callpath to is_terminal:

      --> .cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/lib.rs:33:1
       |
    33 | mod sys;
       | ^^^^^^^^
       |
       = help: to create the module `sys`, create file ".cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/sys.rs" or ".cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/sys/mod.rs"
    
    error[E0425]: cannot find function `errno` in module `sys`
       --> .cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/lib.rs:101:10
        |
    101 |     sys::errno()
        |          ^^^^^ not found in `sys`
    
    error[E0425]: cannot find function `set_errno` in module `sys`
       --> .cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/lib.rs:106:10
        |
    106 |     sys::set_errno(err)
        |          ^^^^^^^^^ not found in `sys`
    

    Would it be feasible to stub out wasm32-u-u that it still builds and provides false to is_terminal ?

    Or at least if the dependency is there it does not try to build stuff in wasm32-u-u that will fail to compile ?

    Considering the below -

    Even if one stamps out the calls to is_terminal then io-lifetimes doesn't build at all if it's present in the dependencies

    Sometimes the dependencies come via default features as in case of clap which made it pretty complicated to remove -

    Yeah I know wasm32-u-u doesn't have terminal but would it be feasible to provide stubs for it to ease migration ?

    Or at least cfg gate some of it out @ target_family = "wasm", target_os = "unknown" ?

    Cheers

    opened by pinkforest 6
  • Fix the GetFileInformationByHandleEx function usage

    Fix the GetFileInformationByHandleEx function usage

    There are two issues in the current implementation of the msys_tty_on function. Firstly, the size returned by the GetFileInformationByHandleEx function is the size in bytes not in characters. Secondly, the path can exceed the MAX_PATH constant.

    I stumbled upon this issue when some of our tests begin to fail on Windows after the update to the env_logger 0.10 version which switches to the is-terminal crate.

    opened by stanislav-tkach 5
  • Rename workflow to fix CI badge

    Rename workflow to fix CI badge

    The current badge image URI returns 404 Not Found as it points to a workflow named "CI". This fixes it by renaming the workflow to "CI" instead of "Continuous integration"

    opened by Cyborus04 4
  • Does not compile on Windows, windows_sys::Win32::Storage missing

    Does not compile on Windows, windows_sys::Win32::Storage missing

    I'm trying to use is_terminal as a dependency in my project but it doesn't compile on Windows, complaining that the module windows_sys::Win32::Storage doesn't exist. Modifying is_terminal's Cargo.toml like this makes it work:

    [target.'cfg(windows)'.dependencies.windows-sys]
    version = "0.42.0"
    features = [
        "Win32_Foundation",
    +   "Win32_Storage_FileSystem",
        "Win32_System_Console",
    ]
    

    So the fix should be straightforward, however, I'm not really sure how all those other people who presumably use this crate on Windows managed to get it to compile without this feature flag?

    opened by michalburger1 2
Owner
Dan Gohman
I'm working on Wasmtime, WASI, and lots of things related to WebAssembly.
Dan Gohman
Detects whether a terminal supports unicode.

Detects whether a terminal supports unicode. This crate is a Rust port mashing together @sindresorhus' is-unicode-supported and @iarna's has-unicode N

Kat Marchán 11 Jul 29, 2022
Detects whether a terminal supports color, and gives details about that support

Detects whether a terminal supports color, and gives details about that support. It takes into account the NO_COLOR environment variable. This crate i

Kat Marchán 30 Dec 29, 2022
decode a byte stream of varint length-encoded messages into a stream of chunks

length-prefixed-stream decode a byte stream of varint length-encoded messages into a stream of chunks This crate is similar to and compatible with the

James Halliday 4 Feb 26, 2022
A Rust library for evaluating log4j substitution queries in order to determine whether or not malicious queries may exist.

log4j_interpreter A Rust library for evaluating log4j substitution queries in order to determine whether or not malicious queries may exist. Limitatio

Fastly 22 Nov 7, 2022
Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're working with files or directories, this tool simplifies common file operations with ease.

Rust FileOps Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're w

Harikesh Ranjan Sinha 5 May 2, 2024
Coppers is a custom test harnass for Rust that measures the energy usage of your test suite.

Coppers Coppers is a test harness for Rust that can measure the evolution of power consumptions of a Rust program between different versions with the

Thijs Raymakers 175 Dec 4, 2022
100% stream-based O(n) syntax highlighter for ANSI terminal

Lex-highlighter 100% stream-based $\mathcal O(n)$ syntax highlighter for ANSI terminal Warning This is proof-of-concept implementation and WON't be co

Abiria 10 Nov 17, 2023
create and test the style and formatting of text in your terminal applications

description: create and test the style and formatting of text in your terminal applications docs: https://docs.rs/termstyle termstyle is a library tha

Rett Berg 18 Jul 3, 2021
Terminal-based typing test.

ttyper Ttyper is a terminal-based typing test built with Rust and tui-rs. installation With Cargo: cargo install ttyper usage For usage instructions,

Max Niederman 527 Dec 17, 2022
dua (-> Disk Usage Analyzer) is a tool to conveniently learn about the usage of disk space of a given directory

dua (-> Disk Usage Analyzer) is a tool to conveniently learn about the usage of disk space of a given directory. It's parallel by default and will max

Sebastian Thiel 1.8k Jan 2, 2023