mccolors_rust/lib.rs
1fn map_minecraft_color_to_ansi(color_code: char) -> &'static str {
2 match color_code {
3 '0' => "\x1b[30m", // Black
4 '1' => "\x1b[34m", // Blue
5 '2' => "\x1b[32m", // Green
6 '3' => "\x1b[36m", // Cyan
7 '4' => "\x1b[31m", // Red
8 '5' => "\x1b[35m", // Magenta
9 '6' => "\x1b[33m", // Yellow
10 '7' => "\x1b[38;5;250m", // White
11 '8' => "\x1b[90m", // Dark Gray
12 '9' => "\x1b[94m", // Light Blue
13 'a' => "\x1b[92m", // Light Green
14 'b' => "\x1b[96m", // Light Cyan
15 'c' => "\x1b[91m", // Light Red
16 'd' => "\x1b[35;1m", // Bold Magenta
17 'e' => "\x1b[93m", // Light Yellow
18 'f' => "\x1b[37m", // White
19 'l' => "\x1b[1m", // Bold
20 'm' => "\x1b[9m", // Strike-through
21 'n' => "\x1b[4m", // Underline
22 'o' => "\x1b[3m", // Italic
23 'r' => "\x1b[0m", // Reset to default
24 _ => "", // If it doesn't match any, do nothing
25 }
26}
27
28pub fn mcreplace(input: &str) -> String {
29 // Create a new mutable string to store the result
30 let mut output = String::new();
31
32 // Create an iterator over the characters of the input with peekable to be able to look at the next character without advancing the iterator
33 let mut chars = input.chars().peekable();
34
35 // Iterate over the characters of the input
36 while let Some(c) = chars.next() {
37 // Check if the current character is '&'
38 if c == '&' {
39 // Try to get the next character from the iterator
40 if let Some(color_code) = chars.next() {
41 // Call the map_minecraft_color_to_ansi function to get the corresponding ANSI sequence for the Minecraft color code
42 let ansi_sequence = map_minecraft_color_to_ansi(color_code);
43
44 // Add the ANSI sequence to the result
45 output.push_str(ansi_sequence);
46 }
47 } else {
48 // If the character is not '&', simply add it to the result
49 output.push(c);
50 }
51 }
52
53 // Reset ANSI formatting before returning the resulting string
54 output.push_str("\x1b[0m");
55
56 // Return the resulting string with Minecraft color codes replaced by ANSI codes
57 output
58}
59
60pub fn mcwrite(input: &str) {
61 // Create an iterator over the characters of the input with peekable to be able to look at the next character without advancing the iterator
62 let mut chars = input.chars().peekable();
63
64 // Iterate over the characters of the input
65 while let Some(c) = chars.next() {
66 // Check if the current character is '&'
67 if c == '&' {
68 // Try to get the next character from the iterator
69 if let Some(color_code) = chars.next() {
70 // Call the map_minecraft_color_to_ansi function to get the corresponding ANSI sequence for the Minecraft color code
71 let ansi_sequence = map_minecraft_color_to_ansi(color_code);
72
73 // Print the ANSI sequence directly to the console
74 print!("{}", ansi_sequence);
75 }
76 } else {
77 // If the character is not '&', simply print it to the console
78 print!("{}", c);
79 }
80 }
81
82 // Reset ANSI formatting before finishing
83 print!("\x1b[0m");
84}
85
86pub fn mcremove(input: &str) -> String {
87 let mut output = String::new();
88 let mut iter = input.chars().peekable();
89
90 while let Some(c) = iter.next() {
91 if c == '&' {
92 // Skip the color code character
93 iter.next();
94 } else {
95 output.push(c);
96 }
97 }
98
99 output
100}