colors.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. #include "fwd.h"
  3. #include <util/generic/string.h>
  4. #include <util/generic/strbuf.h>
  5. #include <cstdio>
  6. namespace NColorizer {
  7. /**
  8. * List of ECMA-48 colors.
  9. *
  10. * When printing elements of this enum via `operator<<`, `AutoColors()` (see below) function will be used
  11. * to produce colors, i.e. nothing will be printed to non-tty streams. When converting elements of this enum
  12. * via `ToString`, escape code is always returned.
  13. *
  14. * Note: as of now (2019-03), `ya make` strips out some escape codes from compiler output.
  15. * It also inserts `RESET` before each color code. See https://st.yandex-team.ru/DEVTOOLS-5269 for details.
  16. * For now, do not use `OLD`, `ST_*`, `FG_*` and `BG_*` in tools that run through `ya make`.
  17. *
  18. * Note: refrain from using black colors because there's high chance they'll not be visible on some terminals.
  19. * Default windows and ubuntu color schemes shows them as black letters on black background.
  20. * Also, white colors are barely visible in default OSX color scheme. Light black is usually fine though.
  21. */
  22. enum EAnsiCode: i8 {
  23. // Note: not using `GENERATE_ENUM_SERIALIZATION` because serialization generator depends on this library.
  24. /// Does not change anything.
  25. INVALID,
  26. /// Reset all styles and colors. Safe to use in `ya make` tools.
  27. RESET,
  28. /// Change style, don't change anything else.
  29. ST_LIGHT,
  30. ST_DARK,
  31. ST_NORMAL,
  32. /// Additional styles.
  33. ITALIC_ON,
  34. ITALIC_OFF,
  35. UNDERLINE_ON,
  36. UNDERLINE_OFF,
  37. /// Change foreground color, don't change anything else.
  38. FG_DEFAULT,
  39. FG_BLACK,
  40. FG_RED,
  41. FG_GREEN,
  42. FG_YELLOW,
  43. FG_BLUE,
  44. FG_MAGENTA,
  45. FG_CYAN,
  46. FG_WHITE,
  47. /// Change background color, don't change anything else.
  48. BG_DEFAULT,
  49. BG_BLACK,
  50. BG_RED,
  51. BG_GREEN,
  52. BG_YELLOW,
  53. BG_BLUE,
  54. BG_MAGENTA,
  55. BG_CYAN,
  56. BG_WHITE,
  57. /// Reset all styles and colors, then enable a (possibly light or dark) color. Safe to use in `ya make` tools.
  58. DEFAULT,
  59. BLACK,
  60. RED,
  61. GREEN,
  62. YELLOW,
  63. BLUE,
  64. MAGENTA,
  65. CYAN,
  66. WHITE,
  67. LIGHT_DEFAULT,
  68. LIGHT_BLACK,
  69. LIGHT_RED,
  70. LIGHT_GREEN,
  71. LIGHT_YELLOW,
  72. LIGHT_BLUE,
  73. LIGHT_MAGENTA,
  74. LIGHT_CYAN,
  75. LIGHT_WHITE,
  76. DARK_DEFAULT,
  77. DARK_BLACK,
  78. DARK_RED,
  79. DARK_GREEN,
  80. DARK_YELLOW,
  81. DARK_BLUE,
  82. DARK_MAGENTA,
  83. DARK_CYAN,
  84. DARK_WHITE,
  85. };
  86. /**
  87. * Produces escape codes or empty stringbuf depending on settings.
  88. * All color functions return zero-terminated stringbuf.
  89. */
  90. class TColors {
  91. public:
  92. static bool CalcIsTTY(FILE* file);
  93. public:
  94. explicit TColors(FILE* = stderr);
  95. explicit TColors(bool ontty);
  96. TStringBuf Reset() const noexcept;
  97. TStringBuf StyleLight() const noexcept;
  98. TStringBuf StyleDark() const noexcept;
  99. TStringBuf StyleNormal() const noexcept;
  100. TStringBuf ItalicOn() const noexcept;
  101. TStringBuf ItalicOff() const noexcept;
  102. TStringBuf UnderlineOn() const noexcept;
  103. TStringBuf UnderlineOff() const noexcept;
  104. TStringBuf ForeDefault() const noexcept;
  105. TStringBuf ForeBlack() const noexcept;
  106. TStringBuf ForeRed() const noexcept;
  107. TStringBuf ForeGreen() const noexcept;
  108. TStringBuf ForeYellow() const noexcept;
  109. TStringBuf ForeBlue() const noexcept;
  110. TStringBuf ForeMagenta() const noexcept;
  111. TStringBuf ForeCyan() const noexcept;
  112. TStringBuf ForeWhite() const noexcept;
  113. TStringBuf BackDefault() const noexcept;
  114. TStringBuf BackBlack() const noexcept;
  115. TStringBuf BackRed() const noexcept;
  116. TStringBuf BackGreen() const noexcept;
  117. TStringBuf BackYellow() const noexcept;
  118. TStringBuf BackBlue() const noexcept;
  119. TStringBuf BackMagenta() const noexcept;
  120. TStringBuf BackCyan() const noexcept;
  121. TStringBuf BackWhite() const noexcept;
  122. TStringBuf Default() const noexcept;
  123. TStringBuf Black() const noexcept;
  124. TStringBuf Red() const noexcept;
  125. TStringBuf Green() const noexcept;
  126. TStringBuf Yellow() const noexcept;
  127. TStringBuf Blue() const noexcept;
  128. TStringBuf Magenta() const noexcept;
  129. TStringBuf Cyan() const noexcept;
  130. TStringBuf White() const noexcept;
  131. TStringBuf LightDefault() const noexcept;
  132. TStringBuf LightBlack() const noexcept;
  133. TStringBuf LightRed() const noexcept;
  134. TStringBuf LightGreen() const noexcept;
  135. TStringBuf LightYellow() const noexcept;
  136. TStringBuf LightBlue() const noexcept;
  137. TStringBuf LightMagenta() const noexcept;
  138. TStringBuf LightCyan() const noexcept;
  139. TStringBuf LightWhite() const noexcept;
  140. TStringBuf DarkDefault() const noexcept;
  141. TStringBuf DarkBlack() const noexcept;
  142. TStringBuf DarkRed() const noexcept;
  143. TStringBuf DarkGreen() const noexcept;
  144. TStringBuf DarkYellow() const noexcept;
  145. TStringBuf DarkBlue() const noexcept;
  146. TStringBuf DarkMagenta() const noexcept;
  147. TStringBuf DarkCyan() const noexcept;
  148. TStringBuf DarkWhite() const noexcept;
  149. /// Compatibility; prefer using methods without `Color` suffix in their names.
  150. /// Note: these behave differently from their un-suffixed counterparts.
  151. /// While functions declared above will reset colors completely, these will only reset foreground color and
  152. /// style, without changing the background color and underline/italic settings. Also, names of these functions
  153. /// don't conform with standard, e.g. `YellowColor` actually emits the `lite yellow` escape code.
  154. TStringBuf OldColor() const noexcept;
  155. TStringBuf BoldColor() const noexcept;
  156. TStringBuf BlackColor() const noexcept;
  157. TStringBuf BlueColor() const noexcept;
  158. TStringBuf GreenColor() const noexcept;
  159. TStringBuf CyanColor() const noexcept;
  160. TStringBuf RedColor() const noexcept;
  161. TStringBuf PurpleColor() const noexcept;
  162. TStringBuf BrownColor() const noexcept;
  163. TStringBuf LightGrayColor() const noexcept;
  164. TStringBuf DarkGrayColor() const noexcept;
  165. TStringBuf LightBlueColor() const noexcept;
  166. TStringBuf LightGreenColor() const noexcept;
  167. TStringBuf LightCyanColor() const noexcept;
  168. TStringBuf LightRedColor() const noexcept;
  169. TStringBuf LightPurpleColor() const noexcept;
  170. TStringBuf YellowColor() const noexcept;
  171. TStringBuf WhiteColor() const noexcept;
  172. inline bool IsTTY() const noexcept {
  173. return IsTTY_;
  174. }
  175. inline void SetIsTTY(bool value) noexcept {
  176. IsTTY_ = value;
  177. }
  178. inline void Enable() noexcept {
  179. SetIsTTY(true);
  180. }
  181. inline void Disable() noexcept {
  182. SetIsTTY(false);
  183. }
  184. private:
  185. bool IsTTY_;
  186. };
  187. /// Singletone `TColors` instances for stderr/stdout.
  188. TColors& StdErr();
  189. TColors& StdOut();
  190. /// Choose `TColors` depending on output stream. If passed stream is stderr/stdout, return a corresponding
  191. /// singletone. Otherwise, return a disabled singletone (which you can, but should *not* enable).
  192. TColors& AutoColors(IOutputStream& os);
  193. /// Calculate total length of all ANSI escape codes in the text.
  194. size_t TotalAnsiEscapeCodeLen(TStringBuf text);
  195. }
  196. TStringBuf ToStringBuf(NColorizer::EAnsiCode x);
  197. TString ToString(NColorizer::EAnsiCode x);