-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathCommonState.hpp
305 lines (257 loc) · 11 KB
/
CommonState.hpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
Module Name:
- CommonState.hpp
Abstract:
- This represents common boilerplate state setup required for unit tests to run
Author(s):
- Michael Niksa (miniksa) 18-Jun-2014
- Paul Campbell (paulcam) 18-Jun-2014
Revision History:
- Transformed to header-only class so it can be included by multiple
unit testing projects in the codebase without a bunch of overhead.
--*/
#pragma once
#include "../host/globals.h"
#include "../host/inputReadHandleData.h"
#include "../interactivity/inc/ServiceLocator.hpp"
class CommonState
{
public:
static const til::CoordType s_csWindowWidth = 80;
static const til::CoordType s_csWindowHeight = 80;
static const til::CoordType s_csBufferWidth = 80;
static const til::CoordType s_csBufferHeight = 300;
CommonState() :
m_heap(GetProcessHeap()),
m_hrTextBufferInfo(E_FAIL),
m_pFontInfo(nullptr),
m_backupTextBufferInfo(),
m_readHandle(nullptr)
{
}
~CommonState()
{
m_heap = nullptr;
}
void InitEvents()
{
Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().hInputEvent.create(wil::EventOptions::ManualReset);
}
void PrepareReadHandle()
{
m_readHandle = std::make_unique<INPUT_READ_HANDLE_DATA>();
}
void CleanupReadHandle()
{
m_readHandle.reset(nullptr);
}
void PrepareGlobalFont(const til::size coordFontSize = { 8, 12 })
{
m_pFontInfo = new FontInfo(L"Consolas", 0, 0, coordFontSize, 0);
}
void CleanupGlobalFont()
{
if (m_pFontInfo != nullptr)
{
delete m_pFontInfo;
}
}
void PrepareGlobalRenderer()
{
Globals& g = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals();
CONSOLE_INFORMATION& gci = g.getConsoleInformation();
g.pRender = new Microsoft::Console::Render::Renderer(gci.GetRenderSettings(), &gci.renderData, nullptr, 0, nullptr);
}
void CleanupGlobalRenderer()
{
Globals& g = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals();
delete g.pRender;
g.pRender = nullptr;
}
void PrepareGlobalScreenBuffer(const til::CoordType viewWidth = s_csWindowWidth,
const til::CoordType viewHeight = s_csWindowHeight,
const til::CoordType bufferWidth = s_csBufferWidth,
const til::CoordType bufferHeight = s_csBufferHeight)
{
Globals& g = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals();
CONSOLE_INFORMATION& gci = g.getConsoleInformation();
til::size coordWindowSize;
coordWindowSize.width = viewWidth;
coordWindowSize.height = viewHeight;
til::size coordScreenBufferSize;
coordScreenBufferSize.width = bufferWidth;
coordScreenBufferSize.height = bufferHeight;
UINT uiCursorSize = 12;
THROW_IF_FAILED(SCREEN_INFORMATION::CreateInstance(coordWindowSize,
*m_pFontInfo,
coordScreenBufferSize,
TextAttribute{},
TextAttribute{ FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED },
uiCursorSize,
&gci.pCurrentScreenBuffer));
// If we have a renderer, we need to call EnablePainting to initialize
// the viewport. If not, we mark the text buffer as inactive so that it
// doesn't try to trigger a redraw on a nonexistent renderer.
if (g.pRender)
{
g.pRender->EnablePainting();
}
else
{
gci.pCurrentScreenBuffer->_textBuffer->SetAsActiveBuffer(false);
}
}
void CleanupGlobalScreenBuffer()
{
const CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
delete gci.pCurrentScreenBuffer;
}
void PrepareGlobalInputBuffer()
{
CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
gci.pInputBuffer = new InputBuffer();
}
void CleanupGlobalInputBuffer()
{
const CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
delete gci.pInputBuffer;
}
void PrepareCookedReadData(const std::wstring_view initialData = {})
{
CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
auto* readData = new COOKED_READ_DATA(gci.pInputBuffer,
m_readHandle.get(),
gci.GetActiveOutputBuffer(),
0,
nullptr,
0,
L"",
initialData,
nullptr);
gci.SetCookedReadData(readData);
}
void CleanupCookedReadData()
{
CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
delete &gci.CookedReadData();
gci.SetCookedReadData(nullptr);
}
void PrepareNewTextBufferInfo(const bool useDefaultAttributes = false,
const til::CoordType bufferWidth = s_csBufferWidth,
const til::CoordType bufferHeight = s_csBufferHeight)
{
Globals& g = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals();
CONSOLE_INFORMATION& gci = g.getConsoleInformation();
til::size coordScreenBufferSize;
coordScreenBufferSize.width = bufferWidth;
coordScreenBufferSize.height = bufferHeight;
UINT uiCursorSize = 12;
auto initialAttributes = useDefaultAttributes ? TextAttribute{} :
TextAttribute{ FOREGROUND_BLUE | FOREGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY };
m_backupTextBufferInfo.swap(gci.pCurrentScreenBuffer->_textBuffer);
try
{
std::unique_ptr<TextBuffer> textBuffer = std::make_unique<TextBuffer>(coordScreenBufferSize,
initialAttributes,
uiCursorSize,
true,
g.pRender);
if (textBuffer.get() == nullptr)
{
m_hrTextBufferInfo = E_OUTOFMEMORY;
}
else
{
m_hrTextBufferInfo = S_OK;
}
gci.pCurrentScreenBuffer->_textBuffer.swap(textBuffer);
// If we have a renderer, we need to call EnablePainting to initialize
// the viewport. If not, we mark the text buffer as inactive so that it
// doesn't try to trigger a redraw on a nonexistent renderer.
if (g.pRender)
{
g.pRender->EnablePainting();
}
else
{
gci.pCurrentScreenBuffer->_textBuffer->SetAsActiveBuffer(false);
}
}
catch (...)
{
m_hrTextBufferInfo = wil::ResultFromCaughtException();
}
}
void CleanupNewTextBufferInfo()
{
CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
VERIFY_IS_TRUE(gci.HasActiveOutputBuffer());
gci.pCurrentScreenBuffer->_textBuffer.swap(m_backupTextBufferInfo);
}
void FillTextBuffer()
{
CONSOLE_INFORMATION& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
// fill with some assorted text that doesn't consume the whole row
const til::CoordType cRowsToFill = 4;
VERIFY_IS_TRUE(gci.HasActiveOutputBuffer());
TextBuffer& textBuffer = gci.GetActiveOutputBuffer().GetTextBuffer();
for (til::CoordType iRow = 0; iRow < cRowsToFill; iRow++)
{
ROW& row = textBuffer.GetMutableRowByOffset(iRow);
FillRow(&row, iRow & 1);
}
textBuffer.GetCursor().SetYPosition(cRowsToFill);
}
[[nodiscard]] HRESULT GetTextBufferInfoInitResult()
{
return m_hrTextBufferInfo;
}
private:
HANDLE m_heap;
HRESULT m_hrTextBufferInfo;
FontInfo* m_pFontInfo;
std::unique_ptr<TextBuffer> m_backupTextBufferInfo;
std::unique_ptr<INPUT_READ_HANDLE_DATA> m_readHandle;
void FillRow(ROW* pRow, bool wrapForced)
{
// fill a row
// - Each row is populated with L"AB\u304bC\u304dDE "
// - 7 characters, 6 spaces. 13 total
// - The characters take up first 9 columns. (The wide glyphs take up 2 columns each)
// - か = \x304b, き = \x304d
uint16_t column = 0;
for (const auto& ch : std::wstring_view{ L"AB\u304bC\u304dDE " })
{
const uint16_t width = ch >= 0x80 ? 2 : 1;
pRow->ReplaceCharacters(column, width, { &ch, 1 });
column += width;
}
// A = bright red on dark gray
// This string starts at index 0
auto Attr = TextAttribute(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
pRow->SetAttrToEnd(0, Attr);
// BかC = dark gold on bright blue
// This string starts at index 1
Attr = TextAttribute(FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
pRow->SetAttrToEnd(1, Attr);
// き = bright white on dark purple
// This string starts at index 5
Attr = TextAttribute(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE);
pRow->SetAttrToEnd(5, Attr);
// DE = black on dark green
// This string starts at index 7
Attr = TextAttribute(BACKGROUND_GREEN);
pRow->SetAttrToEnd(7, Attr);
// odd rows forced a wrap
if (wrapForced)
{
pRow->SetWrapForced(true);
}
else
{
pRow->SetWrapForced(false);
}
}
};