diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-09 17:46:26 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-09 17:46:26 +0200 |
commit | 7a1eccc13dc0828c292dc1cc6d1556fa38011da3 (patch) | |
tree | d252a36e78f42a65aa052aa5f9cc9e7e60ba2c69 /sinksh | |
parent | 7b620d6baf986dfac679fe0ad4a66a8ffc892d86 (diff) | |
download | sink-7a1eccc13dc0828c292dc1cc6d1556fa38011da3.tar.gz sink-7a1eccc13dc0828c292dc1cc6d1556fa38011da3.zip |
sinksh: support for printing tables
Diffstat (limited to 'sinksh')
-rw-r--r-- | sinksh/state.cpp | 38 | ||||
-rw-r--r-- | sinksh/state.h | 6 | ||||
-rw-r--r-- | sinksh/syntax_modules/sink_list.cpp | 21 |
3 files changed, 55 insertions, 10 deletions
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: | |||
48 | QEventLoop *event = 0; | 48 | QEventLoop *event = 0; |
49 | bool timing = false; | 49 | bool timing = false; |
50 | QTextStream outStream; | 50 | QTextStream outStream; |
51 | QList<QStringList> table; | ||
51 | }; | 52 | }; |
52 | 53 | ||
53 | State::State() : d(new Private) | 54 | State::State() : d(new Private) |
@@ -75,6 +76,43 @@ void State::printError(const QString &errorMessage, const QString &errorCode) co | |||
75 | printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); | 76 | printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); |
76 | } | 77 | } |
77 | 78 | ||
79 | void State::stageTableLine(const QStringList &line) const | ||
80 | { | ||
81 | d->table << line; | ||
82 | } | ||
83 | |||
84 | void State::printTable(const QList<QStringList> &table) const | ||
85 | { | ||
86 | //First let's find out the maximum size for each column depending on the content | ||
87 | QVector<int> columnSizes; | ||
88 | columnSizes.fill(0, 10); | ||
89 | for (const auto &row : table) { | ||
90 | for (int columnIndex = 0; columnIndex < row.size(); columnIndex++) { | ||
91 | if (columnSizes.size() <= columnIndex) { | ||
92 | columnSizes.append(0); | ||
93 | } | ||
94 | columnSizes[columnIndex] = qMax(columnSizes[columnIndex], row[columnIndex].size()); | ||
95 | } | ||
96 | } | ||
97 | //And now print the table | ||
98 | for (const auto &row : table) { | ||
99 | for (int columnIndex = 0; columnIndex < row.size(); columnIndex++) { | ||
100 | if (columnIndex > 0) { | ||
101 | d->outStream << " | "; | ||
102 | } | ||
103 | d->outStream << row[columnIndex].leftJustified(columnSizes[columnIndex], ' ', true); | ||
104 | } | ||
105 | d->outStream << "\n"; | ||
106 | } | ||
107 | d->outStream.flush(); | ||
108 | } | ||
109 | |||
110 | void State::flushTable() const | ||
111 | { | ||
112 | printTable(d->table); | ||
113 | d->table.clear(); | ||
114 | } | ||
115 | |||
78 | void State::setDebugLevel(unsigned int level) | 116 | void State::setDebugLevel(unsigned int level) |
79 | { | 117 | { |
80 | if (level < 7) { | 118 | 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: | |||
30 | void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const; | 30 | void printLine(const QString &message = QString(), unsigned int indentationLevel = 0) const; |
31 | void printError(const QString &errorMessage, const QString &errorCode = QString()) const; | 31 | void printError(const QString &errorMessage, const QString &errorCode = QString()) const; |
32 | 32 | ||
33 | void stageTableLine(const QStringList &) const; | ||
34 | void flushTable() const; | ||
35 | |||
33 | void setDebugLevel(unsigned int level); | 36 | void setDebugLevel(unsigned int level); |
34 | unsigned int debugLevel() const; | 37 | unsigned int debugLevel() const; |
35 | 38 | ||
@@ -46,6 +49,9 @@ public: | |||
46 | static bool hasEventLoop(); | 49 | static bool hasEventLoop(); |
47 | 50 | ||
48 | private: | 51 | private: |
52 | void printTable(const QList<QStringList> &) const; | ||
53 | |||
54 | private: | ||
49 | class Private; | 55 | class Private; |
50 | Private *const d; | 56 | Private *const d; |
51 | }; | 57 | }; |
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) | |||
69 | } | 69 | } |
70 | 70 | ||
71 | //qDebug() << "Listing"; | 71 | //qDebug() << "Listing"; |
72 | int colSize = 38; //Necessary to display a complete UUID | 72 | QStringList line; |
73 | state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) + | 73 | line << QObject::tr("Resource") << QObject::tr("Identifier"); |
74 | QObject::tr("Identifier").leftJustified(colSize, ' ', true)); | ||
75 | for (int i = 0; i < model->columnCount(QModelIndex()); i++) { | 74 | for (int i = 0; i < model->columnCount(QModelIndex()); i++) { |
76 | state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true)); | 75 | line << model->headerData(i, Qt::Horizontal).toString(); |
77 | } | 76 | } |
78 | state.printLine(); | 77 | state.stageTableLine(line); |
79 | 78 | ||
80 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) { | 79 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, state](const QModelIndex &index, int start, int end) { |
81 | for (int i = start; i <= end; i++) { | 80 | for (int i = start; i <= end; i++) { |
82 | auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>(); | 81 | auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>(); |
83 | state.print(object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true)); | 82 | QStringList line; |
84 | state.print(object->identifier().leftJustified(colSize, ' ', true)); | 83 | line << object->resourceInstanceIdentifier(); |
84 | line << object->identifier(); | ||
85 | for (int col = 0; col < model->columnCount(QModelIndex()); col++) { | 85 | for (int col = 0; col < model->columnCount(QModelIndex()); col++) { |
86 | state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true)); | 86 | line << model->data(model->index(i, col, index)).toString(); |
87 | } | 87 | } |
88 | state.printLine(); | 88 | state.stageTableLine(line); |
89 | } | 89 | } |
90 | }); | 90 | }); |
91 | 91 | ||
92 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { | 92 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { |
93 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { | 93 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { |
94 | state.flushTable(); | ||
94 | state.commandFinished(); | 95 | state.commandFinished(); |
95 | } | 96 | } |
96 | }); | 97 | }); |