#terminal-colors #builder #terminal #cli #print

terminal_color_builder

Printing colorful terminal outputs using a builder pattern

1 unstable release

0.1.1 Nov 21, 2020

#736 in Command-line interface

Download history 9/week @ 2025-01-01 41/week @ 2025-01-08 93/week @ 2025-01-15 109/week @ 2025-01-22 52/week @ 2025-01-29 41/week @ 2025-02-05 64/week @ 2025-02-12 69/week @ 2025-02-19 16/week @ 2025-02-26 20/week @ 2025-03-05 31/week @ 2025-03-12 8/week @ 2025-03-19 14/week @ 2025-03-26 32/week @ 2025-04-09

57 downloads per month
Used in 11 crates (6 directly)

MIT license

20KB
318 lines

Terminal Color Builder for Rust

Printing colorful terminal outputs using a builder pattern.

This library is useful for whenever you need to print colorfol something to the terminal.

For example if you need to print a warning into the terminal with red background and white font-color, you could use something like this:

use terminal_color_builder::OutputFormatter as tcb;

println!(
    "{}",
    tcb::new()
    .fg() // jump to foreground scope
    .hex("#fff") // apply css-hex-color value #fff (white) as foreground color
    .bg() // jump to background scope
    .red() // apply red as background color
    .text_str("A text in white with a red background.") // print text
    .print() // render to string
);

Result:
img.png

This is chainable for as long as necessary. Building rainbox-esque outputs through this is absolutely possible.

use terminal_color_builder::OutputFormatter as tcb;

/// Building a rainbow-colored text
println!(
    "{}",
    tcb::new()
    .fg().hex("#cc33ff").text_str("R") // violet
    .fg().hex("#6633ff").text_str("A") // indigo
    .fg().blue().text_str("I")
    .fg().green().text_str("N")
    .fg().yellow().text_str("B")
    .fg().hex("#ff6633").text_str("O") // orange
    .fg().red().text_str("W")
    .print() // render to string
);

This prints the following into the CLI:
img.png

No runtime deps