Compare commits

..

2 Commits

7 changed files with 64 additions and 43 deletions

View File

@@ -6,11 +6,8 @@
#include "common/Utils.h"
namespace platform {
void CommandParser::messageError(const std::string& message)
{
std::cout << Colors::RED() << message << Colors::RESET() << std::endl;
}
std::pair<char, int> CommandParser::parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex)
std::tuple<char, int, bool> CommandParser::parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex)
{
bool finished = false;
while (!finished) {
@@ -29,16 +26,17 @@ namespace platform {
oss << "): ";
std::cout << oss.str();
getline(std::cin, line);
std::cout << Colors::RESET();
line = trim(line);
if (line.size() == 0)
continue;
if (line.size() == 0) {
errorMessage = "No command";
return { defaultCommand, 0, true };
}
if (all_of(line.begin(), line.end(), ::isdigit)) {
command = defaultCommand;
index = stoi(line);
if (index > maxIndex || index < minIndex) {
messageError("Index out of range");
continue;
errorMessage = "Index out of range";
return { command, index, true };
}
finished = true;
break;
@@ -53,24 +51,24 @@ namespace platform {
if (std::get<bool>(option)) {
// The option requires a value
if (line.size() == 0) {
messageError("Option " + std::get<std::string>(option) + " requires a value");
break;
errorMessage = "Option " + std::get<std::string>(option) + " requires a value";
return { command, index, true };
}
try {
index = stoi(line);
if (index > maxIndex || index < 0) {
messageError("Index out of range");
break;
errorMessage = "Index out of range";
return { command, index, true };
}
}
catch (const std::invalid_argument& ia) {
messageError("Invalid value: " + line);
break;
errorMessage = "Invalid value: " + line;
return { command, index, true };
}
} else {
if (line.size() > 0) {
messageError("option " + std::get<std::string>(option) + " doesn't accept values");
break;
errorMessage = "option " + std::get<std::string>(option) + " doesn't accept values";
return { command, index, true };
}
}
command = std::get<char>(option);
@@ -79,9 +77,10 @@ namespace platform {
}
}
if (!found) {
messageError("I don't know " + line);
errorMessage = "I don't know " + line;
return { command, index, true };
}
}
return { command, index };
return { command, index, false };
}
} /* namespace platform */

View File

@@ -8,11 +8,12 @@ namespace platform {
class CommandParser {
public:
CommandParser() = default;
std::pair<char, int> parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex);
std::tuple<char, int, bool> parse(const std::string& color, const std::vector<std::tuple<std::string, char, bool>>& options, const char defaultCommand, const int minIndex, const int maxIndex);
char getCommand() const { return command; };
int getIndex() const { return index; };
std::string getErrorMessage() const { return errorMessage; };
private:
void messageError(const std::string& message);
std::string errorMessage;
char command;
int index;
};

View File

@@ -13,21 +13,19 @@ namespace platform {
const std::string STATUS_OK = "Ok.";
const std::string STATUS_COLOR = Colors::GREEN();
ManageResults::ManageResults(int numFiles, const std::string& model, const std::string& score, bool complete, bool partial, bool compare) :
numFiles{ numFiles }, complete{ complete }, partial{ partial }, compare{ compare }, results(ResultsManager(model, score, complete, partial))
numFiles{ numFiles }, complete{ complete }, partial{ partial }, compare{ compare }, didExcel(false), results(ResultsManager(model, score, complete, partial))
{
results.load();
if (!results.empty()) {
results.sortDate();
sort_field = "Date";
indexList = true;
openExcel = false;
workbook = NULL;
if (numFiles == 0 or numFiles > results.size()) {
this->numFiles = results.size();
}
}
paginator = Paginator(numFiles, results.size());
page = 1;
sort_field = "Date";
}
void ManageResults::doMenu()
{
@@ -41,6 +39,9 @@ namespace platform {
if (openExcel) {
workbook_close(workbook);
}
if (didExcel) {
std::cout << Colors::MAGENTA() << "Excel file created: " << Paths::excel() + Paths::excelResults() << std::endl;
}
std::cout << Colors::RESET() << "Done!" << std::endl;
}
void ManageResults::list(const std::string& status_message_init, const std::string& status_color, int index_A, int index_B)
@@ -51,7 +52,7 @@ namespace platform {
int maxModel = results.maxModelSize();
int maxTitle = results.maxTitleSize();
std::vector<int> header_lengths = { 3, 10, maxModel, 10, 9, 3, 7, maxTitle };
int maxLine = std::accumulate(header_lengths.begin(), header_lengths.end(), 0) + header_lengths.size() - 1;
int maxLine = std::max(size_t(140), std::accumulate(header_lengths.begin(), header_lengths.end(), 0) + header_lengths.size() - 1);
auto temp = ConfigLocale();
auto [index_from, index_to] = paginator.getOffset(page);
std::string suffix = "";
@@ -61,8 +62,9 @@ namespace platform {
if (partial) {
suffix = " Only listing partial results ";
}
std::string header = " " + std::to_string(index_to - index_from + 1) + " Results on screen - Page "
+ std::to_string(page) + " of " + std::to_string(paginator.getPages()) + " ";
std::string header = " " + std::to_string(index_to - index_from + 1) + " Results on screen of "
+ std::to_string(results.size()) + " - Page " + std::to_string(page) + " of "
+ std::to_string(paginator.getPages()) + " ";
std::string prefix = std::string(maxLine - suffix.size() - header.size(), ' ');
std::cout << Colors::CLRSCR() << Colors::REVERSE() << Colors::WHITE() << header << prefix << Colors::MAGENTA() << suffix << std::endl;
@@ -136,12 +138,14 @@ namespace platform {
auto data_B = results.at(index_B).getJson();
ReportExcelCompared reporter(data_A, data_B);
reporter.report();
didExcel = true;
return results.at(index_A).getFilename() + " Vs " + results.at(index_B).getFilename();
}
std::string ManageResults::report(const int index, const bool excelReport)
{
auto data = results.at(index).getJson();
if (excelReport) {
didExcel = true;
ReportExcel reporter(data, compare, workbook);
reporter.show();
openExcel = true;
@@ -224,13 +228,21 @@ namespace platform {
};
auto parser = CommandParser();
while (!finished) {
bool parserError = true; // force the first iteration
while (parserError) {
if (indexList) {
auto [min_index, max_index] = paginator.getOffset(page);
std::tie(option, index) = 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 {
std::tie(option, subIndex) = 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', 0, results.at(index).getJson()["results"].size() - 1);
}
if (parserError) {
if (indexList)
list(parser.getErrorMessage(), Colors::RED(), index_A, index_B);
else
showIndex(index, subIndex);
}
}
switch (option) {
case 'p':
if (paginator.valid(index)) {

View File

@@ -21,6 +21,7 @@ namespace platform {
int numFiles;
bool indexList;
bool openExcel;
bool didExcel;
bool complete;
bool partial;
bool compare;

View File

@@ -7,7 +7,7 @@ public:
Paginator() = default;
Paginator(int pageSize, int total) : pageSize(pageSize), total(total)
{
numPages = (total + pageSize - 1) / pageSize;
numPages = pageSize > 0 ? (total + pageSize - 1) / pageSize : 0;
};
~Paginator() = default;
int getPageSize() const { return pageSize; }

View File

@@ -48,6 +48,8 @@ namespace platform {
}
void ResultsManager::sortDate()
{
if (empty())
return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) {
if (a.getDate() == b.getDate()) {
return a.getModel() < b.getModel();
@@ -57,6 +59,8 @@ namespace platform {
}
void ResultsManager::sortModel()
{
if (empty())
return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) {
if (a.getModel() == b.getModel()) {
return a.getDate() > b.getDate();
@@ -66,12 +70,16 @@ namespace platform {
}
void ResultsManager::sortDuration()
{
if (empty())
return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) {
return a.getDuration() > b.getDuration();
});
}
void ResultsManager::sortScore()
{
if (files.empty())
return;
sort(files.begin(), files.end(), [](const Result& a, const Result& b) {
if (a.getScore() == b.getScore()) {
return a.getDate() > b.getDate();

View File

@@ -58,7 +58,7 @@ int main(int argc, char** argv)
auto partial = program.get<bool>("partial");
auto compare = program.get<bool>("compare");
if (number == 0) {
number = std::max(0, numRows() - 5); // 5 is the number of lines used by the menu & header
number = std::max(0, numRows() - 6); // 6 is the number of lines used by the menu & header
}
if (complete)
partial = false;