winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit fa62b8bf54cb2d2c9f503c5ead6da2a6247ec3a2
parent 1b251584211e344c756b8bad10f71db57019aa39
Author: JohnMcPMS <johnmcp@microsoft.com>
Date:   Wed, 16 Oct 2024 08:46:06 -0700

Consume all input during extract sequence (#4882)

## Change
When extracting a sequence, consume all (up to 1KB really) input even if
it doesn't start with an escape character.

This was spurred by an issue where the `peek` call was not returning
(waiting for input). Pressing Enter broke it out of that wait, but other
input was now at the front and blocked future attempts to extract the
sequence.

The best solution to this would be to move all input handling to its own
dedicated thread, but that is a much larger change.
Diffstat:
Msrc/AppInstallerCLICore/VTSupport.cpp | 40++++++++++++++++++++--------------------
1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/AppInstallerCLICore/VTSupport.cpp b/src/AppInstallerCLICore/VTSupport.cpp @@ -61,31 +61,31 @@ namespace AppInstaller::CLI::VirtualTerminal // Extracts a VT sequence, expected one of the form ESCAPE + prefix + result + suffix, returning the result part. std::string ExtractSequence(std::istream& inStream, std::string_view prefix, std::string_view suffix) { - std::string result; + // Force discovery of available input + std::ignore = inStream.peek(); - if (inStream.peek() == AICLI_VT_ESCAPE[0]) - { - result.resize(4095); - inStream.readsome(&result[0], result.size()); - THROW_HR_IF(E_UNEXPECTED, static_cast<size_t>(inStream.gcount()) >= result.size()); + static constexpr std::streamsize s_bufferSize = 1024; + char buffer[s_bufferSize]; + std::streamsize bytesRead = inStream.readsome(buffer, s_bufferSize); + THROW_HR_IF(E_UNEXPECTED, bytesRead >= s_bufferSize); - result.resize(static_cast<size_t>(inStream.gcount())); + std::string_view resultView{ buffer, static_cast<size_t>(bytesRead) }; + size_t escapeIndex = resultView.find(AICLI_VT_ESCAPE[0]); + if (escapeIndex == std::string_view::npos) + { + return {}; + } - std::string_view resultView = result; - size_t overheadLength = 1 + prefix.length() + suffix.length(); - if (resultView.length() <= overheadLength || - resultView.substr(1, prefix.length()) != prefix || - resultView.substr(resultView.length() - suffix.length()) != suffix) - { - result.clear(); - } - else - { - result = result.substr(1 + prefix.length(), result.length() - overheadLength); - } + resultView = resultView.substr(escapeIndex); + size_t overheadLength = 1 + prefix.length() + suffix.length(); + if (resultView.length() <= overheadLength || + resultView.substr(1, prefix.length()) != prefix || + resultView.substr(resultView.length() - suffix.length()) != suffix) + { + return {}; } - return result; + return std::string{ resultView.substr(1 + prefix.length(), resultView.length() - overheadLength) }; } }