CConsoleHandler.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #define VCMI_DLL
  2. #include "stdafx.h"
  3. #include "CConsoleHandler.h"
  4. #include "boost/function.hpp"
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. HANDLE handleIn;
  8. HANDLE handleOut;
  9. #endif
  10. WORD defColor;
  11. void CConsoleHandler::setColor(int level)
  12. {
  13. WORD color;
  14. switch(level)
  15. {
  16. case -1:
  17. color = defColor;
  18. break;
  19. case 0:
  20. #ifdef _WIN32
  21. color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  22. #else
  23. color = "\x1b[1;40;32m";
  24. #endif
  25. break;
  26. case 1:
  27. #ifdef _WIN32
  28. color = FOREGROUND_RED | FOREGROUND_INTENSITY;
  29. #else
  30. color = "\x1b[1;40;31m";
  31. #endif
  32. break;
  33. case 2:
  34. #ifdef _WIN32
  35. color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  36. #else
  37. color = "\x1b[1;40;35m";
  38. #endif
  39. break;
  40. case 3:
  41. #ifdef _WIN32
  42. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  43. #else
  44. color = "\x1b[1;40;32m";
  45. #endif
  46. break;
  47. case 4:
  48. #ifdef _WIN32
  49. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  50. #else
  51. color = "\x1b[1;40;39m";
  52. #endif
  53. break;
  54. case 5:
  55. #ifdef _WIN32
  56. color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  57. #else
  58. color = "\x1b[0;40;39m";
  59. #endif
  60. break;
  61. default:
  62. color = defColor;
  63. break;
  64. }
  65. #ifdef _WIN32
  66. SetConsoleTextAttribute(handleOut,color);
  67. #else
  68. std::cout << color;
  69. #endif
  70. }
  71. int CConsoleHandler::run()
  72. {
  73. char buffer[5000];
  74. while(true)
  75. {
  76. std::cin.getline(buffer, 5000);
  77. if(cb && *cb)
  78. (*cb)(buffer);
  79. }
  80. return -1;
  81. }
  82. CConsoleHandler::CConsoleHandler()
  83. {
  84. #ifdef _WIN32
  85. handleIn = GetStdHandle(STD_INPUT_HANDLE);
  86. handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  87. CONSOLE_SCREEN_BUFFER_INFO csbi;
  88. GetConsoleScreenBufferInfo(handleOut,&csbi);
  89. defColor = csbi.wAttributes;
  90. #else
  91. defColor = "\x1b[0m";
  92. #endif
  93. cb = new boost::function<void(const std::string &)>;
  94. }
  95. CConsoleHandler::~CConsoleHandler()
  96. {
  97. delete cb;
  98. }
  99. #ifndef _WIN32
  100. void CConsoleHandler::killConsole(pthread_t hThread)
  101. #else
  102. void CConsoleHandler::killConsole(void *hThread)
  103. #endif
  104. {
  105. tlog3 << "Killing console... ";
  106. _kill_thread(hThread,0);
  107. tlog3 << "done!\n";
  108. }