-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathQuick.Console.pas
402 lines (357 loc) · 10 KB
/
Quick.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
{ ***************************************************************************
Copyright (c) 2016-2017 Kike Pérez
Unit : Quick.Console
Description : Console output with colors and optional file log
Author : Kike Pérez
Version : 1.6
Created : 10/05/2017
Modified : 20/10/2017
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Console;
{$IFDEF CONDITIONALEXPRESSIONS}
{$ifndef VER140}
{$ifndef LINUX}
{$define WITHUXTHEME}
{$endif}
{$endif}
{$IF CompilerVersion >= 17.0}
{$DEFINE INLINES}
{$ENDIF}
{$IF RTLVersion >= 14.0}
{$DEFINE HASERROUTPUT}
{$ENDIF}
{$ENDIF}
interface
uses
Classes,
Windows,
Winapi.Messages,
System.SysUtils,
Quick.Commons,
Quick.Log;
type
//text colors
TConsoleColor = (
ccBlack = 0,
ccBlue = 1,
ccGreen = 2,
ccCyan = 3,
ccRed = 4,
ccMagenta = 5,
ccBrown = 6,
ccLightGray = 7,
ccDarkGray = 8,
ccLightBlue = 9,
ccLightGreen = 10,
ccLightCyan = 11,
ccLightRed = 12,
ccLightMagenta = 13,
ccYellow = 14,
ccWhite = 15);
TConsoleProperties = record
LogVerbose : TLogVerbose;
Log : TQuickLog;
end;
procedure cout(const cMsg : Integer; cEventType : TEventType); overload;
procedure cout(const cMsg : Double; cEventType : TEventType); overload;
procedure cout(const cMsg : string; cEventType : TEventType); overload;
procedure cout(const cMsg : string; cColor : TConsoleColor); overload;
procedure coutXY(x,y : Integer; const s : string; cEventType : TEventType);
procedure coutBL(const s : string; cEventType : TEventType);
procedure coutFmt(const cMsg : string; params : array of const; cEventType : TEventType);
procedure TextColor(Color: TConsoleColor); overload;
procedure TextColor(Color: Byte); overload;
procedure TextBackground(Color: TConsoleColor); overload;
procedure TextBackground(Color: Byte); overload;
procedure ResetColors;
function ClearScreen : Boolean;
procedure ClearLine;
procedure ConsoleWaitForEnterKey;
procedure InitConsole;
var
Console : TConsoleProperties;
CSConsole : TRTLCriticalSection;
LastMode : Word;
DefConsoleColor : Byte;
TextAttr : Byte;
hStdOut: THandle;
hStdErr: THandle;
ConsoleRect: TSmallRect;
ScreenBufInfo : TConsoleScreenBufferInfo;
implementation
procedure cout(const cMsg : Integer; cEventType : TEventType);
var
FmtSets : TFormatSettings;
begin
try
FmtSets := TFormatSettings.Create;
FmtSets.ThousandSeparator := '.';
FmtSets.DecimalSeparator := ',';
cout(FormatFloat('0,',cMsg,FmtSets),cEventType);
except
cout(cMsg.ToString,cEventType);
end;
end;
procedure cout(const cMsg : Double; cEventType : TEventType);
var
FmtSets : TFormatSettings;
begin
try
FmtSets := TFormatSettings.Create;
FmtSets.ThousandSeparator := '.';
FmtSets.DecimalSeparator := ',';
cout(FormatFloat('.0###,',cMsg,FmtSets),cEventType);
except
cout(cMsg.ToString,cEventType);
end;
end;
procedure cout(const cMsg : string; cEventType : TEventType);
begin
if cEventType in Console.LogVerbose then
begin
EnterCriticalSection(CSConsole);
try
if hStdOut <> 0 then
begin
case cEventType of
etError : TextColor(ccLightRed);
etInfo : TextColor(ccWhite);
etSuccess : TextColor(ccLightGreen);
etWarning : TextColor(ccYellow);
etDebug : TextColor(ccLightCyan);
etTrace : TextColor(ccLightMagenta);
else TextColor(ccWhite);
end;
Writeln(cMsg);
TextColor(LastMode);
end;
finally
LeaveCriticalSection(CSConsole);
end;
if Assigned(Console.Log) then Console.Log.Add(cMsg,cEventType);
end;
end;
procedure cout(const cMsg : string; cColor : TConsoleColor);
begin
EnterCriticalSection(CSConsole);
try
if hStdOut <> 0 then
begin
TextColor(cColor);
Writeln(cMsg);
TextColor(LastMode);
end;
finally
LeaveCriticalSection(CSConsole);
end;
end;
function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
var
BufferInfo: TConsoleScreenBufferInfo;
begin
GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
Result := BufferInfo.dwCursorPosition.X;
end;
function GetCursorY: Integer; {$IFDEF INLINES}inline;{$ENDIF}
var
BufferInfo: TConsoleScreenBufferInfo;
begin
GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
Result := BufferInfo.dwCursorPosition.Y;
end;
function GetCursorMaxBottom : Integer;
var
BufferInfo: TConsoleScreenBufferInfo;
begin
GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
Result := BufferInfo.srWindow.Bottom;
end;
procedure SetCursorPos(NewCoord : TCoord);
begin
SetConsoleCursorPosition(hStdOut, NewCoord);
end;
procedure coutXY(x,y : Integer; const s : string; cEventType : TEventType);
var
NewCoord : TCoord;
LastCoord : TCoord;
begin
if hStdOut = 0 then Exit;
LastCoord.X := GetCursorX;
LastCoord.Y := GetCursorY;
NewCoord.X := x;
NewCoord.Y := y;
ClearLine;
SetCursorPos(NewCoord);
try
cout(s,cEventType);
finally
SetCursorPos(LastCoord);
end;
end;
procedure coutBL(const s : string; cEventType : TEventType);
var
NewCoord : TCoord;
begin
coutXY(0,GetCurSorMaxBottom,s,cEventType);
NewCoord.X := GetCursorX;
NewCoord.Y := GetCurSorMaxBottom - 1;
SetCursorPos(NewCoord);
end;
procedure coutFmt(const cMsg : string; params : array of const; cEventType : TEventType);
begin
cout(Format(cMsg,params),cEventType);
end;
procedure TextColor(Color: TConsoleColor);
begin
TextColor(Integer(Color));
end;
procedure TextColor(Color: Byte);
begin
if hStdOut = 0 then Exit;
LastMode := TextAttr;
TextAttr := (TextAttr and $F0) or (Color and $0F);
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
end;
procedure TextBackground(Color: TConsoleColor);
begin
TextBackground(Integer(Color));
end;
procedure TextBackground(Color: Byte);
begin
if hStdOut = 0 then Exit;
LastMode := TextAttr;
TextAttr := (TextAttr and $0F) or ((Color shl 4) and $F0);
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
end;
procedure ResetColors;
begin
SetConsoleTextAttribute(hStdOut, DefConsoleColor);
TextAttr := DefConsoleColor;
end;
function ClearScreen : Boolean;
const
BUFSIZE = 80*25;
var
Han, Dummy: LongWord;
buf: string;
coord: TCoord;
begin
Result := false;
Han := GetStdHandle(STD_OUTPUT_HANDLE);
if Han <> INVALID_HANDLE_VALUE then
begin
if SetConsoleTextAttribute(han, FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE) then
begin
SetLength(buf,BUFSIZE);
FillChar(buf[1],Length(buf),' ');
if WriteConsole(han,PChar(buf),BUFSIZE,Dummy,nil) then
begin
coord.X := 0;
coord.Y := 0;
if SetConsoleCursorPosition(han,coord) then Result := true;
end;
end;
end;
end;
procedure ClearLine;
var
dwWriteCoord: TCoord;
dwCount, dwSize: DWord;
begin
if hStdOut = 0 then Exit;
dwWriteCoord.X := ConsoleRect.Left;
dwWriteCoord.Y := GetCursorY;
dwSize := ConsoleRect.Right - ConsoleRect.Left + 1;
FillConsoleOutputAttribute(hStdOut, TextAttr, dwSize, dwWriteCoord, dwCount);
FillConsoleOutputCharacter(hStdOut, ' ', dwSize, dwWriteCoord, dwCount);
end;
function ConsoleKeyPressed(ExpectedKey: Word): Boolean;
var
lpNumberOfEvents: DWORD;
lpBuffer: TInputRecord;
lpNumberOfEventsRead : DWORD;
nStdHandle: THandle;
begin
Result := False;
nStdHandle := GetStdHandle(STD_INPUT_HANDLE);
lpNumberOfEvents := 0;
GetNumberOfConsoleInputEvents(nStdHandle, lpNumberOfEvents);
if lpNumberOfEvents <> 0 then
begin
PeekConsoleInput(nStdHandle, lpBuffer, 1, lpNumberOfEventsRead);
if lpNumberOfEventsRead <> 0 then
begin
if lpBuffer.EventType = KEY_EVENT then
begin
if lpBuffer.Event.KeyEvent.bKeyDown and ((ExpectedKey = 0) or (lpBuffer.Event.KeyEvent.wVirtualKeyCode = ExpectedKey)) then Result := true
else FlushConsoleInputBuffer(nStdHandle);
end
else FlushConsoleInputBuffer(nStdHandle);
end;
end;
end;
procedure ConsoleWaitForEnterKey;
var
msg: TMsg;
begin
while not ConsoleKeyPressed(VK_RETURN) do
begin
{$ifndef LVCL}
if GetCurrentThreadID=MainThreadID then CheckSynchronize{$ifdef WITHUXTHEME}(1000){$endif} else
{$endif}
WaitMessage;
while PeekMessage(msg,0,0,0,PM_REMOVE) do
begin
if Msg.Message = WM_QUIT then Exit
else
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
end;
end;
procedure InitConsole;
var
BufferInfo: TConsoleScreenBufferInfo;
begin
Console.LogVerbose := LOG_ALL;
Rewrite(Output);
hStdOut := TTextRec(Output).Handle;
{$IFDEF HASERROUTPUT}
Rewrite(ErrOutput);
hStdErr := TTextRec(ErrOutput).Handle;
{$ELSE}
hStdErr := GetStdHandle(STD_ERROR_HANDLE);
{$ENDIF}
if not GetConsoleScreenBufferInfo(hStdOut, BufferInfo) then
begin
SetInOutRes(GetLastError);
Exit;
end;
ConsoleRect.Left := 0;
ConsoleRect.Top := 0;
ConsoleRect.Right := BufferInfo.dwSize.X - 1;
ConsoleRect.Bottom := BufferInfo.dwSize.Y - 1;
TextAttr := BufferInfo.wAttributes and $FF;
DefConsoleColor := TextAttr;
LastMode := 3; //CO80;
end;
initialization
InitializeCriticalSection(CSConsole);
//init stdout if not a service
if GetStdHandle(STD_OUTPUT_HANDLE) <> 0 then InitConsole;
finalization
DeleteCriticalSection(CSConsole);
end.