-
Notifications
You must be signed in to change notification settings - Fork 186
/
Quick.Console.pas
1119 lines (1032 loc) · 26.8 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ***************************************************************************
Copyright (c) 2016-2024 Kike Pérez
Unit : Quick.Console
Description : Console output with colors and optional file log
Author : Kike Pérez
Version : 1.9
Created : 10/05/2017
Modified : 20/01/2024
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;
{$i QuickLib.inc}
{$IFDEF CONDITIONALEXPRESSIONS}
{$ifndef VER140}
{$ifndef LINUX}
{$define WITHUXTHEME}
{$endif}
{$endif}
{$IFDEF DELPHI2005_UP}
{$DEFINE INLINES}
{$ENDIF}
{$IF RTLVersion >= 14.0}
{$DEFINE HASERROUTPUT}
{$ENDIF}
{$ENDIF}
interface
uses
Classes,
{$IFDEF MSWINDOWS}
Windows,
Messages,
{$ELSE}
{$IFDEF FPC}
crt,
{$ENDIF}
{$ENDIF}
{$IF Defined(DELPHILINUX) OR Defined(MACOS)}
Quick.SyncObjs.Linux.Compatibility,
Posix.StdDef,
{$ENDIF}
SysUtils,
Quick.Commons,
Quick.Log;
type
//text colors
{$IFNDEF DELPHILINUX}
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);
{$ELSE}
TConsoleColor = (
ccDarkGray = 90,
ccLightRed = 91,
ccLightGreen = 92,
ccYellow = 93,
ccLightBlue = 94,
ccLightMagenta = 95,
ccLightCyan = 96,
ccWhite = 97,
ccBlack = 30,
ccRed = 31,
ccGreen = 32,
ccBrown = 33,
ccBlue = 34,
ccMagenta = 35,
ccCyan = 36,
ccLightGray = 37);
{$ENDIF}
TConsoleProperties = record
LogVerbose : TLogVerbose;
Log : TQuickLog;
end;
{$IFNDEF FPC}
TOutputProc<T> = reference to procedure(const aLine : T);
TExecuteProc = reference to procedure;
{$ELSE}
TOutputProc<T> = procedure(const aLine : T) of object;
TExecuteProc = procedure of object;
{$ENDIF}
{$IF DEFINED(FPCLINUX) OR DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
tcrtcoord = Byte;
{$ENDIF}
TCoord = record
X : tcrtcoord;
Y : tcrtcoord;
end;
TSmallRect = record
Left : Byte;
Top : Byte;
Right : Byte;
Bottom : Byte;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
TConsoleMenuOption = record
private
fCaption : string;
fKey : Word;
fOnKeyPressed : TExecuteProc;
public
property Caption : string read fCaption write fCaption;
property Key : Word read fKey write fKey;
property OnKeyPressed : TExecuteProc read fOnKeyPressed write fOnKeyPressed;
procedure DoKeyPressed;
end;
TConsoleMenu = class
private
fConsoleMenu : array of TConsoleMenuOption;
fMenuColor : TConsoleColor;
fIsActive : Boolean;
procedure WriteMenu;
public
constructor Create;
property MenuColor : TConsoleColor read fMenuColor write fMenuColor;
property IsActive : Boolean read fIsActive;
procedure AddMenu(const cMenuCaption : string; const cMenuKey : Word; MenuAction : TExecuteProc); overload;
procedure AddMenu(MenuOption : TConsoleMenuOption); overload;
procedure Refresh(aClearScreen : Boolean = False);
procedure WaitForKeys;
end;
{$ENDIF}
procedure cout(const cMsg : Integer; cEventType : TLogEventType); overload;
procedure cout(const cMsg : Double; cEventType : TLogEventType); overload;
procedure cout(const cMsg : string; cEventType : TLogEventType); overload;
procedure cout(const cMsg : string; cColor : TConsoleColor); overload;
procedure coutSL(const cMsg : string; cColor : TConsoleColor);
procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType); overload;
procedure coutXY(x,y : Integer; const cMsg : string; cEventType : TLogEventType); overload;
procedure coutXY(x,y : Integer; const cMsg : string; cColor : TConsoleColor; cClearLineBefore : Boolean = False); overload;
procedure coutXY(x,y : Integer; const cMsg : string; params : array of const; cEventType : TLogEventType); overload;
procedure coutXY(x,y : Integer; const cMsg : string; params : array of const; cColor : TConsoleColor); overload;
procedure coutTL(const cMsg : string; cEventType : TLogEventType); overload;
procedure coutTL(const cMsg : string; cColor : TConsoleColor); overload;
procedure coutBL(const cMsg : string; cEventType : TLogEventType); overload;
procedure coutBL(const cMsg : string; cColor : TConsoleColor); overload;
procedure coutFmt(const cMsg : string; params : array of const; cEventType : TLogEventType);
procedure TextColor(Color: TConsoleColor); overload;
procedure TextColor(Color: Byte); overload;
procedure TextBackground(Color: TConsoleColor); overload;
procedure TextBackground(Color: Byte); overload;
procedure ResetColors;
{$IFDEF MSWINDOWS}
procedure ConsoleResize(Width, Height : Integer);
{$ENDIF}
procedure ClearScreen;
procedure ClearLine; overload;
procedure ClearLine(Y : Integer); overload;
procedure ShowCursor;
procedure HideCursor;
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
procedure SaveCursor;
procedure RestoreCursor;
procedure CursorOn;
procedure CursorOff;
function ReadKey : Char;
{$ELSE}
function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
function GetCursorY: Integer; {$IFDEF INLINES}inline;{$ENDIF}
{$ENDIF}
function GetCursorMaxBottom : Integer;
{$IFDEF DELPHILINUX}
procedure GotoXY(x,y : Integer);
{$ENDIF}
procedure SetCursorPos(NewCoord : TCoord); overload;
procedure SetCursorPos(x ,y : Integer); overload;
{$IFDEF MSWINDOWS}
procedure ProcessMessages;
{$ENDIF}
procedure ConsoleWaitForEnterKey;
{$IFDEF MSWINDOWS}
function RunConsoleCommand(const aCommand, aParameters : String; CallBack : TOutputProc<PAnsiChar> = nil; OutputLines : TStrings = nil) : Cardinal;
procedure InitConsole;
{$ENDIF}
var
Console : TConsoleProperties;
CSConsole : TRTLCriticalSection;
LastMode : Word;
DefConsoleColor : Byte;
TextAttr : Byte;
hStdOut: THandle;
hStdErr: THandle;
ConsoleRect: TSmallRect;
{$IFDEF MSWINDOWS}
ScreenBufInfo : TConsoleScreenBufferInfo;
CursorInfo : TConsoleCursorInfo;
{$ENDIF}
implementation
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
const
AEC =chr($1B)+chr($5b);
SAVE_CURSOR_POS = chr($1B) + '7';
RESTORE_CURSOR_POS = chr($1B) + '8';
{$ENDIF}
procedure cout(const cMsg : Integer; cEventType : TLogEventType);
var
FmtSets : TFormatSettings;
begin
try
{$IFNDEF FPC}
FmtSets := TFormatSettings.Create;
{$ENDIF}
FmtSets.ThousandSeparator := '.';
FmtSets.DecimalSeparator := ',';
cout(FormatFloat('0,',cMsg,FmtSets),cEventType);
except
cout(cMsg.ToString,cEventType);
end;
end;
procedure cout(const cMsg : Double; cEventType : TLogEventType);
var
FmtSets : TFormatSettings;
begin
try
{$IFNDEF FPC}
FmtSets := TFormatSettings.Create;
{$ENDIF}
FmtSets.ThousandSeparator := '.';
FmtSets.DecimalSeparator := ',';
cout(FormatFloat('.0###,',cMsg,FmtSets),cEventType);
except
cout(cMsg.ToString,cEventType);
end;
end;
procedure cout(const cMsg : string; cEventType : TLogEventType);
begin
if cEventType in Console.LogVerbose then
begin
EnterCriticalSection(CSConsole);
try
{$IFDEF MSWINDOWS}
if hStdOut <> 0 then
{$ENDIF}
begin
case cEventType of
etError : TextColor(ccLightRed);
etInfo : TextColor(ccWhite);
etSuccess : TextColor(ccLightGreen);
etWarning : TextColor(ccYellow);
etDebug : TextColor(ccLightCyan);
etTrace : TextColor(ccLightMagenta);
etCritical : begin TextColor(ccYellow); TextBackground(ccRed); end;
etException : TextColor(ccRed);
else TextColor(ccWhite);
end;
{$I-}
Writeln(cMsg{$IFDEF LINUX} +#13{$ENDIF});
{$I+}
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
{$IFDEF MSWINDOWS}
if hStdOut <> 0 then
{$ENDIF}
begin
TextColor(cColor);
{$I-}
Writeln(cMsg{$IFDEF LINUX} +#13{$ENDIF});
{$I+}
TextColor(LastMode);
end;
finally
LeaveCriticalSection(CSConsole);
end;
end;
procedure coutSL(const cMsg : string; cColor : TConsoleColor);
begin
EnterCriticalSection(CSConsole);
try
{$IFDEF MSWINDOWS}
if hStdOut <> 0 then
{$ENDIF}
begin
TextColor(cColor);
{$I-}
Write(cMsg);//{$IFDEF LINUX} +#13{$ENDIF});
{$I+}
TextColor(LastMode);
end;
finally
LeaveCriticalSection(CSConsole);
end;
end;
procedure cout(const cMsg : string; params : array of const; cEventType : TLogEventType);
begin
cout(Format(cMsg,params),cEventType);
end;
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
procedure SaveCursor;
begin
write(SAVE_CURSOR_POS);
end;
procedure RestoreCursor;
begin
write(RESTORE_CURSOR_POS);
end;
procedure CursorOn;
begin
//not implemented yet
end;
procedure CursorOff;
begin
//not implemented yet
end;
function ReadKey : Char;
begin
Read(Result);
end;
{$ELSE}
function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
{$IFDEF MSWINDOWS}
var
BufferInfo: TConsoleScreenBufferInfo;
begin
GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
Result := BufferInfo.dwCursorPosition.X;
end;
{$ELSE}
begin
{$IFDEF FPC}
Result := WhereX;
{$ELSE}
Result := Byte(ConData(CD_CURRX))-lo(windmin);
{$ENDIF}
end;
{$ENDIF}
function GetCursorY: Integer; {$IFDEF INLINES}inline;{$ENDIF}
{$IFDEF MSWINDOWS}
var
BufferInfo: TConsoleScreenBufferInfo;
begin
GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
Result := BufferInfo.dwCursorPosition.Y;
end;
{$ELSE}
begin
Result := WhereY;
end;
{$ENDIF}
{$ENDIF DELPHILINUX}
function GetCursorMaxBottom : Integer;
{$IFDEF MSWINDOWS}
var
BufferInfo: TConsoleScreenBufferInfo;
begin
GetConsoleSCreenBufferInfo(hStdOut, BufferInfo);
Result := BufferInfo.srWindow.Bottom;
end;
{$ELSE}
begin
Result := 25;
end;
{$ENDIF}
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
procedure GotoXY(x,y : Integer);
begin
Write(AEC, y, ';', x, 'H');
end;
{$ENDIF}
procedure SetCursorPos(NewCoord : TCoord);
begin
{$IFDEF MSWINDOWS}
SetConsoleCursorPosition(hStdOut, NewCoord);
{$ELSE}
GotoXY(NewCoord.X,NewCoord.Y);
{$ENDIF}
end;
procedure SetCursorPos(x ,y : Integer);
var
NewCoord : TCoord;
begin
NewCoord.X := x;
NewCoord.Y := y;
SetCursorPos(NewCoord);
end;
procedure coutXY(x,y : Integer; const cMsg : string; cEventType : TLogEventType);
var
NewCoord : TCoord;
LastCoord : TCoord;
begin
{$IFDEF MSWINDOWS}
if hStdOut = 0 then Exit;
{$ENDIF}
{$IF NOT DEFINED(DELPHILINUX) AND NOT DEFINED(MACOS)}
LastCoord.X := GetCursorX;
LastCoord.Y := GetCursorY;
{$ELSE}
write(SAVE_CURSOR_POS);
{$ENDIF}
NewCoord.X := x;
NewCoord.Y := y;
ClearLine(Y);
SetCursorPos(NewCoord);
try
cout(cMsg,cEventType);
finally
{$IFNDEF DELPHILINUX}
SetCursorPos(LastCoord);
{$ELSE}
write(RESTORE_CURSOR_POS);
{$ENDIF}
end;
end;
procedure coutXY(x,y : Integer; const cMsg : string; cColor : TConsoleColor; cClearLineBefore : Boolean = False); overload;
var
NewCoord : TCoord;
LastCoord : TCoord;
begin
{$IFDEF MSWINDOWS}
if hStdOut = 0 then Exit;
{$ENDIF}
{$IF NOT DEFINED(DELPHILINUX) AND NOT DEFINED(MACOS)}
LastCoord.X := GetCursorX;
LastCoord.Y := GetCursorY;
{$ELSE}
write(SAVE_CURSOR_POS);
{$ENDIF}
NewCoord.X := x;
NewCoord.Y := y;
if cClearLineBefore then ClearLine(Y);
SetCursorPos(NewCoord);
try
cout(cMsg,cColor);
finally
{$IFNDEF DELPHILINUX}
SetCursorPos(LastCoord);
{$ELSE}
write(RESTORE_CURSOR_POS);
{$ENDIF}
end;
end;
procedure coutXY(x,y : Integer; const cMsg : string; params : array of const; cEventType : TLogEventType);
begin
coutXY(x,y,Format(cMsg,params),cEventType);
end;
procedure coutXY(x,y : Integer; const cMsg : string; params : array of const; cColor : TConsoleColor);
begin
coutXY(x,y,Format(cMsg,params),cColor);
end;
procedure coutTL(const cMsg : string; cEventType : TLogEventType);
begin
coutXY(0,0,cMsg,cEventType);
end;
procedure coutTL(const cMsg : string; cColor : TConsoleColor);
begin
coutXY(0,0,cMsg,cColor);
end;
procedure coutBL(const cMsg : string; cEventType : TLogEventType);
begin
coutXY(0,GetCursorMaxBottom - 1,cMsg,cEventType);
end;
procedure coutBL(const cMsg : string; cColor : TConsoleColor);
begin
coutXY(0,GetCursorMaxBottom - 1,cMsg,cColor);
end;
procedure coutFmt(const cMsg : string; params : array of const; cEventType : TLogEventType);
begin
cout(Format(cMsg,params),cEventType);
end;
procedure TextColor(Color: TConsoleColor);
begin
TextColor(Integer(Color));
end;
procedure TextColor(Color: Byte);
begin
{$IFDEF MSWINDOWS}
if hStdOut = 0 then Exit;
LastMode := TextAttr;
TextAttr := (TextAttr and $F0) or (Color and $0F);
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
{$ELSE}
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
write(AEC,';',Color,'m')
{$ELSE}
crt.TextColor(Color);
{$ENDIF}
{$ENDIF}
end;
procedure TextBackground(Color: TConsoleColor);
begin
TextBackground(Integer(Color));
end;
procedure TextBackground(Color: Byte);
begin
{$IFDEF MSWINDOWS}
if hStdOut = 0 then Exit;
LastMode := TextAttr;
TextAttr := (TextAttr and $0F) or ((Color shl 4) and $F0);
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
{$ELSE}
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
write(AEC,Color,';3m');
{$ELSE}
crt.TextBackground(Color);
{$ENDIF}
{$ENDIF}
end;
procedure ResetColors;
begin
{$IFDEF MSWINDOWS}
SetConsoleTextAttribute(hStdOut, DefConsoleColor);
TextAttr := DefConsoleColor;
{$ELSE}
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
write(AEC,0,'m');
{$ELSE}
TextColor(ccLightGray);
TextBackground(ccBlack);
{$ENDIF}
{$ENDIF}
end;
{$IFDEF MSWINDOWS}
procedure ConsoleResize(Width, Height : Integer);
var
Rect: TSmallRect;
Coord: TCoord;
begin
Rect.Left := 1;
Rect.Top := 1;
Rect.Right := Width;
Rect.Bottom := Height;
Coord.X := Rect.Right + 1 - Rect.Left;
Coord.y := Rect.Bottom + 1 - Rect.Top;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), Coord);
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), True, Rect);
end;
{$ENDIF}
procedure ClearScreen;
{$IFDEF MSWINDOWS}
var
stdout: THandle;
bufinfo: TConsoleScreenBufferInfo;
ConsoleSize: DWORD;
NumWritten: DWORD;
Origin: TCoord;
begin
stdout := GetStdHandle(STD_OUTPUT_HANDLE);
if stdout<>INVALID_HANDLE_VALUE then
begin
GetConsoleScreenBufferInfo(stdout,bufinfo);
ConsoleSize := bufinfo.dwSize.X * bufinfo.dwSize.Y;
Origin.X := 0;
Origin.Y := 0;
FillConsoleOutputCharacter(stdout,' ',ConsoleSize,Origin,NumWritten);
FillConsoleOutputAttribute(stdout,bufinfo.wAttributes,ConsoleSize,Origin,NumWritten);
SetConsoleCursorPosition(stdout, Origin);
end;
end;
{$ELSE}
begin
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
write(AEC,2,'J');
{$ELSE}
ClrScr;
{$ENDIF}
end;
{$ENDIF}
procedure ClearLine;
begin
{$IF NOT DEFINED(DELPHILINUX) AND NOT DEFINED(MACOS)}
ClearLine(GetCursorY);
{$ELSE}
write(AEC,'K');
{$ENDIF}
end;
procedure ClearLine(Y : Integer);
{$IFDEF MSWINDOWS}
var
dwWriteCoord: TCoord;
dwCount, dwSize: DWord;
begin
if hStdOut = 0 then Exit;
dwWriteCoord.X := 0;
dwWriteCoord.Y := Y;
dwSize := ConsoleRect.Right + 1;
FillConsoleOutputAttribute(hStdOut, TextAttr, dwSize, dwWriteCoord, dwCount);
FillConsoleOutputCharacter(hStdOut, ' ', dwSize, dwWriteCoord, dwCount);
end;
{$ELSE}
begin
GotoXY(1,Y);
{$IF DEFINED(DELPHILINUX) OR DEFINED(MACOS)}
write(AEC,'K');
{$ELSE}
DelLine;
{$ENDIF}
GotoXY(1,Y);
end;
{$ENDIF}
procedure ShowCursor;
begin
{$IFDEF MSWINDOWS}
GetConsoleCursorInfo(hStdOut,CursorInfo);
CursorInfo.bVisible := True;
SetConsoleCursorInfo(hStdOut,CursorInfo);
{$ELSE}
CursorOn;
{$ENDIF}
end;
procedure HideCursor;
begin
{$IFDEF MSWINDOWS}
GetConsoleCursorInfo(hStdOut,CursorInfo);
CursorInfo.bVisible := False;
SetConsoleCursorInfo(hStdOut,CursorInfo);
{$ELSE}
CursorOff;
{$ENDIF}
end;
function ConsoleKeyPressed(ExpectedKey: Word): Boolean;
{$IFDEF MSWINDOWS}
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;
{$ELSE}
var
kp : Char;
begin
repeat
kp := Readkey;
until kp = Char(ExpectedKey);
Result := True;
end;
{$ENDIF}
function GetConsoleKeyPressed : Word;
{$IFDEF MSWINDOWS}
var
lpNumberOfEvents: DWORD;
lpBuffer: TInputRecord;
lpNumberOfEventsRead : DWORD;
nStdHandle: THandle;
begin
Result := 0;
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
Result := lpBuffer.Event.KeyEvent.wVirtualKeyCode;
FlushConsoleInputBuffer(nStdHandle);
end
else FlushConsoleInputBuffer(nStdHandle);
end;
end;
end;
{$ELSE}
begin
Result := Ord(ReadKey);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure ProcessMessages;
var
Msg: TMsg;
begin
while integer(PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) <> 0 do begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure ConsoleWaitForEnterKey;
var
msg: TMsg;
begin
while not ConsoleKeyPressed(VK_RETURN) do
begin
{$ifndef LVCL}
{$IFDEF FPC}
if GetCurrentThreadID = MainThreadID then
begin
CheckSynchronize;
Sleep(1);
end
else
{$ELSE}
if GetCurrentThreadID = MainThreadID then CheckSynchronize{$IFDEF DELPHI7_UP}(1000){$ENDIF} else
{$ENDIF}
{$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;
{$ELSE}
procedure ConsoleWaitForEnterKey;
begin
ReadLn;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function RunConsoleCommand(const aCommand, aParameters : String; CallBack : TOutputProc<PAnsiChar> = nil; OutputLines : TStrings = nil) : Cardinal;
const
CReadBuffer = 2400;
var
saSecurity: Windows.TSecurityAttributes;
hRead: THandle;
hWrite: THandle;
suiStartup: TStartupInfo;
piProcess: TProcessInformation;
pBuffer: array [0..CReadBuffer] of AnsiChar;
dBuffer: array [0..CReadBuffer] of AnsiChar;
dRead: DWORD;
dRunning: DWORD;
dAvailable: DWORD;
begin
Result := 0;
saSecurity.nLength := SizeOf(Windows.TSecurityAttributes);
saSecurity.bInheritHandle := true;
saSecurity.lpSecurityDescriptor := nil;
if CreatePipe(hRead,hWrite,@saSecurity, 0) then
begin
try
FillChar(suiStartup, SizeOf(TStartupInfo), #0);
suiStartup.cb := SizeOf(TStartupInfo);
suiStartup.hStdInput := hRead;
suiStartup.hStdOutput := hWrite;
suiStartup.hStdError := hWrite;
suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
suiStartup.wShowWindow := SW_HIDE;
if CreateProcess(nil,PChar(Format('%s %s',[aCommand,aParameters])),
@saSecurity,
@saSecurity,
True,
NORMAL_PRIORITY_CLASS,
nil,
nil,
suiStartup,
piProcess) then
begin
try
repeat
dRunning := WaitForSingleObject(piProcess.hProcess,100);
PeekNamedPipe(hRead,nil,0,nil,@dAvailable,nil);
if (dAvailable > 0) then
begin
repeat
dRead := 0;
ReadFile(hRead,pBuffer[0],CReadBuffer,dRead,nil);
pBuffer[dRead] := #0;
OemToCharA(pBuffer,dBuffer);
if Assigned(CallBack) then CallBack(dBuffer);
if Assigned(OutputLines) then OutputLines.Add(string(dBuffer));
until (dRead < CReadBuffer);
end;
//Application.ProcessMessages;
until (dRunning <> WAIT_TIMEOUT);
finally
CloseHandle(piProcess.hProcess);
CloseHandle(piProcess.hThread);
end;
end;
GetExitCodeProcess(piProcess.hProcess,Result);
finally
CloseHandle(hRead);
CloseHandle(hWrite);
end;
end
else raise Exception.Create('Can''t create pipe!');
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure InitConsole;
var
BufferInfo: TConsoleScreenBufferInfo;
begin
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
{$IFNDEF FPC}
SetInOutRes(GetLastError);
{$ENDIF}
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;
{$ELSE}
//AssignCrt(stderr);
//Rewrite(stderr);
{$ENDIF}
{ TConsoleMenu }
{$IFDEF MSWINDOWS}
procedure TConsoleMenu.AddMenu(const cMenuCaption: string; const cMenuKey: Word; MenuAction: TExecuteProc);
var
conmenu : TConsoleMenuOption;
begin
conmenu.Caption := cMenuCaption;
conmenu.Key := cMenuKey;
conmenu.OnKeyPressed := MenuAction;
{$IFDEF DELPHIXE7_UP}
fConsoleMenu := fConsoleMenu + [conmenu];
{$ELSE}
SetLength(fConsoleMenu,Length(fConsoleMenu)+1);
fConsoleMenu[High(fConsoleMenu)] := conmenu;
{$ENDIF}
end;
procedure TConsoleMenu.AddMenu(MenuOption: TConsoleMenuOption);
begin
{$IFDEF DELPHIXE7_UP}
fConsoleMenu := fConsoleMenu + [MenuOption];
{$ELSE}
SetLength(fConsoleMenu,Length(fConsoleMenu)+1);
fConsoleMenu[High(fConsoleMenu)] := MenuOption;
{$ENDIF}
end;
constructor TConsoleMenu.Create;
begin
fMenuColor := ccLightCyan;
fIsActive := False;
end;
procedure TConsoleMenu.Refresh(aClearScreen: Boolean);
begin
if aClearScreen then ClearScreen;
WriteMenu;
end;
procedure TConsoleMenu.WaitForKeys;
var
msg: TMsg;
conmenu : TConsoleMenuOption;
keypressed : Word;
begin
fIsActive := True;
HideCursor;
WriteMenu;
while True do
begin
//check key pressed
keypressed := GetConsoleKeyPressed;
for conmenu in fConsoleMenu do
begin
if keypressed = conmenu.Key then
begin
ClearScreen;
WriteMenu;
conmenu.DoKeyPressed;
end;
end;
if keypressed = VK_ESCAPE then
begin
coutXY(50,12,'Exiting...',etInfo);
Exit;
end;