ansicolor.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. ANSIColor::ANSIColor(std::initializer_list<int> il) : ANSIColor() {
  62. for (auto const &i : il) {
  63. switch (i) {
  64. case 1:
  65. bold = 1;
  66. break;
  67. case 5:
  68. blink = 1;
  69. break;
  70. case 30:
  71. case 31:
  72. case 32:
  73. case 33:
  74. case 34:
  75. case 35:
  76. case 36:
  77. case 37:
  78. fg = (COLOR)(i - 30);
  79. break;
  80. case 40:
  81. case 41:
  82. case 42:
  83. case 43:
  84. case 44:
  85. case 45:
  86. case 46:
  87. case 47:
  88. bg = (COLOR)(i - 40);
  89. break;
  90. }
  91. }
  92. }
  93. /**
  94. * Construct a new ANSIColor::ANSIColor object
  95. * with a foreground and background color.
  96. *
  97. * @param[in] f foreground COLOR
  98. * @param[in] b background COLOR
  99. */
  100. ANSIColor::ANSIColor(COLOR f, COLOR b) : ANSIColor() {
  101. fg = f;
  102. bg = b;
  103. }
  104. /**
  105. * Construct a new ANSIColor::ANSIColor object
  106. * with a foreground color, background color,
  107. * and attribute.
  108. *
  109. * @param[in] f foreground COLOR
  110. * @param[in] b background COLOR
  111. * @param[in] a ATTR
  112. */
  113. ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a) : ANSIColor() {
  114. fg = f;
  115. bg = b;
  116. Attr(a);
  117. }
  118. /**
  119. * Construct a new ANSIColor::ANSIColor object
  120. * with foreground, background color and attributes.
  121. *
  122. * @param[in] f foreground COLOR
  123. * @param[in] b background COLOR
  124. * @param[in] a1 ATTR
  125. * @param[in] a2 ATTR
  126. */
  127. ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
  128. fg = f;
  129. bg = b;
  130. Attr(a1);
  131. Attr(a2);
  132. }
  133. /**
  134. * Set attribute. We return the object so
  135. * calls can be chained.
  136. *
  137. * @param[in] a ATTR
  138. * @return ANSIColor&
  139. */
  140. ANSIColor &ANSIColor::Attr(ATTR a) {
  141. switch (a) {
  142. case ATTR::RESET:
  143. reset = 1;
  144. break;
  145. case ATTR::BOLD:
  146. bold = 1;
  147. break;
  148. case ATTR::BLINK:
  149. blink = 1;
  150. break;
  151. case ATTR::INVERSE:
  152. inverse = 1;
  153. break;
  154. }
  155. return *this;
  156. }
  157. /**
  158. * Equal operator.
  159. *
  160. * This compares colors and attributes, but ignores reset.
  161. *
  162. * @param[in] c const ANSIColor &
  163. * @return bool
  164. */
  165. bool ANSIColor::operator==(const ANSIColor &c) const {
  166. return ((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
  167. (blink == c.blink) and (inverse == c.inverse));
  168. }
  169. /**
  170. * Not-equal operator.
  171. *
  172. * This compares colors and attributes, but ignores reset.
  173. *
  174. * @param[in] c const ANSIColor &
  175. * @return bool
  176. */
  177. bool ANSIColor::operator!=(const ANSIColor &c) const {
  178. return !((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
  179. (blink == c.blink) and (inverse == c.inverse));
  180. }
  181. /**
  182. * @brief Set foreground color
  183. *
  184. * @param[in] f foreground COLOR
  185. */
  186. void ANSIColor::setFg(COLOR f) {
  187. fg = f;
  188. reset = 0;
  189. bold = 0;
  190. blink = 0;
  191. inverse = 0;
  192. }
  193. /**
  194. * @brief Set foreground color and attribute
  195. *
  196. * @param[in] f foreground COLOR
  197. * @param[in] a ATTR
  198. */
  199. void ANSIColor::setFg(COLOR f, ATTR a) {
  200. fg = f;
  201. attr(a);
  202. }
  203. /**
  204. * @brief Set background color
  205. *
  206. * @param[in] b background COLOR
  207. */
  208. void ANSIColor::setBg(COLOR b) { bg = b; }
  209. /**
  210. * @brief Set attribute
  211. *
  212. * This clears all the attributes before setting the selected ATTR.
  213. *
  214. * @param[in] a ATTR
  215. */
  216. void ANSIColor::attr(ATTR a) {
  217. // first, clear all attributes
  218. reset = 0;
  219. bold = 0;
  220. blink = 0;
  221. inverse = 0;
  222. Attr(a);
  223. }
  224. /**
  225. * Output the full ANSI codes for attributes and color.
  226. * This does not look at the previous values.
  227. */
  228. std::string ANSIColor::output(void) const {
  229. std::string clr(CSI);
  230. // check for special cases
  231. if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
  232. clr += "0m";
  233. return clr;
  234. }
  235. if (reset and (fg == COLOR::WHITE) and (bg == COLOR::BLACK)) {
  236. clr += "0m";
  237. return clr;
  238. }
  239. if (reset) {
  240. clr += "0;";
  241. }
  242. if (bold) {
  243. if (blink) {
  244. clr += "5;";
  245. }
  246. clr += "1;";
  247. } else {
  248. if (!reset) clr += "0;";
  249. if (blink) {
  250. clr += "5;";
  251. }
  252. }
  253. clr += std::to_string(30 + (int)fg) + ";";
  254. clr += std::to_string(40 + (int)bg) + "m";
  255. return clr;
  256. }
  257. std::string ANSIColor::operator()(void) const {
  258. std::string clr(CSI);
  259. // check for special cases
  260. if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
  261. clr += "0m";
  262. return clr;
  263. }
  264. if (reset and (fg == COLOR::WHITE) and (bg == COLOR::BLACK)) {
  265. clr += "0m";
  266. return clr;
  267. }
  268. if (reset) {
  269. clr += "0;";
  270. }
  271. if (bold) {
  272. if (blink) {
  273. clr += "5;";
  274. }
  275. clr += "1;";
  276. } else {
  277. if (!reset) clr += "0;";
  278. if (blink) {
  279. clr += "5;";
  280. }
  281. }
  282. clr += std::to_string(30 + (int)fg) + ";";
  283. clr += std::to_string(40 + (int)bg) + "m";
  284. return clr;
  285. }
  286. ANSIColor reset(ATTR::RESET);