diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-09-24 21:50:11 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-09-24 21:50:11 +0200 |
commit | 9f962123be30da94add3f966bb46962f8bfdea88 (patch) | |
tree | ed1a210da3d5ba5d8d7d0807dd4e6ed4de4172f4 /src/Client | |
parent | 1cf531a5949cad1f68575188cf00d147478e029c (diff) | |
download | mad-9f962123be30da94add3f966bb46962f8bfdea88.tar mad-9f962123be30da94add3f966bb46962f8bfdea88.zip |
UserList: Encapsulate list
Diffstat (limited to 'src/Client')
-rw-r--r-- | src/Client/UserListCommands.cpp | 87 | ||||
-rw-r--r-- | src/Client/UserListCommands.h | 3 |
2 files changed, 87 insertions, 3 deletions
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<Common::UserLists::UserList> 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<std::string, unsigned> 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<std::string> details = userList->getDetails(); + for(std::set<std::string>::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<std::string, unsigned>::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<std::string>::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<std::string>::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<std::string>::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("<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<std::string>::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<std::string> &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<std::string> &args); static void showListCommand(CommandParser *commandParser, const std::vector<std::string> &args); + static void printPadded(const std::string &str, unsigned length); + static void printDelimiter(unsigned length); + public: static void userListCommand(CommandParser *commandParser, const std::vector<std::string> &args); }; |