From de7cf091befece1b548f9010e72b74afc696607d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Tue, 25 Feb 2025 13:41:06 +0100 Subject: [PATCH] Add open excel file on b_manage termination --- src/commands/b_manage.cpp | 65 +++++++++++++++++++++++++++++++++++++ src/manage/ManageScreen.cpp | 4 ++- src/manage/ManageScreen.h | 2 ++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/commands/b_manage.cpp b/src/commands/b_manage.cpp index 17c1465..0dda157 100644 --- a/src/commands/b_manage.cpp +++ b/src/commands/b_manage.cpp @@ -51,6 +51,66 @@ 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) { auto program = argparse::ArgumentParser("b_manage", { platform_project_version.begin(), platform_project_version.end() }); @@ -67,6 +127,11 @@ int main(int argc, char** argv) auto [rows, cols] = numRowsCols(); manager = new platform::ManageScreen(rows, cols, model, score, platform, complete, partial, compare); manager->doMenu(); + auto fileName = manager->getExcelFileName(); delete manager; + if (!fileName.empty()) { + std::cout << "Opening " << fileName << std::endl; + openFile(fileName); + } return 0; } diff --git a/src/manage/ManageScreen.cpp b/src/manage/ManageScreen.cpp index 30c2b65..6648d94 100644 --- a/src/manage/ManageScreen.cpp +++ b/src/manage/ManageScreen.cpp @@ -82,10 +82,12 @@ namespace platform { workbook_close(workbook); } if (didExcel) { - std::cout << Colors::MAGENTA() << "Excel file created: " << Paths::excel() + Paths::excelResults() << std::endl; + excelFileName = Paths::excel() + Paths::excelResults(); + std::cout << Colors::MAGENTA() << "Excel file created: " << excelFileName << std::endl; } std::cout << Colors::RESET() << "Done!" << std::endl; } + std::string ManageScreen::getVersions() { std::string kfold_version = folding::KFold(5, 100).version(); diff --git a/src/manage/ManageScreen.h b/src/manage/ManageScreen.h index 40cedc6..7e41896 100644 --- a/src/manage/ManageScreen.h +++ b/src/manage/ManageScreen.h @@ -19,6 +19,7 @@ namespace platform { ~ManageScreen() = default; void doMenu(); void updateSize(int rows, int cols); + std::string getExcelFileName() const { return excelFileName; } private: void list(const std::string& status, const std::string& color); void list_experiments(const std::string& status, const std::string& color); @@ -58,6 +59,7 @@ namespace platform { std::vector paginator; ResultsManager results; lxw_workbook* workbook; + std::string excelFileName; }; } #endif \ No newline at end of file