utils.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "utils.h"
  2. #include "cli.h"
  3. #ifdef _WIN32
  4. #include <windows.h>
  5. void clearConsole() {
  6. COORD topLeft = { 0, 0 };
  7. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  8. CONSOLE_SCREEN_BUFFER_INFO screen;
  9. DWORD written;
  10. GetConsoleScreenBufferInfo(console, &screen);
  11. FillConsoleOutputCharacterA( console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written );
  12. FillConsoleOutputAttribute( console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
  13. screen.dwSize.X * screen.dwSize.Y, topLeft, &written );
  14. SetConsoleCursorPosition(console, topLeft);
  15. }
  16. #else // POSIX
  17. #include <iostream>
  18. void clearConsole() {std::clog << "\e[1;1H\e[2J";}
  19. #endif
  20. bool isSigned(std::string str) {
  21. std::string::const_iterator it = str.begin();
  22. if(*it == '-') ++it;
  23. while (it != str.end() && std::isdigit(*it)) ++it;
  24. return !str.empty() && it == str.end();
  25. }
  26. bool isUnsigned(std::string str) {
  27. std::string::const_iterator it = str.begin();
  28. while (it != str.end() && std::isdigit(*it)) ++it;
  29. return !str.empty() && it == str.end();
  30. }
  31. binom::i64 inputSigned(const char* msg, std::function<bool (binom::i64)> check_callbak) {
  32. std::string input;
  33. while(true) {
  34. std::clog << msg;
  35. std::getline(std::cin, input, '\n');
  36. if(!isSigned(input)) {
  37. std::clog << "Entered isn't number value!\n";
  38. continue;
  39. }
  40. binom::i64 number = std::stoll(input);
  41. if(!check_callbak(number)) continue;
  42. return number;
  43. }
  44. }
  45. binom::ui64 inputUnsigned(const char* msg, std::function<bool (binom::ui64)> check_callbak) {
  46. std::string input;
  47. while(true) {
  48. std::clog << msg;
  49. std::getline(std::cin, input, '\n');
  50. if(!isUnsigned(input)) {
  51. std::clog << "Entered isn't unsigned number value!\n";
  52. continue;
  53. }
  54. binom::ui64 number = std::stoull(input);
  55. if(!check_callbak(number)) continue;
  56. return number;
  57. }
  58. }
  59. bool inputBool(const char* msg) {
  60. std::string input;
  61. while (true) {
  62. std::clog << msg;
  63. std::getline(std::cin, input, '\n');
  64. if(input == "y") return true;
  65. elif(input == "n") return false;
  66. elif(input == "t") return true;
  67. elif(input == "f") return false;
  68. elif(input == "yes") return true;
  69. elif(input == "no") return false;
  70. elif(input == "true") return true;
  71. elif(input == "false") return false;
  72. else continue;
  73. }
  74. }