98 lines
2.5 KiB
C++
98 lines
2.5 KiB
C++
#pragma once
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
// Reference: https://cplusplus.com/forum/unices/36461/
|
|
// Reference: https://isocpp.org/wiki/faq/input-output#turn-off-tty-echo
|
|
// Reference:
|
|
// https://forums.codeguru.com/showthread.php?466009-Reading-from-stdin-(without-echo)
|
|
|
|
namespace ConsoleColorTool {
|
|
enum ConsoleColors {
|
|
black,
|
|
red,
|
|
green,
|
|
brown,
|
|
blue,
|
|
magenta,
|
|
cyan,
|
|
lightGray,
|
|
clear
|
|
};
|
|
};
|
|
|
|
class setoutputcolor {
|
|
private:
|
|
ConsoleColorTool::ConsoleColors color;
|
|
bool bold;
|
|
friend std::ostream &operator<<(std::ostream &out,
|
|
setoutputcolor setOutput);
|
|
|
|
public:
|
|
setoutputcolor(ConsoleColorTool::ConsoleColors _color);
|
|
setoutputcolor(bool bold);
|
|
setoutputcolor(ConsoleColorTool::ConsoleColors _color, bool bold);
|
|
};
|
|
|
|
class setbgcolor {
|
|
private:
|
|
ConsoleColorTool::ConsoleColors color;
|
|
friend std::ostream &operator<<(std::ostream &out, setbgcolor setbg);
|
|
|
|
public:
|
|
setbgcolor(ConsoleColorTool::ConsoleColors _color);
|
|
};
|
|
|
|
std::ostream &resetOutputColor(std::ostream &out);
|
|
|
|
// If the string is shorter than length, returns the string; if the string
|
|
// is longer than length, takes the first lenght - 1 letters and add a "+".
|
|
const std::string cutToLength(const std::string str, const int length);
|
|
|
|
const std::string setMiddle(const std::string str, const int targetLength,
|
|
char fillChar = ' ');
|
|
|
|
void clearScreen();
|
|
|
|
// Disables user input to be echoed back to the console. Operating system
|
|
// specific.
|
|
void disableEchoBack();
|
|
|
|
// Enables user input to be echoed back to the console. Operating system
|
|
// specific
|
|
void enableEchoBack();
|
|
|
|
// Go back num characters, print num spaces, and then go back num spaces, so
|
|
// that the previous num characters are removed. Operating system specific
|
|
// (maybe?)
|
|
void backSpace(int num);
|
|
|
|
struct termSize {
|
|
int width;
|
|
int height;
|
|
};
|
|
|
|
// Return the size of the command line terminal
|
|
const termSize getConsoleSize();
|
|
|
|
void waitForAnyInput();
|
|
|
|
template <typename T>
|
|
T safeInputNum(
|
|
std::string notValidNumberWarning = "Not a valid number, try again.\n",
|
|
std::function<bool(const T &)> const &isValidNumberFunc =
|
|
[](const T &) -> bool { return true; }) {
|
|
std::string temp;
|
|
T result;
|
|
|
|
while (true) {
|
|
std::getline(std::cin, temp);
|
|
std::stringstream tempStrStream(temp);
|
|
if (tempStrStream >> result && isValidNumberFunc(result)) {
|
|
break;
|
|
}
|
|
std::cout << notValidNumberWarning << std::flush;
|
|
}
|
|
|
|
return result;
|
|
} |