Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
            const auto lock = _terminal->LockForWriting();
            // In absolute buffer coordinates, including the scrollback (= Y is offset by the scrollback height).
            const auto viewport = _terminal->GetViewport();
            // The absolute cursor coordinate.
            const auto cursor = _terminal->GetViewportRelativeCursorPosition();

            // GH#18732: Users want the row the cursor is on to be preserved across clears.
            std::wstring sequence;

            if (clearType == ClearBufferType::Scrollback || clearType == ClearBufferType::All)
            {
                sequence.append(L"\x1b[3J");
            }

            if (clearType == ClearBufferType::Screen || clearType == ClearBufferType::All)
            {
                // Erase any viewport contents below (but not including) the cursor row.
                if (cursor.x < viewport.Height())
                {
                    fmt::format_to(std::back_inserter(sequence), FMT_COMPILE(L"\x1b[{};1H\x1b[J"), cursor.y + 2);
                }

                // Erase any viewport contents above (but not including) the cursor row.
                if (cursor.x > 0)
                {
                    fmt::format_to(std::back_inserter(sequence), FMT_COMPILE(L"\x1b[{}S\x1b[1;{}H"), cursor.y, cursor.x + 1);
                }
            }

            _terminal->Write(sequence);
        }

        if (clearType == Control::ClearBufferType::Screen || clearType == Control::ClearBufferType::All)
        {
            if (auto conpty{ _connection.try_as<TerminalConnection::ConptyConnection>() })
            {
                // Since the clearing of ConPTY occurs asynchronously, this call can result weird issues,
                // where a console application still sees contents that we've already deleted, etc.
                conpty.ClearBuffer(true);
            }
        }
    }

    hstring ControlCore::ReadEntireBuffer() const
    {
        const auto lock = _terminal->LockForWriting();

        const auto& textBuffer = _terminal->GetTextBuffer();

        std::wstring str;
        const auto lastRow = textBuffer.GetLastNonSpaceCharacter().y;

Review comment (suggestion):
This is not super important, but I'd prefer we didn't use an `SU` sequence here, because it's not standard (in the way modern terminals implement it). We should ideally be using a `DL` sequence, so something like `\x1b[H\x1b[{}M` in place of the `\x1b[{}S`.

Also, and this is important, that final cursor positioning sequence should be applied outside the if, because it's required for both branches. If you're on the top line of the page, you're going to erase below, and not above, but you'll still need to reposition the cursor.

Code after:
    // Return Value:
    // - <none>
    void ControlCore::ClearBuffer(Control::ClearBufferType clearType)
    {
        {
            const auto lock = _terminal->LockForWriting();
            // In absolute buffer coordinates, including the scrollback (= Y is offset by the scrollback height).
            const auto viewport = _terminal->GetViewport();
            // The absolute cursor coordinate.
            const auto cursor = _terminal->GetViewportRelativeCursorPosition();

            // GH#18732: Users want the row the cursor is on to be preserved across clears.
            std::wstring sequence;

            if (clearType == ClearBufferType::Scrollback || clearType == ClearBufferType::All)
            {
                sequence.append(L"\x1b[3J");
            }

            if (clearType == ClearBufferType::Screen || clearType == ClearBufferType::All)
            {
                // Erase any viewport contents below (but not including) the cursor row.
                if (viewport.Height() - cursor.y > 1)
                {
                    fmt::format_to(std::back_inserter(sequence), FMT_COMPILE(L"\x1b[{};1H\x1b[J"), cursor.y + 2);
                }

                // Erase any viewport contents above (but not including) the cursor row.
                if (cursor.y > 0)
                {
                    // An SU sequence would be simpler than this DL sequence,
                    // but SU isn't well standardized between terminals.
                    // Generally speaking, it's best avoiding it.
                    fmt::format_to(std::back_inserter(sequence), FMT_COMPILE(L"\x1b[H\x1b[{}M"), cursor.y);
                }

                fmt::format_to(std::back_inserter(sequence), FMT_COMPILE(L"\x1b[1;{}H"), cursor.x + 1);
            }

            _terminal->Write(sequence);
        }

        if (clearType == Control::ClearBufferType::Screen || clearType == Control::ClearBufferType::All)
        {
            if (auto conpty{ _connection.try_as<TerminalConnection::ConptyConnection>() })
            {
                // Since the clearing of ConPTY occurs asynchronously, this call can result weird issues,
                // where a console application still sees contents that we've already deleted, etc.
                conpty.ClearBuffer(true);
            }
        }