Add alias command to the interactive console

This commit is contained in:
cantyonion committed 2026-11-24 12:50:49 +08:00
1 parent 1497f114e6
commit 79ad7114b8
6 files changed
+131 -30

No files matched your search

+49 -8
View File
@@ -1,4 +1,5 @@
#include "ConsoleCommand.h"
#include <set>
#include <stdexcept>
#include <utility>
@@ -12,17 +13,57 @@ std::string_view Trim(std::string_view text)
if (first == std::string_view::npos) return {};
return text.substr(first, text.find_last_not_of(" \t\v\f") - first + 1);
}
}
ConsoleCommand ParseConsoleCommand(std::string_view line)
std::pair<std::string_view, std::string_view> SplitFirst(std::string_view text)
{
if (line.size() > 65536 || line.find_first_of("\r\n") != std::string_view::npos ||
line.find('\0') != std::string_view::npos)
const auto split = text.find_first_of(" \t\v\f");
return {text.substr(0, split), split == text.npos ? std::string_view{} : Trim(text.substr(split))};
}
}
ConsoleCommand ConsoleCommandParser::Parse(std::string_view line)
{
constexpr std::size_t maxLength = 65536;
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 {};
const auto split = line.find_first_of(" \t\v\f");
const auto verb = line.substr(0, split);
auto rest = split == std::string_view::npos ? std::string_view{} : Trim(line.substr(split));
auto [verb, rest] = SplitFirst(line);
std::set<std::string_view> expanded; // Views refer to stable map keys.
std::string buffer;
for (auto it = aliases.find(verb); it != aliases.end(); it = aliases.find(verb))
{
if (!expanded.insert(it->first).second)
throw std::runtime_error("<console>:1: alias cycle at " + it->first);
std::string next = it->second;
if (!rest.empty()) next.append(" ").append(rest);
if (next.size() > maxLength) throw std::runtime_error("<console>:1: expanded command exceeds 64 KiB");
buffer = std::move(next);
line = buffer;
const auto parts = SplitFirst(line);
verb = parts.first;
rest = parts.second;
if (verb == it->first) break; // Allow a shortcut such as "alias drop drop all".
}
if (verb == "alias")
{
const auto [name, body] = SplitFirst(rest);
if (name.empty())
{
std::string output;
for (const auto &[key, value] : aliases)
output += "alias " + key + " " + value + "\n";
return {ConsoleKind::Display, output.empty() ? "No aliases defined.\n" : std::move(output), {}};
}
if (body.empty())
{
const auto it = aliases.find(name);
if (it == aliases.end()) throw std::runtime_error("<console>:1: unknown alias: " + std::string(name));
return {ConsoleKind::Display, "alias " + it->first + " " + it->second + "\n", {}};
}
if (name == "alias") throw std::runtime_error("<console>:1: alias is a reserved name");
aliases.insert_or_assign(std::string(name), std::string(body));
return {};
}
if (verb == "run")
{
if (rest.size() >= 2 && ((rest.front() == '"' && rest.back() == '"') ||
@@ -42,7 +83,7 @@ ConsoleCommand ParseConsoleCommand(std::string_view line)
}
std::string ConsoleHelp()
{
return "Console: run <path.act> | tasks | help | quit (or exit)\n"
return "Console: alias [name [command args...]] | run <path.act> | tasks | help | quit (or exit)\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 the running action finishes or reaches wait.\n"