https://github.com/sitkevij/no_color
π¦ rust crate library to detect NO_COLOR
https://github.com/sitkevij/no_color
cli no-color rust rust-crate rust-library terminal
Last synced: over 1 year ago
JSON representation
π¦ rust crate library to detect NO_COLOR
- Host: GitHub
- URL: https://github.com/sitkevij/no_color
- Owner: sitkevij
- License: mit
- Created: 2020-11-20T17:14:56.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-02-13T05:11:36.000Z (over 1 year ago)
- Last Synced: 2025-02-13T05:19:37.044Z (over 1 year ago)
- Topics: cli, no-color, rust, rust-crate, rust-library, terminal
- Language: Rust
- Homepage:
- Size: 14.6 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# no_color
**no_color** is a zero dependency rust library for NO_COLOR environment variable detection.
[](https://crates.io/crates/no_color)
[](https://crates.io/crates/no_color)
[](https://github.com/sitkevij/no_color)
[](https://github.com/sitkevij/no_color/actions/workflows/ci.yml?branch=main)
[](https://docs.rs/no_color/0.2.0/no_color/)
[](https://github.com/sitkevij/no_color/blob/main/LICENSE)
## About
From [no-color.org](https://no-color.org/):
An increasing number of command-line software programs output text with ANSI color escape codes by
default. While some developers and users obviously prefer seeing these colors, many users donβt.
Unfortunately, every new piece of software seems to have a different way of disabling colored text
output and some software has no way at all.
Accepting the futility of trying to reverse this trend, an informal standard is hereby proposed:
> All command-line software which outputs text with ANSI color added should check for the presence
> of a NO_COLOR environment variable that, when present (regardless of its value), prevents the
> addition of ANSI color.
By adopting this standard, users that prefer to have plain, non-colored text output can set one
environment variable in their shell to have it automatically affect all supported software.
## Usage
no_color is a library crate which works with [Cargo](http://crates.io).
### Add to Cargo.toml
To use, add the following to your `Cargo.toml` dependencies section:
```toml
[dependencies]
no_color = "0.2"
```
or use cargo add:
```sh
cargo add no_color
```
### Implementing Code
Suggested updates to your rust code as below:
- Note as of edition 2018 [extern is not needed](https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html)
to import a crate.
- See [examples/simple.rs](examples/simple.rs) for reference.
```rust
extern crate no_color;
use no_color::*;
fn main() {
println!(
"Environment variable NO_COLOR {0} found. Now do something.",
{
if is_no_color() {
"is"
} else {
"is NOT"
}
}
);
}
```