From 9f962123be30da94add3f966bb46962f8bfdea88 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 24 Sep 2009 21:50:11 +0200 Subject: UserList: Encapsulate list --- src/Client/UserListCommands.cpp | 87 +++++++++++++++++++++++++++++++++++++++-- src/Client/UserListCommands.h | 3 ++ 2 files changed, 87 insertions(+), 3 deletions(-) (limited to 'src/Client') diff --git a/src/Client/UserListCommands.cpp b/src/Client/UserListCommands.cpp index eb4aee1..420202e 100644 --- a/src/Client/UserListCommands.cpp +++ b/src/Client/UserListCommands.cpp @@ -162,10 +162,79 @@ void UserListCommands::showListCommand(CommandParser *commandParser, const std:: boost::shared_ptr userList = Common::UserLists::Util::deserializeUserList(result.first.get()); - std::cout << "User list '" << args[2] << "':" << std::endl; + std::cout << "User list '" << args[2]; - for(Common::UserLists::UserList::iterator user = userList->begin(); user != userList->end(); ++user) { - std::cout << " " << user->getName() << std::endl; + if(userList->isEmpty()) { + std::cout << " is empty." << std::endl; + } + else { + std::cout << "' contains " << userList->getLength() << " user"; + if(userList->getLength() > 1) + std::cout << "s"; + std::cout << "." << std::endl << std::endl; + + std::map lengths; + + static const std::string USER_NAME("User name"); + lengths.insert(std::make_pair(USER_NAME, USER_NAME.length())); + static const std::string GROUP_NAME("Group name"); + lengths.insert(std::make_pair(GROUP_NAME, GROUP_NAME.length())); + + std::set details = userList->getDetails(); + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) { + lengths.insert(std::make_pair(*detail, detail->length())); + } + + for(Common::UserLists::UserList::iterator user = userList->begin(); user != userList->end(); ++user) { + std::map::iterator it = lengths.find(USER_NAME); + if(user->getName().length() > it->second) + it->second = user->getName().length(); + + it = lengths.find(GROUP_NAME); + if(user->getGroup().length() > it->second) + it->second = user->getGroup().length(); + + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) { + it = lengths.find(*detail); + std::string detailStr = user->getDetail(*detail); + if(detailStr.length() > it->second) + it->second = detailStr.length(); + } + } + + printPadded(USER_NAME, lengths.find(USER_NAME)->second); + printPadded(GROUP_NAME, lengths.find(GROUP_NAME)->second); + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) { + printPadded(*detail, lengths.find(*detail)->second); + } + std::cout << std::endl; + + printDelimiter(lengths.find(USER_NAME)->second); + printDelimiter(lengths.find(GROUP_NAME)->second); + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) { + printDelimiter(lengths.find(*detail)->second); + } + std::cout << std::endl; + + for(Common::UserLists::UserList::iterator user = userList->begin(); user != userList->end(); ++user) { + static const std::string UNSET(""); + + if(!user->getName().empty()) + printPadded(user->getName(), lengths.find(USER_NAME)->second); + else + printPadded(UNSET, lengths.find(USER_NAME)->second); + + if(!user->getGroup().empty()) + printPadded(user->getGroup(), lengths.find(GROUP_NAME)->second); + else + printPadded(UNSET, lengths.find(GROUP_NAME)->second); + + for(std::set::iterator detail = details.begin(); detail != details.end(); ++detail) { + printPadded(user->getDetail(*detail), lengths.find(*detail)->second); + } + + std::cout << std::endl; + } } std::cout << std::endl; @@ -177,6 +246,18 @@ void UserListCommands::showListCommand(CommandParser *commandParser, const std:: } } +void UserListCommands::printPadded(const std::string &str, unsigned length) { + static const int EXTRA_PADDING = 3; + + std::cout << str; + if(str.length() < length + EXTRA_PADDING) + std::cout << std::string(length-str.length()+EXTRA_PADDING, ' '); +} + +void UserListCommands::printDelimiter(unsigned length) { + printPadded(std::string(length, '-'), length); +} + void UserListCommands::userListCommand(CommandParser *commandParser, const std::vector &args) { std::string cmd; diff --git a/src/Client/UserListCommands.h b/src/Client/UserListCommands.h index 17fcb4e..51a714b 100644 --- a/src/Client/UserListCommands.h +++ b/src/Client/UserListCommands.h @@ -42,6 +42,9 @@ class MAD_CLIENT_EXPORT UserListCommands { static void listCommand(CommandParser *commandParser, const std::vector &args); static void showListCommand(CommandParser *commandParser, const std::vector &args); + static void printPadded(const std::string &str, unsigned length); + static void printDelimiter(unsigned length); + public: static void userListCommand(CommandParser *commandParser, const std::vector &args); }; -- cgit v1.2.3