|
1 | | -#ifdef BUILD_DFHACK_CLIENT |
2 | | - #include "Console.h" |
3 | | -#else |
| 1 | +#include "Console.h" |
| 2 | +#ifndef BUILD_DFHACK_CLIENT |
| 3 | + #include "SDLConsoleDriver.h" |
4 | 4 | #if defined(__linux__) || defined(__unix__) || defined(__APPLE__) |
| 5 | + #define PLATFORM_CONSOLE PosixConsole |
5 | 6 | #include "PosixConsole.h" |
6 | | - #elif defined(_WIN32) || defined(_WIN64) |
| 7 | + #elif defined(_WIN32) |
| 8 | + #define PLATFORM_CONSOLE WindowsConsole |
7 | 9 | #include "WindowsConsole.h" |
8 | 10 | #endif |
9 | | - #include "SDLConsoleDriver.h" |
10 | 11 | #endif |
11 | 12 |
|
| 13 | +#include <chrono> |
| 14 | +#include <thread> |
| 15 | + |
12 | 16 | using namespace DFHack; |
13 | 17 |
|
| 18 | +const char * ANSI_CLS = "\033[2J"; |
| 19 | +const char * ANSI_BLACK = "\033[22;30m"; |
| 20 | +const char * ANSI_RED = "\033[22;31m"; |
| 21 | +const char * ANSI_GREEN = "\033[22;32m"; |
| 22 | +const char * ANSI_BROWN = "\033[22;33m"; |
| 23 | +const char * ANSI_BLUE = "\033[22;34m"; |
| 24 | +const char * ANSI_MAGENTA = "\033[22;35m"; |
| 25 | +const char * ANSI_CYAN = "\033[22;36m"; |
| 26 | +const char * ANSI_GREY = "\033[22;37m"; |
| 27 | +const char * ANSI_DARKGREY = "\033[01;30m"; |
| 28 | +const char * ANSI_LIGHTRED = "\033[01;31m"; |
| 29 | +const char * ANSI_LIGHTGREEN = "\033[01;32m"; |
| 30 | +const char * ANSI_YELLOW = "\033[01;33m"; |
| 31 | +const char * ANSI_LIGHTBLUE = "\033[01;34m"; |
| 32 | +const char * ANSI_LIGHTMAGENTA = "\033[01;35m"; |
| 33 | +const char * ANSI_LIGHTCYAN = "\033[01;36m"; |
| 34 | +const char * ANSI_WHITE = "\033[01;37m"; |
| 35 | +const char * RESETCOLOR = "\033[0m"; |
| 36 | + |
| 37 | +const char * Console::getANSIColor(const int c) |
| 38 | +{ |
| 39 | + switch (c) |
| 40 | + { |
| 41 | + case -1: return RESETCOLOR; // HACK! :P |
| 42 | + case 0 : return ANSI_BLACK; |
| 43 | + case 1 : return ANSI_BLUE; // non-ANSI |
| 44 | + case 2 : return ANSI_GREEN; |
| 45 | + case 3 : return ANSI_CYAN; // non-ANSI |
| 46 | + case 4 : return ANSI_RED; // non-ANSI |
| 47 | + case 5 : return ANSI_MAGENTA; |
| 48 | + case 6 : return ANSI_BROWN; |
| 49 | + case 7 : return ANSI_GREY; |
| 50 | + case 8 : return ANSI_DARKGREY; |
| 51 | + case 9 : return ANSI_LIGHTBLUE; // non-ANSI |
| 52 | + case 10: return ANSI_LIGHTGREEN; |
| 53 | + case 11: return ANSI_LIGHTCYAN; // non-ANSI; |
| 54 | + case 12: return ANSI_LIGHTRED; // non-ANSI; |
| 55 | + case 13: return ANSI_LIGHTMAGENTA; |
| 56 | + case 14: return ANSI_YELLOW; // non-ANSI |
| 57 | + case 15: return ANSI_WHITE; |
| 58 | + default: return ""; |
| 59 | + } |
| 60 | +} |
| 61 | + |
14 | 62 | std::unique_ptr<Console> Console::makeConsole() { |
15 | | - #ifdef BUILD_DFHACK_CLIENT |
16 | | - return std::make_unique<Console>(); |
17 | | - #else |
18 | | - // Return the Windows console |
19 | | - #if defined(_WIN32) || defined(_WIN64) |
20 | | - return std::make_unique<WindowsConsole>(); |
21 | | - |
22 | | - // Return Posix console if launched from a supported terminal |
23 | | - #elif defined(__linux__) || defined(__unix__) || defined(__APPLE__) |
24 | | - if (PosixConsole::is_supported()) |
25 | | - return std::make_unique<PosixConsole>(); |
26 | | - #endif |
27 | | - |
28 | | - return std::make_unique<SDLConsoleDriver>(); |
29 | | - #endif |
| 63 | +#ifdef BUILD_DFHACK_CLIENT |
| 64 | + return std::make_unique<Console>(); |
| 65 | +#else |
| 66 | + if (PLATFORM_CONSOLE::is_supported()) |
| 67 | + return std::make_unique<PLATFORM_CONSOLE>(); |
| 68 | + |
| 69 | + return std::make_unique<SDLConsoleDriver>(); |
| 70 | +#endif |
| 71 | +} |
| 72 | + |
| 73 | +void Console::add_text(color_value color, const std::string &text) { |
| 74 | + if (use_ansi_colors_) { |
| 75 | + std::cout << getANSIColor(color); |
| 76 | + std::cout << text; |
| 77 | + std::cout << RESETCOLOR; |
| 78 | + } else { |
| 79 | + std::cout << text; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void Console::msleep (unsigned int msec) |
| 84 | +{ |
| 85 | + std::this_thread::sleep_for(std::chrono::milliseconds(msec)); |
30 | 86 | } |
0 commit comments