forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutionReporter.cpp
More file actions
146 lines (128 loc) · 3.98 KB
/
ExecutionReporter.cpp
File metadata and controls
146 lines (128 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "ExecutionReporter.h"
namespace AppInstaller::CLI::Execution
{
using namespace Settings;
using namespace VirtualTerminal;
const Sequence& HelpCommandEmphasis = TextFormat::Foreground::BrightWhite;
const Sequence& HelpArgumentEmphasis = TextFormat::Foreground::BrightWhite;
const Sequence& NameEmphasis = TextFormat::Foreground::BrightCyan;
const Sequence& IdEmphasis = TextFormat::Foreground::BrightCyan;
const Sequence& UrlEmphasis = TextFormat::Foreground::BrightBlue;
Reporter::Reporter(std::ostream& outStream, std::istream& inStream) :
m_out(outStream),
m_in(inStream),
m_progressBar(std::in_place, outStream, IsVTEnabled()),
m_spinner(std::in_place, outStream, IsVTEnabled())
{}
Reporter::~Reporter()
{
// The goal of this is to return output to its previous state.
// For now, we assume this means "default".
GetBasicOutputStream() << TextFormat::Default;
}
Reporter::Reporter(const Reporter& other, clone_t) :
Reporter(other.m_out, other.m_in)
{
if (other.m_style.has_value())
{
SetStyle(*other.m_style);
}
}
OutputStream Reporter::GetOutputStream(Level level)
{
OutputStream result = GetBasicOutputStream();
switch (level)
{
case Level::Verbose:
result.AddFormat(TextFormat::Default);
break;
case Level::Info:
result.AddFormat(TextFormat::Default);
break;
case Level::Warning:
result.AddFormat(TextFormat::Foreground::BrightYellow);
break;
case Level::Error:
result.AddFormat(TextFormat::Foreground::BrightRed);
break;
default:
THROW_HR(E_UNEXPECTED);
}
return result;
}
OutputStream Reporter::GetBasicOutputStream()
{
return { m_out, m_channel == Channel::Output, IsVTEnabled() };
}
void Reporter::SetChannel(Channel channel)
{
m_channel = channel;
if (m_channel != Channel::Output)
{
// Disable progress for non-output channels
m_spinner.reset();
m_progressBar.reset();
}
}
void Reporter::SetStyle(VisualStyle style)
{
m_style = style;
if (m_spinner)
{
m_spinner->SetStyle(style);
}
if (m_progressBar)
{
m_progressBar->SetStyle(style);
}
if (style == VisualStyle::NoVT)
{
m_isVTEnabled = false;
}
}
void Reporter::ShowIndefiniteProgress(bool running)
{
if (m_spinner)
{
if (running)
{
m_spinner->ShowSpinner();
}
else
{
m_spinner->StopSpinner();
}
}
}
void Reporter::OnProgress(uint64_t current, uint64_t maximum, ProgressType type)
{
ShowIndefiniteProgress(false);
if (m_progressBar)
{
m_progressBar->ShowProgress(current, maximum, type);
}
}
void Reporter::SetProgressCallback(ProgressCallback* callback)
{
auto lock = m_progressCallbackLock.lock_exclusive();
m_progressCallback = callback;
}
void Reporter::CancelInProgressTask(bool force)
{
// TODO: Maybe ask the user if they really want to cancel?
UNREFERENCED_PARAMETER(force);
auto lock = m_progressCallbackLock.lock_shared();
ProgressCallback* callback = m_progressCallback.load();
if (callback)
{
callback->Cancel();
}
}
bool Reporter::IsVTEnabled() const
{
return m_isVTEnabled && ConsoleModeRestore::Instance().IsVTEnabled();
}
}