From 5d5f49777e4603db415c809cc6a786e7ce2854de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Tue, 16 Jul 2024 11:30:28 +0200 Subject: [PATCH] Fix wrong columns message --- src/manage/ManageScreen.cpp | 49 ++++++++++++++++++++++++++----------- src/manage/ManageScreen.h | 1 + 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/manage/ManageScreen.cpp b/src/manage/ManageScreen.cpp index 0ad5bd4..ebc0973 100644 --- a/src/manage/ManageScreen.cpp +++ b/src/manage/ManageScreen.cpp @@ -30,9 +30,6 @@ namespace platform { header_labels = { " #", "Date", "Model", "Score Name", "Score", "Platform", "SD", "C/P", "Time", "Title" }; sort_fields = { "Date", "Model", "Score", "Time" }; updateSize(rows, cols); - if (min_columns > cols) { - throw std::runtime_error("Make screen bigger to fit the results! " + std::to_string(min_columns - cols) + " columns needed! "); - } // Initializes the paginator for each output type (experiments, datasets, result) for (int i = 0; i < static_cast(OutputType::Count); i++) { paginator.push_back(Paginator(this->rows, results.size())); @@ -56,6 +53,14 @@ namespace platform { paginator_.setPageSize(rows); } } + bool ManageScreen::checkWrongColumns() + { + if (min_columns > cols) { + std::cerr << Colors::MAGENTA() << "Make screen bigger to fit the results! " + std::to_string(min_columns - cols) + " columns needed! " << std::endl; + return true; + } + return false; + } void ManageScreen::updateSize(int rows_, int cols_) { rows = std::max(6, rows_ - 6); // 6 is the number of lines used by the menu & header @@ -65,9 +70,11 @@ namespace platform { void ManageScreen::doMenu() { if (results.empty()) { - std::cout << Colors::MAGENTA() << "No results found!" << Colors::RESET() << std::endl; + std::cerr << Colors::MAGENTA() << "No results found!" << Colors::RESET() << std::endl; return; } + if (checkWrongColumns()) + return; results.sortResults(sort_field, sort_type); list(STATUS_OK, STATUS_COLOR); menu(); @@ -302,15 +309,28 @@ namespace platform { } std::pair ManageScreen::sortList() { - std::cout << Colors::YELLOW() << "Choose sorting field (date='d', score='s', time='t', model='m', ascending='+', descending='-'): "; + std::vector> sortOptions = { + {"date", 'd', false}, + {"score", 's', false}, + {"time", 't', false}, + {"model", 'm', false}, + {"ascending+", '+', false}, + {"descending-", '-', false} + }; + auto sortMenu = OptionsMenu(sortOptions, Colors::YELLOW(), Colors::RED(), cols); std::string invalid_option = "Invalid sorting option"; - std::string line; char option; - getline(std::cin, line); - if (line.size() == 0 || line.size() > 1) { - return { Colors::RED(), invalid_option }; + bool parserError = true; // force the first iteration + while (parserError) { + if (checkWrongColumns()) + return { Colors::RED(), "Invalid column size" }; + auto [min_index, max_index] = paginator[static_cast(output_type)].getOffset(); + std::tie(option, index, parserError) = sortMenu.parse(' ', 0, 0); + sortMenu.updateColumns(cols); + if (parserError) { + return { Colors::RED(), invalid_option }; + } } - option = line[0]; switch (option) { case 'd': sort_field = SortField::DATE; @@ -364,7 +384,7 @@ namespace platform { {"quit", 'q', false}, {"report", 'r', true}, {"list", 'l', false}, - {"excel", 'e', false}, + {"excel", 'e', true}, {"back", 'b', false}, {"page", 'p', true}, {"Page+", '+', false}, @@ -376,11 +396,12 @@ namespace platform { OptionsMenu& menu = output_type == OutputType::EXPERIMENTS ? main_menu : list_menu; bool parserError = true; // force the first iteration while (parserError) { - if (min_columns > cols) { - throw std::runtime_error("Make screen bigger to fit the results! " + std::to_string(min_columns - cols) + " columns needed! "); - } auto [min_index, max_index] = paginator[static_cast(output_type)].getOffset(); std::tie(option, index, parserError) = menu.parse('r', min_index, max_index); + if (min_columns > cols) { + std::cerr << "Make screen bigger to fit the results! " + std::to_string(min_columns - cols) + " columns needed! " << std::endl; + return; + } menu.updateColumns(cols); if (parserError) { list(menu.getErrorMessage(), Colors::RED()); diff --git a/src/manage/ManageScreen.h b/src/manage/ManageScreen.h index d9e0d57..0e6ab28 100644 --- a/src/manage/ManageScreen.h +++ b/src/manage/ManageScreen.h @@ -31,6 +31,7 @@ namespace platform { std::pair sortList(); std::string getVersions(); void computeSizes(); + bool checkWrongColumns(); void menu(); void header(); void footer(const std::string& status, const std::string& color);