Crate fancy

source · []
Expand description

Easily print colored output using ANSI sequences.

This crate makes it easy to print colored text to the terminal. There is no need to use anything then just the format macros themselfs.

Color formatting can be done using the colorize! macro. It takes a color format strig and expands to the original text, with the color modes replaced by an ANSI sequence. Just like for format! there is a shortcut macro printcol! which prints the colored text directly to the console.

use fancy::printcoln;
printcoln!("[bold|cyan]Hello world[magenta]!");

All the color formating macros, do normal formatting too.

use fancy::printcoln;
printcoln!("[bold]{:x} {:x} [red]world!", 104, 105);

Color modes

There are many different color modes (or color format arguments). Color modes can be applied using square-brackets [ ]. Multiple color modes must be seperated a pipe |. The colon : is used to reset all modes. It can just be prependet to any mode. (Eg. [bold]Hello[:italic]world!) All color modes are reset at the end of the formatted string.

[bold|underline|blue] prints any text following it in bold, underlined and colored blue.

[green] prints any text following it colored green

List of modes

This is a list of all available color modes.

Styles

NameShortDescriptionMarkdown
boldbWrite text bold.text
italiciWrite text in italics.text
underlineuWrite text underlined.
inverse!Inverse of the default colors.
strikethroughsWrite text strikethrough.text
:Reset all modes.

Colors

Name
black
red
green
yellow
blue
magenta
cyan
white
default / def

Background color

Prepent any color with a question mark to use it as a background color.

Name
?black
?red
?green
?yellow
?blue
?magenta
?cyan
?white
?default / ?def

Custom colors

This crate supports “ANSI color codes” as well as raw RGB codes (Although not every terminal supports truecolor). You can prepend any of these color codes with a question mark ? to set the background color.

69 - sets the foreground to an ansi code ?187 - sets the background to an ansi code #ababd2 - sets the foreground to a hex rgb value ?#ababd2 - sets the background to a hex rgb value

Ansi color code: [bold|214]Hello world! This is a light orange. Hex color code: [#babaf1|u]Hi there! This is a light blue/purple-ish color.

Escaping

Escaping is done like with normal formatting braces.

use fancy::*;
eprintcoln!("[b|r]error[:] at [[{}:{}]][b]: {}", line, column, message);

Examples

 
use fancy::*;
 
printcoln!("Hello {}", "world!"); // plain "Hello world!"
printcoln!("[bold]Hello world"); // bold "Hello world!"
printcoln!("[b|u]Hello world"); // bold and underline "Hello world!"
printcoln!("[b|#7cd615]space[#c1973c]space!"); // lime/orange "spacespace"
printcoln!("H[!]E[:]L[!]L[:]L[!]O[:]"); // black/white "HELLO"

printcoln!("[b|u|#babaf1]rust[:] is [!]{}", "cool");
 
let error = "ANSI codes are not supportet for this terminal.";
eprintcoln!("[b|red]ERROR[:b]: [:]{}", error);
 

Comparison to other crates

This crate uses a different approach then most other color formatting crates. The inline color formatting syntax is not as flexible as some other approaches but in my opinion it is a bit easier to write and read.

Since this crate uses a procedural macro for generating the color sequences, coloring shouldn’t impact the runtime performance.

Macros

Colorize and format a string.

Print a colorized string to stderr. For more information see printcol!;

Print a colorized string to stderr followed by a newline. This macro is to eprintcol! as eprintln! is to eprint!. For more information see printcol!;

Print a colorized string.

Print a colorized string followed by a newline. This macro is to printcol! as println is to print!. For more information see printcol!;