Files
OpenRCT2-Web/src/openrct2/interface/StdInOutConsole.cpp
T

163 lines
4.2 KiB
C++
Raw Normal View History

2018-06-15 14:07:34 +02:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
// Ignore isatty warning on WIN32
#ifndef _CRT_NONSTDC_NO_WARNINGS
# define _CRT_NONSTDC_NO_WARNINGS
#endif
#include "../Context.h"
2018-03-12 17:39:58 +00:00
#include "../OpenRCT2.h"
2018-03-12 18:00:35 +00:00
#include "../platform/Platform2.h"
#include "../scripting/ScriptEngine.h"
#include "InteractiveConsole.h"
2018-03-11 19:55:52 +00:00
2020-02-23 15:33:01 +00:00
#include <linenoise.hpp>
using namespace OpenRCT2;
2018-03-11 19:55:52 +00:00
void StdInOutConsole::Start()
{
2021-02-13 00:15:27 +00:00
// Only start if stdin/stdout is a TTY
if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
{
return;
}
2018-06-22 22:59:46 +02:00
std::thread replThread([this]() -> void {
2018-03-11 19:55:52 +00:00
linenoise::SetMultiLine(true);
linenoise::SetHistoryMaxLen(32);
std::string prompt = "\033[32mopenrct2 $\x1b[0m ";
2018-03-22 20:44:58 +00:00
bool lastPromptQuit = false;
2018-03-11 19:55:52 +00:00
while (true)
{
std::string line;
std::string left = prompt;
2021-02-13 00:15:27 +00:00
_isPromptShowing = true;
2018-03-11 19:55:52 +00:00
auto quit = linenoise::Readline(left.c_str(), line);
2021-02-13 00:15:27 +00:00
_isPromptShowing = false;
2018-03-22 20:44:58 +00:00
if (quit)
{
if (lastPromptQuit)
{
openrct2_finish();
break;
}
else
{
lastPromptQuit = true;
2021-02-13 00:15:27 +00:00
std::puts("(To exit, press ^C again)");
2018-03-22 20:44:58 +00:00
}
}
else
{
lastPromptQuit = false;
linenoise::AddHistory(line.c_str());
Eval(line).wait();
2018-03-11 19:55:52 +00:00
}
}
});
replThread.detach();
}
2018-06-22 22:59:46 +02:00
std::future<void> StdInOutConsole::Eval(const std::string& s)
2018-03-11 19:55:52 +00:00
{
#ifdef ENABLE_SCRIPTING
auto& scriptEngine = GetContext()->GetScriptEngine();
return scriptEngine.Eval(s);
2020-02-23 12:55:48 +00:00
#else
// Push on-demand evaluations onto a queue so that it can be processed deterministically
// on the main thead at the right time.
std::promise<void> barrier;
auto future = barrier.get_future();
_evalQueue.emplace(std::move(barrier), s);
return future;
#endif
}
void StdInOutConsole::ProcessEvalQueue()
{
#ifndef ENABLE_SCRIPTING
2020-02-23 12:55:48 +00:00
while (_evalQueue.size() > 0)
{
auto item = std::move(_evalQueue.front());
_evalQueue.pop();
auto promise = std::move(std::get<0>(item));
auto command = std::move(std::get<1>(item));
Execute(command);
// Signal the promise so caller can continue
promise.set_value();
}
#endif
2018-03-11 19:55:52 +00:00
}
void StdInOutConsole::Clear()
{
linenoise::linenoiseClearScreen();
}
void StdInOutConsole::Close()
{
openrct2_finish();
}
2020-10-16 00:13:52 +01:00
void StdInOutConsole::WriteLine(const std::string& s, FormatToken colourFormat)
2018-03-11 19:55:52 +00:00
{
std::string formatBegin;
2020-11-10 20:18:17 +00:00
switch (colourFormat)
2018-03-11 19:55:52 +00:00
{
2020-11-10 20:18:17 +00:00
case FormatToken::ColourRed:
formatBegin = "\033[31m";
break;
case FormatToken::ColourYellow:
formatBegin = "\033[33m";
break;
default:
break;
2018-03-11 19:55:52 +00:00
}
2021-02-13 00:15:27 +00:00
if (!Platform::IsColourTerminalSupported())
2018-03-11 19:55:52 +00:00
{
std::printf("%s\n", s.c_str());
2021-02-13 00:15:27 +00:00
std::fflush(stdout);
2018-03-11 19:55:52 +00:00
}
else
{
2021-02-13 00:15:27 +00:00
if (_isPromptShowing)
{
auto* mainString = s.c_str();
// If string contains \n, we need to replace with \r\n
std::string newString;
if (s.find('\n') != std::string::npos)
{
for (auto ch : s)
{
if (ch == '\n')
newString += "\r\n";
else
newString += ch;
}
mainString = newString.c_str();
}
std::printf("\r%s%s\x1b[0m\x1b[0K\r\n", formatBegin.c_str(), mainString);
2021-02-13 00:15:27 +00:00
std::fflush(stdout);
linenoise::linenoiseEditRefreshLine();
}
else
{
std::printf("%s%s\x1b[0m\n", formatBegin.c_str(), s.c_str());
std::fflush(stdout);
}
2018-03-11 19:55:52 +00:00
}
}