#terminal-colors #true-color #level #check #determine #detect #identify

bin+lib term_color_support

Determine whether a terminal supports color or not, and identify the level of color support

2 releases

0.1.1 May 22, 2024
0.1.0 May 21, 2024

#281 in Command-line interface

40 downloads per month

Apache-2.0

39KB
667 lines

Term Color Support

Description

Determine whether a terminal supports color or not, and if it supports color, identify the level of color support available.

Features

  • Detect if the terminal supports color.
  • Identify the level of color support (e.g., no color, basic 16 colors, 256 colors, true color).

Installation

Prerequisites

  • Rust (Ensure you have Rust and Cargo installed)

Adding as a Dependency

To use this package, add the following to your Cargo.toml:

[dependencies]
term_color_support = "0.1.0"  # Replace with the latest version

Usage

use term_color_support::ColorSupport;
fn main() {
    // Detect and print color support for stdout
    println!("Color support for stdout: {:?}", ColorSupport::stdout());

    // Detect and print color support for stderr
    println!("Color support for stderr: {:?}", ColorSupport::stderr());
}

The output of the above code will be something like this:

Color support for stdout: ColorInfo { level: TrueColor, has_basic: true, has_256: true, has_16m: true }
Color support for stderr: ColorInfo { level: TrueColor, has_basic: true, has_256: true, has_16m: true }

API

Structs

ColorInfo

pub struct ColorInfo {
    /// The color support level.
    pub level: ColorSupportLevel,
    /// Indicates if basic color support is available.
    pub has_basic: bool,
    /// Indicates if 256-color support is available.
    pub has_256: bool,
    /// Indicates if true color support (16 million colors) is available.
    pub has_16m: bool,
}

ColorSupportLevel

pub enum ColorSupportLevel {
    /// No color support.
    NoColor,
    /// Basic color support.
    Basic,
    /// Support for 256 colors.
    Colors256,
    /// True color support.
    TrueColor,
}

Examples

For detailed examples of how to use the package, including more complex use cases and scenarios, you can refer to the main.rs file in the src/bin directory. A simple example is provided there.

Contributing

Guidelines for contributing to Term Color Support:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature-name).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature/your-feature-name).
  6. Open a Pull Request.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Acknowledgements

Term Color Support was inspired by the npm package supports-color, which is used to detect whether a terminal supports color. The concepts of Term Color Support are influenced by supports-color.

We are thankful to the maintainers of supports-color, Sindre Sorhus and Josh Junon, for their contribution to the open-source community.

Contact

For support or questions, you can contact me via:

Dependencies

~3–11MB
~116K SLoC