Repository: microsoft/terminal
File: src/terminal/adapter/adaptDispatch.cpp (C++)

Code before:
//  - Erase Scrollback (^[[3J - ED extension by xterm)
//    Because conhost doesn't exactly have a scrollback, We have to be tricky here.
//    We need to move the entire viewport to 0,0, and clear everything outside
//      (0, 0, viewportWidth, viewportHeight) To give the appearance that
//      everything above the viewport was cleared.
//    We don't want to save the text BELOW the viewport, because in *nix, there isn't anything there
//      (There isn't a scroll-forward, only a scrollback)
// Arguments:
// - <none>
// Return value:
// - True if handled successfully. False otherwise.
bool AdaptDispatch::_EraseScrollback()
{
    const auto viewport = _api.GetViewport();
    const auto top = viewport.top;
    const auto height = viewport.bottom - viewport.top;
    auto& textBuffer = _api.GetTextBuffer();
    const auto bufferSize = textBuffer.GetSize().Dimensions();
    auto& cursor = textBuffer.GetCursor();
    const auto row = cursor.GetPosition().y;

    // Scroll all the marks up. This will trim ones that are now "outside" the buffer
    textBuffer.ScrollMarks(-top);
    // Also clear all the marks below the new viewport position.
    textBuffer.ClearMarksInRange(til::point{ 0, height },
                                 til::point{ bufferSize.width, bufferSize.height });

    // Scroll the viewport content to the top of the buffer.
    textBuffer.ScrollRows(top, height, -top);
    // Clear everything after the viewport.
    _FillRect(textBuffer, { 0, height, bufferSize.width, bufferSize.height }, whitespace, {});
    // Also reset the line rendition for all of the cleared rows.
    textBuffer.ResetLineRenditionRange(height, bufferSize.height);
    // Move the viewport
    _api.SetViewportPosition({ viewport.left, 0 });
    // Move the cursor to the same relative location.
    cursor.SetYPosition(row - top);
    cursor.SetHasMoved(true);

    // GH#2715 - If this succeeded, but we're in a conpty, return `false` to
    // make the state machine propagate this ED sequence to the connected
    // terminal application. While we're in conpty mode, we don't really
    // have a scrollback, but the attached terminal might.
    return !_api.IsConsolePty();
}

//Routine Description:
// - Erase All (^[[2J - ED)
//   Performs a VT Erase All operation. In most terminals, this is done by
//   moving the viewport into the scrollback, clearing out the current screen.
//   For them, there can never be any characters beneath the viewport, as the

Review comment (suggestion):
You could swap these two to reduce the amount of work. First clear everything outside of the `viewport` and then scroll the remaining bits up by `-top`.

Code after:
//  - Erase Scrollback (^[[3J - ED extension by xterm)
//    Because conhost doesn't exactly have a scrollback, We have to be tricky here.
//    We need to move the entire viewport to 0,0, and clear everything outside
//      (0, 0, viewportWidth, viewportHeight) To give the appearance that
//      everything above the viewport was cleared.
//    We don't want to save the text BELOW the viewport, because in *nix, there isn't anything there
//      (There isn't a scroll-forward, only a scrollback)
// Arguments:
// - <none>
// Return value:
// - True if handled successfully. False otherwise.
bool AdaptDispatch::_EraseScrollback()
{
    const auto viewport = _api.GetViewport();
    const auto top = viewport.top;
    const auto height = viewport.bottom - viewport.top;
    auto& textBuffer = _api.GetTextBuffer();
    const auto bufferSize = textBuffer.GetSize().Dimensions();
    auto& cursor = textBuffer.GetCursor();
    const auto row = cursor.GetPosition().y;

    // Clear all the marks below the new viewport position.
    textBuffer.ClearMarksInRange(til::point{ 0, height },
                                 til::point{ bufferSize.width, bufferSize.height });
    // Then scroll all the remaining marks up. This will trim ones that are now "outside" the buffer
    textBuffer.ScrollMarks(-top);

    // Scroll the viewport content to the top of the buffer.
    textBuffer.ScrollRows(top, height, -top);
    // Clear everything after the viewport.
    _FillRect(textBuffer, { 0, height, bufferSize.width, bufferSize.height }, whitespace, {});
    // Also reset the line rendition for all of the cleared rows.
    textBuffer.ResetLineRenditionRange(height, bufferSize.height);
    // Move the viewport
    _api.SetViewportPosition({ viewport.left, 0 });
    // Move the cursor to the same relative location.
    cursor.SetYPosition(row - top);
    cursor.SetHasMoved(true);

    // GH#2715 - If this succeeded, but we're in a conpty, return `false` to
    // make the state machine propagate this ED sequence to the connected
    // terminal application. While we're in conpty mode, we don't really
    // have a scrollback, but the attached terminal might.
    return !_api.IsConsolePty();
}

//Routine Description:
// - Erase All (^[[2J - ED)
//   Performs a VT Erase All operation. In most terminals, this is done by
//   moving the viewport into the scrollback, clearing out the current screen.
//   For them, there can never be any characters beneath the viewport, as the