feat: better formating of project list table
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dominik Meyer 2024-08-09 22:30:29 +02:00
parent 39a1f8c7e1
commit 9b640ac751
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97
5 changed files with 94 additions and 55 deletions

3
.gitmodules vendored
View File

@ -16,3 +16,6 @@
[submodule "libs/url"] [submodule "libs/url"]
path = libs/url path = libs/url
url = https://github.com/cpp-netlib/url.git url = https://github.com/cpp-netlib/url.git
[submodule "libs/tableprinter"]
path = libs/tableprinter
url = https://github.com/OzanCansel/tableprinter.git

View File

@ -61,6 +61,9 @@ IF(NOT TARGET httplib::httplib)
add_subdirectory(libs/cpp-httplib EXCLUDE_FROM_ALL) add_subdirectory(libs/cpp-httplib EXCLUDE_FROM_ALL)
ENDIF() ENDIF()
IF(NOT TARGET tableprinter::tableprinter)
add_subdirectory(libs/tableprinter EXCLUDE_FROM_ALL )
ENDIF()
SET(REDMINE_LIBARIES loguru nlohmann_json::nlohmann_json httplib::httplib) 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.hpp
src/Redmine-CLI/Command/IssueStatus.cpp 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 target_include_directories(redmine-cli
PRIVATE PRIVATE
src src

1
libs/tableprinter Submodule

@ -0,0 +1 @@
Subproject commit 3378a6d6261729a4cb8e55e13a0dd850eb4ba79b

View File

@ -19,7 +19,7 @@
#include <Redmine-CLI/Redmine.hpp> #include <Redmine-CLI/Redmine.hpp>
#include "Redmine/API.hpp" #include "Redmine/API.hpp"
#include "nlohmann/json_fwd.hpp" #include "nlohmann/json_fwd.hpp"
#include <tableprinter/tableprinter.hpp>
Command::Project::Project(RedmineCLI::Redmine *r) Command::Project::Project(RedmineCLI::Redmine *r)
: :
@ -56,78 +56,98 @@ void Command::Project::uploadFile_() const
client.uploadFileToProject(projectId_, filePath_, fileName_, fileDescription_,fileVersion_); client.uploadFileToProject(projectId_, filePath_, fileName_, fileDescription_,fileVersion_);
} }
void Command::Project::getList_() const void Command::Project::printProjectList_(const std::vector<Redmine::Project> &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 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 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(); 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<Redmine::Project> &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<Redmine::Project> &projects, bool printName, bool printID, bool printIdentifier) const
{
nlohmann::json data = nlohmann::json::array(); nlohmann::json data = nlohmann::json::array();
std::cout.setf(std::ios::fixed | std::ios::right);
for (auto it = projects.begin(); it!= projects.end(); ++it) for (auto it = projects.begin(); it!= projects.end(); ++it)
{ {
nlohmann::json project; nlohmann::json project;
if (hasId) if (printID)
{ {
if (output_==TEXT) project["id"]=it->getId();
{
std::cout << it->getId() << " ";
}
else if (output_ == YAML)
{
project["id"]=it->getId();
}
} }
if (hasStrI) if (printIdentifier)
{ {
if (output_==TEXT) project["identifier"]=it->getStringID();
{
std::cout << it->getStringID() << " ";
}
else if (output_ == YAML)
{
project["identifier"]=it->getStringID();
}
} }
if (printName)
if (hasName)
{ {
if (output_==TEXT) project["name"]=it->getName();
{
std::cout << "\"" << it->getName() << "\" ";
}
else if (output_ == YAML)
{
project["name"]=it->getName();
}
} }
data.push_back(project);
if (output_ == TEXT)
{
std::cout << std::endl;
}
else if (output_==YAML)
{
data.push_back(project);
}
} }
if (output_ == YAML) std::cout << data.dump(2);
{ }
std::cout << data.dump(2);
} void Command::Project::getList_() const
{
Redmine::API client{redmine_->getUrl(), redmine_->getToken()};
auto projects = client.getProjects();
printProjectList_(projects);
return;
} }

View File

@ -15,12 +15,15 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "Redmine/Project.hpp"
#include "nlohmann/json_fwd.hpp"
#include <CLI/App.hpp> #include <CLI/App.hpp>
#include <Redmine/API.hpp> #include <Redmine/API.hpp>
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
#define LOGURU_WITH_STREAMS 1 #define LOGURU_WITH_STREAMS 1
#include <loguru.hpp> #include <loguru.hpp>
#include <CLI/CLI.hpp> #include <CLI/CLI.hpp>
@ -52,14 +55,14 @@ namespace Command
{ {
/// output is just text /// output is just text
TEXT, TEXT,
/// output is yaml /// output is json
YAML JSON
}; };
/// requested output type /// requested output type
outputType output_; outputType output_;
std::map<std::string, outputType> outputMap{{"text", TEXT}, {"yaml", YAML}}; std::map<std::string, outputType> outputMap{{"text", TEXT}, {"json", JSON}};
/// vector to hold requested attribute fields /// vector to hold requested attribute fields
std::vector<std::string> fields_; std::vector<std::string> fields_;
@ -85,7 +88,16 @@ namespace Command
/// pointer to the upload subcommand /// pointer to the upload subcommand
CLI::App* upload_; CLI::App* upload_;
/**
* @brief method to print the project list
*
* @param projects - the projects to print in json
*/
void printProjectList_(const std::vector<Redmine::Project> &projects) const;
void printProjectListJson_(const std::vector<Redmine::Project> &projects, bool printName, bool printID, bool printIdentifier) const;
void printProjectListText_(const std::vector<Redmine::Project> &projects, bool printName, bool printID, bool printIdentifier) const;
/// the actual implementation of the list command /// the actual implementation of the list command
void getList_() const; void getList_() const;