Crates.io | colored_text |
lib.rs | colored_text |
version | |
source | src |
created_at | 2025-02-10 15:45:40.188599+00 |
updated_at | 2025-02-13 18:01:09.219147+00 |
description | A simple library for adding colors and styles to terminal text |
homepage | |
repository | https://github.com/seapagan/colored_text |
max_upload_size | |
id | 1550229 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A simple and intuitive library for adding colors and styles to terminal text in Rust.
NO_COLOR
environment variable - if this is set, all colors are
disabled and the text is returned uncoloredAdd this to your Cargo.toml
:
[dependencies]
colored_text = "0.3.0"
use colored_text::Colorize;
// Basic colors
println!("{}", "Red text".red());
println!("{}", "Blue text".blue());
println!("{}", "Green text".green());
// Background colors
println!("{}", "Red background".on_red());
println!("{}", "Blue background".on_blue());
// Text styles
println!("{}", "Bold text".bold());
println!("{}", "Italic text".italic());
println!("{}", "Underlined text".underline());
// RGB and Hex colors
println!("{}", "Custom color".rgb(255, 128, 0));
println!("{}", "Custom background".on_rgb(0, 128, 255));
println!("{}", "Hex color".hex("#ff8000"));
println!("{}", "Hex background".on_hex("#0080ff"));
// Chaining styles
println!("{}", "Bold red text".red().bold());
println!("{}", "Italic blue on yellow".blue().italic().on_yellow());
// Using with format! macro
let name = "World";
println!("{}", format!("Hello, {}!", name.blue().bold()));
.red()
.green()
.blue()
.yellow()
.magenta()
.cyan()
.white()
.black()
.bright_red()
.bright_green()
.bright_blue()
.bright_yellow()
.bright_magenta()
.bright_cyan()
.bright_white()
.on_red()
.on_green()
.on_blue()
.on_yellow()
.on_magenta()
.on_cyan()
.on_white()
.on_black()
.bold()
.dim()
.italic()
.underline()
.inverse()
- Swap foreground and background colors.strikethrough()
- Draw a line through the text.rgb(r, g, b)
- Custom text color using RGB values (0-255, compile-time
enforced).on_rgb(r, g, b)
- Custom background color using RGB values (0-255,
compile-time enforced).hsl(h, s, l)
- Custom text color using HSL values (hue: 0-360°, saturation:
0-100%, lightness: 0-100%).on_hsl(h, s, l)
- Custom background color using HSL values.hex(code)
- Custom text color using HTML/CSS hex code (e.g., "#ff8000" or
"ff8000").on_hex(code)
- Custom background color using HTML/CSS hex code.clear()
- Remove all stylingu8
type)// RGB values are constrained to 0-255
println!("{}", "RGB color".rgb(255, 128, 0));
// HSL values (hue: 0-360°, saturation/lightness: 0-100%)
println!("{}", "Red".hsl(0.0, 100.0, 50.0)); // Pure red
println!("{}", "Green".hsl(120.0, 100.0, 50.0)); // Pure green
println!("{}", "Blue".hsl(240.0, 100.0, 50.0)); // Pure blue
println!("{}", "Gray".hsl(0.0, 0.0, 50.0)); // 50% gray
// Hex colors work with or without #
println!("{}", "Hex color".hex("#ff8000"));
println!("{}", "Also valid".hex("ff8000"));
// Invalid hex codes return uncolored text
println!("{}", "Invalid".hex("xyz")); // Returns uncolored text
println!("{}", "Too short".hex("#f8")); // Returns uncolored text
This library respects the NO_COLOR environment
variable. If NO_COLOR
is set (to any value), all color and style methods will
return plain unformatted text. This makes it easy to disable all colors globally
if needed.
// Colors enabled (NO_COLOR not set)
println!("{}", "Red text".red()); // Prints in red
// With NO_COLOR set
std::env::set_var("NO_COLOR", "1");
println!("{}", "Red text".red()); // Prints without color
By default, this library checks if the output is going to a terminal and
disables colors when it's not (e.g., when piping output to a file). This
behavior can be controlled using ColorizeConfig
:
use colored_text::{Colorize, ColorizeConfig};
// Disable terminal detection (colors will be enabled regardless of terminal status)
ColorizeConfig::set_terminal_check(false);
println!("{}", "Always colored".red());
// Re-enable terminal detection (default behavior)
ColorizeConfig::set_terminal_check(true);
println!("{}", "Only colored in terminal".red());
This is particularly useful in test environments where you might want to force-enable colors regardless of the terminal status. The configuration is thread-local, making it safe to use in parallel tests without affecting other threads.
Note: Even when terminal detection is disabled, the NO_COLOR
environment
variable still takes precedence - if it's set, colors will be disabled
regardless of this setting.
This library uses ANSI escape codes for coloring and styling text. Most modern terminals support these codes, but the actual appearance may vary depending on your terminal emulator and its configuration:
Check out the examples directory for more usage examples.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.