11#pragma once
22
33#include < qbytearray.h>
4+ #include < qcontainerfwd.h>
45#include < qlocalsocket.h>
56#include < qobject.h>
67#include < qqmlintegration.h>
@@ -61,8 +62,8 @@ class DataStreamParser: public QObject {
6162 void read (QString data);
6263};
6364
64- // /! Parser for delimited data streams.
65- // / Parser for delimited data streams. @@read() is emitted once per delimited chunk of the stream.
65+ // /! DataStreamParser for delimited data streams.
66+ // / DataStreamParser for delimited data streams. @@read() is emitted once per delimited chunk of the stream.
6667class SplitParser : public DataStreamParser {
6768 Q_OBJECT;
6869 // / The delimiter for parsed data. May be multiple characters. Defaults to `\n`.
@@ -88,3 +89,41 @@ class SplitParser: public DataStreamParser {
8889 QString mSplitMarker = " \n " ;
8990 bool mSplitMarkerChanged = false ;
9091};
92+
93+ // /! DataStreamParser that collects all output into a buffer
94+ // / StdioCollector collects all process output into a buffer exposed as @@text or @@data.
95+ class StdioCollector : public DataStreamParser {
96+ Q_OBJECT;
97+ QML_ELEMENT;
98+ // / The stdio buffer exposed as text. if @@waitForEnd is true, this will not change
99+ // / until the stream ends.
100+ Q_PROPERTY (QString text READ text NOTIFY dataChanged);
101+ // / The stdio buffer exposed as an [ArrayBuffer]. if @@waitForEnd is true, this will not change
102+ // / until the stream ends.
103+ // /
104+ // / [ArrayBuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
105+ Q_PROPERTY (QByteArray data READ data NOTIFY dataChanged);
106+ // / If true, @@text and @@data will not be updated until the stream ends. Defaults to true.
107+ Q_PROPERTY (bool waitForEnd READ waitForEnd WRITE setWaitForEnd NOTIFY waitForEndChanged);
108+
109+ public:
110+ explicit StdioCollector (QObject* parent = nullptr ): DataStreamParser(parent) {}
111+
112+ void parseBytes (QByteArray& incoming, QByteArray& buffer) override ;
113+ void streamEnded (QByteArray& buffer) override ;
114+
115+ [[nodiscard]] QString text () const { return this ->mData ; }
116+ [[nodiscard]] QByteArray data () const { return this ->mData ; }
117+
118+ [[nodiscard]] bool waitForEnd () const { return this ->mWaitForEnd ; }
119+ void setWaitForEnd (bool waitForEnd);
120+
121+ signals:
122+ void waitForEndChanged ();
123+ void dataChanged ();
124+ void streamFinished ();
125+
126+ private:
127+ bool mWaitForEnd = true ;
128+ QByteArray mData ;
129+ };
0 commit comments