13
13
#include < QtQml/QQmlEngine>
14
14
15
15
#include < algorithm>
16
+ #include < filesystem>
17
+ #include < mutex>
16
18
#include < string>
17
19
18
20
using namespace std ::string_literals;
@@ -27,13 +29,15 @@ TerminalSessionManager::TerminalSessionManager(ContourGuiApp& app): _app { app }
27
29
{
28
30
}
29
31
30
- std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty ()
32
+ std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty (std::optional<std::string> cwd )
31
33
{
32
34
auto const & profile = _app.config ().profile (_app.profileName ());
33
35
#if defined(VTPTY_LIBSSH2)
34
36
if (!profile->ssh .value ().hostname .empty ())
35
37
return make_unique<vtpty::SshSession>(profile->ssh .value ());
36
38
#endif
39
+ if (cwd)
40
+ profile->shell .value ().workingDirectory = std::filesystem::path (cwd.value ());
37
41
return make_unique<vtpty::Process>(profile->shell .value (),
38
42
vtpty::createPty (profile->terminalSize .value (), nullopt),
39
43
profile->escapeSandbox .value ());
@@ -43,7 +47,32 @@ TerminalSession* TerminalSessionManager::createSession()
43
47
{
44
48
// TODO: Remove dependency on app-knowledge and pass shell / terminal-size instead.
45
49
// 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);
47
76
48
77
_sessions.push_back (session);
49
78
@@ -57,6 +86,7 @@ TerminalSession* TerminalSessionManager::createSession()
57
86
58
87
// we can close application right after session has been created
59
88
_lastTabChange = std::chrono::steady_clock::now () - std::chrono::seconds (1 );
89
+ _activeSession = session;
60
90
return session;
61
91
}
62
92
@@ -72,21 +102,19 @@ void TerminalSessionManager::setSession(size_t index)
72
102
if (index < _sessions.size ())
73
103
_activeSession = _sessions[index ];
74
104
else
75
- _activeSession = createSession ();
105
+ createSession ();
76
106
77
107
if (oldSession == _activeSession)
78
108
return ;
79
109
80
- if (oldSession)
81
- oldSession->detachDisplay (*display);
82
-
83
110
Require (display != nullptr );
84
111
auto const pixels = display->pixelSize ();
85
112
auto const totalPageSize = display->calculatePageSize () + _activeSession->terminal ().statusLineHeight ();
86
113
87
114
display->setSession (_activeSession);
88
115
_activeSession->terminal ().resizeScreen (totalPageSize, pixels);
89
116
updateStatusLine ();
117
+
90
118
_lastTabChange = std::chrono::steady_clock::now ();
91
119
}
92
120
@@ -106,16 +134,9 @@ void TerminalSessionManager::switchToTabLeft()
106
134
{
107
135
setSession (currentSessionIndex - 1 );
108
136
}
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
117
138
{
118
- setSession (position - 1 );
139
+ setSession (_sessions. size () - 1 );
119
140
}
120
141
}
121
142
@@ -129,6 +150,21 @@ void TerminalSessionManager::switchToTabRight()
129
150
{
130
151
setSession (currentSessionIndex + 1 );
131
152
}
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
+ }
132
168
}
133
169
134
170
void TerminalSessionManager::closeTab ()
0 commit comments