aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/outputformatter.h
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2020-04-14 15:28:44 +0200
committerChristian Kandeler <christian.kandeler@qt.io>2020-04-14 14:15:26 +0000
commitc0c2df203dd26b6af2be5757501bb3cacd692ef6 (patch)
tree961d12dc7ba7db4fb0c9cb3a338250d65efd64b0 /src/libs/utils/outputformatter.h
parent70bddbcab42759c3105db6801a918375449b848c (diff)
Utils: Split up OutputFormatter class
An OutputFormatter takes some string and prints it into a text edit. In addition, it can ask any number of registered OutputLineParsers whether they think any special formatting should be applied to the current line. This mechanism is now properly modeled by our class design, rather than being hidden in a monolithic class where everything had the same type, no matter what its purpose was. Prospective contributors can now simply be pointed to the OutputLineParser class and will see at one glance what they have to do. Change-Id: I9844499f062c94fb038ce73fd6f26576910148c2 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/libs/utils/outputformatter.h')
-rw-r--r--src/libs/utils/outputformatter.h76
1 files changed, 46 insertions, 30 deletions
diff --git a/src/libs/utils/outputformatter.h b/src/libs/utils/outputformatter.h
index d38d573d79..cdb757786e 100644
--- a/src/libs/utils/outputformatter.h
+++ b/src/libs/utils/outputformatter.h
@@ -41,33 +41,10 @@ namespace Utils {
class FormattedText;
-namespace Internal { class OutputFormatterPrivate; }
-
-class QTCREATOR_UTILS_EXPORT OutputFormatter : public QObject
+class QTCREATOR_UTILS_EXPORT OutputLineParser
{
public:
- OutputFormatter();
- ~OutputFormatter() override;
-
- QPlainTextEdit *plainTextEdit() const;
- void setPlainTextEdit(QPlainTextEdit *plainText);
-
- void setFormatters(const QList<OutputFormatter *> &formatters);
-
- void flush();
-
- void appendMessage(const QString &text, OutputFormat format);
-
- virtual bool handleLink(const QString &href);
- void clear();
- void setBoldFontEnabled(bool enabled);
-
- // For unit testing only
- void overrideTextCharFormat(const QTextCharFormat &fmt);
-
-protected:
- QTextCharFormat charFormat(OutputFormat format) const;
- static QTextCharFormat linkFormat(const QTextCharFormat &inputFormat, const QString &href);
+ virtual ~OutputLineParser();
enum class Status { Done, InProgress, NotHandled };
class LinkSpec {
@@ -88,13 +65,51 @@ protected:
optional<QString> newContent; // Hard content override. Only to be used in extreme cases.
};
+ // line contains at most one line feed character, and if it does occur, it's the last character.
+ // Either way, the input is to be considered "complete" for parsing purposes.
+ virtual Result handleLine(const QString &line, OutputFormat format) = 0;
+
+ virtual bool handleLink(const QString &href) { Q_UNUSED(href); return false; }
+ virtual void reset() {}
+};
+
+
+
+namespace Internal { class OutputFormatterPrivate; }
+
+class QTCREATOR_UTILS_EXPORT OutputFormatter : public QObject
+{
+public:
+ OutputFormatter();
+ ~OutputFormatter() override;
+
+ QPlainTextEdit *plainTextEdit() const;
+ void setPlainTextEdit(QPlainTextEdit *plainText);
+
+ void setLineParsers(const QList<OutputLineParser *> &parsers);
+
+ void appendMessage(const QString &text, OutputFormat format);
+ void flush();
+
+ void handleLink(const QString &href);
+ void clear();
+ void setBoldFontEnabled(bool enabled);
+
+#ifdef WITH_TESTS
+ void overrideTextCharFormat(const QTextCharFormat &fmt);
+#endif
+
+#ifndef WITH_TESTS
+private:
+#endif
+ QTextCharFormat charFormat(OutputFormat format) const;
+ static QTextCharFormat linkFormat(const QTextCharFormat &inputFormat, const QString &href);
+
private:
- // text contains at most one line feed character, and if it does occur, it's the last character.
- // Either way, the input is to be considered "complete" for formatting purposes.
void doAppendMessage(const QString &text, OutputFormat format);
- virtual Result handleMessage(const QString &text, OutputFormat format);
- virtual void reset() {}
+ OutputLineParser::Result handleMessage(const QString &text, OutputFormat format);
+ void reset();
void append(const QString &text, const QTextCharFormat &format);
void initFormats();
@@ -103,9 +118,10 @@ private:
void clearLastLine();
QList<FormattedText> parseAnsi(const QString &text, const QTextCharFormat &format);
const QList<Utils::FormattedText> linkifiedText(const QList<FormattedText> &text,
- const LinkSpecs &linkSpecs);
+ const OutputLineParser::LinkSpecs &linkSpecs);
Internal::OutputFormatterPrivate *d;
};
+
} // namespace Utils