Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using is_terminal and environment variables to determine whether to output ANSI colour sequences. #275

Merged
merged 1 commit into from
Aug 1, 2023

Conversation

hovinen
Copy link
Collaborator

@hovinen hovinen commented Jul 24, 2023

This makes use of the trait IsTerminal, stabilised in Rust 1.70. If the Rust version is at least 1.70, then the library uses this trait to detect whether stdout is a terminal and only outputs colours by default if that is the case. When used on prior Rust versions, the library does not detect whether the output is a terminal and defaults to not outputing colours.

This also introduces support for two environment variables to override the default behaviour defined above:

The environment variable GTEST_RUST_NO_COLOR is hereby no longer supported.

This also removes the ANSI colour sequences from nearly all tests. It creates two integration tests which specifically test the colourised output respectively the behaviour of the environment variable NO_COLOR. Otherwise, it globally sets NO_COLOR to suppress colourised output. This greatly reduces the noise in the existing test assertions and makes sure the tests continue to be compatible with all environments.

Since the use of IsTerminal is optional and conditional on the Rust version, this does not change the minimum supported Rust version.

@hovinen hovinen force-pushed the control-colors-with-is-terminal branch 3 times, most recently from 8df6c6c to 8a5fc1b Compare July 24, 2023 12:40
@hovinen
Copy link
Collaborator Author

hovinen commented Jul 24, 2023

This is needed for #221.

@hovinen hovinen force-pushed the control-colors-with-is-terminal branch from 8a5fc1b to 8a0f55b Compare July 26, 2023 15:22
…rmine whether to output ANSI colour sequences.

This makes use of the trait [`IsTerminal`](https://doc.rust-lang.org/stable/std/io/trait.IsTerminal.html), stabilised in Rust 1.70. If the Rust version is at least 1.70, then the library uses this trait to detect whether stdout is a terminal and only outputs colours by default if that is the case. When used on prior Rust versions, the library does not detect whether the output is a terminal and defaults to not outputing colours.

This also introduces support for two environment variables to override the default behaviour defined above:

 * `NO_COLOR`, described in <http://no-color.org>.
 * `FORCE_COLOR`, which is supported by a range of tools such as [Node.js](https://nodejs.org/api/cli.html#force_color1-2-3).

The environment variable `GTEST_RUST_NO_COLOR` is hereby no longer supported.

This also removes the ANSI colour sequences from nearly all tests. It creates two integration tests which specifically test the colourised output respectively the behaviour of the environment variable `NO_COLOR`. Otherwise, it globally sets `NO_COLOR` to suppress colourised output. This greatly reduces the noise in the existing test assertions and makes sure the tests continue to be compatible with all environments.

Since the use of `IsTerminal` is optional and conditional on the Rust version, this does not change the minimum supported Rust version.
@hovinen hovinen force-pushed the control-colors-with-is-terminal branch from 8a0f55b to 25b51c7 Compare July 26, 2023 15:23
@copybara-service copybara-service bot merged commit 4e3d04a into main Aug 1, 2023
@gribozavr gribozavr deleted the control-colors-with-is-terminal branch February 17, 2025 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant