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);
auto header_text = report.getHeader();
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
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
//
@@ -310,27 +310,27 @@ namespace platform {
};
// tuple<Option, digit, requires value>
std::vector<std::tuple<std::string, char, bool>> 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<int>(output_type)].getOffset();
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);
} 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<int>(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<int>(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);

View File

@@ -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<int, int> 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()
{