Crate colored_str

Source
Expand description

Coloring terminal by parsing string content

This crate is an extension to the colored crate that enables terminal coloring. It provides a trait and a macro to parse a given string that incorporates style flags.

§Usage

This crate is on crates.io and can be used by adding colored-str to your dependencies in your project’s Cargo.toml.

[dependencies]
colored-str = "0.1.8"

§How to use

Styles must be written within <...> opening flag and </> closing flag.

Style variations must be written within <+...> opening flag and <-> closing flag.

See below examples.

§Limitations

Blocks cannot be overlapped
Such code <red> ... <blue> ... </> ... </> will not work properly.
This is true as well for variations : <red><+blue> ... <+bold> ... <-><-></> will not work properly.

A style cannot be removed
With <red+bold> ... here I want to keep red only => impossible </>.
The workaround is as follows: <red> <+bold> ... <-> here I have red only </>

§Examples

use colored_str::coloredln;
 
coloredln!("<red>this is red</>");
coloredln!("<#FF0000>this is also red</>");
coloredln!("<blue+red>this is red again</>");
coloredln!("<red+on_blue>this is red on blue</>");
coloredln!("<red+on_#0000FF>this is also red on blue</>");
coloredln!("<bold>this is bold</>");
coloredln!("<red>there is a first line\nthen a second</>");

You can add variables as per println!

use colored_str::coloredln;
 
let message = "this is red";
coloredln!("<red>{message}</>");
coloredln!("<red>{}</>", message);

You can add styles adjustments in a block

use colored_str::coloredln;
 
coloredln!("<red>this is red <+bold>this is red and bold<-> then red again </>");
coloredln!("<red>this is red <+bold+on_blue>this is red on blue and bold<-> then red again </>");

You can also use it as a trait

use colored_str::Colored;
let s: String = "<red>this is red</>".colored().to_string();
println!("{}", s);

§List of styles

§Colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • purple
  • cyan
  • white

All can be used as backgound using on_ prefix.

§Light/Bright Colors

  • lblack
  • lred
  • lgreen
  • lyellow
  • lblue
  • lmagenta
  • lpurple
  • lcyan
  • lwhite

All can be used as backgound using on_ prefix.

§Decorations

  • bold
  • underline
  • italic
  • dimmed
  • reverse
  • reversed
  • blink
  • hidden
  • strikethrough

§True colors

  • #RRGGBB
  • on_#RRGGBB

Macros§

cformat
Creates a new String by parsing given text.
colored
Print colored text to standard output.
coloredln
Print colored text to standard output with newline at the end.

Traits§

Colored
The trait that enables a string to be colorized

Functions§

colored
Creates a new ColoredString by parsing given text.