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/state.cpp | |
parent | 7b620d6baf986dfac679fe0ad4a66a8ffc892d86 (diff) | |
download | sink-7a1eccc13dc0828c292dc1cc6d1556fa38011da3.tar.gz sink-7a1eccc13dc0828c292dc1cc6d1556fa38011da3.zip |
sinksh: support for printing tables
Diffstat (limited to 'sinksh/state.cpp')
-rw-r--r-- | sinksh/state.cpp | 38 |
1 files changed, 38 insertions, 0 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) { |