Skip to content

Commit

Permalink
Improved help command (Kenix3#653)
Browse files Browse the repository at this point in the history
* Remove character limit from ConsoleWindow messages

* Improve help command

* Update src/window/gui/ConsoleWindow.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Pepe20129 and github-actions[bot] authored Aug 1, 2024
1 parent 1e1ab37 commit 96cea7e
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/window/gui/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@ namespace Ship {
int32_t ConsoleWindow::HelpCommand(std::shared_ptr<Console> console, const std::vector<std::string>& args,
std::string* output) {
if (output) {
*output += "Commands:\n";
*output += "Commands:";
for (const auto& cmd : console->GetCommands()) {
*output += " - " + cmd.first + "\n";
*output += "\n - " + cmd.first + ": " + cmd.second.Description;

if (!cmd.second.Arguments.empty()) {
*output += "\n - Arguments:";
for (int i = 0; i < cmd.second.Arguments.size(); i += 1) {
const CommandArgument& argument = cmd.second.Arguments[i];

*output += "\n - Info=" + argument.Info;

if (argument.Type == ArgumentType::NUMBER) {
*output += " Type=Text";
} else if (argument.Type == ArgumentType::TEXT) {
*output += " Type=Number";
} else {
*output += " Type=Unknown";
}

if (argument.Optional) {
*output += " [Optional]";
}
}
}
}

return 0;
Expand Down Expand Up @@ -553,10 +574,23 @@ int ConsoleWindow::CallbackStub(ImGuiInputTextCallbackData* data) {

void ConsoleWindow::Append(const std::string& channel, spdlog::level::level_enum priority, const char* fmt,
va_list args) {
char buf[2048];
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
mLog[channel].push_back({ std::string(buf), priority });
// Determine the size of the formatted string
va_list argsCopy;
va_copy(argsCopy, args);
int size = vsnprintf(nullptr, 0, fmt, argsCopy);
va_end(argsCopy);

if (size < 0) {
SPDLOG_ERROR("Error during formatting.");
SendErrorMessage("There has been an error during formatting!");
return;
}

std::vector<char> buf(size + 1);
vsnprintf(buf.data(), buf.size(), fmt, args);

buf[buf.size() - 1] = 0;
mLog[channel].push_back({ std::string(buf.begin(), buf.end()), priority });
}

void ConsoleWindow::Append(const std::string& channel, spdlog::level::level_enum priority, const char* fmt, ...) {
Expand Down

0 comments on commit 96cea7e

Please sign in to comment.