Support changing color mode at runtime

This commit is contained in:
Ken Matsui
2022-05-27 00:01:28 +09:00
parent e8f922a1b8
commit 9086b1114f
3 changed files with 82 additions and 2 deletions

View File

@@ -17,11 +17,36 @@ namespace color_ansi
{
namespace detail
{
inline int colorize_index()
{
static const int index = std::ios_base::xalloc();
return index;
}
// Control color mode globally
class color_mode {
public:
inline void enable() {
should_color_ = true;
}
inline void disable() {
should_color_ = false;
}
inline bool should_color() const {
return should_color_;
}
static color_mode& status() {
static color_mode status_;
return status_;
}
private:
bool should_color_ = false;
};
} // detail
inline std::ostream& colorize(std::ostream& os)
@@ -55,6 +80,18 @@ inline std::ostream& cyan (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[36m";} return os;}
inline std::ostream& white (std::ostream& os)
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[37m";} return os;}
inline void enable() {
return detail::color_mode::status().enable();
}
inline void disable() {
return detail::color_mode::status().disable();
}
inline bool should_color() {
return detail::color_mode::status().should_color();
}
} // color_ansi
// ANSI escape sequence is the only and default colorization method currently