Repository: microsoft/terminal
File: src/host/consoleInformation.cpp (C++)

Code before:

bool CONSOLE_INFORMATION::GetBracketedPasteMode() const noexcept
{
    return _bracketedPasteMode;
}

void CONSOLE_INFORMATION::SetBracketedPasteMode(const bool enabled) noexcept
{
    _bracketedPasteMode = enabled;
}

void CONSOLE_INFORMATION::CopyTextToClipboard(const std::wstring_view text)
{
    const auto window = ServiceLocator::LocateConsoleWindow();
    if (window)
    {
        // The clipboard can only be updated from the main GUI thread, so we
        // need to post a message to trigger the actual copy operation. But if
        // the pending clipboard content is already set, a message would have
        // already been posted, so there's no need to post another one.
        const auto clipboardMessageSent = _pendingClipboardText.has_value();
        _pendingClipboardText = text;
        if (!clipboardMessageSent)
        {
            PostMessageW(window->GetWindowHandle(), CM_UPDATE_CLIPBOARD, 0, 0);
        }
    }
}

std::optional<std::wstring> CONSOLE_INFORMATION::UsePendingClipboardText()
{
    // Once the pending text has been used, we clear the variable to let the
    // CopyTextToClipboard method know that the last CM_UPDATE_CLIPBOARD message
    // has been processed, and future updates will require another message.
    const auto clipboardText = _pendingClipboardText;
    _pendingClipboardText.reset();
    return clipboardText;
}

// Method Description:
// - Return the active screen buffer of the console.
// Arguments:
// - <none>
// Return Value:
// - the active screen buffer of the console.
SCREEN_INFORMATION& CONSOLE_INFORMATION::GetActiveOutputBuffer()
{
    return *pCurrentScreenBuffer;
}

const SCREEN_INFORMATION& CONSOLE_INFORMATION::GetActiveOutputBuffer() const

Review comment (suggestion):
I don't know if I've just misunderstood how you're supposed to use the windows clipboard, but I couldn't get it working from a background thread, and this technique worked for me. I don't know if maybe there's a better way to do this.

Code after:

bool CONSOLE_INFORMATION::GetBracketedPasteMode() const noexcept
{
    return _bracketedPasteMode;
}

void CONSOLE_INFORMATION::SetBracketedPasteMode(const bool enabled) noexcept
{
    _bracketedPasteMode = enabled;
}

void CONSOLE_INFORMATION::CopyTextToClipboard(const std::wstring_view text)
{
    const auto window = ServiceLocator::LocateConsoleWindow();
    if (window)
    {
        // The clipboard can only be updated from the main GUI thread, so we
        // need to post a message to trigger the actual copy operation. But if
        // the pending clipboard content is already set, a message would have
        // already been posted, so there's no need to post another one.
        const auto clipboardMessageSent = _pendingClipboardText.has_value();
        _pendingClipboardText = text;
        if (!clipboardMessageSent)
        {
            PostMessageW(window->GetWindowHandle(), CM_UPDATE_CLIPBOARD, 0, 0);
        }
    }
}

std::optional<std::wstring> CONSOLE_INFORMATION::UsePendingClipboardText()
{
    // Once the pending text has been used, we clear the variable to let the
    // CopyTextToClipboard method know that the last CM_UPDATE_CLIPBOARD message
    // has been processed, and future updates will require another message.
    return std::exchange(_pendingClipboardText, {});
}

// Method Description:
// - Return the active screen buffer of the console.
// Arguments:
// - <none>
// Return Value:
// - the active screen buffer of the console.
SCREEN_INFORMATION& CONSOLE_INFORMATION::GetActiveOutputBuffer()
{
    return *pCurrentScreenBuffer;
}

const SCREEN_INFORMATION& CONSOLE_INFORMATION::GetActiveOutputBuffer() const
{
    return *pCurrentScreenBuffer;