From d47da275713bf221c73f18b2cb72b840e31dced7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Sun, 17 Mar 2024 11:26:26 +0100 Subject: [PATCH] Complete pagination of result report --- src/manage/ManageScreen.cpp | 33 ++++++++++++++++++--------------- src/manage/Paginator.hpp | 19 +++++++++++++++---- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/manage/ManageScreen.cpp b/src/manage/ManageScreen.cpp index 3db8d8b..b6f2a7a 100644 --- a/src/manage/ManageScreen.cpp +++ b/src/manage/ManageScreen.cpp @@ -109,10 +109,10 @@ namespace platform { ReportConsole report(data, compare); auto header_text = report.getHeader(); auto body = report.getBody(); - paginator[static_cast(output_type)].setTotal(data.size()); + paginator[static_cast(output_type)].setTotal(body.size()); // We need to subtract 8 from the page size to make room for the extra header in report auto page_size = paginator[static_cast(OutputType::EXPERIMENTS)].getPageSize(); - paginator[static_cast(output_type)].setPage(page_size - 8); + paginator[static_cast(output_type)].setPageSize(page_size - 8); // // header // @@ -310,27 +310,27 @@ namespace platform { }; // tuple std::vector> listOptions = { + {"quit", 'q', false}, {"report", 'r', true}, {"list", 'l', false}, {"back", 'b', false}, - {"quit", 'q', false} + {"Page", 'p', true}, + {"Page+", '+', false}, + {"Page-", '-', false} }; auto parser = CommandParser(); while (!finished) { bool parserError = true; // force the first iteration while (parserError) { + auto [min_index, max_index] = paginator[static_cast(output_type)].getOffset(); if (indexList) { - auto [min_index, max_index] = paginator[static_cast(output_type)].getOffset(); 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); + std::tie(option, subIndex, parserError) = parser.parse(Colors::IBLUE(), listOptions, 'r', min_index, max_index); } if (parserError) { - if (indexList) - list(parser.getErrorMessage(), Colors::RED()); - else - report(index, false); + list(parser.getErrorMessage(), Colors::RED()); } } switch (option) { @@ -339,10 +339,13 @@ namespace platform { list_datasets(STATUS_OK, STATUS_COLOR); break; case 'p': - if (paginator[static_cast(output_type)].setPage(index)) { - list(STATUS_OK, STATUS_COLOR); - } else { - list("Invalid page!", Colors::RED()); + { + auto page = output_type == OutputType::EXPERIMENTS ? index : subIndex; + if (paginator[static_cast(output_type)].setPage(page)) { + list(STATUS_OK, STATUS_COLOR); + } else { + list("Invalid page! (" + std::to_string(page) + ")", Colors::RED()); + } } break; case '+': @@ -380,7 +383,7 @@ namespace platform { list("B set to " + std::to_string(index), Colors::GREEN()); } else { // back to show the report - report(index, false); + list(STATUS_OK, STATUS_COLOR); } break; case 'c': @@ -435,7 +438,7 @@ namespace platform { if (indexList) { //report(index, false); output_type = OutputType::RESULT; - list_result(STATUS_OK, STATUS_COLOR); + list(STATUS_OK, STATUS_COLOR); indexList = false; } else { showIndex(subIndex); diff --git a/src/manage/Paginator.hpp b/src/manage/Paginator.hpp index bb384a2..53d21ca 100644 --- a/src/manage/Paginator.hpp +++ b/src/manage/Paginator.hpp @@ -10,26 +10,37 @@ public: computePages(); }; ~Paginator() = default; + // Getters int getPageSize() const { return pageSize; } int getLines() const { auto [start, end] = getOffset(); return std::min(pageSize, end - start + 1); } + int getPage() const { return page; } int getTotal() const { return total; } - void setTotal(int total) { this->total = total; computePages(); } + int getPages() const { return numPages; } std::pair getOffset() const { return { (page - 1) * pageSize, std::min(total - 1, page * pageSize - 1) }; } - int getPages() const { return numPages; } - int getPage() const { return page; } + // Setters + void setTotal(int total) { this->total = total; computePages(); } + void setPageSize(int page) { this->pageSize = page; computePages(); } + bool setPage(int page) { return valid(page) ? this->page = page, true : false; } + // Utils bool valid(int page) const { return page > 0 && page <= numPages; } bool hasPrev(int page) const { return page > 1; } bool hasNext(int page) const { return page < getPages(); } - bool setPage(int page) { return valid(page) ? this->page = page, true : false; } bool addPage() { return page < numPages ? ++page, true : false; } bool subPage() { return page > 1 ? --page, true : false; } + std::string to_string() const + { + auto offset = getOffset(); + return "Paginator: { pageSize: " + std::to_string(pageSize) + ", total: " + std::to_string(total) + + ", page: " + std::to_string(page) + ", numPages: " + std::to_string(numPages) + + " Offset [" + std::to_string(offset.first) + ", " + std::to_string(offset.second) + "]}"; + } private: void computePages() {