feat(process): add Process.environment

This commit is contained in:
outfoxxed 2024-03-03 19:35:10 -08:00
parent 65ad2a9877
commit e06b07ac01
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
3 changed files with 115 additions and 20 deletions

View file

@ -1,5 +1,6 @@
#pragma once
#include <qcontainerfwd.h>
#include <qobject.h>
#include <qprocess.h>
#include <qqmlintegration.h>
@ -42,14 +43,6 @@ class Process: public QObject {
Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged);
/// The process ID of the running process or `null` if `running` is false.
Q_PROPERTY(QVariant pid READ pid NOTIFY pidChanged);
/// The working directory of the process. Defaults to [quickshell's working directory].
///
/// If the process is already running changing this property will affect the next
/// started process. If the property has been changed after starting a process it will
/// return the new value, not the one for the currently running process.
///
/// [quickshell's working directory]: ../../quickshell/quickshell#prop.workingDirectory
Q_PROPERTY(QString workingDirectory READ workingDirectory WRITE setWorkingDirectory NOTIFY workingDirectoryChanged);
/// The command to execute.
///
/// If the process is already running changing this property will affect the next
@ -59,6 +52,57 @@ class Process: public QObject {
/// > [!INFO] You can use `["sh", "-c", <your command>]` to execute your command with
/// > the system shell.
Q_PROPERTY(QList<QString> command READ command WRITE setCommand NOTIFY commandChanged);
/// The working directory of the process. Defaults to [quickshell's working directory].
///
/// If the process is already running changing this property will affect the next
/// started process. If the property has been changed after starting a process it will
/// return the new value, not the one for the currently running process.
///
/// [quickshell's working directory]: ../../quickshell/quickshell#prop.workingDirectory
Q_PROPERTY(QString workingDirectory READ workingDirectory WRITE setWorkingDirectory NOTIFY workingDirectoryChanged);
/// Environment of the executed process.
///
/// This is a javascript object (json). Environment variables can be added by setting
/// them to a string and removed by setting them to null (except when [clearEnvironment] is true,
/// in which case this behavior is inverted, see [clearEnvironment] for details).
///
///
/// ```qml
/// environment: ({
/// ADDED: "value",
/// REMOVED: null,
/// "i'm different": "value",
/// })
/// ```
///
/// > [!INFO] You need to wrap the returned object in () otherwise it won't parse due to javascript ambiguity.
///
/// If the process is already running changing this property will affect the next
/// started process. If the property has been changed after starting a process it will
/// return the new value, not the one for the currently running process.
///
/// [clearEnvironment]: #prop.clearEnvironment
Q_PROPERTY(QMap<QString, QVariant> environment READ environment WRITE setEnvironment NOTIFY environmentChanged);
/// If the process's environment should be cleared prior to applying [environment](#prop.environment).
/// Defaults to false.
///
/// If true, all environment variables will be removed before the [environment](#prop.environment)
/// object is applied, meaning the variables listed will be the only ones visible to the process.
/// This changes the behavior of `null` to pass in the system value of the variable if present instead
/// of removing it.
///
/// ```qml
/// clearEnvironment: true
/// environment: ({
/// ADDED: "value",
/// PASSED_FROM_SYSTEM: null,
/// })
/// ```
///
/// If the process is already running changing this property will affect the next
/// started process. If the property has been changed after starting a process it will
/// return the new value, not the one for the currently running process.
Q_PROPERTY(bool clearEnvironment READ environmentCleared WRITE setEnvironmentCleared NOTIFY environmentClearChanged);
/// The parser for stdout. If the parser is null the process's stdout channel will be closed
/// and no further data will be read, even if a new parser is attached.
Q_PROPERTY(DataStreamParser* stdout READ stdoutParser WRITE setStdoutParser NOTIFY stdoutParserChanged);
@ -85,11 +129,17 @@ public:
[[nodiscard]] QVariant pid() const;
[[nodiscard]] QList<QString> command() const;
void setCommand(QList<QString> command);
[[nodiscard]] QString workingDirectory() const;
void setWorkingDirectory(const QString& workingDirectory);
[[nodiscard]] QList<QString> command() const;
void setCommand(QList<QString> command);
[[nodiscard]] QMap<QString, QVariant> environment() const;
void setEnvironment(QMap<QString, QVariant> environment);
[[nodiscard]] bool environmentCleared() const;
void setEnvironmentCleared(bool cleared);
[[nodiscard]] DataStreamParser* stdoutParser() const;
void setStdoutParser(DataStreamParser* parser);
@ -106,8 +156,10 @@ signals:
void runningChanged();
void pidChanged();
void workingDirectoryChanged();
void commandChanged();
void workingDirectoryChanged();
void environmentChanged();
void environmentClearChanged();
void stdoutParserChanged();
void stderrParserChanged();
void stdinEnabledChanged();
@ -126,8 +178,9 @@ private:
void startProcessIfReady();
QProcess* process = nullptr;
QString mWorkingDirectory;
QList<QString> mCommand;
QString mWorkingDirectory;
QMap<QString, QVariant> mEnvironment;
DataStreamParser* mStdoutParser = nullptr;
DataStreamParser* mStderrParser = nullptr;
QByteArray stdoutBuffer;
@ -135,4 +188,5 @@ private:
bool targetRunning = false;
bool mStdinEnabled = false;
bool mClearEnvironment = false;
};