Skip to content

Lukas22092/ColoredOutputBuffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

start

It works by overriding std::streambuf to intercept what you print to std::cout and decorate it with ANSI escape codes for colored text

Features

  • **Rainbow **: Automatically cycles through colors for each space-separated word
  • Specific Colors: Set output to any supported color by name

Installation

Simply include the header file in your C++ project:

#include "ColoredBuffer.hpp"  
// Create ColoredBuffer instance and wrapper
ColoredBuffer rb(std::cout.rdbuf());
wrapper guard(rb);  
// Now all std::cout output will be colored!

Available Methods

// Enable rainbow cycling (changes color on each space)
rb.rainbow();

// Set a specific color by name
rb.set_color("blue");
rb.set_color("green");

// Disable coloring (resets to default)
rb.disable();

// Show available colors
rb.check_avaiable_colors();

Example Program

#include <iostream>
#include "ColoredBuffer.hpp"

int main() {
    ColoredBuffer rb(std::cout.rdbuf());
    wrapper guard(rb);  

    rb.rainbow();
    std::cout << "This text cycles through rainbow colors word by word!\n";
    rb.disable();

    rb.set_color("yellow");
    std::cout << "Now printing in yellow.\n";

    rb.set_color("cyan");
    std::cout << "And now in cyan.\n";

    rb.set_color("darkgreen"); // not supported -> shows available colors
}

Output

Example Output

Supported Colors

  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan

You can easily add more colors by extending the colors vector with new Color entries by just adding a name aswell its corresponding ANSI Color Code

how to improve

instead of using a vector to store the Colors, consider using a Enum. trying to access colors that are not present will now be a compile time error.

what i learned

  • basic understanding on how iostream/streams really work
  • why RAII wrappers are important
  • using error codes can be more useful than writing methods - like i did in check_avaiable_colors

About

print colored text to your terminal output

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages