Complete pagination of result report

This commit is contained in:
2024-03-17 11:26:26 +01:00
parent faccb09c43
commit d47da27571
2 changed files with 33 additions and 19 deletions

View File

@@ -109,10 +109,10 @@ namespace platform {
ReportConsole report(data, compare); ReportConsole report(data, compare);
auto header_text = report.getHeader(); auto header_text = report.getHeader();
auto body = report.getBody(); auto body = report.getBody();
paginator[static_cast<int>(output_type)].setTotal(data.size()); paginator[static_cast<int>(output_type)].setTotal(body.size());
// We need to subtract 8 from the page size to make room for the extra header in report // We need to subtract 8 from the page size to make room for the extra header in report
auto page_size = paginator[static_cast<int>(OutputType::EXPERIMENTS)].getPageSize(); auto page_size = paginator[static_cast<int>(OutputType::EXPERIMENTS)].getPageSize();
paginator[static_cast<int>(output_type)].setPage(page_size - 8); paginator[static_cast<int>(output_type)].setPageSize(page_size - 8);
// //
// header // header
// //
@@ -310,27 +310,27 @@ namespace platform {
}; };
// tuple<Option, digit, requires value> // tuple<Option, digit, requires value>
std::vector<std::tuple<std::string, char, bool>> listOptions = { std::vector<std::tuple<std::string, char, bool>> listOptions = {
{"quit", 'q', false},
{"report", 'r', true}, {"report", 'r', true},
{"list", 'l', false}, {"list", 'l', false},
{"back", 'b', false}, {"back", 'b', false},
{"quit", 'q', false} {"Page", 'p', true},
{"Page+", '+', false},
{"Page-", '-', false}
}; };
auto parser = CommandParser(); auto parser = CommandParser();
while (!finished) { while (!finished) {
bool parserError = true; // force the first iteration bool parserError = true; // force the first iteration
while (parserError) { while (parserError) {
auto [min_index, max_index] = paginator[static_cast<int>(output_type)].getOffset();
if (indexList) { if (indexList) {
auto [min_index, max_index] = paginator[static_cast<int>(output_type)].getOffset();
std::tie(option, index, parserError) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index); std::tie(option, index, parserError) = parser.parse(Colors::IGREEN(), mainOptions, 'r', min_index, max_index);
} else { } 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 (parserError) {
if (indexList) list(parser.getErrorMessage(), Colors::RED());
list(parser.getErrorMessage(), Colors::RED());
else
report(index, false);
} }
} }
switch (option) { switch (option) {
@@ -339,10 +339,13 @@ namespace platform {
list_datasets(STATUS_OK, STATUS_COLOR); list_datasets(STATUS_OK, STATUS_COLOR);
break; break;
case 'p': case 'p':
if (paginator[static_cast<int>(output_type)].setPage(index)) { {
list(STATUS_OK, STATUS_COLOR); auto page = output_type == OutputType::EXPERIMENTS ? index : subIndex;
} else { if (paginator[static_cast<int>(output_type)].setPage(page)) {
list("Invalid page!", Colors::RED()); list(STATUS_OK, STATUS_COLOR);
} else {
list("Invalid page! (" + std::to_string(page) + ")", Colors::RED());
}
} }
break; break;
case '+': case '+':
@@ -380,7 +383,7 @@ namespace platform {
list("B set to " + std::to_string(index), Colors::GREEN()); list("B set to " + std::to_string(index), Colors::GREEN());
} else { } else {
// back to show the report // back to show the report
report(index, false); list(STATUS_OK, STATUS_COLOR);
} }
break; break;
case 'c': case 'c':
@@ -435,7 +438,7 @@ namespace platform {
if (indexList) { if (indexList) {
//report(index, false); //report(index, false);
output_type = OutputType::RESULT; output_type = OutputType::RESULT;
list_result(STATUS_OK, STATUS_COLOR); list(STATUS_OK, STATUS_COLOR);
indexList = false; indexList = false;
} else { } else {
showIndex(subIndex); showIndex(subIndex);

View File

@@ -10,26 +10,37 @@ public:
computePages(); computePages();
}; };
~Paginator() = default; ~Paginator() = default;
// Getters
int getPageSize() const { return pageSize; } int getPageSize() const { return pageSize; }
int getLines() const int getLines() const
{ {
auto [start, end] = getOffset(); auto [start, end] = getOffset();
return std::min(pageSize, end - start + 1); return std::min(pageSize, end - start + 1);
} }
int getPage() const { return page; }
int getTotal() const { return total; } int getTotal() const { return total; }
void setTotal(int total) { this->total = total; computePages(); } int getPages() const { return numPages; }
std::pair<int, int> getOffset() const std::pair<int, int> getOffset() const
{ {
return { (page - 1) * pageSize, std::min(total - 1, page * pageSize - 1) }; return { (page - 1) * pageSize, std::min(total - 1, page * pageSize - 1) };
} }
int getPages() const { return numPages; } // Setters
int getPage() const { return page; } 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 valid(int page) const { return page > 0 && page <= numPages; }
bool hasPrev(int page) const { return page > 1; } bool hasPrev(int page) const { return page > 1; }
bool hasNext(int page) const { return page < getPages(); } 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 addPage() { return page < numPages ? ++page, true : false; }
bool subPage() { return page > 1 ? --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: private:
void computePages() void computePages()
{ {