ansicolor.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef ANSICOLOR_H
  2. #define ANSICOLOR_H
  3. #include <cstdint>
  4. #include <string>
  5. #define CSI "\x1b["
  6. /**
  7. * ANSI Color codes
  8. */
  9. /**
  10. * @brief The colors available under ANSI-BBS
  11. */
  12. enum class COLOR : std::int8_t {
  13. /// BLACK (0)
  14. BLACK,
  15. /// RED (1)
  16. RED,
  17. /// GREEN (2)
  18. GREEN,
  19. /// BROWN (3)
  20. BROWN,
  21. /// YELLOW (3)
  22. YELLOW = 3,
  23. /// BLUE (4)
  24. BLUE,
  25. /// MAGENTA (5)
  26. MAGENTA,
  27. /// CYAN (6)
  28. CYAN,
  29. /// WHITE (7)
  30. WHITE
  31. };
  32. /**
  33. * @brief ANSI-BBS text attributes
  34. */
  35. enum class ATTR : std::int8_t {
  36. /// RESET forces all attributes (and Colors) to be sent.
  37. RESET,
  38. /// BOLD is the same as BRIGHT.
  39. BOLD,
  40. /// BRIGHT is the same as BOLD.
  41. BRIGHT = 1,
  42. /// SLOW BLINK
  43. BLINK = 5,
  44. /// INVERSE is Background on Foreground.
  45. INVERSE = 7
  46. };
  47. /**
  48. * @class ANSIColor
  49. * This holds foreground, background and ANSI-BBS attribute
  50. * information.
  51. * The special attribute RESET forces attribute and color
  52. * output always.
  53. *
  54. * @brief Foreground, Background and Attributes
  55. *
  56. */
  57. class ANSIColor {
  58. /** Foreground color */
  59. COLOR fg;
  60. /** Background color */
  61. COLOR bg;
  62. // Track attributes (ATTR)
  63. /** reset flag / always send color and attributes */
  64. unsigned int reset : 1;
  65. /** bold / bright flag */
  66. unsigned int bold : 1;
  67. /** blink slow blinking text */
  68. unsigned int blink : 1;
  69. /** inverse */
  70. unsigned int inverse : 1;
  71. public:
  72. ANSIColor();
  73. ANSIColor(ATTR a);
  74. ANSIColor(COLOR f);
  75. ANSIColor(COLOR f, ATTR a);
  76. ANSIColor(COLOR f, ATTR a1, ATTR a2);
  77. ANSIColor(COLOR f, COLOR b);
  78. ANSIColor(COLOR f, COLOR b, ATTR a);
  79. ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2);
  80. ANSIColor(std::initializer_list<int> il);
  81. /*
  82. ANSIColor(int c1);
  83. ANSIColor(int c1, int c2);
  84. */
  85. ANSIColor &Attr(ATTR a);
  86. bool operator==(const ANSIColor &c) const;
  87. bool operator!=(const ANSIColor &c) const;
  88. void setFg(COLOR f);
  89. void setFg(COLOR f, ATTR a);
  90. void setBg(COLOR b);
  91. /**
  92. * Get the foreground color
  93. * @return COLOR
  94. */
  95. COLOR getFg() { return fg; };
  96. /**
  97. * Get the background color
  98. * @return COLOR
  99. */
  100. COLOR getBg() { return bg; };
  101. void attr(ATTR a);
  102. std::string output(void) const;
  103. std::string operator()(void) const;
  104. };
  105. extern ANSIColor reset;
  106. #endif