Skip to content

Commit 25b4ddc

Browse files
committed
various
1 parent 28338bd commit 25b4ddc

10 files changed

Lines changed: 140 additions & 198 deletions

library/Console-posix.cpp

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -92,51 +92,6 @@ static int isUnsupportedTerm(void)
9292
return 0;
9393
}
9494

95-
const char * ANSI_CLS = "\033[2J";
96-
const char * ANSI_BLACK = "\033[22;30m";
97-
const char * ANSI_RED = "\033[22;31m";
98-
const char * ANSI_GREEN = "\033[22;32m";
99-
const char * ANSI_BROWN = "\033[22;33m";
100-
const char * ANSI_BLUE = "\033[22;34m";
101-
const char * ANSI_MAGENTA = "\033[22;35m";
102-
const char * ANSI_CYAN = "\033[22;36m";
103-
const char * ANSI_GREY = "\033[22;37m";
104-
const char * ANSI_DARKGREY = "\033[01;30m";
105-
const char * ANSI_LIGHTRED = "\033[01;31m";
106-
const char * ANSI_LIGHTGREEN = "\033[01;32m";
107-
const char * ANSI_YELLOW = "\033[01;33m";
108-
const char * ANSI_LIGHTBLUE = "\033[01;34m";
109-
const char * ANSI_LIGHTMAGENTA = "\033[01;35m";
110-
const char * ANSI_LIGHTCYAN = "\033[01;36m";
111-
const char * ANSI_WHITE = "\033[01;37m";
112-
const char * RESETCOLOR = "\033[0m";
113-
114-
const char * getANSIColor(const int c)
115-
{
116-
switch (c)
117-
{
118-
case -1: return RESETCOLOR; // HACK! :P
119-
case 0 : return ANSI_BLACK;
120-
case 1 : return ANSI_BLUE; // non-ANSI
121-
case 2 : return ANSI_GREEN;
122-
case 3 : return ANSI_CYAN; // non-ANSI
123-
case 4 : return ANSI_RED; // non-ANSI
124-
case 5 : return ANSI_MAGENTA;
125-
case 6 : return ANSI_BROWN;
126-
case 7 : return ANSI_GREY;
127-
case 8 : return ANSI_DARKGREY;
128-
case 9 : return ANSI_LIGHTBLUE; // non-ANSI
129-
case 10: return ANSI_LIGHTGREEN;
130-
case 11: return ANSI_LIGHTCYAN; // non-ANSI;
131-
case 12: return ANSI_LIGHTRED; // non-ANSI;
132-
case 13: return ANSI_LIGHTMAGENTA;
133-
case 14: return ANSI_YELLOW; // non-ANSI
134-
case 15: return ANSI_WHITE;
135-
default: return "";
136-
}
137-
}
138-
139-
14095
#ifdef HAVE_CUCHAR
14196
// Use u32string for GCC 6 and later and msvc to allow potable implementation
14297
using u32string = std::u32string;
@@ -331,10 +286,10 @@ namespace DFHack
331286
void color(Console::color_value index)
332287
{
333288
if(!rawmode)
334-
fprintf(dfout_C, "%s", getANSIColor(index));
289+
fprintf(dfout_C, "%s", Console::getANSIColor(index));
335290
else
336291
{
337-
const char * colstr = getANSIColor(index);
292+
const char * colstr = Console::getANSIColor(index);
338293
int lstr = strlen(colstr);
339294
if (::write(STDIN_FILENO,colstr,lstr) == -1)
340295
;
@@ -834,7 +789,7 @@ namespace DFHack
834789
};
835790
}
836791

837-
PosixConsole::PosixConsole() : Console(Console::Type::Posix)
792+
PosixConsole::PosixConsole() : Console(this)
838793
{
839794
d = nullptr;
840795
inited = false;
@@ -990,12 +945,6 @@ int PosixConsole::lineedit(const std::string & prompt, std::string & output, Com
990945
return ret;
991946
}
992947

993-
void PosixConsole::msleep (unsigned int msec)
994-
{
995-
if (msec > 1000) sleep(msec/1000000);
996-
usleep((msec % 1000000) * 1000);
997-
}
998-
999948
bool PosixConsole::hide()
1000949
{
1001950
//Warmist: don't know if it's possible...

library/Console-sdl.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,6 @@ namespace DFHack
203203

204204
return ret;
205205
}
206-
/// Position cursor at x,y. 1,1 = top left corner
207-
void gotoxy(int x, int y)
208-
{
209-
}
210-
211-
/// Set color (ANSI color number)
212-
/// Note: unimplemented because widgets share the same font atlas,
213-
/// which will change color for all widgets.
214-
void color(Console::color_value index)
215-
{
216-
}
217-
218-
/// Set color (ANSI color number)
219-
/// Note: unimplemented because widgets share the same font atlas,
220-
/// which will change color for all widgets.
221-
void reset_color(void)
222-
{
223-
}
224206

225207
/// Enable or disable the caret/cursor
226208
void cursor(bool enable = true)
@@ -232,7 +214,7 @@ namespace DFHack
232214
};
233215
}
234216

