GitHub - nik-rev/simply-colored: The simplest crate in existence for terminal colors

crates.io docs.rs license msrv github

This crate is the simplest yet ergonomic way to add color to your terminal.

All this crate contains is a few dozen constants corresponding to particular ANSI escape codes.

Usage

use simply_colored::*;

println!("{BLUE}{BOLD}Simply colored!")

Foreground

Color Type To get
Green {GREEN}Simply colored! Green text color in terminal
Yellow {YELLOW}Simply colored! Yellow text color in terminal
Red {RED}Simply colored! Red text color in terminal
Magenta {MAGENTA}Simply colored! Magenta text color in terminal
Blue {BLUE}Simply colored! Blue text color in terminal
Cyan {CYAN}Simply colored! Cyan text color in terminal
White {WHITE}Simply colored! White text color in terminal
Black {BLACK}Simply colored! Black text color in terminal
Dim green {DIM_GREEN}Simply colored! Dim green text color in terminal
Dim yellow {DIM_YELLOW}Simply colored! Dim yellow text color in terminal
Dim red {DIM_RED}Simply colored! Dim red text color in terminal
Dim magenta {DIM_MAGENTA}Simply colored! Dim magenta text color in terminal
Dim blue {DIM_BLUE}Simply colored! Dim blue text color in terminal
Dim cyan {DIM_CYAN}Simply colored! Dim cyan text color in terminal
Dim white {DIM_WHITE}Simply colored! Dim white text color in terminal
Dim black {DIM_BLACK}Simply colored! Dim black text color in terminal

Background

Color Type To get
Green {BG_GREEN}Simply colored! Green text color in terminal
Yellow {BG_YELLOW}Simply colored! Yellow text color in terminal
Red {BG_RED}Simply colored! Red text color in terminal
Magenta {BG_MAGENTA}Simply colored! Magenta text color in terminal
Blue {BG_BLUE}Simply colored! Blue text color in terminal
Cyan {BG_CYAN}Simply colored! Cyan text color in terminal
White {BG_WHITE}Simply colored! White text color in terminal
Black {BG_BLACK}Simply colored! Black text color in terminal
Dim green {BG_DIM_GREEN}Simply colored! Dim green text color in terminal
Dim yellow {BG_DIM_YELLOW}Simply colored! Dim yellow text color in terminal
Dim red {BG_DIM_RED}Simply colored! Dim red text color in terminal
Dim magenta {BG_DIM_MAGENTA}Simply colored! Dim magenta text color in terminal
Dim blue {BG_DIM_BLUE}Simply colored! Dim blue text color in terminal
Dim cyan {BG_DIM_CYAN}Simply colored! Dim cyan text color in terminal
Dim white {BG_DIM_WHITE}Simply colored! Dim white text color in terminal
Dim black {BG_DIM_BLACK}Simply colored! Dim black text color in terminal

Effects

Effect Type
Italic {ITALIC}Simply colored!
Bold {BOLD}Simply colored!
Underline {UNDERLINE}Simply colored!
Blink {BLINK}Simply colored!
Reverse {REVERSE}Simply colored!
Strikethrough {STRIKETHROUGH}Simply colored!
Dim {DIM}Simply colored!
Hide {HIDE}Simply colored!
Reset all styles {RESET}Simply colored!

All effects can be prefixed with NO_ to disable e.g. NO_BOLD.

Remove colors when they are not supported

You can use the anstream crate to remove colors when they aren’t supported:

use anstream::println;
use simply_colored::*;

println!("My number is {GREEN}10{RESET}!");
println!("My number is not {RED}4{RESET}!");

Extras

These can’t be neatly represented using constants, so they’re not a part of this library. They are included here for your own convenience!

Links

If you want links in the terminal, you can use this:

fn link(text: &str, link: &str) -> String {
    format!("\x1b]8;;{link}\x1b\\{text}\x1b]8;;\x1b\\")
}

Example usage:

let github = link("GitHub", "https://github.com/nik-rev/simply-colored");

println!(
    "Check out simply_colored on {github}!",
);

RGB Colors

If you want arbitrary RGB colors, you can use this:

fn rgb(r: u8, g: u8, b: u8) -> String {
    format!("\x1b[38;2;{r};{g};{b}m")
}

Example usage:

let color = rgb(0xc0, 0xff, 0xee);

println!("So very {color}ful{RESET}!");