Skip to content

Commit

Permalink
Consume all input during extract sequence (#4882)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
JohnMcPMS authored Oct 16, 2024
1 parent 1b25158 commit fa62b8b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/AppInstallerCLICore/VTSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
}

Expand Down

0 comments on commit fa62b8b

Please sign in to comment.