#include "Tools.hpp" #include #if defined __APPLE__ || defined __linux__ #include #include #endif #if defined _WIN64 || defined _WIN32 #include #include #endif using namespace ConsoleColorTool; setoutputcolor::setoutputcolor(ConsoleColors _color) : color(_color), bold(false){}; setoutputcolor::setoutputcolor(bool _bold) : color(clear), bold(_bold){}; setoutputcolor::setoutputcolor(ConsoleColors _color, bool _bold) : color(_color), bold(_bold){}; std::ostream &operator<<(std::ostream &out, setoutputcolor setOutput) { out << "\033["; if (setOutput.color == clear) { out << "0m"; return out; } if (setOutput.bold) { out << "1"; } else { out << "0"; } switch (setOutput.color) { case black: out << ";30m"; break; case red: out << ";31m"; break; case green: out << ";32m"; break; case brown: out << ";33m"; break; case blue: out << ";34m"; break; case magenta: out << ";35m"; break; case cyan: out << ";36m"; break; case lightGray: out << ";37m"; break; default: out << "m\033[0m"; } return out; } std::ostream &operator<<(std::ostream &out, setbgcolor setbg) { switch (setbg.color) { case black: out << "\033[7;30m"; break; case red: out << "\033[7;31m"; break; case green: out << "\033[7;32m"; break; case brown: out << "\033[7;33m"; break; case blue: out << "\033[7;34m"; break; case magenta: out << "\033[7;35m"; break; case cyan: out << "\033[7;36m"; break; case lightGray: out << "\033[7;37m"; break; default: // clang-format off {;} // clang-format on } return out; } setbgcolor::setbgcolor(ConsoleColors _color) : color(_color){}; std::ostream &resetOutputColor(std::ostream &out) { out << setoutputcolor(clear); return out; } const std::string cutToLength(const std::string str, const int length) { if (str.length() <= length) { return str; } else { return str.substr(0, length - 3) + "..."; } } const std::string setMiddle(const std::string str, const int targetLength, char fillChar) { if (str.length() >= targetLength) { return cutToLength(str, targetLength); } int left, right; right = (targetLength - str.length()) / 2; left = targetLength - str.length() - right; std::string result; for (int i = 0; i < left; i++) { result += fillChar; } result += str; for (int i = 0; i < right; i++) { result += fillChar; } return result; } #if defined __APPLE__ || defined __linux__ void clearScreen() { system("clear"); } // May be useful on linux. Does not work on mac OS. // void disableEchoBack() { // termios term; // tcgetattr(STDIN_FILENO, &term); // termios newTerm = term; // newTerm.c_lflag &= -ECHO; // tcsetattr(STDIN_FILENO, TCSANOW, &newTerm); // } // void enableEchoBack() { // termios term; // tcgetattr(STDIN_FILENO, &term); // term.c_lflag &= ECHO; // tcsetattr(STDIN_FILENO, TCSANOW, &term); // } void disableEchoBack() { system("stty -echo"); system("stty raw"); } void enableEchoBack() { system("stty echo"); system("stty cooked"); } const termSize getConsoleSize() { winsize size; ioctl(STDOUT_FILENO, TIOCGWINSZ, &size); return termSize{.width = size.ws_col, .height = size.ws_row}; } #endif #if defined _WIN64 || defined _WIN32 void clearScreen() { std::cout << "\x1B[2J\x1B[H" << std::flush; } void disableEchoBack() { HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode = 0; GetConsoleMode(hStdin, &mode); SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT) & (~ENABLE_LINE_INPUT) & (~ENABLE_PROCESSED_INPUT)); } void enableEchoBack() { HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode = 0; GetConsoleMode(hStdin, &mode); SetConsoleMode(hStdin, mode | (ENABLE_ECHO_INPUT) | (ENABLE_LINE_INPUT) | (ENABLE_PROCESSED_INPUT)); } const termSize getConsoleSize() { CONSOLE_SCREEN_BUFFER_INFO scrInfo; HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdout, &scrInfo); return termSize{.width = scrInfo.dwSize.X, .height = scrInfo.dwSize.Y}; } #endif void backSpace(int num) { for (int i = 0; i < num; i++) { std::cout << "\033[D"; } std::cout << std::string(num, ' '); for (int i = 0; i < num; i++) { std::cout << "\033[D"; } } void waitForAnyInput() { std::cout << "Press any key to continue..." << std::flush; disableEchoBack(); std::cin.get(); enableEchoBack(); }