Add sorting capacity

This commit is contained in:
Ricardo Montañana Gómez 2023-08-13 17:10:18 +02:00
parent 2729b92f06
commit 054567c65a
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
3 changed files with 150 additions and 6 deletions

View File

@ -1,6 +1,7 @@
#include <filesystem> #include <filesystem>
#include "platformUtils.h" #include "platformUtils.h"
#include "Results.h" #include "Results.h"
#include "Report.h"
namespace platform { namespace platform {
const double REFERENCE_SCORE = 22.109799; const double REFERENCE_SCORE = 22.109799;
Result::Result(const string& path, const string& filename) Result::Result(const string& path, const string& filename)
@ -48,13 +49,140 @@ namespace platform {
oss << setw(50) << left << title << " "; oss << setw(50) << left << title << " ";
return oss.str(); return oss.str();
} }
void Results::manage() void Results::show() const
{ {
cout << "Results found: " << files.size() << endl; 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) { for (const auto& result : files) {
cout << setw(3) << fixed << right << i++ << " ";
cout << result.to_string() << endl; 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();
}
} }

View File

@ -13,6 +13,12 @@ namespace platform {
Result(const string& path, const string& filename); Result(const string& path, const string& filename);
json load(); json load();
string to_string() const; 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: private:
string path; string path;
string filename; string filename;
@ -24,14 +30,21 @@ namespace platform {
}; };
class Results { class Results {
public: public:
explicit Results(const string& path) : path(path) { load(); }; explicit Results(const string& path, const int max) : path(path), max(max) { load(); };
void manage(); void manage();
private: private:
string path; string path;
int max;
vector<Result> files; vector<Result> files;
void load(); // Loads the list of results void load(); // Loads the list of results
void show(); void show() const;
int menu(); int getIndex(const string& intent) const;
void menu();
void sortList();
void sortDate();
void sortScore();
void sortModel();
void sortDuration();
}; };
}; };

View File

@ -13,6 +13,9 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
try { try {
program.parse_args(argc, argv); program.parse_args(argc, argv);
auto number = program.get<int>("number"); auto number = program.get<int>("number");
if (number < 0) {
throw runtime_error("Number of results must be greater than or equal to 0");
}
} }
catch (const exception& err) { catch (const exception& err) {
cerr << err.what() << endl; cerr << err.what() << endl;
@ -26,7 +29,7 @@ int main(int argc, char** argv)
{ {
auto program = manageArguments(argc, argv); auto program = manageArguments(argc, argv);
auto number = program.get<int>("number"); auto number = program.get<int>("number");
auto results = platform::Results(PATH_RESULTS); auto results = platform::Results(PATH_RESULTS, number);
results.manage(); results.manage();
return 0; return 0;
} }