Crate chromaterm

Source
Expand description

This crate provides utilities for coloring and styling strings.

§Example

use chromaterm::prelude::*;

// This configures the crate to automatically detect what colors are supported by
// the user's terminal. If the user's environment doesn't support the colors you
// use, it will automatically try to convert to a supported color value. Not all
// terminals support "true colors" (RGB), for example.
chromaterm::config::use_default_color_support();
chromaterm::config::convert_to_supported(true);

println!("Hello, {}!", "World".green().italic().on_rgb(0, 64, 255));

§Advanced usage

§Color support

If you want to have more control over how color support is detected, use the ColorSupport type.

use chromaterm::ColorSupport;

// Detects support from the environment, and additionally respects the well-known
// NO_COLOR environment variable.
let support = ColorSupport::from_env().respect_no_color();
chromaterm::config::use_color_support(support);

§Configuration

use chromaterm::prelude::*;
use chromaterm::ColorSupport;

// Full color support ("true colors" are supported).
chromaterm::config::use_color_support(ColorSupport::True);
assert_eq!("styled".rgb(255, 0, 0).to_string(), "\x1B[38;2;255;0;0mstyled\x1B[0m");

// Only the basic 16 colors are supported, and we try to convert to the closest
// color.
chromaterm::config::use_color_support(ColorSupport::Simple);
chromaterm::config::convert_to_supported(true);
assert_eq!("bright red".rgb(255, 0, 0).to_string(), "bright red".bright_red().to_string());

// Now we refuse to convert to supported colors. If our color can't be displayed,
// we won't color at all! This can be useful if you feel that converting to less
// accurate colors can ruin the look of your output, and it's better to fall back
// to uncolored text.
chromaterm::config::convert_to_supported(false);
assert_eq!("not bright red".rgb(255, 0, 0).to_string(), "not bright red");

// No colors are supported.
chromaterm::config::use_color_support(ColorSupport::None);

// Because no colors are supported, the string is plain.
assert_eq!("not styled".rgb(255, 0, 0).to_string(), "not styled");

Re-exports§

pub use colors::Colors;
pub use styles::Styles;

Modules§

colors
Collection of color types.
config
Get and store global configuration.
conversion
Tools for converting to colors. Conversions may lose color precision.
prelude
Re-exports the traits required to “magically” add the colorization and styling methods to strings.
styles
Text styles.

Structs§

Colorizer
Wraps a value that can be colorized.
Styler
Wraps a value that can be colorized.

Enums§

ColorLevel
The color level.
ColorSupport
Detected color support.

Traits§

Color
Marks a type as being able to represent a color.
Colorize
The main trait to allow a type to be colored.
DisplayWithExact
Like Display, but checks for color support. It should disable any coloring or styling that isn’t supported.
DisplayWithFallback
Like Display, but checks for color support. If the coloring or styling isn’t supported, it should attempt to convert to coloring or styling that is supported.
Style
Marks a type as being able to represent a style.
Stylize
The main trait to allow a type to be styled.
OSZAR »