|
@@ -1,13 +1,13 @@
|
|
{ ***************************************************************************
|
|
{ ***************************************************************************
|
|
|
|
|
|
- Copyright (c) 2016-2018 Kike Pérez
|
|
|
|
|
|
+ Copyright (c) 2016-2019 Kike Pérez
|
|
|
|
|
|
Unit : Quick.Console
|
|
Unit : Quick.Console
|
|
Description : Console output with colors and optional file log
|
|
Description : Console output with colors and optional file log
|
|
Author : Kike Pérez
|
|
Author : Kike Pérez
|
|
- Version : 1.8
|
|
|
|
|
|
+ Version : 1.9
|
|
Created : 10/05/2017
|
|
Created : 10/05/2017
|
|
- Modified : 09/03/2018
|
|
|
|
|
|
+ Modified : 17/02/2019
|
|
|
|
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
This file is part of QuickLib: https://github.com/exilon/QuickLib
|
|
|
|
|
|
@@ -59,6 +59,7 @@ uses
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
{$IFDEF DELPHILINUX}
|
|
{$IFDEF DELPHILINUX}
|
|
Quick.SyncObjs.Linux.Compatibility,
|
|
Quick.SyncObjs.Linux.Compatibility,
|
|
|
|
+ Posix.StdDef,
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
SysUtils,
|
|
SysUtils,
|
|
Quick.Commons,
|
|
Quick.Commons,
|
|
@@ -67,6 +68,7 @@ uses
|
|
type
|
|
type
|
|
|
|
|
|
//text colors
|
|
//text colors
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
TConsoleColor = (
|
|
TConsoleColor = (
|
|
ccBlack = 0,
|
|
ccBlack = 0,
|
|
ccBlue = 1,
|
|
ccBlue = 1,
|
|
@@ -84,6 +86,25 @@ type
|
|
ccLightMagenta = 13,
|
|
ccLightMagenta = 13,
|
|
ccYellow = 14,
|
|
ccYellow = 14,
|
|
ccWhite = 15);
|
|
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
|
|
TConsoleProperties = record
|
|
LogVerbose : TLogVerbose;
|
|
LogVerbose : TLogVerbose;
|
|
@@ -171,9 +192,20 @@ type
|
|
procedure ClearLine(Y : Integer); overload;
|
|
procedure ClearLine(Y : Integer); overload;
|
|
procedure ShowCursor;
|
|
procedure ShowCursor;
|
|
procedure HideCursor;
|
|
procedure HideCursor;
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ procedure SaveCursor;
|
|
|
|
+ procedure RestoreCursor;
|
|
|
|
+ procedure CursorOn;
|
|
|
|
+ procedure CursorOff;
|
|
|
|
+ function ReadKey : Char;
|
|
|
|
+ {$ELSE}
|
|
function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
|
|
function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
|
|
function GetCursorY: Integer; {$IFDEF INLINES}inline;{$ENDIF}
|
|
function GetCursorY: Integer; {$IFDEF INLINES}inline;{$ENDIF}
|
|
|
|
+ {$ENDIF}
|
|
function GetCursorMaxBottom : Integer;
|
|
function GetCursorMaxBottom : Integer;
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ procedure GotoXY(x,y : Integer);
|
|
|
|
+ {$ENDIF}
|
|
procedure SetCursorPos(NewCoord : TCoord); overload;
|
|
procedure SetCursorPos(NewCoord : TCoord); overload;
|
|
procedure SetCursorPos(x ,y : Integer); overload;
|
|
procedure SetCursorPos(x ,y : Integer); overload;
|
|
{$IFDEF MSWINDOWS}
|
|
{$IFDEF MSWINDOWS}
|
|
@@ -203,6 +235,14 @@ var
|
|
implementation
|
|
implementation
|
|
|
|
|
|
|
|
|
|
|
|
+{$IFDEF DELPHILINUX}
|
|
|
|
+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);
|
|
procedure cout(const cMsg : Integer; cEventType : TLogEventType);
|
|
var
|
|
var
|
|
FmtSets : TFormatSettings;
|
|
FmtSets : TFormatSettings;
|
|
@@ -286,6 +326,33 @@ begin
|
|
cout(Format(cMsg,params),cEventType);
|
|
cout(Format(cMsg,params),cEventType);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{$IFDEF DELPHILINUX}
|
|
|
|
+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}
|
|
function GetCursorX: Integer; {$IFDEF INLINES}inline;{$ENDIF}
|
|
{$IFDEF MSWINDOWS}
|
|
{$IFDEF MSWINDOWS}
|
|
var
|
|
var
|
|
@@ -296,7 +363,11 @@ begin
|
|
end;
|
|
end;
|
|
{$ELSE}
|
|
{$ELSE}
|
|
begin
|
|
begin
|
|
|
|
+ {$IFDEF FPC}
|
|
Result := WhereX;
|
|
Result := WhereX;
|
|
|
|
+ {$ELSE}
|
|
|
|
+ Result := Byte(ConData(CD_CURRX))-lo(windmin);
|
|
|
|
+ {$ENDIF}
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
|
|
@@ -313,6 +384,7 @@ begin
|
|
Result := WhereY;
|
|
Result := WhereY;
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+{$ENDIF DELPHILINUX}
|
|
|
|
|
|
function GetCursorMaxBottom : Integer;
|
|
function GetCursorMaxBottom : Integer;
|
|
{$IFDEF MSWINDOWS}
|
|
{$IFDEF MSWINDOWS}
|
|
@@ -324,7 +396,14 @@ begin
|
|
end;
|
|
end;
|
|
{$ELSE}
|
|
{$ELSE}
|
|
begin
|
|
begin
|
|
- Result := 80;
|
|
|
|
|
|
+ Result := 25;
|
|
|
|
+end;
|
|
|
|
+{$ENDIF}
|
|
|
|
+
|
|
|
|
+{$IFDEF DELPHILINUX}
|
|
|
|
+procedure GotoXY(x,y : Integer);
|
|
|
|
+begin
|
|
|
|
+ Write(AEC, y, ';', x, 'H');
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
|
|
@@ -354,8 +433,12 @@ begin
|
|
{$IFDEF MSWINDOWS}
|
|
{$IFDEF MSWINDOWS}
|
|
if hStdOut = 0 then Exit;
|
|
if hStdOut = 0 then Exit;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
LastCoord.X := GetCursorX;
|
|
LastCoord.X := GetCursorX;
|
|
LastCoord.Y := GetCursorY;
|
|
LastCoord.Y := GetCursorY;
|
|
|
|
+ {$ELSE}
|
|
|
|
+ write(SAVE_CURSOR_POS);
|
|
|
|
+ {$ENDIF}
|
|
NewCoord.X := x;
|
|
NewCoord.X := x;
|
|
NewCoord.Y := y;
|
|
NewCoord.Y := y;
|
|
ClearLine(Y);
|
|
ClearLine(Y);
|
|
@@ -363,7 +446,11 @@ begin
|
|
try
|
|
try
|
|
cout(cMsg,cEventType);
|
|
cout(cMsg,cEventType);
|
|
finally
|
|
finally
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
SetCursorPos(LastCoord);
|
|
SetCursorPos(LastCoord);
|
|
|
|
+ {$ELSE}
|
|
|
|
+ write(RESTORE_CURSOR_POS);
|
|
|
|
+ {$ENDIF}
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -375,8 +462,12 @@ begin
|
|
{$IFDEF MSWINDOWS}
|
|
{$IFDEF MSWINDOWS}
|
|
if hStdOut = 0 then Exit;
|
|
if hStdOut = 0 then Exit;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
LastCoord.X := GetCursorX;
|
|
LastCoord.X := GetCursorX;
|
|
LastCoord.Y := GetCursorY;
|
|
LastCoord.Y := GetCursorY;
|
|
|
|
+ {$ELSE}
|
|
|
|
+ write(SAVE_CURSOR_POS);
|
|
|
|
+ {$ENDIF}
|
|
NewCoord.X := x;
|
|
NewCoord.X := x;
|
|
NewCoord.Y := y;
|
|
NewCoord.Y := y;
|
|
ClearLine(Y);
|
|
ClearLine(Y);
|
|
@@ -384,7 +475,11 @@ begin
|
|
try
|
|
try
|
|
cout(cMsg,cColor);
|
|
cout(cMsg,cColor);
|
|
finally
|
|
finally
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
SetCursorPos(LastCoord);
|
|
SetCursorPos(LastCoord);
|
|
|
|
+ {$ELSE}
|
|
|
|
+ write(RESTORE_CURSOR_POS);
|
|
|
|
+ {$ENDIF}
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -436,7 +531,11 @@ begin
|
|
TextAttr := (TextAttr and $F0) or (Color and $0F);
|
|
TextAttr := (TextAttr and $F0) or (Color and $0F);
|
|
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
|
|
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
|
|
{$ELSE}
|
|
{$ELSE}
|
|
- crt.TextColor(Color);
|
|
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ write(AEC,';',Color,'m')
|
|
|
|
+ {$ELSE}
|
|
|
|
+ crt.TextColor(Color);
|
|
|
|
+ {$ENDIF}
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -453,7 +552,11 @@ begin
|
|
TextAttr := (TextAttr and $0F) or ((Color shl 4) and $F0);
|
|
TextAttr := (TextAttr and $0F) or ((Color shl 4) and $F0);
|
|
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
|
|
if TextAttr <> LastMode then SetConsoleTextAttribute(hStdOut, TextAttr);
|
|
{$ELSE}
|
|
{$ELSE}
|
|
- crt.TextBackground(Color);
|
|
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ write(AEC,0,';',Color+10*10);
|
|
|
|
+ {$ELSE}
|
|
|
|
+ crt.TextBackground(Color);
|
|
|
|
+ {$ENDIF}
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
end;
|
|
end;
|
|
|
|
|
|
@@ -508,13 +611,21 @@ begin
|
|
end;
|
|
end;
|
|
{$ELSE}
|
|
{$ELSE}
|
|
begin
|
|
begin
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ write(AEC,2,'J');
|
|
|
|
+ {$ELSE}
|
|
ClrScr;
|
|
ClrScr;
|
|
|
|
+ {$ENDIF}
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
|
|
procedure ClearLine;
|
|
procedure ClearLine;
|
|
begin
|
|
begin
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
ClearLine(GetCursorY);
|
|
ClearLine(GetCursorY);
|
|
|
|
+ {$ELSE}
|
|
|
|
+ write(AEC,'K');
|
|
|
|
+ {$ENDIF}
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure ClearLine(Y : Integer);
|
|
procedure ClearLine(Y : Integer);
|
|
@@ -533,7 +644,11 @@ end;
|
|
{$ELSE}
|
|
{$ELSE}
|
|
begin
|
|
begin
|
|
GotoXY(1,Y);
|
|
GotoXY(1,Y);
|
|
|
|
+ {$IFDEF DELPHILINUX}
|
|
|
|
+ write(AEC,'K');
|
|
|
|
+ {$ELSE}
|
|
DelLine;
|
|
DelLine;
|
|
|
|
+ {$ENDIF}
|
|
GotoXY(1,Y);
|
|
GotoXY(1,Y);
|
|
end;
|
|
end;
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
@@ -755,7 +870,6 @@ procedure InitConsole;
|
|
var
|
|
var
|
|
BufferInfo: TConsoleScreenBufferInfo;
|
|
BufferInfo: TConsoleScreenBufferInfo;
|
|
begin
|
|
begin
|
|
- Console.LogVerbose := LOG_ALL;
|
|
|
|
Rewrite(Output);
|
|
Rewrite(Output);
|
|
hStdOut := TTextRec(Output).Handle;
|
|
hStdOut := TTextRec(Output).Handle;
|
|
{$IFDEF HASERROUTPUT}
|
|
{$IFDEF HASERROUTPUT}
|
|
@@ -943,19 +1057,28 @@ end;
|
|
|
|
|
|
initialization
|
|
initialization
|
|
|
|
|
|
|
|
+Console.LogVerbose := LOG_ALL;
|
|
{$IF DEFINED(FPC) AND DEFINED(LINUX)}
|
|
{$IF DEFINED(FPC) AND DEFINED(LINUX)}
|
|
InitCriticalSection(CSConsole);
|
|
InitCriticalSection(CSConsole);
|
|
{$ELSE}
|
|
{$ELSE}
|
|
-InitializeCriticalSection(CSConsole);
|
|
|
|
-//init stdout if not a service
|
|
|
|
-if GetStdHandle(STD_OUTPUT_HANDLE) <> 0 then InitConsole;
|
|
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
|
|
+ InitializeCriticalSection(CSConsole);
|
|
|
|
+ //init stdout if not a service
|
|
|
|
+ if GetStdHandle(STD_OUTPUT_HANDLE) <> 0 then InitConsole;
|
|
|
|
+ {$ELSE}
|
|
|
|
+ CSConsole := TRTLCriticalSection.Create;
|
|
|
|
+ {$ENDIF}
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
|
|
finalization
|
|
finalization
|
|
{$IF DEFINED(FPC) AND DEFINED(LINUX)}
|
|
{$IF DEFINED(FPC) AND DEFINED(LINUX)}
|
|
DoneCriticalsection(CSConsole);
|
|
DoneCriticalsection(CSConsole);
|
|
{$ELSE}
|
|
{$ELSE}
|
|
-DeleteCriticalSection(CSConsole);
|
|
|
|
|
|
+ {$IFNDEF DELPHILINUX}
|
|
|
|
+ DeleteCriticalSection(CSConsole);
|
|
|
|
+ {$ELSE}
|
|
|
|
+ CSConsole.Free;
|
|
|
|
+ {$ENDIF}
|
|
{$ENDIF}
|
|
{$ENDIF}
|
|
|
|
|
|
end.
|
|
end.
|