Skip to content

Commit 9e2c111

Browse files
committed
Use input handle type to determine if we should attempt to read from it
1 parent 4f3f27a commit 9e2c111

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/AppInstallerCLICore/Core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace AppInstaller::CLI
8383

8484
Logging::UseGlobalTelemetryLoggerActivityIdOnly();
8585

86-
Execution::Context context{ std::cout, std::cin };
86+
Execution::Context context;
8787
auto previousThreadGlobals = context.SetForCurrentThread();
8888

8989
// Set up debug string logging during initialization

src/AppInstallerCLICore/ExecutionContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ namespace AppInstaller::CLI::Execution
9393
// arguments via Execution::Args.
9494
struct Context : EnumBasedVariantMap<Data, details::DataMapping, ContextEnumBasedVariantMapActionCallback>
9595
{
96+
Context() = default;
9697
Context(std::ostream& out, std::istream& in) : Reporter(out, in) {}
9798

9899
// Constructor for creating a sub-context.

src/AppInstallerCLICore/ExecutionReporter.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@ namespace AppInstaller::CLI::Execution
2323
const Sequence& ConfigurationUnitEmphasis = TextFormat::Foreground::BrightCyan;
2424
const Sequence& AuthenticationEmphasis = TextFormat::Foreground::BrightYellow;
2525

26-
Reporter::Reporter() :
27-
Reporter(std::cout, std::cin)
26+
namespace
2827
{
29-
HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
30-
if (outHandle == INVALID_HANDLE_VALUE)
31-
{
32-
LOG_LAST_ERROR();
33-
}
34-
else if (outHandle != NULL)
28+
DWORD GetStdHandleType(DWORD stdHandle)
3529
{
30+
DWORD result = FILE_TYPE_UNKNOWN;
31+
32+
HANDLE handle = GetStdHandle(stdHandle);
33+
if (handle != INVALID_HANDLE_VALUE && handle != NULL)
34+
{
35+
result = GetFileType(handle);
36+
}
3637

38+
return result;
3739
}
3840
}
3941

42+
Reporter::Reporter() :
43+
Reporter(std::cout, std::cin)
44+
{
45+
m_outStreamFileType = GetStdHandleType(STD_OUTPUT_HANDLE);
46+
m_inStreamFileType = GetStdHandleType(STD_INPUT_HANDLE);
47+
}
48+
4049
Reporter::Reporter(std::ostream& outStream, std::istream& inStream) :
4150
Reporter(std::make_shared<BaseStream>(outStream, true, ConsoleModeRestore::Instance().IsVTEnabled()), inStream)
4251
{
@@ -62,6 +71,9 @@ namespace AppInstaller::CLI::Execution
6271
Reporter::Reporter(const Reporter& other, clone_t) :
6372
Reporter(other.m_out, other.m_in)
6473
{
74+
m_outStreamFileType = other.m_outStreamFileType;
75+
m_inStreamFileType = other.m_inStreamFileType;
76+
6577
SetChannel(other.m_channel);
6678

6779
if (other.m_style.has_value())
@@ -154,7 +166,7 @@ namespace AppInstaller::CLI::Execution
154166

155167
bool Reporter::InputStreamIsInteractive() const
156168
{
157-
169+
return m_inStreamFileType == FILE_TYPE_CHAR;
158170
}
159171

160172
bool Reporter::PromptForBoolResponse(Resource::LocString message, Level level, bool resultIfDisabled)

src/AppInstallerCLICore/ExecutionReporter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ namespace AppInstaller::CLI::Execution
208208
wil::srwlock m_progressCallbackLock;
209209
std::atomic<ProgressCallback*> m_progressCallback;
210210
std::atomic<IProgressSink*> m_progressSink;
211+
DWORD m_outStreamFileType = FILE_TYPE_UNKNOWN;
212+
DWORD m_inStreamFileType = FILE_TYPE_UNKNOWN;
211213

212214
// Enable all levels by default
213215
Level m_enabledLevels = Level::All;

0 commit comments

Comments
 (0)