Skip to content
RishabhDeep Singh edited this page Jul 24, 2020 · 1 revision

Use the below template to add more colors to the output.

namespace Color {
enum Code {
  FG_RED = 31,
  FG_GREEN = 32,
  FG_YELLOW = 33,
  FG_BLUE = 34,
  FG_ORANGE = 208,
  FG_DEFAULT = 39,
  BG_RED = 41,
  BG_GREEN = 42,
  BG_BLUE = 44,
  BG_DEFAULT = 49
};
class Modifier {
  Code code;
public:
  Modifier(Code pCode) : code(pCode) {}
  friend std::ostream&
  operator<<(std::ostream& os, const Modifier& mod) {
    return os << "\033[" << mod.code << "m";
  }
};
}
Color::Modifier red(Color::FG_RED);
Color::Modifier green(Color::FG_GREEN);
Color::Modifier yellow(Color::FG_YELLOW);
Color::Modifier blue(Color::FG_BLUE);
Color::Modifier orange(Color::FG_ORANGE);
Color::Modifier def(Color::FG_DEFAULT);

The following code will return "Expected output:" in blue and output in default color. and don't forget to set the color back to default

std::cout << blue << "Expected output: \n" << def << test.output << std::endl;