forked from qt-creator/qt-creator
OutputFormatter: Simplify
Instead of storing a state for \r, select the text right away, so the next insertText will overwrite it. Change-Id: I40d39220106a6c08c12ee0b92b5b3d745277abb6 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
55ab66320d
commit
79cfb784be
@@ -41,7 +41,7 @@ public:
|
|||||||
QTextCharFormat formats[NumberOfFormats];
|
QTextCharFormat formats[NumberOfFormats];
|
||||||
QTextCursor cursor;
|
QTextCursor cursor;
|
||||||
AnsiEscapeCodeHandler escapeCodeHandler;
|
AnsiEscapeCodeHandler escapeCodeHandler;
|
||||||
bool overwriteOutput = false;
|
OutputFormat lastFormat = NumberOfFormats;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
@@ -65,30 +65,22 @@ void OutputFormatter::setPlainTextEdit(QPlainTextEdit *plainText)
|
|||||||
{
|
{
|
||||||
d->plainTextEdit = plainText;
|
d->plainTextEdit = plainText;
|
||||||
d->cursor = plainText ? plainText->textCursor() : QTextCursor();
|
d->cursor = plainText ? plainText->textCursor() : QTextCursor();
|
||||||
|
d->cursor.movePosition(QTextCursor::End);
|
||||||
initFormats();
|
initFormats();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFormatter::appendMessage(const QString &text, OutputFormat format)
|
void OutputFormatter::appendMessage(const QString &text, OutputFormat format)
|
||||||
{
|
{
|
||||||
|
if (!d->cursor.atEnd() && format != d->lastFormat)
|
||||||
|
d->cursor.movePosition(QTextCursor::End);
|
||||||
|
d->lastFormat = format;
|
||||||
appendMessage(text, d->formats[format]);
|
appendMessage(text, d->formats[format]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFormatter::appendMessage(const QString &text, const QTextCharFormat &format)
|
void OutputFormatter::appendMessage(const QString &text, const QTextCharFormat &format)
|
||||||
{
|
{
|
||||||
if (!d->cursor.atEnd())
|
foreach (const FormattedText &output, parseAnsi(text, format))
|
||||||
d->cursor.movePosition(QTextCursor::End);
|
append(output.text, output.format);
|
||||||
|
|
||||||
foreach (const FormattedText &output, parseAnsi(text, format)) {
|
|
||||||
int startPos = 0;
|
|
||||||
int crPos = -1;
|
|
||||||
while ((crPos = output.text.indexOf(QLatin1Char('\r'), startPos)) >= 0) {
|
|
||||||
append(d->cursor, output.text.mid(startPos, crPos - startPos), output.format);
|
|
||||||
startPos = crPos + 1;
|
|
||||||
d->overwriteOutput = true;
|
|
||||||
}
|
|
||||||
if (startPos < output.text.count())
|
|
||||||
append(d->cursor, output.text.mid(startPos), output.format);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextCharFormat OutputFormatter::charFormat(OutputFormat format) const
|
QTextCharFormat OutputFormatter::charFormat(OutputFormat format) const
|
||||||
@@ -101,15 +93,18 @@ QList<FormattedText> OutputFormatter::parseAnsi(const QString &text, const QText
|
|||||||
return d->escapeCodeHandler.parseText(FormattedText(text, format));
|
return d->escapeCodeHandler.parseText(FormattedText(text, format));
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFormatter::append(QTextCursor &cursor, const QString &text,
|
void OutputFormatter::append(const QString &text, const QTextCharFormat &format)
|
||||||
const QTextCharFormat &format)
|
|
||||||
{
|
{
|
||||||
if (d->overwriteOutput) {
|
int startPos = 0;
|
||||||
cursor.clearSelection();
|
int crPos = -1;
|
||||||
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
|
while ((crPos = text.indexOf('\r', startPos)) >= 0) {
|
||||||
d->overwriteOutput = false;
|
d->cursor.insertText(text.mid(startPos, crPos - startPos), format);
|
||||||
|
d->cursor.clearSelection();
|
||||||
|
d->cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
|
||||||
|
startPos = crPos + 1;
|
||||||
}
|
}
|
||||||
cursor.insertText(text, format);
|
if (startPos < text.count())
|
||||||
|
d->cursor.insertText(text.mid(startPos), format);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFormatter::clearLastLine()
|
void OutputFormatter::clearLastLine()
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ protected:
|
|||||||
virtual void clearLastLine();
|
virtual void clearLastLine();
|
||||||
QTextCharFormat charFormat(OutputFormat format) const;
|
QTextCharFormat charFormat(OutputFormat format) const;
|
||||||
QList<FormattedText> parseAnsi(const QString &text, const QTextCharFormat &format);
|
QList<FormattedText> parseAnsi(const QString &text, const QTextCharFormat &format);
|
||||||
void append(QTextCursor &cursor, const QString &text, const QTextCharFormat &format);
|
void append(const QString &text, const QTextCharFormat &format);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void appendMessage(const QString &text, const QTextCharFormat &format);
|
virtual void appendMessage(const QString &text, const QTextCharFormat &format);
|
||||||
|
|||||||
Reference in New Issue
Block a user