diff --git a/src/libs/utils/ansiescapecodehandler.cpp b/src/libs/utils/ansiescapecodehandler.cpp index cfbae1ee844..cbd5ccee052 100644 --- a/src/libs/utils/ansiescapecodehandler.cpp +++ b/src/libs/utils/ansiescapecodehandler.cpp @@ -70,8 +70,7 @@ static QColor ansiColor(uint code) return QColor(red, green, blue); } -QList AnsiEscapeCodeHandler::parseText(const QString &text, - const QTextCharFormat &defaultFormat) +QList AnsiEscapeCodeHandler::parseText(const FormattedText &input) { enum AnsiEscapeCodes { ResetFormat = 0, @@ -86,22 +85,22 @@ QList AnsiEscapeCodeHandler::parseText(const QString &text, DefaultBackgroundColor = 49 }; - QList outputData; + QList outputData; - QTextCharFormat charFormat = m_previousFormatClosed ? defaultFormat : m_previousFormat; + QTextCharFormat charFormat = m_previousFormatClosed ? input.format : m_previousFormat; const QString escape = QLatin1String("\x1b["); - if (!text.contains(escape)) { - outputData << StringFormatPair(text, charFormat); + if (!input.text.contains(escape)) { + outputData << FormattedText(input.text, charFormat); return outputData; - } else if (!text.startsWith(escape)) { - outputData << StringFormatPair(text.left(text.indexOf(escape)), charFormat); + } else if (!input.text.startsWith(escape)) { + outputData << FormattedText(input.text.left(input.text.indexOf(escape)), charFormat); } const QChar semicolon = QLatin1Char(';'); const QChar colorTerminator = QLatin1Char('m'); // strippedText always starts with "\e[" - QString strippedText = text.mid(text.indexOf(escape)); + QString strippedText = input.text.mid(input.text.indexOf(escape)); while (!strippedText.isEmpty()) { while (strippedText.startsWith(escape)) { strippedText.remove(0, 2); @@ -129,7 +128,7 @@ QList AnsiEscapeCodeHandler::parseText(const QString &text, strippedText.remove(0, 1); if (numbers.isEmpty()) { - charFormat = defaultFormat; + charFormat = input.format; endFormatScope(); } @@ -145,7 +144,7 @@ QList AnsiEscapeCodeHandler::parseText(const QString &text, } else { switch (code) { case ResetFormat: - charFormat = defaultFormat; + charFormat = input.format; endFormatScope(); break; case BoldText: @@ -153,11 +152,11 @@ QList AnsiEscapeCodeHandler::parseText(const QString &text, setFormatScope(charFormat); break; case DefaultTextColor: - charFormat.setForeground(defaultFormat.foreground()); + charFormat.setForeground(input.format.foreground()); setFormatScope(charFormat); break; case DefaultBackgroundColor: - charFormat.setBackground(defaultFormat.background()); + charFormat.setBackground(input.format.background()); setFormatScope(charFormat); break; case RgbTextColor: @@ -194,10 +193,10 @@ QList AnsiEscapeCodeHandler::parseText(const QString &text, break; int index = strippedText.indexOf(escape); if (index > 0) { - outputData << StringFormatPair(strippedText.left(index), charFormat); + outputData << FormattedText(strippedText.left(index), charFormat); strippedText.remove(0, index); } else if (index == -1) { - outputData << StringFormatPair(strippedText, charFormat); + outputData << FormattedText(strippedText, charFormat); break; } } diff --git a/src/libs/utils/ansiescapecodehandler.h b/src/libs/utils/ansiescapecodehandler.h index 88a99ee50c3..f36272cc5f2 100644 --- a/src/libs/utils/ansiescapecodehandler.h +++ b/src/libs/utils/ansiescapecodehandler.h @@ -37,13 +37,23 @@ namespace Utils { -typedef QPair StringFormatPair; +class QTCREATOR_UTILS_EXPORT FormattedText { +public: + FormattedText() { } + FormattedText(const FormattedText &other) : text(other.text), format(other.format) { } + FormattedText(const QString &txt, const QTextCharFormat &fmt = QTextCharFormat()) : + text(txt), format(fmt) + { } + + QString text; + QTextCharFormat format; +}; class QTCREATOR_UTILS_EXPORT AnsiEscapeCodeHandler { public: AnsiEscapeCodeHandler(); - QList parseText(const QString &text, const QTextCharFormat &defaultFormat); + QList parseText(const FormattedText &input); void endFormatScope(); private: diff --git a/src/libs/utils/outputformatter.cpp b/src/libs/utils/outputformatter.cpp index 38ed92576d5..419f2e2a151 100644 --- a/src/libs/utils/outputformatter.cpp +++ b/src/libs/utils/outputformatter.cpp @@ -65,8 +65,10 @@ void OutputFormatter::appendMessage(const QString &text, OutputFormat format) QTextCursor cursor(m_plainTextEdit->document()); cursor.movePosition(QTextCursor::End); - foreach (const StringFormatPair &pair, m_escapeCodeHandler->parseText(text, m_formats[format])) - cursor.insertText(pair.first, pair.second); + foreach (const FormattedText &output, + m_escapeCodeHandler->parseText(FormattedText(text, m_formats[format]))) { + cursor.insertText(output.text, output.format); + } } QTextCharFormat OutputFormatter::charFormat(OutputFormat format) const diff --git a/src/plugins/projectexplorer/compileoutputwindow.cpp b/src/plugins/projectexplorer/compileoutputwindow.cpp index 75df27230c6..89253413cfd 100644 --- a/src/plugins/projectexplorer/compileoutputwindow.cpp +++ b/src/plugins/projectexplorer/compileoutputwindow.cpp @@ -205,8 +205,9 @@ void CompileOutputWindow::appendText(const QString &text, ProjectExplorer::Build } - foreach (const Utils::StringFormatPair &pair, m_escapeCodeHandler->parseText(text, textFormat)) - m_outputWindow->appendText(pair.first, pair.second); + foreach (const Utils::FormattedText &output, + m_escapeCodeHandler->parseText(Utils::FormattedText(text, textFormat))) + m_outputWindow->appendText(output.text, output.format); } void CompileOutputWindow::clearContents() diff --git a/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler.cpp b/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler.cpp index b2fa39fb7de..530f8d8870f 100644 --- a/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler.cpp +++ b/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler.cpp @@ -34,11 +34,11 @@ using namespace Utils; -typedef QList ResultList; +typedef QList FormattedTextList; Q_DECLARE_METATYPE(QTextCharFormat); -Q_DECLARE_METATYPE(StringFormatPair); -Q_DECLARE_METATYPE(ResultList); +Q_DECLARE_METATYPE(FormattedText); +Q_DECLARE_METATYPE(FormattedTextList); static QString ansiEscape(const QByteArray &sequence) { @@ -76,16 +76,16 @@ void tst_AnsiEscapeCodeHandler::testSimpleFormat() { QFETCH(QString, text); QFETCH(QTextCharFormat, format); - QFETCH(ResultList, expected); + QFETCH(FormattedTextList, expected); AnsiEscapeCodeHandler handler; - ResultList result = handler.parseText(text, format); + FormattedTextList result = handler.parseText(FormattedText(text, format)); handler.endFormatScope(); QCOMPARE(result.size(), expected.size()); for (int i = 0; i < result.size(); ++i) { - QCOMPARE(result[i].first, expected[i].first); - QCOMPARE(result[i].second, expected[i].second); + QCOMPARE(result[i].text, expected[i].text); + QCOMPARE(result[i].format, expected[i].format); } } @@ -93,40 +93,40 @@ void tst_AnsiEscapeCodeHandler::testSimpleFormat_data() { QTest::addColumn("text"); QTest::addColumn("format"); - QTest::addColumn("expected"); + QTest::addColumn("expected"); // Test pass-through QTextCharFormat defaultFormat; QTest::newRow("Pass-through") << "Hello World" << defaultFormat - << (ResultList() << StringFormatPair("Hello World", defaultFormat)); + << (FormattedTextList() << FormattedText("Hello World", defaultFormat)); // Test text-color change QTextCharFormat redFormat; redFormat.setForeground(QColor(170, 0, 0)); const QString text2 = "This is " + red + "red" + normal + " text"; QTest::newRow("Text-color change") << text2 << QTextCharFormat() - << (ResultList() - << StringFormatPair("This is ", defaultFormat) - << StringFormatPair("red", redFormat) - << StringFormatPair(" text", defaultFormat)); + << (FormattedTextList() + << FormattedText("This is ", defaultFormat) + << FormattedText("red", redFormat) + << FormattedText(" text", defaultFormat)); // Test text format change to bold QTextCharFormat boldFormat; boldFormat.setFontWeight(QFont::Bold); const QString text3 = "A line of " + bold + "bold" + normal + " text"; QTest::newRow("Text-format change") << text3 << QTextCharFormat() - << (ResultList() - << StringFormatPair("A line of ", defaultFormat) - << StringFormatPair("bold", boldFormat) - << StringFormatPair(" text", defaultFormat)); + << (FormattedTextList() + << FormattedText("A line of ", defaultFormat) + << FormattedText("bold", boldFormat) + << FormattedText(" text", defaultFormat)); // Test resetting format to normal with other reset pattern const QString text4 = "A line of " + bold + "bold" + normal1 + " text"; QTest::newRow("Alternative reset pattern (QTCREATORBUG-10132)") << text4 << QTextCharFormat() - << (ResultList() - << StringFormatPair("A line of ", defaultFormat) - << StringFormatPair("bold", boldFormat) - << StringFormatPair(" text", defaultFormat)); + << (FormattedTextList() + << FormattedText("A line of ", defaultFormat) + << FormattedText("bold", boldFormat) + << FormattedText(" text", defaultFormat)); } void tst_AnsiEscapeCodeHandler::testLineOverlappingFormat() @@ -138,22 +138,22 @@ void tst_AnsiEscapeCodeHandler::testLineOverlappingFormat() QTextCharFormat defaultFormat; AnsiEscapeCodeHandler handler; - ResultList result; - result.append(handler.parseText(line1, defaultFormat)); - result.append(handler.parseText(line2, defaultFormat)); + FormattedTextList result; + result.append(handler.parseText(FormattedText(line1, defaultFormat))); + result.append(handler.parseText(FormattedText(line2, defaultFormat))); QTextCharFormat boldFormat; boldFormat.setFontWeight(QFont::Bold); QCOMPARE(result.size(), 4); - QCOMPARE(result[0].first, QLatin1String("A line of ")); - QCOMPARE(result[0].second, defaultFormat); - QCOMPARE(result[1].first, QLatin1String("bold text")); - QCOMPARE(result[1].second, boldFormat); - QCOMPARE(result[2].first, QLatin1String("A line of ")); - QCOMPARE(result[2].second, boldFormat); - QCOMPARE(result[3].first, QLatin1String("normal text")); - QCOMPARE(result[3].second, defaultFormat); + QCOMPARE(result[0].text, QLatin1String("A line of ")); + QCOMPARE(result[0].format, defaultFormat); + QCOMPARE(result[1].text, QLatin1String("bold text")); + QCOMPARE(result[1].format, boldFormat); + QCOMPARE(result[2].text, QLatin1String("A line of ")); + QCOMPARE(result[2].format, boldFormat); + QCOMPARE(result[3].text, QLatin1String("normal text")); + QCOMPARE(result[3].format, defaultFormat); } QTEST_APPLESS_MAIN(tst_AnsiEscapeCodeHandler)