Skip to content

Commit

Permalink
Preserve cwd while spawning new session
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Dec 18, 2024
1 parent ebddfba commit 868b154
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<li>Fixes cropping of underscore character for some fonts (#1603)</li>
<li>Fixes DECFRA limits (#1664)</li>
<li>Fixes underline styles not being reset on switching to a new style (#1365)</li>
<li>Few improvements and bug fixes for the tabs</li>
</ul>
</description>
</release>
Expand Down
34 changes: 28 additions & 6 deletions src/contour/TerminalSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QtQml/QQmlEngine>

#include <algorithm>
#include <filesystem>
#include <string>

using namespace std::string_literals;
Expand All @@ -27,13 +28,15 @@ TerminalSessionManager::TerminalSessionManager(ContourGuiApp& app): _app { app }
{
}

std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty()
std::unique_ptr<vtpty::Pty> 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<vtpty::SshSession>(profile->ssh.value());
#endif
if (!cwd.empty())
profile->shell.value().workingDirectory = std::filesystem::path(cwd);
return make_unique<vtpty::Process>(profile->shell.value(),
vtpty::createPty(profile->terminalSize.value(), nullopt),
profile->escapeSandbox.value());
Expand All @@ -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<vtpty::Process const*>(&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);

Expand All @@ -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;
}

Expand All @@ -72,21 +96,19 @@ 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();

display->setSession(_activeSession);
_activeSession->terminal().resizeScreen(totalPageSize, pixels);
updateStatusLine();

_lastTabChange = std::chrono::steady_clock::now();
}

Expand Down
4 changes: 2 additions & 2 deletions src/contour/TerminalSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TerminalSessionManager: public QAbstractListModel
TerminalSession* getSession() { return _sessions[0]; }

private:
std::unique_ptr<vtpty::Pty> createPty();
std::unique_ptr<vtpty::Pty> createPty(std::string cwd);
[[nodiscard]] auto getCurrentSessionIndex() const
{
return [](auto const& sessions, auto const& activeSession) {
Expand Down Expand Up @@ -87,7 +87,7 @@ class TerminalSessionManager: public QAbstractListModel
TerminalSession* _activeSession = nullptr;
std::vector<TerminalSession*> _sessions;
std::chrono::time_point<std::chrono::steady_clock> _lastTabChange;
std::chrono::milliseconds _timeBetweenTabSwitches { 100 };
std::chrono::milliseconds _timeBetweenTabSwitches { 10 };
};

} // namespace contour
Expand Down
4 changes: 2 additions & 2 deletions src/contour/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 868b154

Please sign in to comment.