Skip to content

Commit

Permalink
FIX for older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Oct 3, 2023
1 parent 9356cca commit 649e7a8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 35 deletions.
89 changes: 56 additions & 33 deletions samples/consolesample/ConsoleSample.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,67 @@ var
lSize: TMVCConsoleSize;

begin
Write('Press ANY key');
var c: Char := GetCh;
ShowCursor;
try
for F := 0 to 15 do
begin
TextBackground(Black);
TextColor(TConsoleColor(F));
lFGColorName := ColorName(TConsoleColor(F));
WriteLn(''.PadLeft(GetConsoleSize.Columns));
WriteLn(('** TEST FOREGROUND COLOR: ' + lFGColorName + ' **').PadRight(GetConsoleSize.Columns));
WriteLn(StringOfChar('_', GetConsoleSize.Columns));
WriteLn(''.PadLeft(GetConsoleSize.Columns));
for B := 0 to 15 do
ClrScr;
GotoXY(0,0);
Write('Press ANY key (cusor is visible)');
GetCh;
GotoXY(0,0);
HideCursor;
Write('Press ANY key (cusor is now hidden)');
GetCh;
try
for F := 0 to 15 do
begin
lBGColorName := ColorName(TConsoleColor(B));
TextBackground(TConsoleColor(B));
WriteLn((lFGColorName + ' on ' + lBGColorName).PadLeft(GetConsoleSize.Columns));
TextBackground(Black);
ClrScr;
TextBackground(Black);
TextColor(TConsoleColor(F));
lFGColorName := ColorName(TConsoleColor(F));
WriteLn(''.PadLeft(GetConsoleSize.Columns));
WriteLn(('** TEST FOREGROUND COLOR: ' + lFGColorName + ' **').PadRight(GetConsoleSize.Columns));
WriteLn(StringOfChar('_', GetConsoleSize.Columns));
WriteLn(''.PadLeft(GetConsoleSize.Columns));
for B := 0 to 15 do
begin
lBGColorName := ColorName(TConsoleColor(B));
TextBackground(TConsoleColor(B));
WriteLn((lFGColorName + ' on ' + lBGColorName).PadLeft(GetConsoleSize.Columns));
end;
GetCh;
end;
ReadLn;
ResetConsole;
ClrScr;
lSize := GetConsoleSize;
WriteLn(Format('Console Size: %d columns x %d rows', [lSize.Columns, lSize.Rows]));
lSize := GetConsoleBufferSize;
WriteLn(Format('Console Buffer Size: %d columns x %d rows', [lSize.Columns, lSize.Rows]));
GetCh;

// limits
ClrScr;
TextColor(TConsoleColor.Red);
GotoXY(0, 0);
Write('X');
GotoXY(lSize.Columns - 1, 0);
Write('X');
GotoXY(lSize.Columns - 1, lSize.Rows - 2);
Write('X');
GotoXY(0, lSize.Rows - 2);
Write('X');
CenterInScreen('CONSOLE LIMITS');
GetCh;
ResetConsole;
ClrScr;
GotoXY(0,0);
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
ReadLn;
ResetConsole;
ClrScr;
lSize := GetConsoleSize;
WriteLn(Format('Console Size: %d columns x %d rows', [lSize.Columns, lSize.Rows]));
lSize := GetConsoleBufferSize;
WriteLn(Format('Console Buffer Size: %d columns x %d rows', [lSize.Columns, lSize.Rows]));
readln;
GotoXY(0, 0);
TextColor(TConsoleColor.Red);
Write('X');
GotoXY(lSize.Columns - 1, lSize.Rows - 2);
Write('X');
ResetConsole;
GotoXY(0,0);
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
finally
ShowCursor;
end;
if DebugHook <> 0 then
begin
Expand Down
57 changes: 55 additions & 2 deletions sources/MVCFramework.Console.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

unit MVCFramework.Console;


{$I dmvcframework.inc}

interface

uses
Expand Down Expand Up @@ -84,6 +87,9 @@ function TextAttr: Word;
procedure SetTextAttr(const TextAttr: Word);
function BackgroundAttr: Word;
procedure SetBackgroundAttr(const BackgroundAttr: Word);
procedure HideCursor;
procedure ShowCursor;
procedure CenterInScreen(const Text: String);


function ColorName(const color: TConsoleColor): String;
Expand All @@ -104,13 +110,25 @@ implementation
GIsConsoleAllocated: Boolean = False;
GLock: TObject = nil;



function ColorName(const color: TConsoleColor): String;
begin
Result := GetEnumName(TypeInfo(TConsoleColor), Ord(color));
end;


{$IFDEF LINUX}
procedure HideCursor;
begin

end;

procedure ShowCursor;
begin

end;

procedure Init; inline;
begin

Expand Down Expand Up @@ -154,13 +172,18 @@ procedure ClrScr;
{$ENDIF}
{$IFDEF MSWINDOWS}

{.$IF not Defined(RIOORBETTER)}
const
ATTACH_PARENT_PROCESS = DWORD(-1);
function AttachConsole(dwProcessId: DWORD): BOOL; stdcall; external kernel32 name 'AttachConsole';
{.$ENDIF}

procedure WinCheck(const Value: LongBool);
begin
if not Value then
raise EMVCConsole.CreateFmt('GetLastError() = %d', [GetLastError]);
end;


procedure Init;
begin
if not GIsConsoleAllocated then
Expand All @@ -187,6 +210,16 @@ procedure Init;
end;
end;

procedure InternalShowCursor(const ShowCursor: Boolean);
var
info: CONSOLE_CURSOR_INFO;
begin
Init;
GetConsoleCursorInfo(GOutHandle, info);
info.bVisible := ShowCursor;
SetConsoleCursorInfo(GOutHandle, info);
end;

procedure WaitForReturn;
begin
Init;
Expand Down Expand Up @@ -290,8 +323,29 @@ procedure GotoXY(const X, Y: Byte);
end;
end;

procedure HideCursor;
begin
InternalShowCursor(False);
end;

procedure ShowCursor;
begin
InternalShowCursor(True);
end;

{$ENDIF}

{ ******************************************* }
{ * HIGH LEVEL FUNCTION - no IFDEF required * }
{ ******************************************* }

procedure CenterInScreen(const Text: String);
begin
Init;
GotoXY(GetConsoleSize.Columns div 2 - Length(Text) div 2, GetConsoleSize.Rows div 2 - 1);
Write(Text)
end;

procedure ResetConsole;
begin
SetDefaultColors;
Expand Down Expand Up @@ -347,7 +401,6 @@ procedure SetConsoleAttr(const TextAttr: Integer);
UpdateMode;
end;


function TextAttr: Word;
begin
Result := GForeGround;
Expand Down

0 comments on commit 649e7a8

Please sign in to comment.