Repository: microsoft/terminal
File: src/cascadia/TerminalCore/Terminal.cpp (C++)

Code before:
    //
    // But also, in the case of something like pwsh, there might be MORE "ghost"
    // text in the buffer _after_ the commandline.
    //
    // We need to trim off the leading DELs, then pad out the rest of the line
    // to cover any other ghost text.
    std::wstring_view view{ input };
    // Where do the DELs end?
    const auto strBegin = view.find_first_not_of(L"\x7f");
    if (strBegin != std::wstring::npos)
    {
        // Trim them off.
        view = view.substr(strBegin);
    }
    // How many spaces do we need, so that the preview exactly covers the entire
    // prompt, all the way to the end of the viewport?
    const auto bufferWidth = _GetMutableViewport().Width();
    const auto cursorX = _activeBuffer().GetCursor().GetPosition().x;
    const auto expectedLenTillEnd = strBegin + (bufferWidth - cursorX);
    std::wstring preview{ view };
    const auto originalSize{ preview.size() };
    if (expectedLenTillEnd > originalSize)
    {
        // pad it out
        preview.insert(originalSize, expectedLenTillEnd - originalSize, L' ');
    }
    snippetPreview.text = til::visualize_nonspace_control_codes(preview);
    // Build our composition data
    const auto len = snippetPreview.text.size();
    snippetPreview.attributes.clear();
    snippetPreview.attributes.emplace_back(len, previewAttrs);
    snippetPreview.cursorPos = len;
    _activeBuffer().NotifyPaintFrame();
}

// These functions are used by TerminalInput, which must build in conhost
// against OneCore compatible signatures. See the definitions in
// VtApiRedirection.hpp (which we cannot include cross-project.)
// Since we don't run on OneCore, we can dispense with the compatibility
// shims.
extern "C" UINT OneCoreSafeMapVirtualKeyW(_In_ UINT uCode, _In_ UINT uMapType)
{
    return MapVirtualKeyW(uCode, uMapType);
}

extern "C" SHORT OneCoreSafeVkKeyScanW(_In_ WCHAR ch)
{
    return VkKeyScanW(ch);
}

extern "C" SHORT OneCoreSafeGetKeyState(_In_ int nVirtKey)

Review comment (suggestion):
(This is not compatible with wide glyphs or much of Unicode. We talked about it internally and agreed to fix it later when it's not an experimental feature. FWIW the `\x7f`-search is also not that good of a representation and we may want to replace it with something else as well. I proposed sending an Esc key press, because it clears the prompt in most shells.)

Code after:
    // characters to backspace off the old command.
    //
    // But also, in the case of something like pwsh, there might be MORE "ghost"
    // text in the buffer _after_ the commandline.
    //
    // We need to trim off the leading DELs, then pad out the rest of the line
    // to cover any other ghost text.
    // Where do the DELs end?
    const auto strBegin = input.find_first_not_of(L"\x7f");
    if (strBegin != std::wstring::npos)
    {
        // Trim them off.
        input = input.substr(strBegin);
    }
    // How many spaces do we need, so that the preview exactly covers the entire
    // prompt, all the way to the end of the viewport?
    const auto bufferWidth = _GetMutableViewport().Width();
    const auto cursorX = _activeBuffer().GetCursor().GetPosition().x;
    const auto expectedLenTillEnd = strBegin + (static_cast<size_t>(bufferWidth) - static_cast<size_t>(cursorX));
    std::wstring preview{ input };
    const auto originalSize{ preview.size() };
    if (expectedLenTillEnd > originalSize)
    {
        // pad it out
        preview.insert(originalSize, expectedLenTillEnd - originalSize, L' ');
    }
    snippetPreview.text = til::visualize_nonspace_control_codes(preview);
    // Build our composition data
    const auto len = snippetPreview.text.size();
    snippetPreview.attributes.clear();
    snippetPreview.attributes.emplace_back(len, previewAttrs);
    snippetPreview.cursorPos = len;
    _activeBuffer().NotifyPaintFrame();
}

// These functions are used by TerminalInput, which must build in conhost
// against OneCore compatible signatures. See the definitions in
// VtApiRedirection.hpp (which we cannot include cross-project.)
// Since we don't run on OneCore, we can dispense with the compatibility
// shims.
extern "C" UINT OneCoreSafeMapVirtualKeyW(_In_ UINT uCode, _In_ UINT uMapType)
{
    return MapVirtualKeyW(uCode, uMapType);
}

extern "C" SHORT OneCoreSafeVkKeyScanW(_In_ WCHAR ch)
{
    return VkKeyScanW(ch);
}

extern "C" SHORT OneCoreSafeGetKeyState(_In_ int nVirtKey)