diff options
author | David Faure <david.faure@kdab.com> | 2013-09-02 10:26:21 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-11-17 00:45:03 +0100 |
commit | 2026e502596cf6d447caf9545ffed8d3f73fe597 (patch) | |
tree | 34a1b9181221c6da45d0f0edaeabe46ffccafbca /src/corelib/tools/qcommandlineparser.cpp | |
parent | afe8e368721b6cac34d3b8de7689b53200f8c6fb (diff) |
QCommandLineParser: add word-wrapping algorithm
Rather than breaking at column 79 precisely, break entire words,
to improve readability.
Change-Id: Ie30db00f0e6ed95cce87480c3b91804826c6076b
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Diffstat (limited to 'src/corelib/tools/qcommandlineparser.cpp')
-rw-r--r-- | src/corelib/tools/qcommandlineparser.cpp | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index 8054542333..5463e4f0c1 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -845,11 +845,50 @@ static QString wrapText(const QString &names, int longestOptionNameString, const { const QLatin1Char nl('\n'); QString text = QStringLiteral(" ") + names.leftJustified(longestOptionNameString) + QLatin1Char(' '); - const int leftColumnWidth = text.length(); - const int rightColumnWidth = 79 - leftColumnWidth; - text += description.left(rightColumnWidth) + nl; - for (int n = rightColumnWidth; n < description.length(); n += rightColumnWidth) - text += QStringLiteral(" ").repeated(leftColumnWidth) + description.mid(n, rightColumnWidth) + nl; + const int indent = text.length(); + int lineStart = 0; + int lastBreakable = -1; + const int max = 79 - indent; + int x = 0; + const int len = description.length(); + + for (int i = 0; i < len; ++i) { + ++x; + const QChar c = description.at(i); + if (c.isSpace()) + lastBreakable = i; + + int breakAt = -1; + int nextLineStart = -1; + if (x > max && lastBreakable != -1) { + // time to break and we know where + breakAt = lastBreakable; + nextLineStart = lastBreakable + 1; + } else if ((x > max - 1 && lastBreakable == -1) || i == len - 1) { + // time to break but found nowhere [-> break here], or end of last line + breakAt = i + 1; + nextLineStart = breakAt; + } else if (c == nl) { + // forced break + breakAt = i; + nextLineStart = i + 1; + } + + if (breakAt != -1) { + const int numChars = breakAt - lineStart; + //qDebug() << "breakAt=" << description.at(breakAt) << "breakAtSpace=" << breakAtSpace << lineStart << "to" << breakAt << description.mid(lineStart, numChars); + if (lineStart > 0) + text += QString(indent, QLatin1Char(' ')); + text += description.midRef(lineStart, numChars) + nl; + x = 0; + lastBreakable = -1; + lineStart = nextLineStart; + if (lineStart < len && description.at(lineStart).isSpace()) + ++lineStart; // don't start a line with a space + i = lineStart; + } + } + return text; } |