123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- #include "door.h"
- #include <string>
- namespace door {
- ANSIColor::ANSIColor()
- : fg(COLOR::WHITE), bg(COLOR::BLACK), reset(0), bold(0), blink(0),
- inverse(0) {}
- ANSIColor::ANSIColor(ATTR a) : ANSIColor() { Attr(a); }
- ANSIColor::ANSIColor(COLOR f) : ANSIColor() { fg = f; }
- ANSIColor::ANSIColor(COLOR f, ATTR a) : ANSIColor() {
- fg = f;
- Attr(a);
- }
- ANSIColor::ANSIColor(COLOR f, ATTR a1, ATTR a2) : ANSIColor() {
- fg = f;
- Attr(a1);
- Attr(a2);
- }
- ANSIColor::ANSIColor(COLOR f, COLOR b) : ANSIColor() {
- fg = f;
- bg = b;
- }
- ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a) : ANSIColor() {
- fg = f;
- bg = b;
- Attr(a);
- }
- ANSIColor::ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2) : ANSIColor() {
- fg = f;
- bg = b;
- Attr(a1);
- Attr(a2);
- }
- ANSIColor &ANSIColor::Attr(ATTR a) {
- switch (a) {
- case ATTR::RESET:
- reset = 1;
- break;
- case ATTR::BOLD:
- bold = 1;
- break;
- case ATTR::BLINK:
- blink = 1;
- break;
- case ATTR::INVERSE:
- inverse = 1;
- break;
- }
- return *this;
- }
- bool ANSIColor::operator==(const ANSIColor &c) const {
- return ((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
- (blink == c.blink) and (inverse == c.inverse));
- }
- bool ANSIColor::operator!=(const ANSIColor &c) const {
- return !((fg == c.fg) and (bg == c.bg) and (bold == c.bold) and
- (blink == c.blink) and (inverse == c.inverse));
- }
- std::string ANSIColor::output(void) const {
- std::string clr(CSI);
-
- if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
- clr += "0m";
- return clr;
- }
- if (reset and (fg == COLOR::WHITE) and (bg == COLOR::BLACK)) {
- clr += "0m";
- return clr;
- }
- if (reset) {
- clr += "0;";
- }
- if (bold) {
- if (blink) {
- clr += "5;";
- }
- clr += "1;";
- } else {
- if (!reset)
- clr += "0;";
- if (blink) {
- clr += "5;";
- }
- }
- clr += std::to_string(30 + (int)fg) + ";";
- clr += std::to_string(40 + (int)bg) + "m";
- return clr;
- }
- std::string ANSIColor::output(ANSIColor &previous) const {
- std::string clr(CSI);
-
-
- if (reset and (fg == COLOR::BLACK) and (bg == COLOR::BLACK)) {
- clr += "0m";
- previous = *this;
- previous.reset = 0;
- return clr;
- }
- bool temp_reset = false;
- if ((!blink) and (blink != previous.blink)) {
- temp_reset = true;
- }
- if ((reset) or (temp_reset)) {
-
- if (temp_reset) {
- clr += "0m";
- }
-
- if (clr.compare(CSI) == 0)
- clr.clear();
- clr += output();
-
- previous = *this;
- previous.reset = 0;
- return clr;
- }
- if (*this == previous) {
- clr.clear();
- return clr;
- }
-
- if (bold != previous.bold) {
-
- if (bold) {
- if (blink) {
- clr += "5;";
- }
- clr += "1;";
- if (fg != previous.fg)
- clr += std::to_string((int)fg + 30) + ";";
- if (bg != previous.bg)
- clr += std::to_string((int)bg + 40) + ";";
- } else {
- clr += "0;";
- if (blink) {
- clr += "5;";
- }
-
- if (fg != COLOR::WHITE)
- clr += std::to_string((int)fg + 30) + ";";
- if (bg != COLOR::BLACK)
- clr += std::to_string((int)bg + 40) + ";";
- }
- } else {
-
- if (blink) {
- clr += "5;";
- }
- if (fg != previous.fg)
- clr += std::to_string((int)fg + 30) + ";";
- if (bg != previous.bg)
- clr += std::to_string((int)bg + 40) + ";";
- };
-
- std::string::iterator si = clr.end() - 1;
- if (*si == ';') {
- clr.erase(si);
- }
- if (clr.compare(CSI) == 0)
- clr.clear();
- else
- clr += "m";
-
- previous = *this;
- return clr;
- };
- std::ostream &operator<<(std::ostream &os, const ANSIColor &c) {
- std::string out;
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- out = c.output(d->previous);
-
- if (out.compare("\x1b[") == 0)
- std::abort();
- *d << out;
- d->track = true;
- } else {
-
-
- std::string out = c.output();
- os << out;
- }
- return os;
- }
- ANSIColor reset(ATTR::RESET);
- }
|