From 8635603e2f4f8b61026f69532f3cda51bd711f91 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 1 Oct 2009 00:38:20 +0200 Subject: UserListCommands: import: Edit column names before saving the list --- src/Client/UserListCommands.cpp | 59 ++++++++++++++++++++++++++++++++++++----- src/Client/XLSSheet.h | 4 +++ 2 files changed, 56 insertions(+), 7 deletions(-) (limited to 'src/Client') diff --git a/src/Client/UserListCommands.cpp b/src/Client/UserListCommands.cpp index 581b060..1da2d38 100644 --- a/src/Client/UserListCommands.cpp +++ b/src/Client/UserListCommands.cpp @@ -236,18 +236,50 @@ void UserListCommands::importCommand(CommandParser *commandParser, const std::ve std::cout << "Imported worksheet:" << std::endl; printSheet(**sheet, 10); - std::cout << std::endl << std::endl; + std::cout << std::endl; std::cout << "Does the first line of the worksheet contain the column names? (Y/n) " << std::flush; Core::String input = Core::String::getline(std::cin); + std::cout << std::endl << std ::endl; input.toLower(); if(input.isEmpty() || input[0] != 'n') { (*sheet)->useFirstRowAsColumnNames(true); + printSheet(**sheet, 10); + } + else { + printSheet(**sheet, 5); } + std::cout << std::endl; - std::cout << std::endl << std ::endl; - printSheet(**sheet, 10); - std::cout << std::endl << std ::endl; + while(true) { + std::cout << "Edit column names:" << std::endl << std::endl; + + for(unsigned i = 0; i < (*sheet)->getColumnCount(); ++i) { + std::cout << "[" << i+1 << "] " << (*sheet)->getColumnName(i) << std::endl; + } + + std::cout << std::endl; + std::cout << "Enter number of column to edit or press to save the user list: " << std::flush; + input = Core::String::getline(std::cin); + if(input.isEmpty()) + break; + + unsigned col = strtoul(input.toString().c_str(), 0, 10); + if(col < 1 || col > (*sheet)->getColumnCount()) { + std::cout << "Invalid input. " << std::endl << std::endl; + printSheet(**sheet, 5); + std::cout << std::endl; + continue; + } + + std::cout << "Enter the new name for column " << col << ". An empty name will mark the column for deletion" << std::endl; + std::cout << "New name: " << std::flush; + input = Core::String::getline(std::cin); + (*sheet)->setColumnName(col-1, input); + + printSheet(**sheet, 5); + std::cout << std::endl; + } boost::shared_ptr userList(new Common::UserLists::UserList); const std::vector &rows = (*sheet)->getRows(); @@ -262,7 +294,8 @@ void UserListCommands::importCommand(CommandParser *commandParser, const std::ve entry.setName(stream.str().c_str()); for(unsigned col = 0; col < (*sheet)->getColumnCount(); ++col) { - entry.setDetail((*sheet)->getColumnName(col), row[col]); + if(!(*sheet)->getColumnName(col).isEmpty()) + entry.setDetail((*sheet)->getColumnName(col), row[col]); } userList->addUser(entry); @@ -395,11 +428,17 @@ void UserListCommands::printUserList(const Common::UserLists::UserList &list) { } void UserListCommands::printSheet(const XLSSheet &sheet, unsigned rowCount) { + static const Core::String DELETE = ""; + const std::vector &rows = sheet.getRows(); std::vector lengths(sheet.getColumnCount()); for(unsigned col = 0; col < sheet.getColumnCount(); ++col) { - lengths[col] = sheet.getColumnName(col).length(); + boost::int32_t length = sheet.getColumnName(col).length(); + if(length > 0) + lengths[col] = length; + else + lengths[col] = DELETE.length(); } for(std::vector::const_iterator rowIt = rows.begin(); rowIt != rows.end() && (rowCount == 0 || rowIt != rows.begin()+rowCount); ++rowIt) { @@ -413,7 +452,11 @@ void UserListCommands::printSheet(const XLSSheet &sheet, unsigned rowCount) { for(unsigned col = 0; col < sheet.getColumnCount(); ++col) { std::cout << " "; - printPadded(sheet.getColumnName(col), lengths[col]); + + if(!sheet.getColumnName(col).isEmpty()) + printPadded(sheet.getColumnName(col), lengths[col]); + else + printPadded(DELETE, lengths[col]); } std::cout << std::endl; @@ -441,6 +484,8 @@ void UserListCommands::printSheet(const XLSSheet &sheet, unsigned rowCount) { std::cout << " "; printPadded("...", lengths[col]); } + + std::cout << std::endl; } } diff --git a/src/Client/XLSSheet.h b/src/Client/XLSSheet.h index 0960fc0..8cd06e2 100644 --- a/src/Client/XLSSheet.h +++ b/src/Client/XLSSheet.h @@ -80,6 +80,10 @@ class MAD_CLIENT_EXPORT XLSSheet : private boost::noncopyable { return colNames[col]; } + void setColumnName(unsigned col, const Core::String &name) { + colNames[col] = name; + } + const std::vector& getRows() const { return rows; } -- cgit v1.2.3