๐ LTC provides a convenient way to use Color in the Terminal using C/C++ & Rust
When you want to use it in your project it's really Easy just get the newest libtermcolor.h from the Releases Tab.
Then put this file in your src/ folder and include the Libary with
#include "libtermcolor.h"If you have it another location have to replace libtermcolor.h with your path to the .h file.
For Rust you have to add it the following in your Cargo.toml:
[dependencies]
libtermcolor = "<version>"or use the following command:
cargo add libtermcolorIf you just want an example just can look at for C/C++ example/main.cpp and example.rs for Rust.
colors::RED.regular
^ ^
| |
Color TYPEuse libtermcolor::colors; <-- Import the Libary
colors::red().regular
^ ^
| |
Color TYPEColor: This specifies what color you want to use as List can be found at #Colors or by typing colors:: and pressing CTRL + SPACE in your code.
Type: The type says if it should be:
- Bold
- Underline
- Background (This is for changing Background Color)
- Reset (This will reset the effect of before specified Colors)
Note
Reset can also be used with:
colors::RESETYou can also get The Colors by string with the following:
colors::getColorCode("RED", "REGULAR")
^ ^
| |
Color Typecolors::get_color_code("RED", "REGULAR");
^ ^
| |
Color TypeNote
The capitalization does not matter.
I have written a quick example that will output every Color + It's states:
#include <iostream>
#include "libtermcolor.h"
int main() {
for (const auto& pair : colors::color_map) {
std::cout << pair.second.background << pair.first << colors::RESET << ":\n";
std::cout << colors::BLACK.background << " Regular: " << colors::RESET << pair.second.regular << pair.first << pair.second.reset << std::endl;
std::cout << colors::BLACK.background << " Bold: " << colors::RESET << pair.second.bold << pair.first << pair.second.reset << std::endl;
std::cout << colors::BLACK.background << " Underline: " << colors::RESET << pair.second.underline << pair.first << pair.second.reset << std::endl;
std::cout << colors::BLACK.background << " Background: " << colors::RESET << pair.second.background << pair.first << pair.second.reset << std::endl;
}
return 0;
}This code can also be found at example/main.cpp.
use libtermcolor::colors;
fn main() {
for (color_name, color_attributes) in colors::COLOR_MAP.iter() {
println!("{}{}{}:", color_attributes.background, color_name, colors::reset());
println!(" Regular: {}{}{}{}", color_attributes.regular, color_name, color_attributes.reset, colors::reset());
println!(" Bold: {}{}{}{}", color_attributes.bold, color_name, color_attributes.reset, colors::reset());
println!(" Underline: {}{}{}{}", color_attributes.underline, color_name, color_attributes.reset, colors::reset());
println!(" Background: {}{}{}{}", color_attributes.background, color_name, color_attributes.reset, colors::reset());
}
}
This code can also be found at example/main.rs.
- BLACK
- BRIGHT_BLACK
- RED
- BRIGHT_RED
- GREEN
- BRIGHT_GREEN
- YELLOW
- BRIGHT_YELLOW
- BLUE
- BRIGHT_BLUE
- MAGENTA
- BRIGHT_MAGENTA
- CYAN
- BRIGHT_CYAN
- WHITE
- BRIGHT_WHITE
You can use the example to test your Code.
You can compile and test the example with the following command:
make clean
make build
./output/maincd example
cargo buildcargo build