ansicolor.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include <string>
  2. #include "door.h"
  3. /**
  4. * @file
  5. * @brief ANSIColor
  6. */
  7. namespace door {
  8. /**
  9. * Construct a new ANSIColor::ANSIColor object
  10. * with sensible defaults (White on Black).
  11. *
  12. */
  13. ANSIColor::ANSIColor() : fg(COLOR::WHITE), bg(COLOR::BLACK), attr(0) {}
  14. /**
  15. * Construct a new ANSIColor::ANSIColor object
  16. * with attribute set.
  17. *
  18. * @param[in] a ATTR
  19. */
  20. ANSIColor::ANSIColor(ATTR a) : ANSIColor() { Attr(a); }
  21. /**
  22. * Construct a new ANSIColor::ANSIColor object
  23. * with a foreground color.
  24. *
  25. * @param[in] f COLOR
  26. */
  27. ANSIColor::ANSIColor(COLOR f) : ANSIColor() { fg = f; }
  28. /**
  29. * Construct a new ANSIColor::ANSIColor object
  30. * with a foreground color and attribute.
  31. *
  32. * @param[in] f COLOR
  33. * @param[in] a ATTR
  34. */
  35. ANSIColor::ANSIColor(COLOR f, ATTR a) : ANSIColor() {
  36. fg = f;
  37. Attr(a);
  38. }
  39. /**
  40. * Construct a new ANSIColor::ANSIColor object
  41. * with a foreground color and attributes.
  42. *
  43. * @param[in] f COLOR
  44. * @param[in] a1 ATTR
  45. * @param[in] a2 ATTR
  46. */
  47. ANSIColor::ANSIColor(COLOR f, ATTR a1, ATTR a2) : ANSIColor() {
  48. fg = f;
  49. Attr(a1);
  50. Attr(a2);
  51. }
  52. /**
  53. * Construct a new ANSIColor::ANSIColor object
  54. * with a foreground and background color.
  55. *
  56. * @param[in] f foreground COLOR
  57. * @param[in] b background COLOR
  58. */
  59. ANSIColor::ANSIColor(COLOR f, COLOR b) : ANSIColor() {
  60. fg = f;
  61. bg = b;
  62. }
  63. /**
  64. * Construct a new ANSIColor::ANSIColor object
  65. * with a foreground color, background color,
  66. * and attribute.
  67. *
  68. * @param[in] f foreground COLOR
  69. * @param[in] b background COLOR
  70. * @param[in] a ATTR
  71. */
  72. ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a) : ANSIColor() {
  73. fg = f;
  74. bg = b;
  75. Attr(a);
  76. }
  77. /**
  78. * Construct a new ANSIColor::ANSIColor object
  79. * with foreground, background color and attributes.
  80. *
  81. * @param[in] f foreground COLOR
  82. * @param[in] b background COLOR
  83. * @param[in] a1 ATTR
  84. * @param[in] a2 ATTR
  85. */
  86. ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
  87. fg = f;
  88. bg = b;
  89. Attr(a1);
  90. Attr(a2);
  91. }
  92. /**
  93. * Set attribute. We return the object so
  94. * calls can be chained.
  95. *
  96. * @param[in] a ATTR
  97. * @return ANSIColor&
  98. */
  99. ANSIColor &ANSIColor::Attr(ATTR a) {
  100. switch (a) {
  101. case ATTR::RESET:
  102. attr |= ATTR_RESET;
  103. break;
  104. case ATTR::BOLD:
  105. attr |= ATTR_BOLD;
  106. break;
  107. case ATTR::BLINK:
  108. attr |= ATTR_BLINK;
  109. break;
  110. case ATTR::INVERSE:
  111. attr |= ATTR_INVERSE;
  112. break;
  113. }
  114. return *this;
  115. }
  116. /**
  117. * Equal operator.
  118. *
  119. * This compares colors and attributes, but ignores reset.
  120. *
  121. * @param[in] c const ANSIColor &
  122. * @return bool
  123. */
  124. bool ANSIColor::operator==(const ANSIColor &c) const {
  125. return ((fg == c.fg) and (bg == c.bg) and
  126. ((attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)) ==
  127. ((c.attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)))));
  128. }
  129. /**
  130. * Not-equal operator.
  131. *
  132. * This compares colors and attributes, but ignores reset.
  133. *
  134. * @param[in] c const ANSIColor &
  135. * @return bool
  136. */
  137. bool ANSIColor::operator!=(const ANSIColor &c) const {
  138. return !((fg == c.fg) and (bg == c.bg) and
  139. ((attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)) ==
  140. ((c.attr & (ATTR_BOLD | ATTR_BLINK | ATTR_INVERSE)))));
  141. }
  142. /**
  143. * @brief Set foreground color
  144. *
  145. * @param[in] f foreground COLOR
  146. */
  147. void ANSIColor::setFg(COLOR f) {
  148. fg = f;
  149. attr = 0;
  150. }
  151. /**
  152. * @brief Set foreground color and attribute
  153. *
  154. * @param[in] f foreground COLOR
  155. * @param[in] a ATTR
  156. */
  157. void ANSIColor::setFg(COLOR f, ATTR a) {
  158. fg = f;
  159. setAttr(a);
  160. }
  161. /**
  162. * @brief Set background color
  163. *
  164. * @param[in] b background COLOR
  165. */
  166. void ANSIColor::setBg(COLOR b) { bg = b; }
  167. /**
  168. * @brief Set attribute
  169. *
  170. * This clears all the attributes before setting the selected ATTR.
  171. *
  172. * @param[in] a ATTR
  173. */
  174. void ANSIColor::setAttr(ATTR a) {
  175. // first, clear all attributes
  176. attr = 0;
  177. Attr(a);
  178. }
  179. /**
  180. * Output the full ANSI codes for attributes and color.
  181. * This does not look at the previous values.
  182. */
  183. std::string ANSIColor::output(void) const {
  184. std::string clr(CSI);
  185. // check for special cases
  186. if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::BLACK) and
  187. (bg == COLOR::BLACK)) {
  188. clr += "0m";
  189. return clr;
  190. }
  191. if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::WHITE) and
  192. (bg == COLOR::BLACK)) {
  193. clr += "0m";
  194. return clr;
  195. }
  196. if ((attr & ATTR_RESET) == ATTR_RESET) {
  197. clr += "0;";
  198. }
  199. if ((attr & ATTR_BOLD) == ATTR_BOLD) {
  200. if ((attr & ATTR_BLINK) == ATTR_BLINK) {
  201. clr += "5;";
  202. }
  203. clr += "1;";
  204. } else {
  205. if (!((attr & ATTR_RESET) == ATTR_RESET)) clr += "0;";
  206. if ((attr & ATTR_BLINK) == ATTR_BLINK) {
  207. clr += "5;";
  208. }
  209. }
  210. clr += std::to_string(30 + (int)fg) + ";";
  211. clr += std::to_string(40 + (int)bg) + "m";
  212. return clr;
  213. }
  214. /**
  215. * @brief Output debug string for ANSIColor
  216. *
  217. * @return std::string
  218. */
  219. std::string ANSIColor::debug(void) {
  220. std::string output;
  221. output = "ANSIColor FG";
  222. output += std::to_string((int)fg);
  223. output += ", BG";
  224. output += std::to_string((int)bg);
  225. output += ", B";
  226. output += std::to_string((attr & ATTR_BOLD) == ATTR_BOLD);
  227. output += ", R";
  228. output += std::to_string((attr & ATTR_RESET) == ATTR_RESET);
  229. return output;
  230. }
  231. /**
  232. * Output only what ANSI attributes and colors have changed.
  233. * This uses the previous ANSIColor value to determine what
  234. * has changed.
  235. *
  236. * This sets previous to the current upon completion.
  237. */
  238. std::string ANSIColor::output(ANSIColor &previous) const {
  239. std::string clr(CSI);
  240. // color output optimization
  241. // check for special cases
  242. if (((attr & ATTR_RESET) == ATTR_RESET) and (fg == COLOR::BLACK) and
  243. (bg == COLOR::BLACK)) {
  244. clr += "0m";
  245. previous = *this;
  246. previous.attr &= ~ATTR_RESET; // reset = 0;
  247. return clr;
  248. }
  249. bool temp_reset = false;
  250. if ((!((attr & ATTR_BLINK) == ATTR_BLINK)) and
  251. (((attr & ATTR_BLINK) == ATTR_BLINK) !=
  252. ((previous.attr & ATTR_BLINK) == ATTR_BLINK))) {
  253. temp_reset = true;
  254. }
  255. if (((attr & ATTR_RESET) == ATTR_RESET) or (temp_reset)) {
  256. // current has RESET, so default to always sending colors.
  257. if (temp_reset) {
  258. clr += "0m";
  259. }
  260. // this fixes the extra \x1b that shows up with reset.
  261. if (clr.compare(CSI) == 0) clr.clear();
  262. clr += output();
  263. /*
  264. std::ofstream logf;
  265. logf.open("ansicolor.log", std::ofstream::out | std::ofstream::app);
  266. logf << "clr = [" << clr << "]" << std::endl;
  267. logf.close();
  268. */
  269. previous = *this;
  270. previous.attr &= ~ATTR_RESET;
  271. return clr;
  272. }
  273. if (*this == previous) {
  274. clr.clear();
  275. return clr;
  276. }
  277. // resume "optimization"
  278. if (((attr & ATTR_BOLD) == ATTR_BOLD) !=
  279. ((previous.attr & ATTR_BOLD) == ATTR_BOLD)) {
  280. // not the same, so handle this.
  281. if ((attr & ATTR_BOLD) == ATTR_BOLD) {
  282. if ((attr & ATTR_BLINK) == ATTR_BLINK) {
  283. clr += "5;";
  284. }
  285. clr += "1;";
  286. if (fg != previous.fg) clr += std::to_string((int)fg + 30) + ";";
  287. if (bg != previous.bg) clr += std::to_string((int)bg + 40) + ";";
  288. } else {
  289. clr += "0;";
  290. if ((attr & ATTR_BLINK) == ATTR_BLINK) {
  291. clr += "5;";
  292. }
  293. // RESET to turn OFF BOLD, clears previous
  294. if (fg != COLOR::WHITE) clr += std::to_string((int)fg + 30) + ";";
  295. if (bg != COLOR::BLACK) clr += std::to_string((int)bg + 40) + ";";
  296. }
  297. } else {
  298. // not bold.
  299. if ((attr & ATTR_BLINK) == ATTR_BLINK) {
  300. clr += "5;";
  301. }
  302. if (fg != previous.fg) clr += std::to_string((int)fg + 30) + ";";
  303. if (bg != previous.bg) clr += std::to_string((int)bg + 40) + ";";
  304. };
  305. // Remove ';' if last character
  306. std::string::iterator si = clr.end() - 1;
  307. if (*si == ';') {
  308. clr.erase(si);
  309. }
  310. if (clr.compare(CSI) == 0)
  311. clr.clear();
  312. else
  313. clr += "m";
  314. // final step, set previous to current and return the string;
  315. previous = *this;
  316. return clr;
  317. };
  318. /**
  319. * This converts ANSI \ref COLOR and \ref ATTR to ANSI codes
  320. * understood by the \ref Door output class.
  321. */
  322. std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
  323. std::string out;
  324. Door *d = dynamic_cast<Door *>(&os);
  325. if (d != nullptr) {
  326. d->track = false;
  327. out = c.output(d->previous);
  328. // if (!out.empty())
  329. if (out.compare("\x1b[") == 0) std::abort();
  330. *d << out;
  331. d->track = true;
  332. } else {
  333. // "dumb" version that can't remember anything/ doesn't optimize color
  334. // output.
  335. std::string out = c.output();
  336. os << out;
  337. }
  338. return os;
  339. }
  340. ANSIColor reset(ATTR::RESET);
  341. } // namespace door