colorscheme.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "engine.h"
  3. #include <library/cpp/colorizer/output.h>
  4. #ifndef DBG_OUTPUT_DEFAULT_COLOR_SCHEME
  5. #define DBG_OUTPUT_DEFAULT_COLOR_SCHEME NDbgDump::NColorScheme::TPlain
  6. #endif
  7. #define DBG_OUTPUT_COLOR_HANDLER(NAME) \
  8. template <class S> \
  9. inline void NAME(S& stream)
  10. namespace NDbgDump {
  11. namespace NColorScheme {
  12. /// Start by copying this one if you want to define a custom color scheme.
  13. struct TPlain {
  14. // Foreground color modifiers
  15. DBG_OUTPUT_COLOR_HANDLER(Markup) {
  16. Y_UNUSED(stream);
  17. }
  18. DBG_OUTPUT_COLOR_HANDLER(String) {
  19. Y_UNUSED(stream);
  20. }
  21. DBG_OUTPUT_COLOR_HANDLER(Literal) {
  22. Y_UNUSED(stream);
  23. }
  24. DBG_OUTPUT_COLOR_HANDLER(ResetType) {
  25. Y_UNUSED(stream);
  26. }
  27. // Background color modifiers
  28. DBG_OUTPUT_COLOR_HANDLER(Key) {
  29. Y_UNUSED(stream);
  30. }
  31. DBG_OUTPUT_COLOR_HANDLER(Value) {
  32. Y_UNUSED(stream);
  33. }
  34. DBG_OUTPUT_COLOR_HANDLER(ResetRole) {
  35. Y_UNUSED(stream);
  36. }
  37. };
  38. /// Use this one if you want colors but are lazy enough to define a custom color scheme.
  39. /// Be careful enough to use DumpRaw for avoiding an endless recursion.
  40. /// Enforce controls whether colors should be applied even if stdout is not a TTY.
  41. template <bool Enforce = false>
  42. class TEyebleed {
  43. public:
  44. TEyebleed() {
  45. if (Enforce) {
  46. Colors.Enable();
  47. }
  48. }
  49. // Foreground color modifiers
  50. DBG_OUTPUT_COLOR_HANDLER(Markup) {
  51. stream << DumpRaw(Colors.LightGreenColor());
  52. }
  53. DBG_OUTPUT_COLOR_HANDLER(String) {
  54. stream << DumpRaw(Colors.YellowColor());
  55. }
  56. DBG_OUTPUT_COLOR_HANDLER(Literal) {
  57. stream << DumpRaw(Colors.LightRedColor());
  58. }
  59. DBG_OUTPUT_COLOR_HANDLER(ResetType) {
  60. stream << DumpRaw(Colors.OldColor());
  61. }
  62. // Background color modifiers
  63. // TODO: support backgrounds in library/cpp/colorizer
  64. DBG_OUTPUT_COLOR_HANDLER(Key) {
  65. if (Depth++ == 0 && Colors.IsTTY()) {
  66. stream << DumpRaw(TStringBuf("\033[42m"));
  67. }
  68. }
  69. DBG_OUTPUT_COLOR_HANDLER(Value) {
  70. if (Depth++ == 0 && Colors.IsTTY()) {
  71. stream << DumpRaw(TStringBuf("\033[44m"));
  72. }
  73. }
  74. DBG_OUTPUT_COLOR_HANDLER(ResetRole) {
  75. if (--Depth == 0 && Colors.IsTTY()) {
  76. stream << DumpRaw(TStringBuf("\033[49m"));
  77. }
  78. }
  79. private:
  80. NColorizer::TColors Colors;
  81. size_t Depth = 0;
  82. };
  83. }
  84. }
  85. namespace NPrivate {
  86. template <typename CS>
  87. struct TColorSchemeContainer {
  88. CS ColorScheme;
  89. };
  90. }