forked from quickshell/quickshell
feat: add support for getting and setting workdir
This commit is contained in:
parent
b5f50cd68f
commit
bbe64f42f3
8 changed files with 109 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
|||
#include <csignal> // NOLINT
|
||||
#include <utility>
|
||||
|
||||
#include <qdir.h>
|
||||
#include <qlist.h>
|
||||
#include <qlogging.h>
|
||||
#include <qobject.h>
|
||||
|
@ -9,8 +10,18 @@
|
|||
#include <qtmetamacros.h>
|
||||
#include <qtypes.h>
|
||||
|
||||
#include "../core/qmlglobal.hpp"
|
||||
#include "datastream.hpp"
|
||||
|
||||
Process::Process(QObject* parent): QObject(parent) {
|
||||
QObject::connect(
|
||||
QuickshellGlobal::instance(),
|
||||
&QuickshellGlobal::workingDirectoryChanged,
|
||||
this,
|
||||
&Process::onGlobalWorkingDirectoryChanged
|
||||
);
|
||||
}
|
||||
|
||||
bool Process::isRunning() const { return this->process != nullptr; }
|
||||
|
||||
void Process::setRunning(bool running) {
|
||||
|
@ -24,6 +35,25 @@ QVariant Process::pid() const {
|
|||
return QVariant::fromValue(this->process->processId());
|
||||
}
|
||||
|
||||
QString Process::workingDirectory() const {
|
||||
if (this->mWorkingDirectory.isEmpty()) return QDir::current().absolutePath();
|
||||
else return this->mWorkingDirectory;
|
||||
}
|
||||
|
||||
void Process::setWorkingDirectory(const QString& workingDirectory) {
|
||||
auto absolute =
|
||||
workingDirectory.isEmpty() ? workingDirectory : QDir(workingDirectory).absolutePath();
|
||||
if (absolute == this->mWorkingDirectory) return;
|
||||
this->mWorkingDirectory = absolute;
|
||||
emit this->workingDirectoryChanged();
|
||||
}
|
||||
|
||||
void Process::onGlobalWorkingDirectoryChanged() {
|
||||
if (this->mWorkingDirectory.isEmpty()) {
|
||||
emit this->workingDirectoryChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QList<QString> Process::command() const { return this->mCommand; }
|
||||
|
||||
void Process::setCommand(QList<QString> command) {
|
||||
|
@ -137,6 +167,8 @@ void Process::startProcessIfReady() {
|
|||
if (this->mStderrParser == nullptr) this->process->closeReadChannel(QProcess::StandardError);
|
||||
if (!this->mStdinEnabled) this->process->closeWriteChannel();
|
||||
|
||||
if (!this->mWorkingDirectory.isEmpty())
|
||||
this->process->setWorkingDirectory(this->mWorkingDirectory);
|
||||
this->process->start(cmd, args);
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,14 @@ 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
|
||||
|
@ -64,7 +72,7 @@ class Process: public QObject {
|
|||
QML_ELEMENT;
|
||||
|
||||
public:
|
||||
explicit Process(QObject* parent = nullptr): QObject(parent) {}
|
||||
explicit Process(QObject* parent = nullptr);
|
||||
|
||||
/// Sends a signal to the process if `running` is true, otherwise does nothing.
|
||||
Q_INVOKABLE void signal(qint32 signal);
|
||||
|
@ -77,6 +85,9 @@ public:
|
|||
|
||||
[[nodiscard]] QVariant pid() const;
|
||||
|
||||
[[nodiscard]] QString workingDirectory() const;
|
||||
void setWorkingDirectory(const QString& workingDirectory);
|
||||
|
||||
[[nodiscard]] QList<QString> command() const;
|
||||
void setCommand(QList<QString> command);
|
||||
|
||||
|
@ -95,6 +106,7 @@ signals:
|
|||
|
||||
void runningChanged();
|
||||
void pidChanged();
|
||||
void workingDirectoryChanged();
|
||||
void commandChanged();
|
||||
void stdoutParserChanged();
|
||||
void stderrParserChanged();
|
||||
|
@ -108,11 +120,13 @@ private slots:
|
|||
void onStderrReadyRead();
|
||||
void onStdoutParserDestroyed();
|
||||
void onStderrParserDestroyed();
|
||||
void onGlobalWorkingDirectoryChanged();
|
||||
|
||||
private:
|
||||
void startProcessIfReady();
|
||||
|
||||
QProcess* process = nullptr;
|
||||
QString mWorkingDirectory;
|
||||
QList<QString> mCommand;
|
||||
DataStreamParser* mStdoutParser = nullptr;
|
||||
DataStreamParser* mStderrParser = nullptr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue