Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show parts of the scrollback on restore #17334

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2994,15 +2994,6 @@ namespace winrt::TerminalApp::implementation

const auto content = _manager.CreateCore(settings.DefaultSettings(), settings.UnfocusedSettings(), connection);
const TermControl control{ content };

if (const auto id = settings.DefaultSettings().SessionId(); id != winrt::guid{})
{
const auto settingsDir = CascadiaSettings::SettingsDirectory();
const auto idStr = Utils::GuidToPlainString(id);
const auto path = fmt::format(FMT_COMPILE(L"{}\\buffer_{}.txt"), settingsDir, idStr);
control.RestoreFromPath(path);
}

return _SetupControl(control);
}

Expand Down Expand Up @@ -3102,7 +3093,10 @@ namespace winrt::TerminalApp::implementation
return nullptr;
}

auto connection = existingConnection ? existingConnection : _CreateConnectionFromSettings(profile, controlSettings.DefaultSettings(), false);
const auto sessionId = controlSettings.DefaultSettings().SessionId();
const auto hasSessionId = sessionId != winrt::guid{};

auto connection = existingConnection ? existingConnection : _CreateConnectionFromSettings(profile, controlSettings.DefaultSettings(), hasSessionId);
if (existingConnection)
{
connection.Resize(controlSettings.DefaultSettings().InitialRows(), controlSettings.DefaultSettings().InitialCols());
Expand All @@ -3124,6 +3118,14 @@ namespace winrt::TerminalApp::implementation

const auto control = _CreateNewControlAndContent(controlSettings, connection);

if (hasSessionId)
{
const auto settingsDir = CascadiaSettings::SettingsDirectory();
const auto idStr = Utils::GuidToPlainString(sessionId);
const auto path = fmt::format(FMT_COMPILE(L"{}\\buffer_{}.txt"), settingsDir, idStr);
control.RestoreFromPath(path);
}

auto paneContent{ winrt::make<TerminalPaneContent>(profile, _terminalSettingsCache, control) };

auto resultPane = std::make_shared<Pane>(paneContent);
Expand Down
19 changes: 13 additions & 6 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,12 +1787,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// * prints " [Restored <date> <time>] <spaces until end of line> "
// * resets the color ("\x1b[m")
// * newlines
// * clears the screen ("\x1b[2J")
// The last step is necessary because we launch ConPTY without PSEUDOCONSOLE_INHERIT_CURSOR by default.
// This will cause ConPTY to emit a \x1b[2J sequence on startup to ensure it and the terminal are in-sync.
// If we didn't do a \x1b[2J ourselves as well, the user would briefly see the last state of the terminal,
// before it's quickly scrolled away once ConPTY has finished starting up, which looks weird.
message = fmt::format(FMT_COMPILE(L"\x1b[100;37m [{} {} {}]\x1b[K\x1b[m\r\n\x1b[2J"), msg, date, time);
message = fmt::format(FMT_COMPILE(L"\x1b[100;37m [{} {} {}]\x1b[K\x1b[m\r\n"), msg, date, time);
}

wchar_t buffer[32 * 1024];
Expand Down Expand Up @@ -1830,6 +1825,18 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}

_terminal->Write(message);

// Show 3 lines of scrollback to the user, so they know it's there. Otherwise, in particular with the well
// hidden touch scrollbars in WinUI 2 and later, there's no indication that there's something to scroll up to.
//
// We only show 3 lines because ConPTY doesn't know about our restored buffer contents initially,
// and so ReadConsole calls will return whitespace.
//
// We also avoid using actual newlines or similar here, because if we ever change our text buffer implementation
// to actually track the written contents, we don't want this to be part of the next buffer snapshot.
const auto cursorPosition = _terminal->GetCursorPosition();
const auto y = std::max(0, cursorPosition.y - 4);
_terminal->SetViewportPosition({ 0, y });
}
}

Expand Down
Loading