1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402 |
- #include "door.h"
- #include <algorithm>
- #include <chrono>
- #include <ctype.h>
- #include <string.h>
- #include <string>
- #include <thread>
- #include <libgen.h> // basename
- #include <ctime>
- #include <iomanip>
- #include <signal.h>
- #include <unistd.h> //
- #include <iconv.h>
- #include <algorithm>
- #include <iostream>
- #include <pwd.h> // getpwuid
- namespace door {
- std::list<char> pushback;
- void to_lower(std::string &text) {
- transform(text.begin(), text.end(), text.begin(), ::tolower);
- }
- bool replace(std::string &str, const std::string &from, const std::string &to) {
- size_t start_pos = str.find(from);
- if (start_pos == std::string::npos)
- return false;
- str.replace(start_pos, from.length(), to);
- return true;
- }
- bool replace(std::string &str, const char *from, const char *to) {
- size_t start_pos = str.find(from);
- if (start_pos == std::string::npos)
- return false;
- str.replace(start_pos, strlen(from), to);
- return true;
- }
- static bool hangup = false;
- void sig_handler(int signal) {
- hangup = true;
-
-
- }
- class IConv {
- iconv_t ic;
- public:
- IConv(const char *to, const char *from);
- ~IConv();
- int convert(char *input, char *output, size_t outbufsize);
- };
- IConv::IConv(const char *to, const char *from) : ic(iconv_open(to, from)) {}
- IConv::~IConv() { iconv_close(ic); }
- int IConv::convert(char *input, char *output, size_t outbufsize) {
- size_t inbufsize = strlen(input);
-
-
-
- int r = iconv(ic, &input, &inbufsize, &output, &outbufsize);
- *output = 0;
- return r;
- }
- static IConv converter("UTF-8", "CP437");
- void cp437toUnicode(std::string input, std::string &out) {
- char buffer[10240];
- char output[16384];
- strcpy(buffer, input.c_str());
- converter.convert(buffer, output, sizeof(output));
- out.assign(output);
- }
- void cp437toUnicode(const char *input, std::string &out) {
- char buffer[10240];
- char output[16384];
- strcpy(buffer, input);
- converter.convert(buffer, output, sizeof(output));
- out.assign(output);
- }
- bool unicode = false;
- bool full_cp437 = false;
- bool debug_capture = false;
- Door::Door(std::string dname, int argc, char *argv[])
- : std::ostream(this), doorname{dname},
- has_dropfile{false}, debugging{false}, seconds_elapsed{0},
- previous(COLOR::WHITE), track{true}, cx{1}, cy{1},
- inactivity{120}, node{1} {
-
- opt.addUsage("Door++ library by BUGZ (C) 2021");
- opt.addUsage("");
- opt.addUsage(" -h --help Displays this help");
- opt.addUsage(" -l --local Local Mode");
- opt.addUsage(" -d --dropfile [FILENAME] Load Dropfile");
- opt.addUsage(" -n --node N Set node number");
-
-
- opt.addUsage(" -u --username NAME Set Username");
- opt.addUsage(" -t --timeleft N Set time left");
- opt.addUsage(" --maxtime N Set max time");
- opt.addUsage("");
- opt.setFlag("help", 'h');
- opt.setFlag("local", 'l');
- opt.setFlag("cp437", 'c');
- opt.setFlag("unicode");
- opt.setFlag("debuggering");
- opt.setOption("dropfile", 'd');
-
- opt.setOption("username", 'u');
- opt.setOption("timeleft", 't');
- opt.setOption("maxtime");
- opt.processCommandArgs(argc, argv);
- if (!opt.hasOptions()) {
- opt.printUsage();
- exit(1);
- }
- if (opt.getFlag("help") || opt.getFlag('h')) {
- opt.printUsage();
- exit(1);
- }
- if (opt.getValue("username") != nullptr) {
- username = opt.getValue("username");
- handle = username;
- }
- if (opt.getFlag("debuggering")) {
- debugging = true;
- }
- if (opt.getValue("node") != nullptr) {
- node = atoi(opt.getValue("node"));
- }
- if (opt.getValue("timeleft") != nullptr) {
- time_left = atoi(opt.getValue("timeleft"));
- } else {
-
- time_left = 25;
- }
- time_used = 0;
-
- std::string logFileName = dname + ".log";
- logf.open(logFileName.c_str(), std::ofstream::out | std::ofstream::app);
- parse_dropfile(opt.getValue("dropfile"));
-
- if (opt.getValue("maxtime") != nullptr) {
- int maxtime = atoi(opt.getValue("maxtime"));
- if (time_left > maxtime) {
- logf << "Adjusting time from " << time_left << " to " << maxtime
- << std::endl;
- time_left = maxtime;
- }
- }
- if (opt.getFlag("local")) {
- if (username.empty()) {
- uid_t uid = geteuid();
- struct passwd *pw;
- pw = getpwuid(uid);
- if (pw) {
-
- std::string name = pw->pw_gecos;
- size_t pos = name.find(',');
- username = pw->pw_name;
- if (pos != std::string::npos) {
- name = name.substr(0, pos);
- if (!name.empty()) {
- username = name;
- handle = pw->pw_name;
- }
- }
- logf << "username: " << username << " handle: " << handle << std::endl;
- } else {
- std::cout << "Local mode requires username to be set." << std::endl;
- opt.printUsage();
- exit(1);
- };
- }
-
-
- if (handle.empty())
- handle = username;
- } else {
-
- if (!has_dropfile) {
- std::cout << "I require a dropfile. And a shrubbery." << std::endl;
- opt.printUsage();
- exit(1);
- }
- }
-
- log() << "Door init" << std::endl;
- init();
-
- if (!debugging) {
- detect_unicode_and_screen();
- logf << "Screen " << width << " X " << height << " unicode " << unicode
- << " full_cp437 " << full_cp437 << std::endl;
- }
- if (opt.getFlag("cp437")) {
- unicode = false;
- }
- if (opt.getFlag("unicode")) {
- unicode = true;
- }
- }
- Door::~Door() {
-
-
- log() << "dtor" << std::endl;
- tcsetattr(STDIN_FILENO, TCOFLUSH, &tio_default);
- signal(SIGHUP, SIG_DFL);
- signal(SIGPIPE, SIG_DFL);
-
- stop_thread.set_value();
- time_thread.join();
- log() << "done" << std::endl;
- logf.close();
- }
- void Door::time_thread_run(std::future<void> future) {
- while (future.wait_for(std::chrono::milliseconds(1)) ==
- std::future_status::timeout) {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- ++seconds_elapsed;
- if (seconds_elapsed % 60 == 0) {
- if (time_left > 0)
- --time_left;
- ++time_used;
- }
- }
- }
- void Door::init(void) {
-
-
-
-
- struct termios tio_raw;
- tcgetattr(STDIN_FILENO, &tio_default);
- tio_raw = tio_default;
- cfmakeraw(&tio_raw);
-
- tio_raw.c_cc[VMIN] = 0;
- tio_raw.c_cc[VTIME] = 1;
- tcsetattr(STDIN_FILENO, TCSANOW, &tio_raw);
- startup = std::time(nullptr);
- signal(SIGHUP, sig_handler);
- signal(SIGPIPE, sig_handler);
-
- std::future<void> stop_future = stop_thread.get_future();
- time_thread =
- std::thread(&Door::time_thread_run, this, std::move(stop_future));
- }
- void Door::detect_unicode_and_screen(void) {
- unicode = false;
- full_cp437 = false;
- width = 0;
- height = 0;
- if (!isatty(STDIN_FILENO)) {
-
- *this << "\377\375\042\377\373\001";
- }
-
-
- *this << "\x1b[0;30;40m\x1b[2J\x1b[H";
- *this << "\x03\x04"
- << "\x1b[6n";
- *this << door::nl << "\u2615"
- << "\x1b[6n";
- *this << "\x1b[999C\x1b[999B\x1b[6n";
- *this << reset << "\x1b[2J\x1b[H";
- this->flush();
- usleep(1000 * 1000);
- if (haskey()) {
- char buffer[101];
- int len;
- len = read(STDIN_FILENO, &buffer, sizeof(buffer) - 1);
-
- if (len > 0) {
- buffer[len] = 0;
- int x;
- #ifdef DEBUG_OUTPUT
- for (x = 0; x < len; x++) {
- if (buffer[x] < 0x20)
- logf << std::hex << (int)buffer[x] << " ";
- else
- logf << buffer[x] << " ";
- }
- #endif
- for (x = 0; x < len; x++) {
- if (buffer[x] == 0)
- buffer[x] = ' ';
- }
- #ifdef DEBUG_OUTPUT
- logf << std::endl;
- logf << "BUFFER [" << (char *)buffer << "]" << std::endl;
- #endif
-
- if (1) {
- std::string cleanbuffer = buffer;
- std::string esc = "\x1b";
- std::string esc_text = "^[";
- while (replace(cleanbuffer, esc, esc_text)) {
- };
- logf << "BUFFER [" << cleanbuffer << "]" << std::endl;
- }
-
-
-
- if (((strstr(buffer, "1;1R") != nullptr) or
- (strstr(buffer, "1;3R") != nullptr)) and
- ((strstr(buffer, "2;2R") != nullptr) or
- (strstr(buffer, "2;3R") != nullptr))) {
- unicode = true;
- log() << "unicode enabled \u2615" << std::endl;
- } else {
- if (strstr(buffer, "1;3R") != nullptr) {
- full_cp437 = true;
- }
- }
-
- char *cp;
- cp = strrchr(buffer, '\x1b');
- if (cp != nullptr) {
- cp++;
- if (*cp == '[') {
- cp++;
- height = atoi(cp);
- cp = strchr(cp, ';');
- if (cp != nullptr) {
- cp++;
- width = atoi(cp);
- if (width > 900) {
-
- width = 0;
- height = 0;
- }
- } else {
- height = 0;
- }
- }
- }
- }
- } else {
- logf << "FAIL-WHALE, no response to terminal getposition." << std::endl;
- }
- }
- void Door::parse_dropfile(const char *filepath) {
- if (filepath == nullptr)
- return;
-
- std::ifstream file(filepath);
- std::string line;
- while (std::getline(file, line)) {
-
- if (!line.empty() && line[line.size() - 1] == '\r')
- line.erase(line.size() - 1);
- dropfilelines.push_back(line);
- }
- file.close();
- std::string filename;
- {
-
- char *temp = strdup(filepath);
- filename = basename(temp);
- free(temp);
- }
- to_lower(filename);
-
- if (filename == "door.sys") {
-
- node = atoi(dropfilelines[3].c_str());
- username = dropfilelines[9];
- location = dropfilelines[10];
- time_left = atoi(dropfilelines[18].c_str());
- sysop = dropfilelines[34];
- handle = dropfilelines[35];
- } else {
- if (filename == "door32.sys") {
-
-
-
-
-
- username = dropfilelines[4];
- handle = dropfilelines[5];
- time_left = atoi(dropfilelines[6].c_str());
-
- node = atoi(dropfilelines[8].c_str());
- } else {
- std::string msg = "Unknown dropfile: ";
- msg += filename;
- log() << msg << std::endl;
- *this << msg << std::endl;
- exit(2);
- }
- }
- log() << "node:" << node << " username: " << username << " handle: " << handle
- << " time: " << time_left << std::endl;
- has_dropfile = true;
- }
- ofstream &Door::log(void) {
- std::time_t t = std::time(nullptr);
- std::tm tm = *std::localtime(&t);
- logf << std::put_time(&tm, "%c ");
- return logf;
- }
- bool Door::haskey(void) {
- fd_set socket_set;
- struct timeval tv;
- int select_ret = -1;
- if (hangup)
- return -2;
- if (time_left < 2)
- return -3;
- while (select_ret == -1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO, &socket_set);
- tv.tv_sec = 0;
- tv.tv_usec = 1;
- select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
- if (select_ret == -1) {
- if (errno == EINTR)
- continue;
- log() << "hangup detected" << std::endl;
- hangup = true;
- return (-2);
- }
- if (select_ret == 0)
- return false;
- }
- return true;
- }
- signed int Door::getch(void) {
- fd_set socket_set;
- struct timeval tv;
- int select_ret = -1;
- int recv_ret;
- char key;
- if (door::hangup)
- return -2;
- if (time_left < 2)
- return -3;
- while (select_ret == -1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO, &socket_set);
-
-
- tv.tv_sec = 0;
- tv.tv_usec = 100;
-
- select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
-
- if (select_ret == -1) {
- if (errno == EINTR)
- continue;
- log() << "hangup detected" << std::endl;
- door::hangup = true;
- return (-2);
- }
- if (select_ret == 0)
- return (-1);
- }
- recv_ret = read(STDIN_FILENO, &key, 1);
- if (recv_ret != 1) {
-
- log() << "hangup" << std::endl;
- hangup = true;
- return -2;
- }
-
-
- return key;
- }
- signed int Door::getkey_or_pushback(void) {
- signed int c;
- if (!door::pushback.empty()) {
- c = door::pushback.front();
- door::pushback.pop_front();
- return c;
- }
- return getch();
- }
- signed int Door::getkey(void) {
- signed int c, c2;
- c = getkey_or_pushback();
- if (c < 0)
- return c;
-
- if (c == 0x0d) {
- c2 = getkey_or_pushback();
- if ((c2 != 0) and (c2 >= 0) and (c2 != 0x0a)) {
- log() << "got " << (int)c2 << " so stuffing it into unget buffer."
- << std::endl;
- door::pushback.push_front(c2);
- }
- return c;
- }
- if (c == 0) {
-
- int tries = 0;
- c2 = getkey_or_pushback();
- while (c2 < 0) {
- if (tries > 7) {
- log() << "ok, got " << c2 << " and " << tries << " so returning 0x00!"
- << std::endl;
- return c;
- }
- c2 = getkey_or_pushback();
- ++tries;
- }
- if (tries > 0) {
- log() << "tries " << tries << std::endl;
- }
- switch (c2) {
- case 0x50:
- return XKEY_DOWN_ARROW;
- case 0x48:
- return XKEY_UP_ARROW;
- case 0x4b:
- return XKEY_LEFT_ARROW;
- case 0x4d:
- return XKEY_RIGHT_ARROW;
- case 0x47:
- return XKEY_HOME;
- case 0x4f:
- return XKEY_END;
- case 0x49:
- return XKEY_PGUP;
- case 0x51:
- return XKEY_PGDN;
- case 0x3b:
- return XKEY_F1;
- case 0x3c:
- return XKEY_F2;
- case 0x3d:
- return XKEY_F3;
- case 0x3e:
- return XKEY_F4;
- case 0x3f:
- return XKEY_F5;
- case 0x40:
- return XKEY_F6;
- case 0x41:
- return XKEY_F7;
- case 0x42:
- return XKEY_F8;
- case 0x43:
- return XKEY_F9;
- case 0x44:
- return XKEY_F10;
-
- case 0x52:
- return XKEY_INSERT;
- case 0x53:
- return XKEY_DELETE;
- }
- logf << "\r\nDEBUG:\r\n0x00 + 0x" << std::hex << (int)c2;
- logf << "\r\n";
- logf.flush();
- }
- if (c == 0x1b) {
-
- c2 = getkey_or_pushback();
- if (c2 < 0) {
-
- return c;
- }
-
- char extended[16];
- unsigned int pos = 0;
- extended[pos] = (char)c2;
- extended[pos + 1] = 0;
- pos++;
- while ((pos < sizeof(extended) - 1) and ((c2 = getch()) >= 0)) {
-
-
- if (c2 == 0x1b) {
- door::pushback.push_front(c2);
- break;
- }
- extended[pos] = (char)c2;
- extended[pos + 1] = 0;
- pos++;
- }
-
- if (extended[0] == '[') {
- switch (extended[1]) {
- case 'A':
- return XKEY_UP_ARROW;
- case 'B':
- return XKEY_DOWN_ARROW;
- case 'C':
- return XKEY_RIGHT_ARROW;
- case 'D':
- return XKEY_LEFT_ARROW;
- case 'H':
- return XKEY_HOME;
- case 'F':
- return XKEY_END;
- case 'K':
- return XKEY_END;
- case 'U':
- return XKEY_PGUP;
- case 'V':
- return XKEY_PGDN;
- case '@':
- return XKEY_INSERT;
- }
- if (extended[pos - 1] == '~') {
-
- int number = atoi(extended + 1);
- switch (number) {
- case 2:
- return XKEY_INSERT;
- case 3:
- return XKEY_DELETE;
- case 5:
- return XKEY_PGUP;
- case 6:
- return XKEY_PGDN;
- case 15:
- return XKEY_F5;
- case 17:
- return XKEY_F6;
- case 18:
- return XKEY_F7;
- case 19:
- return XKEY_F8;
- case 20:
- return XKEY_F9;
- case 21:
- return XKEY_F10;
- case 23:
- return XKEY_F11;
- case 24:
- return XKEY_F12;
- }
- }
- }
- if (extended[0] == 'O') {
- switch (extended[1]) {
- case 'P':
- return XKEY_F1;
- case 'Q':
- return XKEY_F2;
- case 'R':
- return XKEY_F3;
- case 'S':
- return XKEY_F4;
- case 't':
- return XKEY_F5;
- }
- }
-
- logf << "\r\nDEBUG:\r\nESC + ";
- for (unsigned int x = 0; x < pos; x++) {
- char z = extended[x];
- if (iscntrl(z)) {
- logf << (int)z << " ";
- } else {
- logf << "'" << (char)z << "'"
- << " ";
- };
- }
- logf << "\r\n";
- logf.flush();
- return XKEY_UNKNOWN;
- }
- return c;
- }
- signed int Door::sleep_key(int secs) {
- fd_set socket_set;
- struct timeval tv;
- int select_ret = -1;
-
- if (hangup)
- return -2;
- if (time_left < 2)
- return -3;
- while (select_ret == -1) {
- FD_ZERO(&socket_set);
- FD_SET(STDIN_FILENO, &socket_set);
- tv.tv_sec = secs;
- tv.tv_usec = 0;
- select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
-
- if (select_ret == -1) {
- if (errno == EINTR)
- continue;
- hangup = true;
- log() << "hangup detected" << std::endl;
- return (-2);
- }
- if (select_ret == 0)
- return (-1);
- }
- return getkey();
- }
- std::string Door::input_string(int max) {
- std::string input;
-
- *this << std::string(max, ' ');
- *this << std::string(max, '\x08');
- int c;
- while (true) {
- c = sleep_key(inactivity);
- if (c < 0) {
- input.clear();
- return input;
- }
- if (c > 0x1000)
- continue;
- if (isprint(c)) {
- if (int(input.length()) < max) {
- *this << char(c);
- input.append(1, c);
- } else {
-
- *this << '\x07';
- }
- } else {
- switch (c) {
- case 0x08:
- case 0x7f:
- if (input.length() > 0) {
- *this << "\x08 \x08";
-
- input.erase(input.length() - 1);
- };
- break;
- case 0x0d:
- return input;
- }
- }
- }
- }
- int Door::get_one_of(const char *keys) {
- int c;
- while (true) {
- c = sleep_key(inactivity);
- if (c < 0)
- return c;
- if (c > 0x1000)
- continue;
- const char *key = strchr(keys, (char)toupper(c));
- if (key != nullptr) {
- return *key;
- }
- *this << '\x07';
- }
- return c;
- }
- std::streamsize Door::xsputn(const char *s, std::streamsize n) {
- if (debug_capture) {
- debug_buffer.append(s, n);
- } else {
- static std::string buffer;
- buffer.append(s, n);
-
- if (!hangup) {
- std::cout << buffer;
- std::cout.flush();
- }
-
- if (track)
- cx += n;
- buffer.clear();
- }
- return n;
- }
- int Door::overflow(int c) {
- if (debug_capture) {
- debug_buffer.append(1, (char)c);
- } else {
- if (!hangup) {
- std::cout << (char)c;
- std::cout.flush();
- }
- }
- if (track)
- cx++;
-
- return c;
- }
- ColorOutput::ColorOutput() : c(COLOR::BLACK, COLOR::BLACK) {
- pos = 0;
- len = 0;
- }
- void ColorOutput::reset(void) {
- pos = 0;
- len = 0;
- }
- Render::Render(const std::string txt) : text{txt} {}
- void Render::output(std::ostream &os) {
- for (auto out : outputs) {
- std::string fragment = text.substr(out.pos, out.len);
- os << out.c << fragment;
- }
- }
- void Render::append(ANSIColor color, int len) {
- if (outputs.empty()) {
- ColorOutput co;
- co.c = color;
- co.pos = 0;
- co.len = len;
- outputs.push_back(co);
- return;
- }
- ColorOutput ¤t = outputs.back();
- if (current.c == color) {
- current.len += len;
- return;
- }
-
-
- ColorOutput co;
- co.pos = current.pos + current.len;
- co.c = color;
- co.len = len;
- outputs.push_back(co);
- }
- Clrscr::Clrscr() {}
- std::ostream &operator<<(std::ostream &os, const Clrscr &clr) {
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- *d << "\x1b[2J"
- "\x1b[H";
- d->cx = 1;
- d->cy = 1;
- d->track = true;
- } else {
- os << "\x1b[2J"
- "\x1b[H";
- }
- return os;
- }
- Clrscr cls;
- NewLine::NewLine() {}
- std::ostream &operator<<(std::ostream &os, const NewLine &nl) {
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- *d << "\r\n";
- d->cx = 1;
- d->cy++;
- d->track = true;
- } else {
- os << "\r\n";
- };
- return os;
- }
- NewLine nl;
- Goto::Goto(int xpos, int ypos) {
- x = xpos;
- y = ypos;
- }
- void Goto::set(int xpos, int ypos) {
- x = xpos;
- y = ypos;
- }
- std::ostream &operator<<(std::ostream &os, const Goto &g) {
- Door *d = dynamic_cast<Door *>(&os);
- if (d != nullptr) {
- d->track = false;
- *d << "\x1b[";
- if (g.y > 1)
- *d << std::to_string(g.y);
- if (g.x > 1) {
- os << ";";
- *d << std::to_string(g.x);
- }
- *d << "H";
- d->cx = g.x;
- d->cy = g.y;
- d->track = true;
- } else {
- os << "\x1b[" << std::to_string(g.y) << ";" << std::to_string(g.x) << "H";
- };
- return os;
- }
- const char SaveCursor[] = "\x1b[s";
- const char RestoreCursor[] = "\x1b[u";
- renderFunction rBlueYellow = [](const std::string &txt) -> Render {
- Render r(txt);
- ANSIColor blue(COLOR::BLUE, ATTR::BOLD);
- ANSIColor cyan(COLOR::YELLOW, ATTR::BOLD);
- for (char const &c : txt) {
- if (isupper(c))
- r.append(blue);
- else
- r.append(cyan);
- }
- return r;
- };
- }
|