Search code examples
c++11outputheader-files

←[0m←[42m←[37mIf in the console output


This is the source.cpp

#include "color.hpp"
#include<conio.h>
int main()
{
    std::cout << std::endl
              << color::style::reset << color::bg::green << color::fg::gray
              << "If you're seeing green bg, then color works!"
              << color::style::reset << std::endl;
    _getch();
    return 0;
}

And this is the code snippet from color.hpp :

template <typename T>
using enable = typename std::enable_if
    <
        std::is_same<T, color::style>::value ||
        std::is_same<T, color::fg>::value ||
        std::is_same<T, color::bg>::value ||
        std::is_same<T, color::fgB>::value ||
        std::is_same<T, color::bgB>::value,
        std::ostream &
    >::type;

template <typename T>
inline enable<T> operator<<(std::ostream &os, T const value)
{
    std::streambuf const *osbuf = os.rdbuf();
    return ((supportsColor()) && (isTerminal(osbuf)))
               ? os << "\033[" << static_cast<int>(value) << "m"
               : os;
}
}

Actually this is a header only library for making colorful console. I tried to compile this project as a console application with C++11 support but the output is unexpected. What do the output suggest?


Solution

  • The output suggests that color.hpp depends on a terminal which is interpreting special "escape" output code, but your terminal does not interpret these codes.

    This isn't standard C++, BTW.