io/process: add StdioCollector data stream parser

This commit is contained in:
outfoxxed 2025-06-09 22:26:34 -07:00
parent 0224fa942b
commit 2b01a75679
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
3 changed files with 67 additions and 4 deletions

View file

@ -1,6 +1,7 @@
#pragma once
#include <qbytearray.h>
#include <qcontainerfwd.h>
#include <qlocalsocket.h>
#include <qobject.h>
#include <qqmlintegration.h>
@ -61,8 +62,8 @@ signals:
void read(QString data);
};
///! Parser for delimited data streams.
/// Parser for delimited data streams. @@read() is emitted once per delimited chunk of the stream.
///! DataStreamParser for delimited data streams.
/// DataStreamParser for delimited data streams. @@read() is emitted once per delimited chunk of the stream.
class SplitParser: public DataStreamParser {
Q_OBJECT;
/// The delimiter for parsed data. May be multiple characters. Defaults to `\n`.
@ -88,3 +89,41 @@ private:
QString mSplitMarker = "\n";
bool mSplitMarkerChanged = false;
};
///! DataStreamParser that collects all output into a buffer
/// StdioCollector collects all process output into a buffer exposed as @@text or @@data.
class StdioCollector: public DataStreamParser {
Q_OBJECT;
QML_ELEMENT;
/// The stdio buffer exposed as text. if @@waitForEnd is true, this will not change
/// until the stream ends.
Q_PROPERTY(QString text READ text NOTIFY dataChanged);
/// The stdio buffer exposed as an [ArrayBuffer]. if @@waitForEnd is true, this will not change
/// until the stream ends.
///
/// [ArrayBuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
Q_PROPERTY(QByteArray data READ data NOTIFY dataChanged);
/// If true, @@text and @@data will not be updated until the stream ends. Defaults to true.
Q_PROPERTY(bool waitForEnd READ waitForEnd WRITE setWaitForEnd NOTIFY waitForEndChanged);
public:
explicit StdioCollector(QObject* parent = nullptr): DataStreamParser(parent) {}
void parseBytes(QByteArray& incoming, QByteArray& buffer) override;
void streamEnded(QByteArray& buffer) override;
[[nodiscard]] QString text() const { return this->mData; }
[[nodiscard]] QByteArray data() const { return this->mData; }
[[nodiscard]] bool waitForEnd() const { return this->mWaitForEnd; }
void setWaitForEnd(bool waitForEnd);
signals:
void waitForEndChanged();
void dataChanged();
void streamFinished();
private:
bool mWaitForEnd = true;
QByteArray mData;
};