ansicolor.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "ansicolor.h"
  2. #include <string>
  3. /**
  4. * @file
  5. * @brief ANSIColor
  6. */
  7. /**
  8. * Construct a new ANSIColor::ANSIColor object
  9. * with sensible defaults (White on Black).
  10. *
  11. */
  12. ANSIColor::ANSIColor()
  13. : fg(COLOR::WHITE),
  14. bg(COLOR::BLACK),
  15. reset(0),
  16. bold(0),
  17. blink(0),
  18. inverse(0) {}
  19. /**
  20. * Construct a new ANSIColor::ANSIColor object
  21. * with attribute set.
  22. *
  23. * @param[in] a ATTR
  24. */
  25. ANSIColor::ANSIColor(ATTR a) : ANSIColor() { Attr(a); }
  26. /* // This won't work. There's no clear way to set bold colors easily.
  27. ANSIColor::ANSIColor(int c1) {}
  28. ANSIColor::ANSIColor(int c1, int c2) {}
  29. */
  30. /**
  31. * Construct a new ANSIColor::ANSIColor object
  32. * with a foreground color.
  33. *
  34. * @param[in] f COLOR
  35. */
  36. ANSIColor::ANSIColor(COLOR f) : ANSIColor() { fg = f; }
  37. /**
  38. * Construct a new ANSIColor::ANSIColor object
  39. * with a foreground color and attribute.
  40. *
  41. * @param[in] f COLOR
  42. * @param[in] a ATTR
  43. */
  44. ANSIColor::ANSIColor(COLOR f, ATTR a) : ANSIColor() {
  45. fg = f;
  46. Attr(a);
  47. }
  48. /**
  49. * Construct a new ANSIColor::ANSIColor object
  50. * with a foreground color and attributes.
  51. *
  52. * @param[in] f COLOR
  53. * @param[in] a1 ATTR
  54. * @param[in] a2 ATTR
  55. */
  56. ANSIColor::ANSIColor(COLOR f, ATTR a1, ATTR a2) : ANSIColor() {
  57. fg = f;
  58. Attr(a1);
  59. Attr(a2);
  60. }
  61. /**
  62. * Construct a new ANSIColor::ANSIColor object
  63. * with a foreground and background color.
  64. *
  65. * @param[in] f foreground COLOR
  66. * @param[in] b background COLOR
  67. */
  68. ANSIColor::ANSIColor(COLOR f, COLOR b) : ANSIColor() {
  69. fg = f;
  70. bg = b;
  71. }
  72. /**
  73. * Construct a new ANSIColor::ANSIColor object
  74. * with a foreground color, background color,
  75. * and attribute.
  76. *
  77. * @param[in] f foreground COLOR
  78. * @param[in] b background COLOR
  79. * @param[in] a ATTR
  80. */
  81. ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a) : ANSIColor() {
  82. fg = f;
  83. bg = b;
  84. Attr(a);
  85. }
  86. /**
  87. * Construct a new ANSIColor::ANSIColor object
  88. * with foreground, background color and attributes.
  89. *
  90. * @param[in] f foreground COLOR
  91. * @param[in] b background COLOR
  92. * @param[in] a1 ATTR
  93. * @param[in] a2 ATTR
  94. */
  95. ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
  96. fg = f;
  97. bg = b;
  98. Attr(a1);
  99. Attr(a2);
  100. }
  101. /**
  102. * Set attribute. We return the object so
  103. * calls can be chained.
  104. *
  105. * @param[in] a ATTR
  106. * @return ANSIColor&
  107. */
  108. ANSIColor &ANSIColor::Attr(ATTR a) {
  109. switch (a) {
  110. case ATTR::RESET:
  111. reset = 1;
  112. break;
  113. case ATTR::BOLD:
  114. bold = 1;
  115. break;
  116. case ATTR::BLINK:
  117. blink = 1;
  118. break;
  119. case ATTR::INVERSE:
  120. inverse = 1;
  121. break;
  122. }
  123. return *this;
  124. }
  125. /**
  126. * Equal operator.
  127. *
  128. * This compares colors and attributes, but ignores reset.
  129. *
  130. * @param[in] c const ANSIColor &
  131. * @return bool
  132. */
  133. bool ANSIColor::operator==(const ANSIColor &c) const {
  134. return ((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
  135. (blink == c.blink) and (inverse == c.inverse));
  136. }
  137. /**
  138. * Not-equal operator.
  139. *
  140. * This compares colors and attributes, but ignores reset.
  141. *
  142. * @param[in] c const ANSIColor &
  143. * @return bool
  144. */
  145. bool ANSIColor::operator!=(const ANSIColor &c) const {
  146. return !((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
  147. (blink == c.blink) and (inverse == c.inverse));
  148. }
  149. /**
  150. * @brief Set foreground color
  151. *
  152. * @param[in] f foreground COLOR
  153. */
  154. void ANSIColor::setFg(COLOR f) {
  155. fg = f;
  156. reset = 0;
  157. bold = 0;
  158. blink = 0;
  159. inverse = 0;
  160. }
  161. /**
  162. * @brief Set foreground color and attribute
  163. *
  164. * @param[in] f foreground COLOR
  165. * @param[in] a ATTR
  166. */
  167. void ANSIColor::setFg(COLOR f, ATTR a) {
  168. fg = f;
  169. attr(a);
  170. }
  171. /**
  172. * @brief Set background color
  173. *
  174. * @param[in] b background COLOR
  175. */
  176. void ANSIColor::setBg(COLOR b) { bg = b; }
  177. /**
  178. * @brief Set attribute
  179. *
  180. * This clears all the attributes before setting the selected ATTR.
  181. *
  182. * @param[in] a ATTR
  183. */
  184. void ANSIColor::attr(ATTR a) {
  185. // first, clear all attributes
  186. reset = 0;
  187. bold = 0;
  188. blink = 0;
  189. inverse = 0;
  190. Attr(a);
  191. }
  192. /**
  193. * Output the full ANSI codes for attributes and color.
  194. * This does not look at the previous values.
  195. */
  196. std::string ANSIColor::output(void) const {
  197. std::string clr(CSI);
  198. // check for special cases
  199. if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
  200. clr += "0m";
  201. return clr;
  202. }
  203. if (reset and (fg == COLOR::WHITE) and (bg == COLOR::BLACK)) {
  204. clr += "0m";
  205. return clr;
  206. }
  207. if (reset) {
  208. clr += "0;";
  209. }
  210. if (bold) {
  211. if (blink) {
  212. clr += "5;";
  213. }
  214. clr += "1;";
  215. } else {
  216. if (!reset) clr += "0;";
  217. if (blink) {
  218. clr += "5;";
  219. }
  220. }
  221. clr += std::to_string(30 + (int)fg) + ";";
  222. clr += std::to_string(40 + (int)bg) + "m";
  223. return clr;
  224. }
  225. std::string ANSIColor::operator()(void) const {
  226. std::string clr(CSI);
  227. // check for special cases
  228. if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
  229. clr += "0m";
  230. return clr;
  231. }
  232. if (reset and (fg == COLOR::WHITE) and (bg == COLOR::BLACK)) {
  233. clr += "0m";
  234. return clr;
  235. }
  236. if (reset) {
  237. clr += "0;";
  238. }
  239. if (bold) {
  240. if (blink) {
  241. clr += "5;";
  242. }
  243. clr += "1;";
  244. } else {
  245. if (!reset) clr += "0;";
  246. if (blink) {
  247. clr += "5;";
  248. }
  249. }
  250. clr += std::to_string(30 + (int)fg) + ";";
  251. clr += std::to_string(40 + (int)bg) + "m";
  252. return clr;
  253. }
  254. ANSIColor reset(ATTR::RESET);