forked from quickshell/quickshell
		
	io/process: add StdioCollector data stream parser
This commit is contained in:
		
							parent
							
								
									0224fa942b
								
							
						
					
					
						commit
						2b01a75679
					
				
					 3 changed files with 67 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -111,3 +111,27 @@ void SplitParser::setSplitMarker(QString marker) {
 | 
			
		|||
	this->mSplitMarkerChanged = true;
 | 
			
		||||
	emit this->splitMarkerChanged();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void StdioCollector::parseBytes(QByteArray& incoming, QByteArray& buffer) {
 | 
			
		||||
	buffer.append(incoming);
 | 
			
		||||
 | 
			
		||||
	if (!this->mWaitForEnd) {
 | 
			
		||||
		this->mData = buffer;
 | 
			
		||||
		emit this->dataChanged();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void StdioCollector::streamEnded(QByteArray& buffer) {
 | 
			
		||||
	if (this->mWaitForEnd) {
 | 
			
		||||
		this->mData = buffer;
 | 
			
		||||
		emit this->dataChanged();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	emit this->streamFinished();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void StdioCollector::setWaitForEnd(bool waitForEnd) {
 | 
			
		||||
	if (waitForEnd == this->mWaitForEnd) return;
 | 
			
		||||
	this->mWaitForEnd = waitForEnd;
 | 
			
		||||
	emit this->waitForEndChanged();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,8 +22,8 @@
 | 
			
		|||
/// Process {
 | 
			
		||||
///   running: true
 | 
			
		||||
///   command: [ "some-command", "arg" ]
 | 
			
		||||
///   stdout: SplitParser {
 | 
			
		||||
///     onRead: data => console.log(`line read: ${data}`)
 | 
			
		||||
///   stdout: @@StdioCollector {
 | 
			
		||||
///     onStreamFinished: console.log(`line read: ${this.text}`)
 | 
			
		||||
///   }
 | 
			
		||||
/// }
 | 
			
		||||
/// ```
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue