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

Code before:
            }

            if (!row.WasWrapForced())
            {
                str.append(L"\r\n");
            }
        }

        return hstring{ str };
    }

    // Get all of our recent commands. This will only really work if the user has enabled shell integration.
    Control::CommandHistoryContext ControlCore::CommandHistory() const
    {
        auto terminalLock = _terminal->LockForWriting();
        auto context = winrt::make_self<CommandHistoryContext>();
        const auto& textBuffer = _terminal->GetTextBuffer();

        for (const auto& mark : _terminal->GetScrollMarks())
        {
            // The command text is between the `end` (which denotes the end of
            // the prompt) and the `commandEnd`.
            bool markHasCommand = mark.commandEnd.has_value() &&
                                  mark.commandEnd != mark.end;
            if (!markHasCommand)
                continue;

            // Get the text of the command
            const auto line = mark.end.y;
            const auto& row = textBuffer.GetRowByOffset(line);
            const auto commandText = row.GetText(mark.end.x, mark.commandEnd->x);

            // Trim off trailing spaces.
            const auto strEnd = commandText.find_last_not_of(UNICODE_SPACE);
            if (strEnd != std::string::npos)
            {
                const auto trimmed = commandText.substr(0, strEnd + 1);
                context->History().Append(winrt::hstring{ trimmed });
            }
        }
        return *context;
    }

    Core::Scheme ControlCore::ColorScheme() const noexcept
    {
        Core::Scheme s;

        // This part is definitely a hack.
        //
        // This function is usually used by the "Preview Color Scheme"
        // functionality in TerminalPage. If we've got an unfocused appearance,

Review comment (suggestion):
```suggestion
            if (!markHasCommand)
            {
                continue;
            }
```

Code after:
            {
                (*shared->updatePatternLocations)();
            }
        }
        catch (...)
        {
            // We're expecting to receive an exception here if the terminal
            // is closed while we're blocked playing a MIDI note.
        }
    }

    uint64_t ControlCore::SwapChainHandle() const
    {
        // This is only ever called by TermControl::AttachContent, which occurs
        // when we're taking an existing core and moving it to a new control.
        // Otherwise, we only ever use the value from the SwapChainChanged
        // event.
        return reinterpret_cast<uint64_t>(_lastSwapChainHandle.get());
    }

    // Method Description:
    // - Clear the contents of the buffer. The region cleared is given by
    //   clearType:
    //   * Screen: Clear only the contents of the visible viewport, leaving the
    //     cursor row at the top of the viewport.
    //   * Scrollback: Clear the contents of the scrollback.
    //   * All: Do both - clear the visible viewport and the scrollback, leaving
    //     only the cursor row at the top of the viewport.
    // Arguments:
    // - clearType: The type of clear to perform.
    // Return Value:
    // - <none>
    void ControlCore::ClearBuffer(Control::ClearBufferType clearType)
    {
        if (clearType == Control::ClearBufferType::Scrollback || clearType == Control::ClearBufferType::All)
        {
            _terminal->EraseScrollback();
        }

        if (clearType == Control::ClearBufferType::Screen || clearType == Control::ClearBufferType::All)
        {
            // Send a signal to conpty to clear the buffer.
            if (auto conpty{ _connection.try_as<TerminalConnection::ConptyConnection>() })
            {
                // ConPTY will emit sequences to sync up our buffer with its new
                // contents.
                conpty.ClearBuffer();
            }
        }
    }