From 8fa2366ed9d5757fa63e7fa433c9931f51ce462f Mon Sep 17 00:00:00 2001 From: rbn42 Date: Wed, 6 May 2020 22:31:18 +0800 Subject: [PATCH] Expose current session dir (#15) * expose the current working directory * test current dir interface Co-authored-by: rbn42 --- lib/Session.cpp | 12 ++++++++++++ lib/Session.h | 5 +++++ src/ksession.cpp | 5 +++++ src/ksession.h | 6 ++++++ test-app/test-app.qml | 7 +++++++ 5 files changed, 35 insertions(+) diff --git a/lib/Session.cpp b/lib/Session.cpp index b8e0595..3776fc5 100644 --- a/lib/Session.cpp +++ b/lib/Session.cpp @@ -943,6 +943,18 @@ QString Session::foregroundProcessName() return name; } +QString Session::currentDir() +{ + QString path; + if (updateForegroundProcessInfo()) { + bool ok = false; + path= _foregroundProcessInfo->currentDir(&ok); + if (!ok) + path.clear(); + } + return path; +} + bool Session::updateForegroundProcessInfo() { Q_ASSERT(_shellProcess); diff --git a/lib/Session.h b/lib/Session.h index 59dfeb8..b577d69 100644 --- a/lib/Session.h +++ b/lib/Session.h @@ -333,6 +333,11 @@ public: */ QString foregroundProcessName(); + /** + * Returns the current working directory of the process. + */ + QString currentDir(); + /** Returns the terminal session's window size in lines and columns. */ QSize size(); /** diff --git a/src/ksession.cpp b/src/ksession.cpp index d5befd7..204a9eb 100644 --- a/src/ksession.cpp +++ b/src/ksession.cpp @@ -307,3 +307,8 @@ QString KSession::foregroundProcessName() { return m_session->foregroundProcessName(); } + +QString KSession::currentDir() +{ + return m_session->currentDir(); +} diff --git a/src/ksession.h b/src/ksession.h index ef8aed5..41ccad4 100644 --- a/src/ksession.h +++ b/src/ksession.h @@ -42,6 +42,7 @@ class KSession : public QObject Q_PROPERTY(QString history READ getHistory) Q_PROPERTY(bool hasActiveProcess READ hasActiveProcess) Q_PROPERTY(QString foregroundProcessName READ foregroundProcessName) + Q_PROPERTY(QString currentDir READ currentDir) public: KSession(QObject *parent = 0); @@ -105,6 +106,11 @@ public: */ QString foregroundProcessName(); + /** + * Returns the current working directory of the process. + */ + QString currentDir(); + signals: void started(); void finished(); diff --git a/test-app/test-app.qml b/test-app/test-app.qml index 0752d26..190dad9 100644 --- a/test-app/test-app.qml +++ b/test-app/test-app.qml @@ -21,6 +21,13 @@ Rectangle { shortcut: "Ctrl+F" } + Action{ + onTriggered:{ + console.log('open new terminal window in:'+mainsession.currentDir) + } + shortcut: "Ctrl+Shift+T" + } + QMLTermWidget { id: terminal anchors.fill: parent