From 868b1543c6a67a44685429153712cb271e16cc8c Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Wed, 18 Dec 2024 15:17:27 +0200 Subject: [PATCH] Preserve cwd while spawning new session --- metainfo.xml | 1 + src/contour/TerminalSessionManager.cpp | 34 +++++++++++++++++++++----- src/contour/TerminalSessionManager.h | 4 +-- src/contour/helper.cpp | 4 +-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/metainfo.xml b/metainfo.xml index 5f34b99883..26afe00840 100644 --- a/metainfo.xml +++ b/metainfo.xml @@ -120,6 +120,7 @@
  • Fixes cropping of underscore character for some fonts (#1603)
  • Fixes DECFRA limits (#1664)
  • Fixes underline styles not being reset on switching to a new style (#1365)
  • +
  • Few improvements and bug fixes for the tabs
  • diff --git a/src/contour/TerminalSessionManager.cpp b/src/contour/TerminalSessionManager.cpp index 727d0853bf..bae983e71d 100644 --- a/src/contour/TerminalSessionManager.cpp +++ b/src/contour/TerminalSessionManager.cpp @@ -13,6 +13,7 @@ #include #include +#include #include using namespace std::string_literals; @@ -27,13 +28,15 @@ TerminalSessionManager::TerminalSessionManager(ContourGuiApp& app): _app { app } { } -std::unique_ptr TerminalSessionManager::createPty() +std::unique_ptr TerminalSessionManager::createPty(std::string cwd) { auto const& profile = _app.config().profile(_app.profileName()); #if defined(VTPTY_LIBSSH2) if (!profile->ssh.value().hostname.empty()) return make_unique(profile->ssh.value()); #endif + if (!cwd.empty()) + profile->shell.value().workingDirectory = std::filesystem::path(cwd); return make_unique(profile->shell.value(), vtpty::createPty(profile->terminalSize.value(), nullopt), profile->escapeSandbox.value()); @@ -43,7 +46,27 @@ TerminalSession* TerminalSessionManager::createSession() { // TODO: Remove dependency on app-knowledge and pass shell / terminal-size instead. // The GuiApp *or* (Global)Config could be made a global to be accessable from within QML. - auto* session = new TerminalSession(createPty(), _app); + auto* session = new TerminalSession( + createPty([this]() -> std::string { + if (_activeSession) + { + auto& terminal = _activeSession->terminal(); + auto const wd = [&]() -> std::string { +#if !defined(_WIN32) + if (auto const* ptyProcess = dynamic_cast(&terminal.device())) + return ptyProcess->workingDirectory(); +#else + auto const _l = scoped_lock { _terminal }; + return _terminal.currentWorkingDirectory(); +#endif + return ""s; + }(); + return wd; + } + return ""s; + }()), + _app); + managerLog()("CREATE SESSION, new session: {}", (void*) session); _sessions.push_back(session); @@ -57,6 +80,7 @@ TerminalSession* TerminalSessionManager::createSession() // we can close application right after session has been created _lastTabChange = std::chrono::steady_clock::now() - std::chrono::seconds(1); + _activeSession = session; return session; } @@ -72,14 +96,11 @@ void TerminalSessionManager::setSession(size_t index) if (index < _sessions.size()) _activeSession = _sessions[index]; else - _activeSession = createSession(); + createSession(); if (oldSession == _activeSession) return; - if (oldSession) - oldSession->detachDisplay(*display); - Require(display != nullptr); auto const pixels = display->pixelSize(); auto const totalPageSize = display->calculatePageSize() + _activeSession->terminal().statusLineHeight(); @@ -87,6 +108,7 @@ void TerminalSessionManager::setSession(size_t index) display->setSession(_activeSession); _activeSession->terminal().resizeScreen(totalPageSize, pixels); updateStatusLine(); + _lastTabChange = std::chrono::steady_clock::now(); } diff --git a/src/contour/TerminalSessionManager.h b/src/contour/TerminalSessionManager.h index e3d593ac3d..c1b7af3cde 100644 --- a/src/contour/TerminalSessionManager.h +++ b/src/contour/TerminalSessionManager.h @@ -49,7 +49,7 @@ class TerminalSessionManager: public QAbstractListModel TerminalSession* getSession() { return _sessions[0]; } private: - std::unique_ptr createPty(); + std::unique_ptr createPty(std::string cwd); [[nodiscard]] auto getCurrentSessionIndex() const { return [](auto const& sessions, auto const& activeSession) { @@ -87,7 +87,7 @@ class TerminalSessionManager: public QAbstractListModel TerminalSession* _activeSession = nullptr; std::vector _sessions; std::chrono::time_point _lastTabChange; - std::chrono::milliseconds _timeBetweenTabSwitches { 100 }; + std::chrono::milliseconds _timeBetweenTabSwitches { 10 }; }; } // namespace contour diff --git a/src/contour/helper.cpp b/src/contour/helper.cpp index 23e410856f..b5935458ba 100644 --- a/src/contour/helper.cpp +++ b/src/contour/helper.cpp @@ -607,8 +607,8 @@ void applyResize(vtbackend::ImageSize newPixelSize, TerminalSession& session, vtrasterizer::Renderer& renderer) { - if (*newPixelSize.width == 0 - || *newPixelSize.height == 0) return; + if (*newPixelSize.width == 0 || *newPixelSize.height == 0) + return; auto const oldPageSize = session.terminal().pageSize(); auto const newPageSize = pageSizeForPixels(