Add ascending/descending sort to b_manage

This commit is contained in:
2024-04-10 19:42:40 +02:00
parent ee93789ca3
commit 7ed9073d15
7 changed files with 91 additions and 41 deletions

View File

@@ -9,8 +9,9 @@ namespace platform {
inline static const std::string black_star{ "\u2605" }; inline static const std::string black_star{ "\u2605" };
inline static const std::string cross{ "\u2717" }; inline static const std::string cross{ "\u2717" };
inline static const std::string upward_arrow{ "\u27B6" }; inline static const std::string upward_arrow{ "\u27B6" };
inline static const std::string down_arrow{ "\u27B4" }; inline static const std::string downward_arrow{ "\u27B4" };
inline static const std::string downward_arrow{ "\u2B07" }; inline static const std::string up_arrow{ "\u2B06" };
inline static const std::string down_arrow{ "\u2B07" };
inline static const std::string equal_best{ check_mark }; inline static const std::string equal_best{ check_mark };
inline static const std::string better_best{ black_star }; inline static const std::string better_best{ black_star };
inline static const std::string notebook{ "\U0001F5C8" }; inline static const std::string notebook{ "\U0001F5C8" };

View File

@@ -22,8 +22,6 @@ namespace platform {
rows{ rows }, cols{ cols }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(model, score, complete, partial)) rows{ rows }, cols{ cols }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(model, score, complete, partial))
{ {
results.load(); results.load();
results.sortDate();
sort_field = "Date";
openExcel = false; openExcel = false;
workbook = NULL; workbook = NULL;
this->rows = std::max(0, rows - 6); // 6 is the number of lines used by the menu & header this->rows = std::max(0, rows - 6); // 6 is the number of lines used by the menu & header
@@ -44,7 +42,7 @@ namespace platform {
std::cout << Colors::MAGENTA() << "No results found!" << Colors::RESET() << std::endl; std::cout << Colors::MAGENTA() << "No results found!" << Colors::RESET() << std::endl;
return; return;
} }
results.sortDate(); results.sortResults(sort_field, sort_type);
list(STATUS_OK, STATUS_COLOR); list(STATUS_OK, STATUS_COLOR);
menu(); menu();
if (openExcel) { if (openExcel) {
@@ -209,17 +207,18 @@ namespace platform {
int maxModel = results.maxModelSize(); int maxModel = results.maxModelSize();
int maxTitle = results.maxTitleSize(); int maxTitle = results.maxTitleSize();
std::vector<int> header_lengths = { 3, 10, maxModel, 10, 9, 3, 7, maxTitle }; std::vector<int> header_lengths = { 3, 10, maxModel, 10, 9, 3, 7, maxTitle };
//
std::cout << Colors::RESET(); std::cout << Colors::RESET();
std::string arrow = Symbols::downward_arrow + " "; std::string arrow_dn = Symbols::down_arrow + " ";
std::string arrow_up = Symbols::up_arrow + " ";
std::vector<std::string> header_labels = { " #", "Date", "Model", "Score Name", "Score", "C/P", "Time", "Title" }; std::vector<std::string> header_labels = { " #", "Date", "Model", "Score Name", "Score", "C/P", "Time", "Title" };
std::vector<std::string> sort_fields = { "Date", "Model", "Score", "Time" };
for (int i = 0; i < header_labels.size(); i++) { for (int i = 0; i < header_labels.size(); i++) {
std::string suffix = "", color = Colors::GREEN(); std::string suffix = "", color = Colors::GREEN();
int diff = 0; int diff = 0;
if (header_labels[i] == sort_field) { if (header_labels[i] == sort_fields[static_cast<int>(sort_field)]) {
color = Colors::YELLOW(); color = Colors::YELLOW();
diff = 2; diff = 2;
suffix = arrow; suffix = sort_type == SortType::ASC ? arrow_up : arrow_dn;
} }
std::cout << color << std::setw(header_lengths[i] + diff) << std::left << std::string(header_labels[i] + suffix) << " "; std::cout << color << std::setw(header_lengths[i] + diff) << std::left << std::string(header_labels[i] + suffix) << " ";
} }
@@ -290,34 +289,40 @@ namespace platform {
} }
std::pair<std::string, std::string> ManageScreen::sortList() std::pair<std::string, std::string> ManageScreen::sortList()
{ {
std::cout << Colors::YELLOW() << "Choose sorting field (date='d', score='s', time='t', model='m'): "; std::cout << Colors::YELLOW() << "Choose sorting field (date='d', score='s', time='t', model='m', ascending='+', descending='-'): ";
std::vector<std::string> fields = { "Date", "Model", "Score", "Time" };
std::string invalid_option = "Invalid sorting option";
std::string line; std::string line;
char option; char option;
getline(std::cin, line); getline(std::cin, line);
if (line.size() == 0 || line.size() > 1) { if (line.size() == 0 || line.size() > 1) {
return { Colors::RED(), "Invalid sorting option" }; return { Colors::RED(), invalid_option };
} }
option = line[0]; option = line[0];
switch (option) { switch (option) {
case 'd': case 'd':
results.sortDate(); sort_field = SortField::DATE;
sort_field = "Date"; break;
return { Colors::GREEN(), "Sorted by date" };
case 's': case 's':
results.sortScore(); sort_field = SortField::SCORE;
sort_field = "Score"; break;
return { Colors::GREEN(), "Sorted by score" };
case 't': case 't':
results.sortDuration(); sort_field = SortField::DURATION;
sort_field = "Time"; break;
return { Colors::GREEN(), "Sorted by time" };
case 'm': case 'm':
results.sortModel(); sort_field = SortField::MODEL;
sort_field = "Model"; break;
return { Colors::GREEN(), "Sorted by model" }; case '+':
sort_type = SortType::ASC;
break;
case '-':
sort_type = SortType::DESC;
break;
default: default:
return { Colors::RED(), "Invalid sorting option" }; return { Colors::RED(), invalid_option };
} }
results.sortResults(sort_field, sort_type);
return { Colors::GREEN(), "Sorted by " + fields[static_cast<int>(sort_field)] + " " + (sort_type == SortType::ASC ? "ascending" : "descending") };
} }
void ManageScreen::menu() void ManageScreen::menu()
{ {

View File

@@ -43,7 +43,8 @@ namespace platform {
bool complete; bool complete;
bool partial; bool partial;
bool compare; bool compare;
std::string sort_field; SortField sort_field = SortField::DATE;
SortType sort_type = SortType::DESC;
std::vector<Paginator> paginator; std::vector<Paginator> paginator;
ResultsManager results; ResultsManager results;
lxw_workbook* workbook; lxw_workbook* workbook;

View File

@@ -46,47 +46,79 @@ namespace platform {
{ {
return files.size(); return files.size();
} }
void ResultsManager::sortDate() void ResultsManager::sortDate(SortType type)
{ {
if (empty()) if (empty())
return; return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) { sort(files.begin(), files.end(), [type](const Result& a, const Result& b) {
if (a.getDate() == b.getDate()) { if (a.getDate() == b.getDate()) {
return a.getModel() < b.getModel(); if (type == SortType::ASC)
return a.getModel() < b.getModel();
return a.getModel() > b.getModel();
} }
if (type == SortType::ASC)
return a.getDate() < b.getDate();
return a.getDate() > b.getDate(); return a.getDate() > b.getDate();
}); });
} }
void ResultsManager::sortModel() void ResultsManager::sortModel(SortType type)
{ {
if (empty()) if (empty())
return; return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) { sort(files.begin(), files.end(), [type](const Result& a, const Result& b) {
if (a.getModel() == b.getModel()) { if (a.getModel() == b.getModel()) {
if (type == SortType::ASC)
return a.getDate() < b.getDate();
return a.getDate() > b.getDate(); return a.getDate() > b.getDate();
} }
if (type == SortType::ASC)
return a.getModel() < b.getModel();
return a.getModel() > b.getModel(); return a.getModel() > b.getModel();
}); });
} }
void ResultsManager::sortDuration() void ResultsManager::sortDuration(SortType type)
{ {
if (empty()) if (empty())
return; return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) { sort(files.begin(), files.end(), [type](const Result& a, const Result& b) {
if (type == SortType::ASC)
return a.getDuration() < b.getDuration();
return a.getDuration() > b.getDuration(); return a.getDuration() > b.getDuration();
}); });
} }
void ResultsManager::sortScore() void ResultsManager::sortScore(SortType type)
{ {
if (files.empty()) if (empty())
return; return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) { sort(files.begin(), files.end(), [type](const Result& a, const Result& b) {
if (a.getScore() == b.getScore()) { if (a.getScore() == b.getScore()) {
if (type == SortType::ASC)
return a.getDate() < b.getDate();
return a.getDate() > b.getDate(); return a.getDate() > b.getDate();
} }
if (type == SortType::ASC)
return a.getScore() < b.getScore();
return a.getScore() > b.getScore(); return a.getScore() > b.getScore();
}); });
} }
void ResultsManager::sortResults(SortField field, SortType type)
{
switch (field) {
case SortField::DATE:
sortDate(type);
break;
case SortField::MODEL:
sortModel(type);
break;
case SortField::SCORE:
sortScore(type);
break;
case SortField::DURATION:
sortDuration(type);
break;
}
}
bool ResultsManager::empty() const bool ResultsManager::empty() const
{ {
return files.empty(); return files.empty();

View File

@@ -6,14 +6,25 @@
#include "results/Result.h" #include "results/Result.h"
namespace platform { namespace platform {
using json = nlohmann::json; using json = nlohmann::json;
enum class SortType {
ASC = 0,
DESC = 1,
};
enum class SortField {
DATE = 0,
MODEL = 1,
SCORE = 2,
DURATION = 3,
};
class ResultsManager { class ResultsManager {
public: public:
ResultsManager(const std::string& model, const std::string& score, bool complete, bool partial); ResultsManager(const std::string& model, const std::string& score, bool complete, bool partial);
void load(); // Loads the list of results void load(); // Loads the list of results
void sortDate(); void sortResults(SortField field, SortType type); // Sorts the list of results
void sortScore(); void sortDate(SortType type);
void sortModel(); void sortScore(SortType type);
void sortDuration(); void sortModel(SortType type);
void sortDuration(SortType type);
int maxModelSize() const { return maxModel; }; int maxModelSize() const { return maxModel; };
int maxTitleSize() const { return maxTitle; }; int maxTitleSize() const { return maxTitle; };
void hideResult(int index, const std::string& pathHidden); void hideResult(int index, const std::string& pathHidden);