ansicolor.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /*
  81. ANSIColor(int c1);
  82. ANSIColor(int c1, int c2);
  83. */
  84. ANSIColor &Attr(ATTR a);
  85. bool operator==(const ANSIColor &c) const;
  86. bool operator!=(const ANSIColor &c) const;
  87. void setFg(COLOR f);
  88. void setFg(COLOR f, ATTR a);
  89. void setBg(COLOR b);
  90. /**
  91. * Get the foreground color
  92. * @return COLOR
  93. */
  94. COLOR getFg() { return fg; };
  95. /**
  96. * Get the background color
  97. * @return COLOR
  98. */
  99. COLOR getBg() { return bg; };
  100. void attr(ATTR a);
  101. std::string output(void) const;
  102. std::string operator()(void) const;
  103. };
  104. extern ANSIColor reset;
  105. #endif