pub fn ansi_text(bg: Rgb, s: &str) -> String
Expand description
Changes the background color of a string using three ANSI escape codes if the terminal support truecolors.
If the environment variable COLORTERM
is not defined or contains neither truecolor
nor 24bit
, the string parameter
is returned as is.
This function also changes the foreground color according to the specified background color in order to ensure that the text is visible.
§Examples
use chocodye::{Rgb, ansi_text};
use std::env;
env::remove_var("COLORTERM");
assert_eq!(ansi_text(Rgb::RED, "hello world!"), "hello world!");
env::set_var("COLORTERM", "truecolor");
assert_eq!(ansi_text(Rgb::RED, "hello world!"), "\x1B[48;2;255;0;0m\x1B[38;2;255;255;255mhello world!\x1B[0m");
// ^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^
// background foreground text