#include <CGAL/IO/Color_ostream.h>
#include <iostream>
void demonstrate_basic_colors() {
std::cout << "=== Basic Color Usage ===\n\n";
{
std::cout << "1. Single color (Red):\n";
std::cout << "This text is red\n";
std::cout << "All lines in this scope are red\n";
}
std::cout << "Back to normal color\n\n";
{
std::cout << "2. Different colors:\n";
{
std::cout << "Green text\n";
}
{
std::cout << "Blue text\n";
}
{
std::cout << "Yellow text\n";
}
}
std::cout << "Back to normal\n\n";
{
std::cout << "3. Bright colors:\n";
{
std::cout << "Bright red text\n";
}
{
std::cout << "Bright green text\n";
}
{
std::cout << "Bright cyan text\n";
}
}
std::cout << "Back to normal\n\n";
}
void demonstrate_combined_colors() {
std::cout << "=== Combined Colors (Bold, Underline, etc.) ===\n\n";
{
std::cout << "1. Bold colors:\n";
std::cout << "This text is bold and red\n";
}
std::cout << "Back to normal\n\n";
{
std::cout << "2. Underlined colors:\n";
std::cout << "This text is underlined and blue\n";
}
std::cout << "Back to normal\n\n";
{
std::cout << "3. Multiple attributes:\n";
std::cout << "This text is bold, underlined, and green\n";
}
std::cout << "Back to normal\n\n";
}
void demonstrate_background_colors() {
std::cout << "=== Background Colors ===\n\n";
{
std::cout << "1. Red background:\n";
std::cout << "Text with red background\n";
}
std::cout << "Back to normal\n\n";
{
std::cout << "2. Foreground + background:\n";
std::cout << "Yellow text on blue background\n";
}
std::cout << "Back to normal\n\n";
{
std::cout << "3. Bold white on bright green background:\n";
std::cout << "Bold white on bright green\n";
}
std::cout << "Back to normal\n\n";
}
void demonstrate_nested_colors() {
std::cout << "=== Nested Color Scopes ===\n\n";
{
std::cout << "Outer scope: blue text\n";
{
std::cout << " Inner scope: red text\n";
{
std::cout << " Innermost scope: green text\n";
}
std::cout << " Back to inner scope: red text\n";
}
std::cout << "Back to outer scope: blue text\n";
}
std::cout << "Back to normal\n\n";
}
void simulate_colored_debug_output() {
std::cout << "=== Simulated Colored Debug Output ===\n\n";
std::cout << "Algorithm: Starting computation\n";
{
std::cout << "INFO: Initializing data structures\n";
}
{
std::cout << "SUCCESS: Data loaded successfully\n";
}
{
std::cout << "WARNING: Large dataset detected, may take longer\n";
}
{
std::cout << "Processing vertices...\n";
std::cout << "Processing edges...\n";
std::cout << "Processing faces...\n";
}
{
std::cout << "SUCCESS: Computation completed\n";
}
std::cout << "Algorithm finished\n\n";
}
void demonstrate_multiple_streams() {
std::cout << "=== Multiple Streams with make_color_guards ===\n\n";
std::cout << "Before coloring:\n";
std::cout << " cout: Normal output\n";
std::cerr << " cerr: Normal output\n";
{
std::cout << "\nWith make_color_guards (Red):\n";
auto guards = CGAL::IO::make_color_guards<CGAL::IO::Ansi_color::Red>(std::cout, std::cerr);
std::cout << "Red text on cout\n";
std::cerr << "Red text on cerr\n";
}
std::cout << "\nAfter color scope:\n";
std::cout << " cout: Normal output again\n";
std::cerr << " cerr: Normal output again\n";
{
std::cout << "\nBold green on multiple streams:\n";
auto guards =
CGAL::IO::make_color_guards<CGAL::IO::Ansi_color::Bold, CGAL::IO::Ansi_color::Green>(std::cout, std::clog);
std::cout << "cout: Bold green output\n";
std::clog << "clog: Bold green log message\n";
}
std::cout << "\n";
}
void test_color_detection() {
std::cout << "=== Color Detection Tests ===\n\n";
std::cout << "1. Testing automatic color detection in Basic_color_streambuf:\n";
{
std::cout << " Colors automatically detected as: " << (color_buf.colors_enabled() ? "ENABLED" : "DISABLED")
<< "\n";
auto* old_buf = std::cout.rdbuf(&color_buf);
std::cout << " (This text should be green only if colors were auto-detected)\n";
std::cout.rdbuf(old_buf);
}
std::cout << "\n";
std::cout << "2. Testing stdout_supports_color() function:\n";
std::cout << " stdout supports colors: YES\n";
{
std::cout << " (This text should be green if color is truly supported)\n";
}
} else {
std::cout << " stdout supports colors: NO\n";
std::cout << " (Colors are disabled - may be due to NO_COLOR env var,\n";
std::cout << " redirection to file, or unsupported terminal)\n";
}
std::cout << "\n";
std::cout << "3. Testing stderr_supports_color():\n";
std::cout << " stderr supports colors: YES\n";
{
std::cerr << " (This text should be yellow if color is truly supported)\n";
}
} else {
std::cout << " stderr supports colors: NO\n";
}
std::cout << "\n";
std::cout << "4. Testing stream_supports_color() for different streams:\n";
std::cout << "\n";
std::cout << "5. Automatic coloring example (no manual checking!):\n";
{
std::cout << " This message is automatically colored only if terminal supports it!\n";
std::cout << " No need to check stream_supports_color() manually - it's automatic!\n";
}
std::cout << "\n";
std::cout << "6. Smart colored output (automatically adapts to terminal capabilities):\n";
std::cout << " ";
{
std::cout << "INFO:";
}
std::cout << " Normal information message\n";
std::cout << " ";
{
std::cout << "SUCCESS:";
}
std::cout << " Operation completed successfully\n";
std::cout << " ";
{
std::cout << "WARNING:";
}
std::cout << " Potential issue detected\n";
std::cerr << " ";
{
std::cerr << "ERROR:";
}
std::cerr << " Critical error occurred\n";
std::cout << "\n";
std::cout << "\n";
std::cout << " Tip: To disable colors, set the NO_COLOR environment variable:\n";
std::cout << " export NO_COLOR=1\n";
std::cout << " Tip: When redirecting to a file, colors are automatically disabled:\n";
std::cout << " ./color_ostream > output.txt\n";
std::cout << "\n";
}
int main() {
test_color_detection();
demonstrate_basic_colors();
demonstrate_combined_colors();
demonstrate_background_colors();
demonstrate_nested_colors();
simulate_colored_debug_output();
demonstrate_multiple_streams();
return 0;
}
RAII helper class for temporarily installing a color streambuf on a stream.
Definition: Color_ostream.h:435
The class template Basic_color_streambuf wraps another basic_streambuf and automatically adds ANSI co...
Definition: Color_ostream.h:127
@ Cyan
Cyan foreground color.
@ Bold
Bold or increased intensity.
@ White
White foreground color.
@ BrightRed
Bright red foreground color.
@ Yellow
Yellow foreground color.
@ Underline
Underlined text.
@ BgBrightGreen
Bright green background color.
@ Blue
Blue foreground color.
@ BrightGreen
Bright green foreground color.
@ BgBlue
Blue background color.
@ BgRed
Red background color.
@ BrightCyan
Bright cyan foreground color.
@ Green
Green foreground color.
@ Red
Red foreground color.
bool stream_supports_color(const std::basic_ostream< CharT, Traits > &stream)
checks if a stream is attached to a terminal that supports colors.
Definition: Color_ostream.h:572