terminal_paint/
lib.rs

1//! # Terminal paint
2//! 
3//! `terminal-paint` is a library that simplifies printing colorful text to your console.
4//! It is based on ANSI Escape Code. Should work on Linux, macOS and Windows (from Windows 10 1511+) 
5
6// pub const RESET: u8 = 0;
7pub const BOLD: ColorInstruction = ColorInstruction::One(1);
8pub const INVERTED: ColorInstruction = ColorInstruction::One(7);
9pub const BLACK: ColorInstruction = ColorInstruction::One(30);
10pub const RED: ColorInstruction = ColorInstruction::One(31);
11pub const GREEN: ColorInstruction = ColorInstruction::One(32);
12pub const YELLOW: ColorInstruction = ColorInstruction::One(33);
13pub const BLUE: ColorInstruction = ColorInstruction::One(34);
14pub const PURPLE: ColorInstruction = ColorInstruction::One(35);
15pub const CYAN: ColorInstruction = ColorInstruction::One(36);
16pub const WHITE: ColorInstruction = ColorInstruction::One(37);
17pub const GRAY: ColorInstruction = ColorInstruction::One(90);
18pub const ON_BLACK: ColorInstruction = ColorInstruction::One(40);
19pub const ON_RED: ColorInstruction = ColorInstruction::One(41);
20pub const ON_GREEN: ColorInstruction = ColorInstruction::One(42);
21pub const ON_YELLOW: ColorInstruction = ColorInstruction::One(43);
22pub const ON_BLUE: ColorInstruction = ColorInstruction::One(44);
23pub const ON_PURPLE: ColorInstruction = ColorInstruction::One(45);
24pub const ON_CYAN: ColorInstruction = ColorInstruction::One(46);
25pub const ON_WHITE: ColorInstruction = ColorInstruction::One(47);
26pub const BLACK_ON_RED: ColorInstruction = ColorInstruction::Two(30, 41);
27pub const BLACK_ON_GREEN: ColorInstruction = ColorInstruction::Two(30, 42);
28pub const BLACK_ON_YELLOW: ColorInstruction = ColorInstruction::Two(30, 43);
29pub const BLACK_ON_BLUE: ColorInstruction = ColorInstruction::Two(30, 44);
30pub const BLACK_ON_PURPLE: ColorInstruction = ColorInstruction::Two(30, 45);
31pub const BLACK_ON_CYAN: ColorInstruction = ColorInstruction::Two(30, 46);
32pub const BLACK_ON_WHITE: ColorInstruction = ColorInstruction::Two(30, 47);
33pub const RED_ON_BLACK: ColorInstruction = ColorInstruction::Two(31, 40);
34pub const RED_ON_GREEN: ColorInstruction = ColorInstruction::Two(31, 42);
35pub const RED_ON_YELLOW: ColorInstruction = ColorInstruction::Two(31, 43);
36
37/// `ColorInstruction` is a helper `enum`,
38/// that helps to represent numbers in ANSI Escape Code.
39/// `ColorInstruction::One` represents either background or font color;
40/// `ColorInstruction::Two` represents combination of both; 
41pub enum ColorInstruction {
42    One(u8),
43    Two(u8, u8)
44}
45
46/// Returns `String` with the text of specified color
47/// 
48/// # Examples
49/// 
50/// ```
51/// use terminal_paint as tp;
52/// 
53/// let my_str = tp::paint("hello world!", tp::YELLOW);
54/// let my_str2 = tp::paint("world hello", tp::ON_RED);
55/// println!("{}, {}", my_str, my_str2);
56/// ```
57pub fn paint(text: impl Into<String>, color: ColorInstruction) -> String {
58    match color {
59        ColorInstruction::One(font_color) => format!("\x1b[{}m{}\x1b[0m", font_color, text.into()),
60        ColorInstruction::Two(font_color, background_color) => format!("\x1b[{};{}m{}\x1b[0m", font_color, background_color, text.into()),
61    }
62}
63
64/// calls `print!` but with specified color
65pub fn color_print(text: impl Into<String>, color: ColorInstruction) {
66    print!("{}", paint(text, color))
67}
68
69/// calls `println!` but with specified color
70pub fn color_println(text: impl Into<String>, color: ColorInstruction) {
71    println!("{}", paint(text, color))
72}