You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.7 KiB

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