| 
318
 | 
     1 #ifndef TOML11_COLOR_HPP
 | 
| 
 | 
     2 #define TOML11_COLOR_HPP
 | 
| 
 | 
     3 #include <cstdint>
 | 
| 
 | 
     4 #include <ostream>
 | 
| 
 | 
     5 
 | 
| 
 | 
     6 #ifdef TOML11_COLORIZE_ERROR_MESSAGE
 | 
| 
 | 
     7 #define TOML11_ERROR_MESSAGE_COLORIZED true
 | 
| 
 | 
     8 #else
 | 
| 
 | 
     9 #define TOML11_ERROR_MESSAGE_COLORIZED false
 | 
| 
 | 
    10 #endif
 | 
| 
 | 
    11 
 | 
| 
 | 
    12 namespace toml
 | 
| 
 | 
    13 {
 | 
| 
 | 
    14 
 | 
| 
 | 
    15 // put ANSI escape sequence to ostream
 | 
| 
 | 
    16 namespace color_ansi
 | 
| 
 | 
    17 {
 | 
| 
 | 
    18 namespace detail
 | 
| 
 | 
    19 {
 | 
| 
 | 
    20 
 | 
| 
 | 
    21 inline int colorize_index()
 | 
| 
 | 
    22 {
 | 
| 
 | 
    23     static const int index = std::ios_base::xalloc();
 | 
| 
 | 
    24     return index;
 | 
| 
 | 
    25 }
 | 
| 
 | 
    26 
 | 
| 
 | 
    27 // Control color mode globally
 | 
| 
 | 
    28 class color_mode
 | 
| 
 | 
    29 {
 | 
| 
 | 
    30   public:
 | 
| 
 | 
    31     inline void enable()
 | 
| 
 | 
    32     {
 | 
| 
 | 
    33         should_color_ = true;
 | 
| 
 | 
    34     }
 | 
| 
 | 
    35     inline void disable()
 | 
| 
 | 
    36     {
 | 
| 
 | 
    37         should_color_ = false;
 | 
| 
 | 
    38     }
 | 
| 
 | 
    39 
 | 
| 
 | 
    40     inline bool should_color() const
 | 
| 
 | 
    41     {
 | 
| 
 | 
    42         return should_color_;
 | 
| 
 | 
    43     }
 | 
| 
 | 
    44 
 | 
| 
 | 
    45     static color_mode& status()
 | 
| 
 | 
    46     {
 | 
| 
 | 
    47         static color_mode status_;
 | 
| 
 | 
    48         return status_;
 | 
| 
 | 
    49     }
 | 
| 
 | 
    50 
 | 
| 
 | 
    51   private:
 | 
| 
 | 
    52     bool should_color_ = false;
 | 
| 
 | 
    53 };
 | 
| 
 | 
    54 
 | 
| 
 | 
    55 } // detail
 | 
| 
 | 
    56 
 | 
| 
 | 
    57 inline std::ostream& colorize(std::ostream& os)
 | 
| 
 | 
    58 {
 | 
| 
 | 
    59     // by default, it is zero.
 | 
| 
 | 
    60     os.iword(detail::colorize_index()) = 1;
 | 
| 
 | 
    61     return os;
 | 
| 
 | 
    62 }
 | 
| 
 | 
    63 inline std::ostream& nocolorize(std::ostream& os)
 | 
| 
 | 
    64 {
 | 
| 
 | 
    65     os.iword(detail::colorize_index()) = 0;
 | 
| 
 | 
    66     return os;
 | 
| 
 | 
    67 }
 | 
| 
 | 
    68 inline std::ostream& reset  (std::ostream& os)
 | 
| 
 | 
    69 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[00m";} return os;}
 | 
| 
 | 
    70 inline std::ostream& bold   (std::ostream& os)
 | 
| 
 | 
    71 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[01m";} return os;}
 | 
| 
 | 
    72 inline std::ostream& grey   (std::ostream& os)
 | 
| 
 | 
    73 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[30m";} return os;}
 | 
| 
 | 
    74 inline std::ostream& red    (std::ostream& os)
 | 
| 
 | 
    75 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[31m";} return os;}
 | 
| 
 | 
    76 inline std::ostream& green  (std::ostream& os)
 | 
| 
 | 
    77 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[32m";} return os;}
 | 
| 
 | 
    78 inline std::ostream& yellow (std::ostream& os)
 | 
| 
 | 
    79 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[33m";} return os;}
 | 
| 
 | 
    80 inline std::ostream& blue   (std::ostream& os)
 | 
| 
 | 
    81 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[34m";} return os;}
 | 
| 
 | 
    82 inline std::ostream& magenta(std::ostream& os)
 | 
| 
 | 
    83 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[35m";} return os;}
 | 
| 
 | 
    84 inline std::ostream& cyan   (std::ostream& os)
 | 
| 
 | 
    85 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[36m";} return os;}
 | 
| 
 | 
    86 inline std::ostream& white  (std::ostream& os)
 | 
| 
 | 
    87 {if(os.iword(detail::colorize_index()) == 1) {os << "\033[37m";} return os;}
 | 
| 
 | 
    88 
 | 
| 
 | 
    89 inline void enable()
 | 
| 
 | 
    90 {
 | 
| 
 | 
    91     return detail::color_mode::status().enable();
 | 
| 
 | 
    92 }
 | 
| 
 | 
    93 inline void disable()
 | 
| 
 | 
    94 {
 | 
| 
 | 
    95     return detail::color_mode::status().disable();
 | 
| 
 | 
    96 }
 | 
| 
 | 
    97 
 | 
| 
 | 
    98 inline bool should_color()
 | 
| 
 | 
    99 {
 | 
| 
 | 
   100     return detail::color_mode::status().should_color();
 | 
| 
 | 
   101 }
 | 
| 
 | 
   102 
 | 
| 
 | 
   103 } // color_ansi
 | 
| 
 | 
   104 
 | 
| 
 | 
   105 // ANSI escape sequence is the only and default colorization method currently
 | 
| 
 | 
   106 namespace color = color_ansi;
 | 
| 
 | 
   107 
 | 
| 
 | 
   108 } // toml
 | 
| 
 | 
   109 #endif// TOML11_COLOR_HPP
 |