From 514968a0820a3f95312b265dd3d2df27d594f921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Wed, 28 May 2025 17:37:53 +0200 Subject: [PATCH] Open excel file automatically when generated --- src/best/BestResults.cpp | 2 + src/best/BestResults.h | 2 + src/commands/b_best.cpp | 6 +++ src/commands/b_list.cpp | 21 +++++++--- src/commands/b_manage.cpp | 64 ++--------------------------- src/common/Paths.h | 2 + src/common/Utils.h | 61 +++++++++++++++++++++++++++ src/reports/DatasetsExcel.cpp | 3 +- src/reports/DatasetsExcel.h | 1 + src/results/ResultsDatasetExcel.cpp | 3 +- src/results/ResultsDatasetExcel.h | 1 + 11 files changed, 97 insertions(+), 69 deletions(-) diff --git a/src/best/BestResults.cpp b/src/best/BestResults.cpp index 4cffc84..bd7f82a 100644 --- a/src/best/BestResults.cpp +++ b/src/best/BestResults.cpp @@ -336,6 +336,7 @@ namespace platform { BestResultsExcel excel_report(path, score, datasets); excel_report.reportSingle(model, path + Paths::bestResultsFile(score, model)); messageOutputFile("Excel", excel_report.getFileName()); + excelFileName = excel_report.getFileName(); } } void BestResults::reportAll(bool excel, bool tex, bool index) @@ -373,6 +374,7 @@ namespace platform { excel.reportSingle(model, path + Paths::bestResultsFile(score, model)); } messageOutputFile("Excel", excel.getFileName()); + excelFileName = excel.getFileName(); } } void BestResults::messageOutputFile(const std::string& title, const std::string& fileName) diff --git a/src/best/BestResults.h b/src/best/BestResults.h index 4d3c307..6d1af54 100644 --- a/src/best/BestResults.h +++ b/src/best/BestResults.h @@ -15,6 +15,7 @@ namespace platform { void reportSingle(bool excel); void reportAll(bool excel, bool tex, bool index); void buildAll(); + std::string getExcelFileName() const { return excelFileName; } private: std::vector getModels(); std::vector getDatasets(json table); @@ -33,6 +34,7 @@ namespace platform { int maxModelName = 0; int maxDatasetName = 0; int minLength = 13; // Minimum length for scores + std::string excelFileName; }; } #endif \ No newline at end of file diff --git a/src/commands/b_best.cpp b/src/commands/b_best.cpp index aaefbec..8c8b89e 100644 --- a/src/commands/b_best.cpp +++ b/src/commands/b_best.cpp @@ -4,6 +4,7 @@ #include "main/modelRegister.h" #include "common/Paths.h" #include "common/Colors.h" +#include "common/Utils.h" #include "best/BestResults.h" #include "common/DotEnv.h" #include "config_platform.h" @@ -80,6 +81,11 @@ int main(int argc, char** argv) std::cout << Colors::GREEN() << fileName << " created!" << Colors::RESET() << std::endl; results.reportSingle(excel); } + if (excel) { + auto fileName = results.getExcelFileName(); + std::cout << "Opening " << fileName << std::endl; + platform::openFile(fileName); + } std::cout << Colors::RESET(); return 0; } diff --git a/src/commands/b_list.cpp b/src/commands/b_list.cpp index 5309101..96950b0 100644 --- a/src/commands/b_list.cpp +++ b/src/commands/b_list.cpp @@ -8,6 +8,7 @@ #include "common/Paths.h" #include "common/Colors.h" #include "common/Datasets.h" +#include "common/Utils.h" #include "reports/DatasetsExcel.h" #include "reports/DatasetsConsole.h" #include "results/ResultsDatasetConsole.h" @@ -24,9 +25,13 @@ void list_datasets(argparse::ArgumentParser& program) std::cout << report.getOutput(); if (excel) { auto data = report.getData(); - auto report = platform::DatasetsExcel(); - report.report(data); - std::cout << std::endl << Colors::GREEN() << "Output saved in " << report.getFileName() << std::endl; + auto ereport = new platform::DatasetsExcel(); + ereport->report(data); + std::cout << std::endl << Colors::GREEN() << "Output saved in " << ereport->getFileName() << std::endl; + auto fileName = ereport->getExcelFileName(); + delete ereport; + std::cout << "Opening " << fileName << std::endl; + platform::openFile(fileName); } } @@ -42,9 +47,13 @@ void list_results(argparse::ArgumentParser& program) std::cout << report.getOutput(); if (excel) { auto data = report.getData(); - auto report = platform::ResultsDatasetExcel(); - report.report(data); - std::cout << std::endl << Colors::GREEN() << "Output saved in " << report.getFileName() << std::endl; + auto ereport = new platform::ResultsDatasetExcel(); + ereport->report(data); + std::cout << std::endl << Colors::GREEN() << "Output saved in " << ereport->getFileName() << std::endl; + auto fileName = ereport->getExcelFileName(); + delete ereport; + std::cout << "Opening " << fileName << std::endl; + platform::openFile(fileName); } } diff --git a/src/commands/b_manage.cpp b/src/commands/b_manage.cpp index 1d88ca2..8a0deb4 100644 --- a/src/commands/b_manage.cpp +++ b/src/commands/b_manage.cpp @@ -1,7 +1,7 @@ + +#include #include #include -#include -#include #include "common/Paths.h" #include #include "manage/ManageScreen.h" @@ -53,65 +53,7 @@ void handleResize(int sig) manager->updateSize(rows, cols); } -void openFile(const std::string& fileName) -{ - // #ifdef __APPLE__ - // // macOS uses the "open" command - // std::string command = "open"; - // #elif defined(__linux__) - // // Linux typically uses "xdg-open" - // std::string command = "xdg-open"; - // #else - // // For other OSes, do nothing or handle differently - // std::cerr << "Unsupported platform." << std::endl; - // return; - // #endif - // execlp(command.c_str(), command.c_str(), fileName.c_str(), NULL); -#ifdef __APPLE__ - const char* tool = "/usr/bin/open"; -#elif defined(__linux__) - const char* tool = "/usr/bin/xdg-open"; -#else - std::cerr << "Unsupported platform." << std::endl; - return; -#endif - // We'll build an argv array for execve: - std::vector argv; - argv.push_back(const_cast(tool)); // argv[0] - argv.push_back(const_cast(fileName.c_str())); // argv[1] - argv.push_back(nullptr); - - // Make a new environment array, skipping BASH_FUNC_ variables - std::vector filteredEnv; - for (char** env = environ; *env != nullptr; ++env) { - // *env is a string like "NAME=VALUE" - // We want to skip those starting with "BASH_FUNC_" - if (strncmp(*env, "BASH_FUNC_", 10) == 0) { - // skip it - continue; - } - filteredEnv.push_back(*env); - } - - // Convert filteredEnv into a char* array - std::vector envp; - for (auto& var : filteredEnv) { - envp.push_back(const_cast(var.c_str())); - } - envp.push_back(nullptr); - - // Now call execve with the cleaned environment - // NOTE: You may need a full path to the tool if it's not in PATH, or use which() logic - // For now, let's assume "open" or "xdg-open" is found in the default PATH: - execve(tool, argv.data(), envp.data()); - - // If we reach here, execve failed - perror("execve failed"); - // This would terminate your current process if it's not in a child - // Usually you'd do something like: - _exit(EXIT_FAILURE); -} int main(int argc, char** argv) { @@ -137,7 +79,7 @@ int main(int argc, char** argv) delete manager; if (!fileName.empty()) { std::cout << "Opening " << fileName << std::endl; - openFile(fileName); + platform::openFile(fileName); } return 0; } diff --git a/src/common/Paths.h b/src/common/Paths.h index 15a42d1..6861457 100644 --- a/src/common/Paths.h +++ b/src/common/Paths.h @@ -49,6 +49,7 @@ namespace platform { return "BestResults_" + score + ".xlsx"; } static std::string excelResults() { return "some_results.xlsx"; } + static std::string excelDatasets() { return "datasets.xlsx"; } static std::string grid_input(const std::string& model) { return grid() + "grid_" + model + "_input.json"; @@ -73,6 +74,7 @@ namespace platform { { return "post_hoc.md"; } + }; } #endif \ No newline at end of file diff --git a/src/common/Utils.h b/src/common/Utils.h index 92e5a4a..e371d89 100644 --- a/src/common/Utils.h +++ b/src/common/Utils.h @@ -1,5 +1,7 @@ #ifndef UTILS_H #define UTILS_H + +#include #include #include #include @@ -66,5 +68,64 @@ namespace platform { oss << std::put_time(timeinfo, "%H:%M:%S"); return oss.str(); } + static void openFile(const std::string& fileName) + { + // #ifdef __APPLE__ + // // macOS uses the "open" command + // std::string command = "open"; + // #elif defined(__linux__) + // // Linux typically uses "xdg-open" + // std::string command = "xdg-open"; + // #else + // // For other OSes, do nothing or handle differently + // std::cerr << "Unsupported platform." << std::endl; + // return; + // #endif + // execlp(command.c_str(), command.c_str(), fileName.c_str(), NULL); +#ifdef __APPLE__ + const char* tool = "/usr/bin/open"; +#elif defined(__linux__) + const char* tool = "/usr/bin/xdg-open"; +#else + std::cerr << "Unsupported platform." << std::endl; + return; +#endif + + // We'll build an argv array for execve: + std::vector argv; + argv.push_back(const_cast(tool)); // argv[0] + argv.push_back(const_cast(fileName.c_str())); // argv[1] + argv.push_back(nullptr); + + // Make a new environment array, skipping BASH_FUNC_ variables + std::vector filteredEnv; + for (char** env = environ; *env != nullptr; ++env) { + // *env is a string like "NAME=VALUE" + // We want to skip those starting with "BASH_FUNC_" + if (strncmp(*env, "BASH_FUNC_", 10) == 0) { + // skip it + continue; + } + filteredEnv.push_back(*env); + } + + // Convert filteredEnv into a char* array + std::vector envp; + for (auto& var : filteredEnv) { + envp.push_back(const_cast(var.c_str())); + } + envp.push_back(nullptr); + + // Now call execve with the cleaned environment + // NOTE: You may need a full path to the tool if it's not in PATH, or use which() logic + // For now, let's assume "open" or "xdg-open" is found in the default PATH: + execve(tool, argv.data(), envp.data()); + + // If we reach here, execve failed + perror("execve failed"); + // This would terminate your current process if it's not in a child + // Usually you'd do something like: + _exit(EXIT_FAILURE); + } } #endif \ No newline at end of file diff --git a/src/reports/DatasetsExcel.cpp b/src/reports/DatasetsExcel.cpp index a24def6..cb1dd37 100644 --- a/src/reports/DatasetsExcel.cpp +++ b/src/reports/DatasetsExcel.cpp @@ -1,8 +1,9 @@ +#include "common/Paths.h" #include "DatasetsExcel.h" namespace platform { DatasetsExcel::DatasetsExcel() { - file_name = "datasets.xlsx"; + file_name = Paths::excelDatasets(); workbook = workbook_new(getFileName().c_str()); createFormats(); setProperties("Datasets"); diff --git a/src/reports/DatasetsExcel.h b/src/reports/DatasetsExcel.h index cd543cd..4b528b3 100644 --- a/src/reports/DatasetsExcel.h +++ b/src/reports/DatasetsExcel.h @@ -11,6 +11,7 @@ namespace platform { DatasetsExcel(); ~DatasetsExcel(); void report(json& data); + std::string getExcelFileName() { return getFileName(); } }; } #endif \ No newline at end of file diff --git a/src/results/ResultsDatasetExcel.cpp b/src/results/ResultsDatasetExcel.cpp index b4f1225..df1604e 100644 --- a/src/results/ResultsDatasetExcel.cpp +++ b/src/results/ResultsDatasetExcel.cpp @@ -1,8 +1,9 @@ +#include "common/Paths.h" #include "ResultsDatasetExcel.h" namespace platform { ResultsDatasetExcel::ResultsDatasetExcel() { - file_name = "some_results.xlsx"; + file_name = Paths::excelResults(); workbook = workbook_new(getFileName().c_str()); createFormats(); setProperties("Results"); diff --git a/src/results/ResultsDatasetExcel.h b/src/results/ResultsDatasetExcel.h index 83226cc..3f9b968 100644 --- a/src/results/ResultsDatasetExcel.h +++ b/src/results/ResultsDatasetExcel.h @@ -12,6 +12,7 @@ namespace platform { ResultsDatasetExcel(); ~ResultsDatasetExcel(); void report(json& data); + std::string getExcelFileName() { return getFileName(); } }; } #endif \ No newline at end of file