Skip to content

Commit 2b01a75

Browse files
committed
io/process: add StdioCollector data stream parser
1 parent 0224fa9 commit 2b01a75

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

src/io/datastream.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,27 @@ void SplitParser::setSplitMarker(QString marker) {
111111
this->mSplitMarkerChanged = true;
112112
emit this->splitMarkerChanged();
113113
}
114+
115+
void StdioCollector::parseBytes(QByteArray& incoming, QByteArray& buffer) {
116+
buffer.append(incoming);
117+
118+
if (!this->mWaitForEnd) {
119+
this->mData = buffer;
120+
emit this->dataChanged();
121+
}
122+
}
123+
124+
void StdioCollector::streamEnded(QByteArray& buffer) {
125+
if (this->mWaitForEnd) {
126+
this->mData = buffer;
127+
emit this->dataChanged();
128+
}
129+
130+
emit this->streamFinished();
131+
}
132+
133+
void StdioCollector::setWaitForEnd(bool waitForEnd) {
134+
if (waitForEnd == this->mWaitForEnd) return;
135+
this->mWaitForEnd = waitForEnd;
136+
emit this->waitForEndChanged();
137+
}

src/io/datastream.hpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.
6667
class 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+
};

src/io/process.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
/// Process {
2323
/// running: true
2424
/// command: [ "some-command", "arg" ]
25-
/// stdout: SplitParser {
26-
/// onRead: data => console.log(`line read: ${data}`)
25+
/// stdout: @@StdioCollector {
26+
/// onStreamFinished: console.log(`line read: ${this.text}`)
2727
/// }
2828
/// }
2929
/// ```

0 commit comments

Comments
 (0)