winget-cli

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

commit 51333037c621c0bfb26568a188ab0d8d3f7a97eb
parent 12dbc22b75f04a94c19f85828122f06c8daa48db
Author: Akinwale Alagbe <24412729+hackean-msft@users.noreply.github.com>
Date:   Thu, 14 Oct 2021 09:39:07 -0700

Check enablement state for all output write operations (#1581)

* enabled check for output stream

* Only disble output stream when use force close

* addressed PR comments

Co-authored-by: Akinwale Alagbe <akalagbe@microsoft.com>
Diffstat:
Msrc/AppInstallerCLICore/ChannelStreams.cpp | 19+++++++++++++++----
Msrc/AppInstallerCLICore/ChannelStreams.h | 13+++++++++----
Msrc/AppInstallerCLICore/ExecutionContext.cpp | 2+-
Msrc/AppInstallerCLICore/ExecutionReporter.cpp | 8++++++--
Msrc/AppInstallerCLICore/ExecutionReporter.h | 2+-
5 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/AppInstallerCLICore/ChannelStreams.cpp b/src/AppInstallerCLICore/ChannelStreams.cpp @@ -41,15 +41,19 @@ namespace AppInstaller::CLI::Execution return *this; } - void BaseStream::Close() + void BaseStream::RestoreDefault() { - m_enabled = false; if (m_VTUpdated) { Write(TextFormat::Default, true); } } + void BaseStream::Disable() + { + m_enabled = false; + } + OutputStream::OutputStream(BaseStream& out, bool enabled, bool VTEnabled) : m_out(out), m_enabled(enabled), @@ -114,8 +118,15 @@ namespace AppInstaller::CLI::Execution OutputStream& OutputStream::operator<<(const std::filesystem::path& path) { - ApplyFormat(); - m_out << path.u8string(); + if (m_enabled) + { + if (m_VTEnabled) + { + ApplyFormat(); + } + m_out << path.u8string(); + } return *this; + } } \ No newline at end of file diff --git a/src/AppInstallerCLICore/ChannelStreams.h b/src/AppInstallerCLICore/ChannelStreams.h @@ -57,7 +57,9 @@ namespace AppInstaller::CLI::Execution BaseStream& operator<<(const VirtualTerminal::Sequence& sequence); BaseStream& operator<<(const VirtualTerminal::ConstructedSequence& sequence); - void Close(); + void RestoreDefault(); + + void Disable(); private: template <typename T> @@ -100,11 +102,14 @@ namespace AppInstaller::CLI::Execution // informs the output that there is no localized version to use. // TODO: Convert the rest of the code base and uncomment to enforce localization. //static_assert(details::IsApprovedForOutput<std::decay_t<T>>::value, "This type may not be localized, see comment for more information"); - if (m_VTEnabled) + if (m_enabled) { - ApplyFormat(); + if (m_VTEnabled) + { + ApplyFormat(); + } + m_out << t; } - m_out << t; return *this; } diff --git a/src/AppInstallerCLICore/ExecutionContext.cpp b/src/AppInstallerCLICore/ExecutionContext.cpp @@ -169,7 +169,7 @@ namespace AppInstaller::CLI::Execution // Unless we want to spin a separate thread for all work, we have to just exit here. if (m_CtrlSignalCount >= 2) { - Reporter.CloseOutputStream(); + Reporter.CloseOutputStream(true); Logging::Telemetry().LogCommandTermination(hr, file, line); std::exit(hr); } diff --git a/src/AppInstallerCLICore/ExecutionReporter.cpp b/src/AppInstallerCLICore/ExecutionReporter.cpp @@ -218,8 +218,12 @@ namespace AppInstaller::CLI::Execution return m_isVTEnabled && ConsoleModeRestore::Instance().IsVTEnabled(); } - void Reporter::CloseOutputStream() + void Reporter::CloseOutputStream(bool forceDisable) { - m_out->Close(); + if (forceDisable) + { + m_out->Disable(); + } + m_out->RestoreDefault(); } } \ No newline at end of file diff --git a/src/AppInstallerCLICore/ExecutionReporter.h b/src/AppInstallerCLICore/ExecutionReporter.h @@ -131,7 +131,7 @@ namespace AppInstaller::CLI::Execution // Cancels the in progress task. void CancelInProgressTask(bool force); - void CloseOutputStream(); + void CloseOutputStream(bool forceDisable = false); void SetProgressSink(IProgressSink* sink) {