Skip to content

Commit 6e7ed2a

Browse files
committed
sdlconsole: test color on Windows
1 parent a27af58 commit 6e7ed2a

5 files changed

Lines changed: 115 additions & 44 deletions

File tree

library/CMakeLists.txt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,24 @@ set(MAIN_SOURCES
119119
file(GLOB_RECURSE TEST_SOURCES
120120
LIST_DIRECTORIES false
121121
*test.cpp)
122-
list(APPEND TEST_SOURCES SDLConsole_impl.cpp)
122+
123+
if (DFHACK_SDL_CONSOLE)
124+
list(APPEND TEST_SOURCES SDLConsole_impl.cpp)
125+
endif()
123126
dfhack_test(dfhack-test "${TEST_SOURCES}")
124127

125-
if (NOT DFHACK_SDL_CONSOLE)
126-
if(WIN32)
127-
set(CONSOLE_SOURCES Console-windows.cpp Console.cpp)
128-
else()
129-
set(CONSOLE_SOURCES Console-posix.cpp Console.cpp)
130-
endif()
131-
set(DFCLIENT_CONSOLE_SOURCES ${CONSOLE_SOURCES})
128+
if(WIN32)
129+
set(CONSOLE_SOURCES Console-windows.cpp)
132130
else()
133-
if(WIN32)
134-
set(DFCLIENT_CONSOLE_SOURCES Console-windows.cpp Console.cpp)
135-
else()
136-
set(DFCLIENT_CONSOLE_SOURCES Console-posix.cpp Console.cpp)
137-
endif()
138-
set(CONSOLE_SOURCES ${DFCLIENT_CONSOLE_SOURCES})
131+
set(CONSOLE_SOURCES Console-posix.cpp)
132+
endif()
133+
list(APPEND CONSOLE_SOURCES Console.cpp)
134+
if (DFHACK_SDL_CONSOLE)
139135
list(APPEND CONSOLE_SOURCES Console-sdl.cpp SDLConsole_impl.cpp)
140136
endif()
141137

138+
set(DFCLIENT_CONSOLE_SOURCES Console.cpp)
139+
142140
set(MAIN_SOURCES_WINDOWS
143141
${CONSOLE_SOURCES}
144142
Hooks.cpp
@@ -387,8 +385,7 @@ add_dependencies(dfhack generate_proto_core)
387385
add_dependencies(dfhack generate_headers)
388386

389387
add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS} ${DFCLIENT_CONSOLE_SOURCES})
390-
# SDLConsole requires to be run with df
391-
target_compile_definitions(dfhack-client PUBLIC DISABLE_SDL_CONSOLE)
388+
target_compile_definitions(dfhack-client PUBLIC BUILD_DFHACK_CLIENT)
392389
add_dependencies(dfhack-client dfhack)
393390

394391
add_executable(dfhack-run dfhack-run.cpp)

library/Console-sdl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ bool SDLConsoleDriver::show()
372372
bool SDLConsoleDriver::sdl_event_hook(SDL_Event &e)
373373
{
374374
auto& con = d->con;
375+
//auto& con = SDLConsole::get_console();
375376
if (con.state.is_active()) [[likely]] {
376377
return con.sdl_event_hook(e);
377378
} else if (con.state.is_shutdown()) {

library/Console.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
#include "Console.h"
2-
#include "PosixConsole.h"
3-
#include "WindowsConsole.h"
4-
#include "SDLConsoleDriver.h"
1+
#ifdef BUILD_DFHACK_CLIENT
2+
#include "Console.h"
3+
#else
4+
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
5+
#include "PosixConsole.h"
6+
#elif defined(_WIN32) || defined(_WIN64)
7+
#include "WindowsConsole.h"
8+
#endif
9+
#include "SDLConsoleDriver.h"
10+
#endif
511

612
using namespace DFHack;
713

814
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>();
921

10-
// Return the Windows console
11-
#if defined(_WIN32) || defined(_WIN64)
12-
return std::make_unique<WindowsConsole>();
13-
14-
// Return Posix console if launched from a supported terminal
15-
#elif defined(__linux__) || defined(__unix__) || defined(__APPLE__)
16-
if (PosixConsole::is_supported())
17-
return std::make_unique<PosixConsole>();
18-
#endif
19-
20-
// Return the SDL console if not launched from dfhack_run
21-
#ifndef DISABLE_SDL_CONSOLE
22-
return std::make_unique<SDLConsoleDriver>();
23-
#endif
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
2427

25-
// Return the DUMMY do nothing console
26-
// Should not get here when the SDL console is available
27-
return std::make_unique<Console>();
28+
return std::make_unique<SDLConsoleDriver>();
29+
#endif
2830
}

library/dfhack-run.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ POSSIBILITY OF SUCH DAMAGE.
5454

5555
#include <memory>
5656

57+
#if defined(_WIN32) || defined(_WIN64)
58+
#include <windows.h>
59+
60+
void enablevt100() {
61+
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
62+
if (hOut == INVALID_HANDLE_VALUE || hOut == NULL) {
63+
return;
64+
}
65+
DWORD dwMode = 0;
66+
if (GetConsoleMode(hOut, &dwMode)) {
67+
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
68+
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
69+
SetConsoleMode(hOut, dwMode);
70+
}
71+
}
72+
}
73+
74+
const char* exeName = "Dwarf Fortress.exe";
75+
#else
76+
// Assume Posix
77+
void enablevt100() { return; }
78+
const char* exeName = "./dfhack";
79+
#endif
80+
5781
using namespace DFHack;
5882
using namespace dfproto;
5983

