From 054567c65a252dafb6090a85918bc3c22d4038bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Sun, 13 Aug 2023 17:10:18 +0200 Subject: [PATCH] Add sorting capacity --- src/Platform/Results.cc | 132 +++++++++++++++++++++++++++++++++++++++- src/Platform/Results.h | 19 +++++- src/Platform/manage.cc | 5 +- 3 files changed, 150 insertions(+), 6 deletions(-) diff --git a/src/Platform/Results.cc b/src/Platform/Results.cc index ee5315b..056f0b1 100644 --- a/src/Platform/Results.cc +++ b/src/Platform/Results.cc @@ -1,6 +1,7 @@ #include #include "platformUtils.h" #include "Results.h" +#include "Report.h" namespace platform { const double REFERENCE_SCORE = 22.109799; Result::Result(const string& path, const string& filename) @@ -48,13 +49,140 @@ namespace platform { oss << setw(50) << left << title << " "; return oss.str(); } - void Results::manage() + void Results::show() const { cout << "Results found: " << files.size() << endl; - cout << "========================" << endl; + cout << "-------------------" << endl; + auto i = 0; + cout << " # Date Model Score Duration Title" << endl; + cout << "=== ========== ============ ========= ========= =============================================================" << endl; for (const auto& result : files) { + cout << setw(3) << fixed << right << i++ << " "; cout << result.to_string() << endl; + if (i == max && max != 0) { + break; + } + } } + int Results::getIndex(const string& intent) const + { + cout << "Choose result to " << intent << ": "; + int index; + cin >> index; + if (index >= 0 && index < files.size()) { + return index; + } + + cout << "Invalid index" << endl; + return -1; + } + void Results::menu() + { + cout << "Choose option (quit='q', list='l', delete='d', hide='h', sort='s', report='r'): "; + char option; + int index; + string filename; + cin >> option; + switch (option) { + case 'q': + exit(0); + case 'l': + show(); + menu(); + break; + case 'd': + index = getIndex("delete"); + if (index == -1) + break; + filename = files[index].getFilename(); + cout << "Deleting " << filename << endl; + remove((path + "/" + filename).c_str()); + files.erase(files.begin() + index); + show(); + menu(); + break; + case 'h': + index = getIndex("hide"); + if (index == -1) + break; + filename = files[index].getFilename(); + cout << "Hiding " << filename << endl; + rename((path + "/" + filename).c_str(), (path + "/." + filename).c_str()); + files.erase(files.begin() + index); + show(); + menu(); + break; + case 's': + sortList(); + show(); + menu(); + break; + case 'r': + index = getIndex("report"); + if (index == -1) + break; + filename = files[index].getFilename(); + cout << "Reporting " << filename << endl; + auto data = files[index].load(); + Report report(data); + report.show(); + menu(); + break; + + } + } + void Results::sortList() + { + cout << "Choose sorting field (date='d', score='s', duration='u', model='m'): "; + char option; + cin >> option; + switch (option) { + case 'd': + sortDate(); + break; + case 's': + sortScore(); + break; + case 'u': + sortDuration(); + break; + case 'm': + sortModel(); + break; + default: + cout << "Invalid option" << endl; + } + + } + void Results::sortDate() + { + sort(files.begin(), files.end(), [](const Result& a, const Result& b) { + return a.getDate() > b.getDate(); + }); + } + void Results::sortModel() + { + sort(files.begin(), files.end(), [](const Result& a, const Result& b) { + return a.getModel() > b.getModel(); + }); + } + void Results::sortDuration() + { + sort(files.begin(), files.end(), [](const Result& a, const Result& b) { + return a.getDuration() > b.getDuration(); + }); + } + void Results::sortScore() + { + sort(files.begin(), files.end(), [](const Result& a, const Result& b) { + return a.getScore() > b.getScore(); + }); + } + void Results::manage() + { + show(); + menu(); + } } \ No newline at end of file diff --git a/src/Platform/Results.h b/src/Platform/Results.h index 5d36f32..945901f 100644 --- a/src/Platform/Results.h +++ b/src/Platform/Results.h @@ -13,6 +13,12 @@ namespace platform { Result(const string& path, const string& filename); json load(); string to_string() const; + string getFilename() const { return filename; }; + string getDate() const { return date; }; + double getScore() const { return score; }; + string getTitle() const { return title; }; + double getDuration() const { return duration; }; + string getModel() const { return model; }; private: string path; string filename; @@ -24,14 +30,21 @@ namespace platform { }; class Results { public: - explicit Results(const string& path) : path(path) { load(); }; + explicit Results(const string& path, const int max) : path(path), max(max) { load(); }; void manage(); private: string path; + int max; vector files; void load(); // Loads the list of results - void show(); - int menu(); + void show() const; + int getIndex(const string& intent) const; + void menu(); + void sortList(); + void sortDate(); + void sortScore(); + void sortModel(); + void sortDuration(); }; }; diff --git a/src/Platform/manage.cc b/src/Platform/manage.cc index b901601..f97dae3 100644 --- a/src/Platform/manage.cc +++ b/src/Platform/manage.cc @@ -13,6 +13,9 @@ argparse::ArgumentParser manageArguments(int argc, char** argv) try { program.parse_args(argc, argv); auto number = program.get("number"); + if (number < 0) { + throw runtime_error("Number of results must be greater than or equal to 0"); + } } catch (const exception& err) { cerr << err.what() << endl; @@ -26,7 +29,7 @@ int main(int argc, char** argv) { auto program = manageArguments(argc, argv); auto number = program.get("number"); - auto results = platform::Results(PATH_RESULTS); + auto results = platform::Results(PATH_RESULTS, number); results.manage(); return 0; }