-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNtUiLib.Console.pas
355 lines (292 loc) · 8.47 KB
/
NtUiLib.Console.pas
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
unit NtUiLib.Console;
{
This module includes functions that help building console applications.
}
interface
uses
NtUtils, DelphiApi.Reflection;
type
[NamingStyle(nsCamelCase, 'ch')]
TConsoleHostState = (
chUnknown,
chNone,
chInherited,
chCreated
);
[NamingStyle(nsCamelCase, 'cc')]
TConsoleColor = (
ccBlack,
ccDarkBlue,
ccDarkGreen,
ccDarkCyan,
ccDarkRed,
ccDarkMagenta,
ccDarkYellow,
ccGray,
ccDarkGray,
ccBlue,
ccGreen,
ccCyan,
ccRed,
ccMagenta,
ccYellow,
ccWhite,
ccUnchanged
);
IAutoConsoleColor = interface (IAutoReleasable)
['{4D298AF8-60A5-4500-B7B0-1219E8EF0264}']
// Public
function GetForeground: TConsoleColor;
function GetBackground: TConsoleColor;
function GetLayerId: NativeUInt;
property Foreground: TConsoleColor read GetForeground;
property Background: TConsoleColor read GetBackground;
property LayerId: NativeUInt read GetLayerId;
end;
var
// Allow using command-line parameters instead of user input
PreferParametersOverConsoleIO: Boolean = True;
// Do not immediately close the console if the app was invoked from GUI
UseSmartCloseOnExit: Boolean = True;
RETRY_MSG: String = 'Invalid input; try again: ';
// Read a string input from the console
function ReadString(AllowEmpty: Boolean = True): String;
// Read a boolean choice from the console
function ReadBoolean: Boolean;
// Read an unsigned integer from the console
function ReadCardinal(
MinValue: Cardinal = 0;
MaxValue: Cardinal = Cardinal(-1)
): Cardinal;
// Change console output color and revert it back later
function RtlxSetConsoleColor(
Foreground: TConsoleColor;
Background: TConsoleColor = ccUnchanged
): IAutoConsoleColor;
// Determine whether the current process inherited or created the console
function RtlxConsoleHostState: TConsoleHostState;
implementation
uses
Ntapi.WinNt, Ntapi.ntpsapi, Ntapi.ConsoleApi, Ntapi.ntpebteb,
NtUtils.SysUtils, NtUtils.Processes, NtUtils.Processes.Info,
DelphiUtils.AutoObjects, DelphiUtils.AutoEvents;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Input }
var
OverrideIndex: Integer = 1;
function ReadString;
begin
// Apply I/O override
if PreferParametersOverConsoleIO then
begin
Result := RtlxParamStr(OverrideIndex);
Inc(OverrideIndex);
if Result <> '' then
begin
writeln(Result);
Exit;
end
else
PreferParametersOverConsoleIO := False;
end;
repeat
readln(Result);
if not AllowEmpty and (Result = '') then
write(RETRY_MSG)
else
Break;
until False;
end;
function ReadBoolean;
begin
Result := RtlxCompareStrings(ReadString, 'y') = 0;
end;
function ReadCardinal;
begin
while not RtlxStrToUInt(ReadString(False), Result) or (Result > MaxValue) or
(Result < MinValue) do
begin
write(RETRY_MSG);
PreferParametersOverConsoleIO := False; // Failed to parse, need user interaction
end;
end;
{ Output }
// Change console output color
function RtlxSetConsoleColorInternal(
Foreground: TConsoleColor;
Background: TConsoleColor;
[out, opt] PreviousAttributes: PWord = nil
): TNtxStatus;
var
Info: TConsoleScreenBufferInfo;
NewAttributes: Word;
begin
if (Foreground = ccUnchanged) and (Background = ccUnchanged) then
Exit(NtxSuccess);
Result.Location := 'GetConsoleScreenBufferInfo';
Result.Win32Result := GetConsoleScreenBufferInfo(
RtlGetCurrentPeb.ProcessParameters.StandardOutput, Info);
if not Result.IsSuccess then
Exit;
NewAttributes := Info.Attributes;
if Foreground <> ccUnchanged then
NewAttributes := (NewAttributes and $FFF0) or (Word(Foreground) and $F);
if Background <> ccUnchanged then
NewAttributes := (NewAttributes and $FF0F) or
((Word(Background) and $F) shl 4);
Result.Location := 'SetConsoleTextAttribute';
Result.Win32Result := SetConsoleTextAttribute(
RtlGetCurrentPeb.ProcessParameters.StandardOutput, NewAttributes);
if not Result.IsSuccess then
Exit;
if Assigned(PreviousAttributes) then
PreviousAttributes^ := Info.Attributes;
end;
type
TAutoConsoleColor = class (TCustomAutoReleasable, IAutoConsoleColor)
private
FForeground: TConsoleColor;
FBackground: TConsoleColor;
FLayerId: NativeUInt;
FApplied: Boolean;
class var CurrentLayerId: NativeUInt;
class var InitialStateCaptured: Boolean;
class var InitialForeground: TConsoleColor;
class var InitialBackground: TConsoleColor;
class var ColorStack: TWeakArray<IAutoConsoleColor>;
procedure Release; override;
constructor Create(Foreground, Background: TConsoleColor);
public
function GetForeground: TConsoleColor;
function GetBackground: TConsoleColor;
function GetLayerId: NativeUInt;
class function Apply(Foreground, Background: TConsoleColor):
IAutoConsoleColor; static;
end;
class function TAutoConsoleColor.Apply;
var
Obj: TAutoConsoleColor;
begin
Obj := TAutoConsoleColor.Create(Foreground, Background);
Result := Obj;
// Register ourselves on the (weak reference) stack
if Obj.FApplied then
TAutoConsoleColor.ColorStack.Add(Result);
end;
constructor TAutoConsoleColor.Create;
var
PreviousAttributes: Word;
begin
FForeground := Foreground;
FBackground := Background;
FLayerId := AtomicIncrement(CurrentLayerId);
// Try to apply the changes
FApplied := RtlxSetConsoleColorInternal(Foreground, Background,
@PreviousAttributes).IsSuccess;
// Save initial attributes
if FApplied and not TAutoConsoleColor.InitialStateCaptured then
begin
TAutoConsoleColor.InitialForeground :=
TConsoleColor(PreviousAttributes and $0F);
TAutoConsoleColor.InitialBackground :=
TConsoleColor((PreviousAttributes and $F0) shr 4);
TAutoConsoleColor.InitialStateCaptured := True;
end;
end;
function TAutoConsoleColor.GetBackground;
begin
Result := FBackground;
end;
function TAutoConsoleColor.GetForeground;
begin
Result := FForeground;
end;
function TAutoConsoleColor.GetLayerId;
begin
Result := FLayerId;
end;
procedure TAutoConsoleColor.Release;
var
Entry: IAutoConsoleColor;
LowerBackground, LowerForeground: TConsoleColor;
HigherBackground, HigherForeground: TConsoleColor;
NewBackground, NewForeground: TConsoleColor;
begin
inherited;
if not FApplied then
Exit;
LowerForeground := TAutoConsoleColor.InitialForeground;
LowerBackground := TAutoConsoleColor.InitialBackground;
HigherForeground := ccUnchanged;
HigherBackground := ccUnchanged;
// Determine the underlying/overlaying colors
for Entry in TAutoConsoleColor.ColorStack.Entries do
if Entry.LayerId < FLayerId then
begin
if Entry.Foreground <> ccUnchanged then
LowerForeground := Entry.Foreground;
if Entry.Background <> ccUnchanged then
LowerBackground := Entry.Background;
end
else if Entry.LayerId > FLayerId then
begin
if Entry.Foreground <> ccUnchanged then
HigherForeground := Entry.Foreground;
if Entry.Background <> ccUnchanged then
HigherBackground := Entry.Background;
end;
// Choose the new foreground
if HigherForeground = ccUnchanged then
NewForeground := LowerForeground
else
NewForeground := HigherForeground;
// Choose the new background
if HigherBackground = ccUnchanged then
NewBackground := LowerBackground
else
NewBackground := HigherBackground;
// Set the colors
RtlxSetConsoleColorInternal(NewForeground, NewBackground);
end;
function RtlxSetConsoleColor;
begin
Result := TAutoConsoleColor.Apply(Foreground, Background);
end;
{ Console Host }
function RtlxConsoleHostState;
var
PID: TProcessId;
hxProcess: IHandle;
ConhostInfo, OurInfo: TKernelUserTimes;
begin
Result := chUnknown;
// Determine conhost's PID
if not NtxProcess.Query(NtxCurrentProcess, ProcessConsoleHostProcess, PID)
.IsSuccess then
Exit;
if PID = 0 then
Exit(chNone);
// Query its and our creation time
if NtxOpenProcess(hxProcess, PID, PROCESS_QUERY_LIMITED_INFORMATION).IsSuccess
and NtxProcess.Query(hxProcess, ProcessTimes, ConhostInfo).IsSuccess
and NtxProcess.Query(NtxCurrentProcess, ProcessTimes, OurInfo).IsSuccess then
begin
// Compare them
if ConhostInfo.CreateTime > OurInfo.CreateTime then
Result := chCreated
else
Result := chInherited;
end;
end;
initialization
finalization
{$IFDEF Console}
if UseSmartCloseOnExit and (RtlxConsoleHostState = chCreated) then
begin
write(#$D#$A'Press enter to exit...');
readln;
end;
{$ENDIF}
end.