string_colorization

Crates.iostring_colorization
lib.rsstring_colorization
version1.0.0
sourcesrc
created_at2024-06-16 14:45:10.860114
updated_at2024-06-16 14:45:10.860114
descriptionAbstracts colorizing strings through a struct to then mass-apply multiple colorizations within the same string, created on top of the colored crate
homepage
repositoryhttps://github.com/JorgeRicoVivas/string_colorization
max_upload_size
id1273578
size46,220
Jorge Rico Vivas (JorgeRicoVivas)

documentation

README

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].

Commit count: 12

cargo fmt