From 96cea7ec304ce12782dd8893b54743d9641d5d13 Mon Sep 17 00:00:00 2001 From: Pepe20129 <72659707+Pepe20129@users.noreply.github.com> Date: Fri, 2 Aug 2024 00:46:11 +0200 Subject: [PATCH] Improved help command (#653) * 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> --- src/window/gui/ConsoleWindow.cpp | 46 +++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/window/gui/ConsoleWindow.cpp b/src/window/gui/ConsoleWindow.cpp index bf43477ed..53c17f9ce 100644 --- a/src/window/gui/ConsoleWindow.cpp +++ b/src/window/gui/ConsoleWindow.cpp @@ -11,9 +11,30 @@ namespace Ship { int32_t ConsoleWindow::HelpCommand(std::shared_ptr console, const std::vector& 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; @@ -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 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, ...) {