allow dfhack-run to output colors.

refactor Console-posix to avoid having a parameter equivalent to not calling the function at all.
develop
Ben Lubar 2020-03-06 15:02:03 -06:00
parent 885fa541fd
commit fa574cfbec
No known key found for this signature in database
GPG Key ID: 92939677AB59EDA4
5 changed files with 33 additions and 19 deletions

@ -79,8 +79,14 @@ set(MAIN_SOURCES
RemoteTools.cpp RemoteTools.cpp
) )
if(WIN32)
set(CONSOLE_SOURCES Console-windows.cpp)
else()
set(CONSOLE_SOURCES Console-posix.cpp)
endif()
set(MAIN_SOURCES_WINDOWS set(MAIN_SOURCES_WINDOWS
Console-windows.cpp ${CONSOLE_SOURCES}
Hooks-windows.cpp Hooks-windows.cpp
PlugLoad-windows.cpp PlugLoad-windows.cpp
Process-windows.cpp Process-windows.cpp
@ -92,21 +98,21 @@ if(WIN32)
endif() endif()
set(MAIN_SOURCES_LINUX set(MAIN_SOURCES_LINUX
Console-posix.cpp ${CONSOLE_SOURCES}
Hooks-linux.cpp Hooks-linux.cpp
PlugLoad-posix.cpp PlugLoad-posix.cpp
Process-linux.cpp Process-linux.cpp
) )
set(MAIN_SOURCES_DARWIN set(MAIN_SOURCES_DARWIN
Console-posix.cpp ${CONSOLE_SOURCES}
Hooks-darwin.cpp Hooks-darwin.cpp
PlugLoad-posix.cpp PlugLoad-posix.cpp
Process-darwin.cpp Process-darwin.cpp
) )
set(MAIN_SOURCES_LINUX_EGGY set(MAIN_SOURCES_LINUX_EGGY
Console-linux.cpp ${CONSOLE_SOURCES}
Hooks-egg.cpp Hooks-egg.cpp
PlugLoad-linux.cpp PlugLoad-linux.cpp
Process-linux.cpp Process-linux.cpp
@ -359,7 +365,7 @@ add_library(dfhack SHARED ${PROJECT_SOURCES})
add_dependencies(dfhack generate_proto_core) add_dependencies(dfhack generate_proto_core)
add_dependencies(dfhack generate_headers) add_dependencies(dfhack generate_headers)
add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS}) add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS} ${CONSOLE_SOURCES})
add_dependencies(dfhack-client dfhack) add_dependencies(dfhack-client dfhack)
add_executable(dfhack-run dfhack-run.cpp) add_executable(dfhack-run dfhack-run.cpp)

@ -827,21 +827,23 @@ Console::~Console()
delete d; delete d;
} }
bool Console::init(bool sharing) bool Console::init(bool dont_redirect)
{ {
if(sharing)
{
inited = false;
return false;
}
if (!freopen("stdout.log", "w", stdout))
;
d = new Private(); d = new Private();
// make our own weird streams so our IO isn't redirected // make our own weird streams so our IO isn't redirected
d->dfout_C = fopen("/dev/tty", "w"); if (dont_redirect)
{
d->dfout_C = fopen("/dev/stdout", "w");
}
else
{
if (!freopen("stdout.log", "w", stdout))
;
d->dfout_C = fopen("/dev/tty", "w");
}
std::cin.tie(this); std::cin.tie(this);
clear(); clear();
d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO); d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO);
// init the exit mechanism // init the exit mechanism
if (pipe(d->exit_pipe) == -1) if (pipe(d->exit_pipe) == -1)
; ;
@ -858,6 +860,8 @@ bool Console::shutdown(void)
return true; return true;
lock_guard <recursive_mutex> g(*wlock); lock_guard <recursive_mutex> g(*wlock);
close(d->exit_pipe[1]); close(d->exit_pipe[1]);
if (d->state != Private::con_lineedit)
inited = false;
return true; return true;
} }

@ -1707,7 +1707,6 @@ bool Core::Init()
} }
if (is_text_mode && !is_headless) if (is_text_mode && !is_headless)
{ {
con.init(true);
cerr << "Console is not available. Use dfhack-run to send commands.\n"; cerr << "Console is not available. Use dfhack-run to send commands.\n";
if (!is_text_mode) if (!is_text_mode)
{ {

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <string> #include <string>
#include <stdint.h> #include <stdint.h>
#include "Console.h"
#include "RemoteClient.h" #include "RemoteClient.h"
#include <cstdio> #include <cstdio>
@ -55,11 +56,10 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace DFHack; using namespace DFHack;
using namespace dfproto; using namespace dfproto;
using std::cout;
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
color_ostream_wrapper out(cout); Console out;
if (argc <= 1) if (argc <= 1)
{ {
@ -85,12 +85,15 @@ int main (int argc, char *argv[])
if (!client.connect()) if (!client.connect())
return 2; return 2;
out.init(true);
command_result rv; command_result rv;
if (strcmp(argv[1], "--lua") == 0) if (strcmp(argv[1], "--lua") == 0)
{ {
if (argc <= 3) if (argc <= 3)
{ {
out.shutdown();
fprintf(stderr, "Usage: dfhack-run --lua <module> <function> [args...]\n"); fprintf(stderr, "Usage: dfhack-run --lua <module> <function> [args...]\n");
return 2; return 2;
} }
@ -99,6 +102,7 @@ int main (int argc, char *argv[])
if (!run_call.bind(&client, "RunLua")) if (!run_call.bind(&client, "RunLua"))
{ {
out.shutdown();
fprintf(stderr, "No RunLua protocol function found."); fprintf(stderr, "No RunLua protocol function found.");
return 3; return 3;
} }
@ -130,6 +134,7 @@ int main (int argc, char *argv[])
} }
out.flush(); out.flush();
out.shutdown();
if (rv != CR_OK) if (rv != CR_OK)
return 1; return 1;

@ -135,7 +135,7 @@ namespace DFHack
///dtor, NOT thread-safe ///dtor, NOT thread-safe
~Console(); ~Console();
/// initialize the console. NOT thread-safe /// initialize the console. NOT thread-safe
bool init( bool sharing ); bool init( bool dont_redirect );
/// shutdown the console. NOT thread-safe /// shutdown the console. NOT thread-safe
bool shutdown( void ); bool shutdown( void );