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

Code before:

        _vtInputShouldSuppress = true;
        auto resetVtInputSuppress = wil::scope_exit([&]() { _vtInputShouldSuppress = false; });

        // Write to buffer.
        size_t EventsWritten;
        bool SetWaitEvent;
        _WriteBuffer(inEvents, EventsWritten, SetWaitEvent);

        if (SetWaitEvent)
        {
            ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
        }

        // Alert any writers waiting for space.
        WakeUpReadersWaitingForData();
        return EventsWritten;
    }
    catch (...)
    {
        LOG_HR(wil::ResultFromCaughtException());
        return 0;
    }
}

void InputBuffer::WriteDirect(const std::span<const INPUT_RECORD>& inEvents)
try
{
    if (inEvents.empty())
    {
        return;
    }

    const auto initiallyEmptyQueue = _storage.empty();

    for (const auto& inEvent : inEvents)
    {
        _storage.push_back(inEvent);
    }

    if (initiallyEmptyQueue && !_storage.empty())
    {
        ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
    }

    WakeUpReadersWaitingForData();
}
CATCH_LOG()

// This can be considered a "privileged" variant of Write() which allows FOCUS_EVENTs to generate focus VT sequences.
// If we didn't do this, someone could write a FOCUS_EVENT_RECORD with WriteConsoleInput, exit without flushing the

Review comment (suggestion):
Instead of creating a new method for this, couldn't you just make the existing `_HandleTerminalInputCallback` method public, and call it with a string instead of an event list? That way you could drop all that event generation code from the `ReturnResponse` method, and pass the string through directly.

Code after:

        _vtInputShouldSuppress = true;
        auto resetVtInputSuppress = wil::scope_exit([&]() { _vtInputShouldSuppress = false; });

        // Write to buffer.
        size_t EventsWritten;
        bool SetWaitEvent;
        _WriteBuffer(inEvents, EventsWritten, SetWaitEvent);

        if (SetWaitEvent)
        {
            ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
        }

        // Alert any writers waiting for space.
        WakeUpReadersWaitingForData();
        return EventsWritten;
    }
    catch (...)
    {
        LOG_HR(wil::ResultFromCaughtException());
        return 0;
    }
}

void InputBuffer::WriteString(const std::wstring_view& text)
try
{
    if (text.empty())
    {
        return;
    }

    const auto initiallyEmptyQueue = _storage.empty();

    _writeString(text);

    if (initiallyEmptyQueue && !_storage.empty())
    {
        ServiceLocator::LocateGlobals().hInputEvent.SetEvent();
    }

    WakeUpReadersWaitingForData();
}
CATCH_LOG()

// This can be considered a "privileged" variant of Write() which allows FOCUS_EVENTs to generate focus VT sequences.
// If we didn't do this, someone could write a FOCUS_EVENT_RECORD with WriteConsoleInput, exit without flushing the
// input buffer and the next application will suddenly get a "\x1b[I" sequence in their input. See GH#13238.
void InputBuffer::WriteFocusEvent(bool focused) noexcept
{