From 4f135f1e9ceb382930559af7a89af636e69f1737 Mon Sep 17 00:00:00 2001 From: Derick Diaz Date: Thu, 25 Jan 2024 05:19:20 -0600 Subject: [PATCH] Change user info display on history command to include display name and username --- libdnf5-cli/output/transactioninfo.cpp | 32 +++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/libdnf5-cli/output/transactioninfo.cpp b/libdnf5-cli/output/transactioninfo.cpp index 75f271458..ec0a433ce 100644 --- a/libdnf5-cli/output/transactioninfo.cpp +++ b/libdnf5-cli/output/transactioninfo.cpp @@ -22,9 +22,39 @@ along with libdnf. If not, see . #include "utils/string.hpp" +#include + +#include +#include namespace libdnf5::cli::output { +namespace { +std::string generate_user_info_str(uint32_t user_id) { + auto results = std::to_string(user_id); + auto user_info = getpwuid(user_id); + + if (user_info == NULL) { + return results; + }; + + // If gecos information does exists, it is generally CSV formatted. + // This section assumes that the first entry in the gecos information is the display name. + if (user_info->pw_gecos && strlen(user_info->pw_gecos) > 0) { + std::stringstream s_stream(user_info->pw_gecos); + std::string display_name; + std::getline(s_stream, display_name, ','); + results += std::format(" {}", display_name); + }; + + if (user_info->pw_name && strlen(user_info->pw_name) > 0) { + results += std::format(" <{}>", user_info->pw_name); + }; + + return results; +} +} // namespace + void print_transaction_info(libdnf5::transaction::Transaction & transaction) { KeyValueTable info; info.add_line("Transaction ID", transaction.get_id(), "bold"); @@ -33,7 +63,7 @@ void print_transaction_info(libdnf5::transaction::Transaction & transaction) { info.add_line("End time", libdnf5::utils::string::format_epoch(transaction.get_dt_end())); info.add_line("End rpmdb", transaction.get_rpmdb_version_end()); - info.add_line("User", transaction.get_user_id()); + info.add_line("User", generate_user_info_str(transaction.get_user_id())); info.add_line("Status", libdnf5::transaction::transaction_state_to_string(transaction.get_state())); info.add_line("Releasever", transaction.get_releasever()); info.add_line("Description", transaction.get_description());