Crate string_colorization

Source
Expand description

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

You are reading the documentation for string_colorization version 1.0.0

Abstracts colorizing string from the colored crate by giving a struct Colorizer combining foreground, background and stylizations to strings that can be applied later, and then uses them on the colorize function to allow you to colorize a string given a series substring and colorizers, for example, this code prints: 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"Rainbow");

If one of the rule’s substring is a reference to another string different from the input argument, then the rule will just not be applied, for example, the following code prints 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"Red, no red");

Find more information and examples in the function colorize and the struct Colorizer.

Modules§

background
Constants for creating background Colorizers
foreground
Constants for creating foreground Colorizers
style
Constants for creating stylized Colorizers

Structs§

Colorizer
Defines a foreground, background, and styles that can be appied on a string to format it.

Functions§

colorize
Colorizes every substring over a string and returns a String where every substring has been stylized according to these rules.