Files
bilicraft-bot/src/ConsoleCommand.cpp
T

74 lines
3.2 KiB
C++

#include "ConsoleCommand.h"
#include <stdexcept>
#include <utility>
namespace Bilicraft
{
namespace
{
constexpr std::size_t maxLength = 65536;
constexpr std::string_view whitespace = " \t\v\f";
std::string_view Trim(std::string_view text)
{
const auto first = text.find_first_not_of(whitespace);
if (first == std::string_view::npos) return {};
return text.substr(first, text.find_last_not_of(whitespace) - first + 1);
}
std::pair<std::string_view, std::string_view> SplitFirst(std::string_view text)
{
const auto split = text.find_first_of(whitespace);
return {text.substr(0, split), split == text.npos ? std::string_view{} : Trim(text.substr(split))};
}
}
ConsoleCommand ConsoleCommandParser::Parse(std::string_view line)
{
if (line.size() > maxLength || line.find_first_of("\r\n") != line.npos || line.find('\0') != line.npos)
throw std::runtime_error("<console>:1: expected a single line of at most 64 KiB");
line = Trim(line.substr(0, line.find('#')));
if (line.empty()) return {};
std::string display;
std::string expanded;
try
{
expanded = bindings.Expand(line, maxLength, &display);
}
catch (const std::runtime_error &error)
{
throw std::runtime_error(std::string("<console>:1: ") + error.what());
}
if (!display.empty()) return {ConsoleKind::Display, std::move(display), {}};
if (expanded.empty()) return {};
line = expanded;
auto [verb, rest] = SplitFirst(line);
if (verb == "run")
{
if (rest.size() >= 2 && ((rest.front() == '"' && rest.back() == '"') ||
(rest.front() == '\'' && rest.back() == '\'')))
rest = rest.substr(1, rest.size() - 2);
if (rest.empty()) throw std::runtime_error("<console>:1: run requires a .act file path");
return {ConsoleKind::Run, std::string(rest), {}};
}
if (verb == "help" || verb == "tasks" || verb == "quit" || verb == "exit")
{
if (!rest.empty()) throw std::runtime_error("<console>:1: console control command takes no arguments");
return {verb == "help" ? ConsoleKind::Help : verb == "tasks" ? ConsoleKind::Tasks : ConsoleKind::Quit,
{}, {}};
}
auto action = ParseInteractiveCommand(line);
return {action ? ConsoleKind::Action : ConsoleKind::Empty, {}, std::move(action)};
}
std::string ConsoleHelp()
{
return "Console: alias [name [command args...]] | run <path.act> | tasks | help | quit (or exit)\n"
"define [name [value...]]: store session parameters; use $name as a whole argument.\n"
"Game commands: goto, rclick, lclick, grclick, glclick, sw, swb, drop, sendcmd, sendmsg\n"
"rclick/lclick: add entry [t|b|s|n|w|e] for item frames; omit face to require a unique frame.\n"
"A game command waits until completion, wait, or the next loop iteration.\n"
"Only one interactive game command can be pending. wait/import/do are only available in action files.\n"
"run validates and queues a file immediately; newest ready action runs first.\n"
"Native terminal: fixed input bar, Up/Down history, Left/Right/Home/End editing.\n"
"Ctrl+C/quit cancels; EOF drains queued tasks, then disconnects.\n";
}
} // namespace Bilicraft