From 9b640ac75101536650d377501c5abc2e4db03760 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Fri, 9 Aug 2024 22:30:29 +0200 Subject: [PATCH] feat: better formating of project list table --- .gitmodules | 3 + CMakeLists.txt | 5 +- libs/tableprinter | 1 + src/Redmine-CLI/Command/Project.cpp | 122 ++++++++++++++++------------ src/Redmine-CLI/Command/Project.hpp | 18 +++- 5 files changed, 94 insertions(+), 55 deletions(-) create mode 160000 libs/tableprinter diff --git a/.gitmodules b/.gitmodules index 2f71f67..a538aba 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "libs/url"] path = libs/url url = https://github.com/cpp-netlib/url.git +[submodule "libs/tableprinter"] + path = libs/tableprinter + url = https://github.com/OzanCansel/tableprinter.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 3044490..7040f07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,9 @@ IF(NOT TARGET httplib::httplib) add_subdirectory(libs/cpp-httplib EXCLUDE_FROM_ALL) ENDIF() +IF(NOT TARGET tableprinter::tableprinter) + add_subdirectory(libs/tableprinter EXCLUDE_FROM_ALL ) +ENDIF() SET(REDMINE_LIBARIES loguru nlohmann_json::nlohmann_json httplib::httplib) @@ -127,7 +130,7 @@ add_executable(redmine-cli src/Redmine-CLI/Command/IssueStatus.hpp src/Redmine-CLI/Command/IssueStatus.cpp ) -target_link_libraries(redmine-cli redmine-api-cpp-static loguru CLI11) +target_link_libraries(redmine-cli redmine-api-cpp-static loguru CLI11 tableprinter::tableprinter) target_include_directories(redmine-cli PRIVATE src diff --git a/libs/tableprinter b/libs/tableprinter new file mode 160000 index 0000000..3378a6d --- /dev/null +++ b/libs/tableprinter @@ -0,0 +1 @@ +Subproject commit 3378a6d6261729a4cb8e55e13a0dd850eb4ba79b diff --git a/src/Redmine-CLI/Command/Project.cpp b/src/Redmine-CLI/Command/Project.cpp index 3c41298..208c901 100644 --- a/src/Redmine-CLI/Command/Project.cpp +++ b/src/Redmine-CLI/Command/Project.cpp @@ -19,7 +19,7 @@ #include #include "Redmine/API.hpp" #include "nlohmann/json_fwd.hpp" - +#include Command::Project::Project(RedmineCLI::Redmine *r) : @@ -56,78 +56,98 @@ void Command::Project::uploadFile_() const client.uploadFileToProject(projectId_, filePath_, fileName_, fileDescription_,fileVersion_); } -void Command::Project::getList_() const +void Command::Project::printProjectList_(const std::vector &projects) const { - Redmine::API client{redmine_->getUrl(), redmine_->getToken()}; - - auto projects = client.getProjects(); - bool hasName = std::find(fields_.begin(), fields_.end(),"name") != fields_.end() || fields_.empty(); bool hasId = std::find(fields_.begin(), fields_.end(),"id") != fields_.end() || fields_.empty(); bool hasStrI = std::find(fields_.begin(), fields_.end(),"strid") != fields_.end() || fields_.empty(); + if (output_ == TEXT) + { + printProjectListText_(projects, hasName, hasId, hasStrI); + } + else if (output_ == JSON) + { + printProjectListJson_(projects, hasName, hasId, hasStrI); + } +} + +void Command::Project::printProjectListText_(const std::vector &projects, bool printName, bool printID, bool printIdentifier) const +{ + tableprinter::printer p + { + { + { tableprinter::name { "ID" } , tableprinter::width { 8 } } , + { tableprinter::name { "Identifier" } , tableprinter::width { 20 } } , + { tableprinter::name { "Name" } , tableprinter::width { 30 } } , + } , + { std::cout } + }; + + p.sanity_check(); + p.print_headers(); + + for (auto it = projects.begin(); it!= projects.end(); ++it) + { + if (printID && printIdentifier && printName) + { + p.print(it->getId(), it->getStringID(), it->getName()); + } + else if (printID && printIdentifier && !printName) + { + p.print(it->getId(), it->getStringID()); + } + else if (printID && !printIdentifier && printName) + { + p.print(it->getId(), it->getName()); + } + else if (!printID && printIdentifier && printName) + { + p.print(it->getStringID(), it->getName()); + } + } + +} + +void Command::Project::printProjectListJson_(const std::vector &projects, bool printName, bool printID, bool printIdentifier) const +{ nlohmann::json data = nlohmann::json::array(); - - std::cout.setf(std::ios::fixed | std::ios::right); - + for (auto it = projects.begin(); it!= projects.end(); ++it) { nlohmann::json project; - if (hasId) + if (printID) { - if (output_==TEXT) - { - std::cout << it->getId() << " "; - } - else if (output_ == YAML) - { - project["id"]=it->getId(); - } + project["id"]=it->getId(); } - if (hasStrI) + if (printIdentifier) { - if (output_==TEXT) - { - std::cout << it->getStringID() << " "; - } - else if (output_ == YAML) - { - project["identifier"]=it->getStringID(); - } + project["identifier"]=it->getStringID(); } - - if (hasName) + if (printName) { - if (output_==TEXT) - { - std::cout << "\"" << it->getName() << "\" "; - } - else if (output_ == YAML) - { - project["name"]=it->getName(); - } + project["name"]=it->getName(); } - - - if (output_ == TEXT) - { - std::cout << std::endl; - } - else if (output_==YAML) - { - data.push_back(project); - } + data.push_back(project); } + + std::cout << data.dump(2); +} - if (output_ == YAML) - { - std::cout << data.dump(2); - } +void Command::Project::getList_() const +{ + Redmine::API client{redmine_->getUrl(), redmine_->getToken()}; + + auto projects = client.getProjects(); + + printProjectList_(projects); + + return; } \ No newline at end of file diff --git a/src/Redmine-CLI/Command/Project.hpp b/src/Redmine-CLI/Command/Project.hpp index 4bca1cc..49ec723 100644 --- a/src/Redmine-CLI/Command/Project.hpp +++ b/src/Redmine-CLI/Command/Project.hpp @@ -15,12 +15,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include "Redmine/Project.hpp" +#include "nlohmann/json_fwd.hpp" #include #include #include #include #include #include +#include #define LOGURU_WITH_STREAMS 1 #include #include @@ -52,14 +55,14 @@ namespace Command { /// output is just text TEXT, - /// output is yaml - YAML + /// output is json + JSON }; /// requested output type outputType output_; - std::map outputMap{{"text", TEXT}, {"yaml", YAML}}; + std::map outputMap{{"text", TEXT}, {"json", JSON}}; /// vector to hold requested attribute fields std::vector fields_; @@ -85,7 +88,16 @@ namespace Command /// pointer to the upload subcommand CLI::App* upload_; + /** + * @brief method to print the project list + * + * @param projects - the projects to print in json + */ + void printProjectList_(const std::vector &projects) const; + void printProjectListJson_(const std::vector &projects, bool printName, bool printID, bool printIdentifier) const; + + void printProjectListText_(const std::vector &projects, bool printName, bool printID, bool printIdentifier) const; /// the actual implementation of the list command void getList_() const;