235-
SDLConsoleDriver::SDLConsoleDriver() : Console(Console::Type::SDL)
217+
SDLConsoleDriver::SDLConsoleDriver() : Console(this)
236218
{
237219
d = new Private();
238220
inited.store(false);
@@ -345,11 +327,6 @@ int SDLConsoleDriver::lineedit(const std::string & prompt, std::string & output,
345327
return d->lineedit(prompt,output,ch);
346328
}
347329

348-
void SDLConsoleDriver::msleep (unsigned int msec)
349-
{
350-
std::this_thread::sleep_for(std::chrono::milliseconds(msec));
351-
}
352-
353330
bool SDLConsoleDriver::hide()
354331
{
355332
d->con.hide_window();

library/Console-windows.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ namespace DFHack
409409
}
410410

411411

412-
WindowsConsole::WindowsConsole() : Console(Console::Type::Windows)
412+
WindowsConsole::WindowsConsole() : Console(this)
413413
{
414414
d = 0;
415415
wlock = 0;
@@ -598,11 +598,6 @@ int WindowsConsole::lineedit(const std::string & prompt, std::string & output, C
598598
return ret;
599599
}
600600

601-
void WindowsConsole::msleep (unsigned int msec)
602-
{
603-
Sleep(msec);
604-
}
605-
606601
bool WindowsConsole::hide()
607602
{
608603
ShowWindow( GetConsoleWindow(), SW_HIDE );

library/Console.cpp

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,86 @@
1-
#ifdef BUILD_DFHACK_CLIENT
2-
#include "Console.h"
3-
#else
1+
#include "Console.h"
2+
#ifndef BUILD_DFHACK_CLIENT
3+
#include "SDLConsoleDriver.h"
44
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
5+
#define PLATFORM_CONSOLE PosixConsole
56
#include "PosixConsole.h"
6-
#elif defined(_WIN32) || defined(_WIN64)
7+
#elif defined(_WIN32)
8+
#define PLATFORM_CONSOLE WindowsConsole
79
#include "WindowsConsole.h"
810
#endif
9-
#include "SDLConsoleDriver.h"
1011
#endif
1112

13+
#include <chrono>
14+
#include <thread>
15+
1216
using namespace DFHack;
1317

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+
1462
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));
3086
}

library/Core.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ bool Core::InitMainThread() {
17081708

17091709
perf_counters.reset();
17101710

1711-
if (con.get_type() == Console::Type::SDL
1711+
if (con.get_type() == ConsoleType::SDL
17121712
&& !con.as<SDLConsoleDriver>().init_sdl())
17131713
std::cerr << "SDLConsole: failed to initialize.\n";
17141714

@@ -2525,10 +2525,10 @@ void Core::setArmokTools(const std::vector<std::string> &tool_names) {
25252525

25262526
// returns true if the event is handled
25272527
bool Core::DFH_SDL_Event(SDL_Event* ev) {
2528-
uint32_t start_ms = p->getTickCount();
2529-
if (con.get_type() == Console::Type::SDL
2528+
if (con.get_type() == ConsoleType::SDL
25302529
&& con.as<SDLConsoleDriver>().sdl_event_hook(*ev))
25312530
return true;
2531+
uint32_t start_ms = p->getTickCount();
25322532
bool ret = doSdlInputEvent(ev);
25332533
perf_counters.incCounter(perf_counters.total_keybinding_ms, start_ms);
25342534
return ret;

library/SDLConsole_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ class CommandPipe {
22792279
});
22802280
}
22812281

2282-
void push(std::u32string s)
2282+
void push(const std::u32string& s)
22832283
{
22842284
{
22852285
std::scoped_lock lock(mutex_);
@@ -3174,7 +3174,7 @@ int set_draw_color(SDL_Renderer* renderer, const SDL_Color& color)
31743174
struct SDLConsole_pshare {
31753175
Property props;
31763176
std::weak_ptr<SDLConsole_impl> impl_weak;
3177-
std::thread::id render_thread_id;
3177+
std::thread::id render_thread_id{};
31783178
};
31793179

31803180
class SDLConsole_impl : public std::enable_shared_from_this<SDLConsole_impl> {

library/dfhack-run.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,29 @@ POSSIBILITY OF SUCH DAMAGE.
5454

5555
#include <memory>
5656

57-
#if defined(_WIN32) || defined(_WIN64)
57+
#if defined(_WIN32)
5858
#include <windows.h>
5959

60-
void enablevt100() {
60+
bool enableAnsiColors() {
6161
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
6262
if (hOut == INVALID_HANDLE_VALUE || hOut == NULL) {
63-
return;
63+
return false;
6464
}
6565
DWORD dwMode = 0;
6666
if (GetConsoleMode(hOut, &dwMode)) {
6767
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
6868
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
6969
SetConsoleMode(hOut, dwMode);
7070
}
71+
return true;
7172
}
73+
return false;
7274
}
7375

7476
const char* exeName = "Dwarf Fortress.exe";
7577
#else
7678
// Assume Posix
77-
void enablevt100() { return; }
79+
bool enableAnsiColors() { return true; }
7880
const char* exeName = "./dfhack";
7981
#endif
8082

@@ -85,6 +87,7 @@ int main (int argc, char *argv[])
8587
{
8688
auto conPtr = Console::makeConsole();
8789
Console& out = *conPtr;
90+
out.use_ansi_colors(enableAnsiColors());
8891

8992
if (argc <= 1)
9093
{
@@ -101,8 +104,6 @@ int main (int argc, char *argv[])
101104
return 2;
102105
}
103106

104-
enablevt100();
105-
106107
// Connect to DFHack
107108
RemoteClient client(&out);
108109
if (!client.connect())
@@ -125,7 +126,6 @@ int main (int argc, char *argv[])
125126

126127
if (!run_call.bind(&client, "RunLua"))
127128
{
128-
out.shutdown();
129129
fprintf(stderr, "No RunLua protocol function found.");
130130
return 3;
131131
}
@@ -157,7 +157,6 @@ int main (int argc, char *argv[])
157157
}
158158

159159
out.flush();
160-
out.shutdown();
161160

162161
if (rv != CR_OK)
163162
return 1;

0 commit comments

Comments
 (0)