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
)
if(WIN32)
set(CONSOLE_SOURCES Console-windows.cpp)
else()
set(CONSOLE_SOURCES Console-posix.cpp)
endif()
set(MAIN_SOURCES_WINDOWS
Console-windows.cpp
${CONSOLE_SOURCES}
Hooks-windows.cpp
PlugLoad-windows.cpp
Process-windows.cpp
@ -92,21 +98,21 @@ if(WIN32)
endif()
set(MAIN_SOURCES_LINUX
Console-posix.cpp
${CONSOLE_SOURCES}
Hooks-linux.cpp
PlugLoad-posix.cpp
Process-linux.cpp
)
set(MAIN_SOURCES_DARWIN
Console-posix.cpp
${CONSOLE_SOURCES}
Hooks-darwin.cpp
PlugLoad-posix.cpp
Process-darwin.cpp
)
set(MAIN_SOURCES_LINUX_EGGY
Console-linux.cpp
${CONSOLE_SOURCES}
Hooks-egg.cpp
PlugLoad-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_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_executable(dfhack-run dfhack-run.cpp)

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

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

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

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