#string #color-string #color #term-painter

no-std string_colorization

通过结构体将字符串着色抽象化,以便在同一个字符串中应用多个着色,基于 colored crate 创建

1 个稳定版本

1.0.0 2024年6月16日

#123值格式化

自定义许可

37KB
238

crates.io GitHub Actions Workflow Status docs.rs GitHub License

您正在阅读 string_colorization 版本 1.0.0 的文档

通过提供一个名为 [Colorizer] 的结构体从 [colored] crate 抽象化字符串着色,该结构体将前景、背景和样式组合到字符串中,可以在以后应用,然后使用它们在 [colorize] 函数上,允许您使用一系列子字符串和着色器来着色字符串,例如,以下代码打印: R a i n b o w '

colored::control::set_override(true); // Forces colorization,
                                      // this won't be necessary in your code.
use string_colorization::{background, foreground};

let rainbow = "Rainbow";
let default_colorizer = foreground::White+background::true_color(200,200,200);
let colored_rainbow = string_colorization::colorize(&rainbow, Some(default_colorizer), [
    (&rainbow[0..6], foreground::Red), // Turns 'Rainbo' into red letter, but since the rules
                                       // below override 'ainbo', only the 'R' results in
                                       // turning red.
    (&rainbow[1..6], foreground::true_color(255,160,0)), //Turns 'ainbo' into orange letters.
    (&rainbow[2..6], foreground::Yellow), // Turns 'inbo' into yellow.
    (&rainbow[3..6], foreground::Green),  // Turns 'nbo' into green.
    (&rainbow[4..6], foreground::Blue),   // Turns 'bo' into blue.
    (&rainbow[5..6], foreground::Magenta),// Turns 'o' into purple.
]);                                       // The letter 'n' wasn't reached by any of the other
                                          // patterns, meaning the 'general_colorization'
                                          // parameter will set its color, in this case, a white
                                          // lettering, if not indicated, it wouldn't colorize
                                          // the letter 'n', leaving it as plain.
println!("{colored_rainbow}");  //Prints Rainbow with colors
assert_eq!(colored_rainbow, r"[31m[48;2;200;200;200mR[0m[31m[0m[38;2;255;160;0m[48;2;200;200;200ma[0m[38;2;255;160;0m[0m[33m[48;2;200;200;200mi[0m[33m[0m[32m[48;2;200;200;200mn[0m[32m[0m[34m[48;2;200;200;200mb[0m[34m[0m[35m[48;2;200;200;200mo[0m[35m[0m[37m[48;2;200;200;200mw[0m[37m[0m");

如果规则中的一个子字符串是另一个不同于 input 参数的字符串的引用,则该规则将不会应用,例如,以下代码打印 'Red, no red'

colored::control::set_override(true); // Forces colorization,
                                      // this won't be necessary in your code.
use string_colorization::foreground;

let string_to_colorize = "Red, no red";
let another_string = "Another string";
let colorized_string = string_colorization::colorize(&string_to_colorize, None, [
    (&string_to_colorize[0..3], foreground::Red), // This will turn 'Red' into red lettering
    (&another_string[5..], foreground::Green),    // This is a substring to a different string
]);                                               // from the input one (string_to_colorize),
                                                  // meaning no changes will be applied, and
                                                  // therefore, no text will turn green.

println!("{colorized_string}"); //Prints 'Red' in red coloring and 'no red' without color.
assert_eq!(colorized_string, r"[31mRed[0m, no red");

有关更多信息和方法,请参阅 [colorize] 函数和 [Colorizer] 结构体。

依赖关系

~0–9.5MB
~41K SLoC