From 7a1eccc13dc0828c292dc1cc6d1556fa38011da3 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 9 Apr 2016 17:46:26 +0200 Subject: sinksh: support for printing tables --- sinksh/state.cpp | 38 +++++++++++++++++++++++++++++++++++++ sinksh/state.h | 6 ++++++ sinksh/syntax_modules/sink_list.cpp | 21 ++++++++++---------- 3 files changed, 55 insertions(+), 10 deletions(-) (limited to 'sinksh') diff --git a/sinksh/state.cpp b/sinksh/state.cpp index 7fd3959..6fce21f 100644 --- a/sinksh/state.cpp +++ b/sinksh/state.cpp @@ -48,6 +48,7 @@ public: QEventLoop *event = 0; bool timing = false; QTextStream outStream; + QList table; }; State::State() : d(new Private) @@ -75,6 +76,43 @@ void State::printError(const QString &errorMessage, const QString &errorCode) co printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); } +void State::stageTableLine(const QStringList &line) const +{ + d->table << line; +} + +void State::printTable(const QList &table) const +{ + //First let's find out the maximum size for each column depending on the content + QVector columnSizes; + columnSizes.fill(0, 10); + for (const auto &row : table) { + for (int columnIndex = 0; columnIndex < row.size(); columnIndex++) { + if (columnSizes.size() <= columnIndex) { + columnSizes.append(0); + } + columnSizes[columnIndex] = qMax(columnSizes[columnIndex], row[columnIndex].size()); + } + } + //And now print the table + for (const auto &row : table) { + for (int columnIndex = 0; columnIndex < row.size(); columnIndex++) { + if (columnIndex > 0) { + d->outStream << " | "; + } + d->outStream << row[columnIndex].leftJustified(columnSizes[columnIndex], ' ', true); + } + d->outStream << "\n"; + } + d->outStream.flush(); +} + +void State::flushTable() const +{ + printTable(d->table); + d->table.clear(); +} + void State::setDebugLevel(unsigned int level) { if (level < 7) { diff --git a/sinksh/state.h b/sinksh/state.h index 2a0eb7c..0796caf 100644 --- a/sinksh/state.h +++ b/sinksh/state.h @@ -30,6 +30,9 @@ public: void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const; void printError(const QString &errorMessage, const QString &errorCode = QString()) const; + void stageTableLine(const QStringList &) const; + void flushTable() const; + void setDebugLevel(unsigned int level); unsigned int debugLevel() const; @@ -45,6 +48,9 @@ public: static void setHasEventLoop(bool evented); static bool hasEventLoop(); +private: + void printTable(const QList &) const; + private: class Private; Private *const d; diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp index 9712b6f..cb71ce4 100644 --- a/sinksh/syntax_modules/sink_list.cpp +++ b/sinksh/syntax_modules/sink_list.cpp @@ -69,28 +69,29 @@ bool list(const QStringList &args, State &state) } //qDebug() << "Listing"; - int colSize = 38; //Necessary to display a complete UUID - state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) + - QObject::tr("Identifier").leftJustified(colSize, ' ', true)); + QStringList line; + line << QObject::tr("Resource") << QObject::tr("Identifier"); for (int i = 0; i < model->columnCount(QModelIndex()); i++) { - state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true)); + line << model->headerData(i, Qt::Horizontal).toString(); } - state.printLine(); + state.stageTableLine(line); - QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) { + QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, state](const QModelIndex &index, int start, int end) { for (int i = start; i <= end; i++) { auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value(); - state.print(object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true)); - state.print(object->identifier().leftJustified(colSize, ' ', true)); + QStringList line; + line << object->resourceInstanceIdentifier(); + line << object->identifier(); for (int col = 0; col < model->columnCount(QModelIndex()); col++) { - state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true)); + line << model->data(model->index(i, col, index)).toString(); } - state.printLine(); + state.stageTableLine(line); } }); QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { if (roles.contains(Sink::Store::ChildrenFetchedRole)) { + state.flushTable(); state.commandFinished(); } }); -- cgit v1.2.3