Skip to content

Commit 96a01b6

Browse files
Merge pull request #1673 from contour-terminal/improvements/tabs
Some Improvements to the tabs
2 parents 46f74e9 + 455d08b commit 96a01b6

File tree

4 files changed

+61
-21
lines changed

4 files changed

+61
-21
lines changed

src/contour/TerminalSessionManager.cpp

+51-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <QtQml/QQmlEngine>
1414

1515
#include <algorithm>
16+
#include <filesystem>
17+
#include <mutex>
1618
#include <string>
1719

1820
using namespace std::string_literals;
@@ -27,13 +29,15 @@ TerminalSessionManager::TerminalSessionManager(ContourGuiApp& app): _app { app }
2729
{
2830
}
2931

30-
std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty()
32+
std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty(std::optional<std::string> cwd)
3133
{
3234
auto const& profile = _app.config().profile(_app.profileName());
3335
#if defined(VTPTY_LIBSSH2)
3436
if (!profile->ssh.value().hostname.empty())
3537
return make_unique<vtpty::SshSession>(profile->ssh.value());
3638
#endif
39+
if (cwd)
40+
profile->shell.value().workingDirectory = std::filesystem::path(cwd.value());
3741
return make_unique<vtpty::Process>(profile->shell.value(),
3842
vtpty::createPty(profile->terminalSize.value(), nullopt),
3943
profile->escapeSandbox.value());
@@ -43,7 +47,32 @@ TerminalSession* TerminalSessionManager::createSession()
4347
{
4448
// TODO: Remove dependency on app-knowledge and pass shell / terminal-size instead.
4549
// The GuiApp *or* (Global)Config could be made a global to be accessable from within QML.
46-
auto* session = new TerminalSession(createPty(), _app);
50+
//
51+
52+
#if !defined(_WIN32)
53+
auto ptyPath = [this]() -> std::optional<std::string> {
54+
if (_activeSession)
55+
{
56+
auto& terminal = _activeSession->terminal();
57+
if (auto const* ptyProcess = dynamic_cast<vtpty::Process const*>(&terminal.device()))
58+
return ptyProcess->workingDirectory();
59+
}
60+
return std::nullopt;
61+
}();
62+
#else
63+
std::optional<std::string> ptyPath = std::nullopt;
64+
if (_activeSession)
65+
{
66+
auto& terminal = _activeSession->terminal();
67+
{
68+
auto _l = std::scoped_lock { terminal };
69+
ptyPath = terminal.currentWorkingDirectory();
70+
}
71+
}
72+
#endif
73+
74+
auto* session = new TerminalSession(createPty(ptyPath), _app);
75+
managerLog()("CREATE SESSION, new session: {}", (void*) session);
4776

4877
_sessions.push_back(session);
4978

@@ -57,6 +86,7 @@ TerminalSession* TerminalSessionManager::createSession()
5786

5887
// we can close application right after session has been created
5988
_lastTabChange = std::chrono::steady_clock::now() - std::chrono::seconds(1);
89+
_activeSession = session;
6090
return session;
6191
}
6292

@@ -72,21 +102,19 @@ void TerminalSessionManager::setSession(size_t index)
72102
if (index < _sessions.size())
73103
_activeSession = _sessions[index];
74104
else
75-
_activeSession = createSession();
105+
createSession();
76106

77107
if (oldSession == _activeSession)
78108
return;
79109

80-
if (oldSession)
81-
oldSession->detachDisplay(*display);
82-
83110
Require(display != nullptr);
84111
auto const pixels = display->pixelSize();
85112
auto const totalPageSize = display->calculatePageSize() + _activeSession->terminal().statusLineHeight();
86113

87114
display->setSession(_activeSession);
88115
_activeSession->terminal().resizeScreen(totalPageSize, pixels);
89116
updateStatusLine();
117+
90118
_lastTabChange = std::chrono::steady_clock::now();
91119
}
92120

@@ -106,16 +134,9 @@ void TerminalSessionManager::switchToTabLeft()
106134
{
107135
setSession(currentSessionIndex - 1);
108136
}
109-
}
110-
111-
void TerminalSessionManager::switchToTab(int position)
112-
{
113-
managerLog()(std::format(
114-
"switchToTab from {} to {} (out of {})", getCurrentSessionIndex(), position - 1, _sessions.size()));
115-
116-
if (1 <= position && position <= static_cast<int>(_sessions.size()))
137+
else // wrap
117138
{
118-
setSession(position - 1);
139+
setSession(_sessions.size() - 1);
119140
}
120141
}
121142

@@ -129,6 +150,21 @@ void TerminalSessionManager::switchToTabRight()
129150
{
130151
setSession(currentSessionIndex + 1);
131152
}
153+
else // wrap
154+
{
155+
setSession(0);
156+
}
157+
}
158+
159+
void TerminalSessionManager::switchToTab(int position)
160+
{
161+
managerLog()(std::format(
162+
"switchToTab from {} to {} (out of {})", getCurrentSessionIndex(), position - 1, _sessions.size()));
163+
164+
if (1 <= position && position <= static_cast<int>(_sessions.size()))
165+
{
166+
setSession(position - 1);
167+
}
132168
}
133169

134170
void TerminalSessionManager::closeTab()

src/contour/TerminalSessionManager.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TerminalSessionManager: public QAbstractListModel
4949
TerminalSession* getSession() { return _sessions[0]; }
5050

5151
private:
52-
std::unique_ptr<vtpty::Pty> createPty();
52+
std::unique_ptr<vtpty::Pty> createPty(std::optional<std::string> cwd);
5353
[[nodiscard]] auto getCurrentSessionIndex() const
5454
{
5555
return [](auto const& sessions, auto const& activeSession) {
@@ -87,7 +87,7 @@ class TerminalSessionManager: public QAbstractListModel
8787
TerminalSession* _activeSession = nullptr;
8888
std::vector<TerminalSession*> _sessions;
8989
std::chrono::time_point<std::chrono::steady_clock> _lastTabChange;
90-
std::chrono::milliseconds _timeBetweenTabSwitches { 100 };
90+
std::chrono::milliseconds _timeBetweenTabSwitches { 10 };
9191
};
9292

9393
} // namespace contour

src/contour/display/TerminalDisplay.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ void TerminalDisplay::setSession(TerminalSession* newSession)
311311
window()->setFlag(Qt::FramelessWindowHint, !profile().showTitleBar.value());
312312

313313
if (!_renderer)
314+
{
315+
314316
_renderer = make_unique<vtrasterizer::Renderer>(
315317
_session->profile().terminalSize.value(),
316318
sanitizeFontDescription(profile().fonts.value(), fontDPI()),
@@ -323,9 +325,11 @@ void TerminalDisplay::setSession(TerminalSession* newSession)
323325
// TODO: , WindowMargin(windowMargin_.left, windowMargin_.bottom);
324326
);
325327

326-
applyFontDPI();
327-
updateImplicitSize();
328-
updateMinimumSize();
328+
// setup once with the renderer creation
329+
applyFontDPI();
330+
updateImplicitSize();
331+
updateMinimumSize();
332+
}
329333

330334
_session->attachDisplay(*this); // NB: Requires Renderer to be instanciated to retrieve grid metrics.
331335

src/vtrasterizer/TextClusterGrouper_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <libunicode/convert.h>
1010

11-
#include <range/v3/to_container.hpp>
11+
#include <range/v3/range/conversion.hpp>
1212
#include <range/v3/view/join.hpp>
1313
#include <range/v3/view/transform.hpp>
1414

0 commit comments

Comments
 (0)