183 lines
3.9 KiB
C++
183 lines
3.9 KiB
C++
#include "Tools.hpp"
|
|
#include <string>
|
|
|
|
#ifdef __API_AVAILABLE_PLATFORM_macosx
|
|
#include <sys/ioctl.h>
|
|
#include <unistd.h>
|
|
#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;
|
|
}
|
|
|
|
#ifdef __API_AVAILABLE_PLATFORM_macosx
|
|
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{.height = size.ws_row, .width = size.ws_col};
|
|
}
|
|
#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();
|
|
} |