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

96 lines
2.5 KiB
C++
Raw Normal View History

2018-06-15 14:07:34 +02:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 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.
*****************************************************************************/
#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"
2019-07-23 23:21:42 +01:00
#include "../thirdparty/linenoise.hpp"
#include "InteractiveConsole.h"
2018-03-11 19:55:52 +00:00
using namespace OpenRCT2;
2018-03-11 19:55:52 +00:00
void StdInOutConsole::Start()
{
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;
auto quit = linenoise::Readline(left.c_str(), line);
2018-03-22 20:44:58 +00:00
if (quit)
{
if (lastPromptQuit)
{
openrct2_finish();
break;
}
else
{
lastPromptQuit = true;
std::puts("(To exit, press ^C again or type exit)");
}
}
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
{
auto& scriptEngine = GetContext()->GetScriptEngine();
return scriptEngine.Eval(s);
2018-03-11 19:55:52 +00:00
}
void StdInOutConsole::Clear()
{
linenoise::linenoiseClearScreen();
}
void StdInOutConsole::Close()
{
openrct2_finish();
}
2018-06-22 22:59:46 +02:00
void StdInOutConsole::WriteLine(const std::string& s, uint32_t colourFormat)
2018-03-11 19:55:52 +00:00
{
std::string formatBegin;
if (colourFormat != FORMAT_WINDOW_COLOUR_2)
{
switch (colourFormat)
{
case FORMAT_RED:
formatBegin = "\033[31m";
break;
case FORMAT_YELLOW:
formatBegin = "\033[33m";
break;
}
}
2018-03-12 18:00:35 +00:00
if (formatBegin.empty() || !Platform::IsColourTerminalSupported())
2018-03-11 19:55:52 +00:00
{
std::printf("%s\n", s.c_str());
}
else
{
std::printf("%s%s%s\n", formatBegin.c_str(), s.c_str(), "\x1b[0m");
}
}