@@ -67,20 +91,18 @@ int main (int argc, char *argv[])
6791
fprintf(stderr, "Usage: dfhack-run <command> [args...]\n\n");
6892
fprintf(stderr, "Note: this command does not start DFHack; it is intended to connect\n"
6993
"to a running DFHack instance. If you were trying to start DFHack, run\n"
70-
#ifdef _WIN32
71-
" Dwarf Fortress.exe\n"
72-
#else
73-
" ./dfhack\n"
74-
#endif
94+
" %s\n"
7595
"or see the documentation in hack/docs/index.html for more help.\n"
76-
);
96+
, exeName);
7797
#ifdef _WIN32
7898
fprintf(stderr, "\nPress Enter to quit.\n");
7999
fgetc(stdin);
80100
#endif
81101
return 2;
82102
}
83103

104+
enablevt100();
105+
84106
// Connect to DFHack
85107
RemoteClient client(&out);
86108
if (!client.connect())

library/include/Console.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,67 @@ namespace DFHack
123123
class DFHACK_EXPORT Console : public color_ostream
124124
{
125125
public:
126+
const char * ANSI_CLS = "\033[2J";
127+
const char * ANSI_BLACK = "\033[22;30m";
128+
const char * ANSI_RED = "\033[22;31m";
129+
const char * ANSI_GREEN = "\033[22;32m";
130+
const char * ANSI_BROWN = "\033[22;33m";
131+
const char * ANSI_BLUE = "\033[22;34m";
132+
const char * ANSI_MAGENTA = "\033[22;35m";
133+
const char * ANSI_CYAN = "\033[22;36m";
134+
const char * ANSI_GREY = "\033[22;37m";
135+
const char * ANSI_DARKGREY = "\033[01;30m";
136+
const char * ANSI_LIGHTRED = "\033[01;31m";
137+
const char * ANSI_LIGHTGREEN = "\033[01;32m";
138+
const char * ANSI_YELLOW = "\033[01;33m";
139+
const char * ANSI_LIGHTBLUE = "\033[01;34m";
140+
const char * ANSI_LIGHTMAGENTA = "\033[01;35m";
141+
const char * ANSI_LIGHTCYAN = "\033[01;36m";
142+
const char * ANSI_WHITE = "\033[01;37m";
143+
const char * RESETCOLOR = "\033[0m";
144+
145+
126146
enum class Type {
127147
Posix,
128148
SDL,
129149
Windows,
130150
DUMMY
131151
};
132152

153+
const char * getANSIColor(const int c)
154+
{
155+
switch (c)
156+
{
157+
case -1: return RESETCOLOR; // HACK! :P
158+
case 0 : return ANSI_BLACK;
159+
case 1 : return ANSI_BLUE; // non-ANSI
160+
case 2 : return ANSI_GREEN;
161+
case 3 : return ANSI_CYAN; // non-ANSI
162+
case 4 : return ANSI_RED; // non-ANSI
163+
case 5 : return ANSI_MAGENTA;
164+
case 6 : return ANSI_BROWN;
165+
case 7 : return ANSI_GREY;
166+
case 8 : return ANSI_DARKGREY;
167+
case 9 : return ANSI_LIGHTBLUE; // non-ANSI
168+
case 10: return ANSI_LIGHTGREEN;
169+
case 11: return ANSI_LIGHTCYAN; // non-ANSI;
170+
case 12: return ANSI_LIGHTRED; // non-ANSI;
171+
case 13: return ANSI_LIGHTMAGENTA;
172+
case 14: return ANSI_YELLOW; // non-ANSI
173+
case 15: return ANSI_WHITE;
174+
default: return "";
175+
}
176+
}
177+
133178
protected:
134179
Type con_type{Type::DUMMY};
135180

136181
virtual void begin_batch() {};
137-
virtual void add_text(color_value color, const std::string &text) {};
182+
virtual void add_text(color_value color, const std::string &text) {
183+
std::cout << getANSIColor(color);
184+
std::cout << text;
185+
std::cout << RESETCOLOR;
186+
};
138187
virtual void end_batch() {};
139188
virtual void flush_proxy() {};
140189

0 commit comments

Comments
 (0)