coloured_strings/lib.rs
1/// This library takes in a str and returns a coloured String.
2
3
4/// this function returns the String with the colour.
5fn colourise(text: &str, colour: i8) -> String {
6 return "\x1b[".to_owned() + &colour.to_string() + "m" + text + "\x1b[0m";
7}
8
9
10/// This function takes the &str, then calls the "colourise" function to convert it into a coloured string.
11///
12/// ___
13///
14/// Examples
15/// ```rust
16/// colour("this is a black string", "black");
17/// colour("this is a underlined string", "u");
18/// ```
19/// ___
20/// Available Colours
21/// ```rust
22/// // normal colours
23/// "black" | "b"
24/// "red" | "r"
25/// "green" | "g"
26/// "yellow" | "y"
27/// "blue" | "bl"
28/// "magenta" | "m"
29/// "cyan" | "c"
30/// "white"| "w"
31///
32/// // bright colours
33/// "brightblack" | "bb"
34/// "brightred" | "br"
35/// "brightgreen" | "bg"
36/// "brightyellow" | "by"
37/// "brightblue" | "bbl"
38/// "brightmagenta" | "bm"
39/// "brightcyan" | "bc"
40/// "brightwhite" | "bw"
41///
42/// //background colours
43/// "backgroundred" | "b-r"
44/// "backgroundgreen" | "b-g"
45/// "backgroundyellow" | "b-y"
46/// "backgroundblue" | "b-b"
47/// "backgroundmagenta" | "b-m"
48/// "backgroundcyan" | "b-c"
49/// "bacgroundwhite" | "b-w"
50///
51/// //bright background colours
52/// "backgroundbrightblack" | "b-bb"
53/// "backgroundbrightred" | "b-br"
54/// "backgroundbrightgreen" | "b-bg"
55/// "backgroundbrightyellow" | "b-by"
56/// "backgroundbrightblue" | "b-bbl"
57/// "backgroundbrightmagenta" | "b-bm"
58/// "backgroundbrightcyan" | "b-bc"
59/// "backgroundbrightwhite" | "b-bw"
60///
61/// // other styles
62/// "clear" | "cl"
63/// "dimmed" | "d"
64/// "italic" | "i"
65/// "underline" | "u"
66/// "blink" | "bli"
67/// "reversed" | "re"
68/// "hidden" | "h"
69/// "strikethrough" | "s"
70/// ```
71///
72pub fn colour(text: &str, colour: &str) -> String {
73 match colour {
74 //normal colours
75 "black" | "b" => return colourise(text, 30),
76 "red" | "r" => return colourise(text, 31),
77 "green" | "g" => return colourise(text, 32),
78 "yellow" | "y" => return colourise(text, 33),
79 "blue" | "bl" => return colourise(text, 34),
80 "magenta" | "m" => return colourise(text, 35),
81 "cyan" | "c" => return colourise(text, 36),
82 "white"| "w" => return colourise(text, 37),
83
84 // bright colours
85 "brightblack" | "bb" => return colourise(text, 90),
86 "brightred" | "br" => return colourise(text, 91),
87 "brightgreen" | "bg" => return colourise(text, 92),
88 "brightyellow" | "by" => return colourise(text, 93),
89 "brightblue" | "bbl"=> return colourise(text, 94),
90 "brightmagenta" | "bm" => return colourise(text, 95),
91 "brightcyan" | "bc" => return colourise(text, 96),
92 "brightwhite" | "bw" => return colourise(text, 97),
93
94 //background colours
95 "backgroundred" | "b-r" => return colourise(text, 41),
96 "backgroundgreen" | "b-g" => return colourise(text, 42),
97 "backgroundyellow" | "b-y" => return colourise(text, 43),
98 "backgroundblue" | "b-b" => return colourise(text, 44),
99 "backgroundmagenta" | "b-m" => return colourise(text, 45),
100 "backgroundcyan" | "b-c" => return colourise(text, 46),
101 "bacgroundwhite" | "b-w" => return colourise(text, 47),
102
103 //bright background colours
104 "backgroundbrightblack" | "b-bb" => return colourise(text, 100),
105 "backgroundbrightred" | "b-br" => return colourise(text, 101),
106 "backgroundbrightgreen" | "b-bg" => return colourise(text, 102),
107 "backgroundbrightyellow" | "b-by" => return colourise(text, 103),
108 "backgroundbrightblue" | "b-bbl" => return colourise(text, 104),
109 "backgroundbrightmagenta" | "b-bm" => return colourise(text, 105),
110 "backgroundbrightcyan" | "b-bc" => return colourise(text, 106),
111 "backgroundbrightwhite" | "b-bw" => return colourise(text, 107),
112
113 // other styles
114 "clear" | "cl" => return colourise(text, 1),
115 "dimmed" | "d" => return colourise(text, 2),
116 "italic" | "i" => return colourise(text, 3),
117 "underline" | "u" => return colourise(text, 4),
118 "blink" | "bli" => return colourise(text, 5),
119 "reversed" | "re" => return colourise(text, 7),
120 "hidden" | "h" => return colourise(text, 8),
121 "strikethrough" | "s" => return colourise(text, 9),
122 // edge case
123 _ => return colourise("no colour selected!", 93)
124
125
126 }
127}
128
129/// This function takes a &str, which acts as the text; a boolean value to determine if the colour should be applied to the foreground or background; and red, blue and green values to determine colour.
130///
131/// ---
132/// Examples
133///
134/// ```rust
135/// custom_colour("foreground", true, 200, 240, 100);
136/// custom_colour("background", false, 200, 100, 200);
137/// ```
138///
139/// ---
140pub fn custom_colour(text: &str, foreground: bool, red: u8, green: u8, blue: u8) -> String {
141 // turn red, blue and green into strs to prepare for formatting
142 let r = red.to_string(); let g = green.to_string(); let b = blue.to_string();
143 // match statement to determine if foreground or background is being called
144 match foreground {
145 // foreground
146 true => return "\x1b[".to_owned() + &format!("38;2;{};{};{}", r, g, b).to_string() + "m" + text + "\x1b[0m",
147 // background
148 false => return "\x1b[".to_owned() + &format!("48;2;{};{};{}", r, g, b).to_string() + "m" + text + "\x1b[0m",
149
150 }
151}