From 3691363b8e9008793cca6890576a694fa6a48252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Fri, 15 Mar 2024 19:28:37 +0100 Subject: [PATCH] Parsing errors to to status in b_manage --- src/manage/CommandParser.cpp | 39 ++++++++++++++++++------------------ src/manage/CommandParser.h | 5 +++-- src/manage/ManageResults.cpp | 20 ++++++++++++------ src/manage/b_manage.cpp | 2 +- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/manage/CommandParser.cpp b/src/manage/CommandParser.cpp index 9e83644..47bcb26 100644 --- a/src/manage/CommandParser.cpp +++ b/src/manage/CommandParser.cpp @@ -6,11 +6,8 @@ #include "common/Utils.h" namespace platform { - void CommandParser::messageError(const std::string& message) - { - std::cout << Colors::RED() << message << Colors::RESET() << std::endl; - } - std::pair CommandParser::parse(const std::string& color, const std::vector>& options, const char defaultCommand, const int minIndex, const int maxIndex) + + std::tuple CommandParser::parse(const std::string& color, const std::vector>& options, const char defaultCommand, const int minIndex, const int maxIndex) { bool finished = false; while (!finished) { @@ -29,16 +26,17 @@ namespace platform { oss << "): "; std::cout << oss.str(); getline(std::cin, line); - std::cout << Colors::RESET(); line = trim(line); - if (line.size() == 0) - continue; + if (line.size() == 0) { + errorMessage = "No command"; + return { defaultCommand, 0, true }; + } if (all_of(line.begin(), line.end(), ::isdigit)) { command = defaultCommand; index = stoi(line); if (index > maxIndex || index < minIndex) { - messageError("Index out of range"); - continue; + errorMessage = "Index out of range"; + return { command, index, true }; } finished = true; break; @@ -53,24 +51,24 @@ namespace platform { if (std::get(option)) { // The option requires a value if (line.size() == 0) { - messageError("Option " + std::get(option) + " requires a value"); - break; + errorMessage = "Option " + std::get(option) + " requires a value"; + return { command, index, true }; } try { index = stoi(line); if (index > maxIndex || index < 0) { - messageError("Index out of range"); - break; + errorMessage = "Index out of range"; + return { command, index, true }; } } catch (const std::invalid_argument& ia) { - messageError("Invalid value: " + line); - break; + errorMessage = "Invalid value: " + line; + return { command, index, true }; } } else { if (line.size() > 0) { - messageError("option " + std::get(option) + " doesn't accept values"); - break; + errorMessage = "option " + std::get(option) + " doesn't accept values"; + return { command, index, true }; } } command = std::get(option); @@ -79,9 +77,10 @@ namespace platform { } } if (!found) { - messageError("I don't know " + line); + errorMessage = "I don't know " + line; + return { command, index, true }; } } - return { command, index }; + return { command, index, false }; } } /* namespace platform */ \ No newline at end of file diff --git a/src/manage/CommandParser.h b/src/manage/CommandParser.h index 8bbbc5e..ac6f21d 100644 --- a/src/manage/CommandParser.h +++ b/src/manage/CommandParser.h @@ -8,11 +8,12 @@ namespace platform { class CommandParser { public: CommandParser() = default; - std::pair parse(const std::string& color, const std::vector>& options, const char defaultCommand, const int minIndex, const int maxIndex); + std::tuple parse(const std::string& color, const std::vector>& options, const char defaultCommand, const int minIndex, const int maxIndex); char getCommand() const { return command; }; int getIndex() const { return index; }; + std::string getErrorMessage() const { return errorMessage; }; private: - void messageError(const std::string& message); + std::string errorMessage; char command; int index; }; diff --git a/src/manage/ManageResults.cpp b/src/manage/ManageResults.cpp index 8f13090..606c070 100644 --- a/src/manage/ManageResults.cpp +++ b/src/manage/ManageResults.cpp @@ -224,13 +224,21 @@ namespace platform { }; auto parser = CommandParser(); while (!finished) { - if (indexList) { - auto [min_index, max_index] = paginator.getOffset(page); - std::tie(option, index) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index); - } else { - std::tie(option, subIndex) = parser.parse(Colors::IBLUE(), listOptions, 'r', 0, results.at(index).getJson()["results"].size() - 1); + bool parserError = true; // force the first iteration + while (parserError) { + if (indexList) { + auto [min_index, max_index] = paginator.getOffset(page); + std::tie(option, index, parserError) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index); + } else { + std::tie(option, subIndex, parserError) = parser.parse(Colors::IBLUE(), listOptions, 'r', 0, results.at(index).getJson()["results"].size() - 1); + } + if (parserError) { + if (indexList) + list(parser.getErrorMessage(), Colors::RED(), index_A, index_B); + else + showIndex(index, subIndex); + } } - switch (option) { case 'p': if (paginator.valid(index)) { diff --git a/src/manage/b_manage.cpp b/src/manage/b_manage.cpp index a35eb13..ce32c43 100644 --- a/src/manage/b_manage.cpp +++ b/src/manage/b_manage.cpp @@ -58,7 +58,7 @@ int main(int argc, char** argv) auto partial = program.get("partial"); auto compare = program.get("compare"); if (number == 0) { - number = std::max(0, numRows() - 5); // 5 is the number of lines used by the menu & header + number = std::max(0, numRows() - 6); // 6 is the number of lines used by the menu & header } if (complete) partial = false;