mirror of
https://github.com/vapaamies/KolibriOS.git
synced 2025-09-22 15:13:49 +02:00
CRT unit with colored console example added
This commit is contained in:
168
Lib/CRT.pas
Normal file
168
Lib/CRT.pas
Normal file
@@ -0,0 +1,168 @@
|
||||
unit CRT;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
KolibriOS;
|
||||
|
||||
type
|
||||
TConsolePoint = record
|
||||
X, Y: Integer;
|
||||
end;
|
||||
|
||||
const
|
||||
Black = 0;
|
||||
Blue = 1;
|
||||
Green = 2;
|
||||
Cyan = 3;
|
||||
Red = 4;
|
||||
Magenta = 5;
|
||||
Brown = 6; // none in KolibriOS?
|
||||
LightGray = 7;
|
||||
DarkGray = 8; // none in KolibriOS?
|
||||
LightBlue = 9;
|
||||
LightGreen = 10;
|
||||
LightCyan = 11;
|
||||
LightRed = 12;
|
||||
LightMagenta = 13;
|
||||
Yellow = 14;
|
||||
White = 15;
|
||||
|
||||
procedure ConsoleInit(Title: PKolibriChar);
|
||||
|
||||
function GetCursorPos: TConsolePoint;
|
||||
procedure SetCursorPos(X, Y: Integer); overload;
|
||||
procedure SetCursorPos(const Point: TConsolePoint); overload;
|
||||
|
||||
procedure ResetAttributes;
|
||||
procedure TextAttribute(Color, Background: Integer);
|
||||
procedure TextBackground(Color: Integer);
|
||||
procedure TextColor(Color: Integer);
|
||||
|
||||
function WriteLn(LineBreaks: Integer = 1): LongInt; overload;
|
||||
function WriteLn(Text: PKolibriChar; LineBreaks: Integer = 1): LongInt; overload;
|
||||
|
||||
var
|
||||
ConsoleExit: procedure(CloseWindow: Boolean); stdcall;
|
||||
KeyPressed: function: Boolean;
|
||||
ReadKey: function: KolibriChar; stdcall;
|
||||
SetCursorHeight: function(Height: Integer): Integer; stdcall;
|
||||
Write: function(const Text: PKolibriChar): LongInt; cdecl varargs;
|
||||
WriteText: procedure(Text: PKolibriChar; Length: LongWord); stdcall;
|
||||
|
||||
implementation
|
||||
|
||||
procedure ResetAttributes;
|
||||
begin
|
||||
Write(#27'[0m');
|
||||
end;
|
||||
|
||||
procedure TextAttribute(Color, Background: Integer);
|
||||
begin
|
||||
TextColor(Color);
|
||||
TextBackground(Background);
|
||||
end;
|
||||
|
||||
procedure TextBackground(Color: Integer);
|
||||
const
|
||||
Light = #27'[1m';
|
||||
Colors: array[Black..LightGray] of PKolibriChar = (
|
||||
#27'[40m', // Black
|
||||
#27'[44m', // Blue
|
||||
#27'[42m', // Green
|
||||
#27'[46m', // Cyan
|
||||
#27'[41m', // Red
|
||||
#27'[45m', // Magenta
|
||||
#27'[43m', // Brown
|
||||
#27'[37m' // LightGray
|
||||
);
|
||||
begin
|
||||
case Color of
|
||||
Black..LightGray:
|
||||
Write(Colors[Color]);
|
||||
DarkGray..White:
|
||||
begin
|
||||
Write(Colors[Color]);
|
||||
Write(Light);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TextColor(Color: Integer);
|
||||
const
|
||||
Light = #27'[5m';
|
||||
Colors: array[Black..LightGray] of PKolibriChar = (
|
||||
#27'[30m', // Black
|
||||
#27'[34m', // Blue
|
||||
#27'[32m', // Green
|
||||
#27'[36m', // Cyan
|
||||
#27'[31m', // Red
|
||||
#27'[35m', // Magenta
|
||||
#27'[33m', // Brown
|
||||
#27'[37m' // LightGray
|
||||
);
|
||||
begin
|
||||
case Color of
|
||||
Black..LightGray:
|
||||
Write(Colors[Color]);
|
||||
DarkGray..White:
|
||||
begin
|
||||
Write(Colors[Color]);
|
||||
Write(Light);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
function WriteLn(LineBreaks: Integer): LongInt;
|
||||
var
|
||||
I: Integer;
|
||||
begin
|
||||
Result := 0;
|
||||
for I := 0 to LineBreaks - 1 do
|
||||
Inc(Result, Write(#10));
|
||||
end;
|
||||
|
||||
function WriteLn(Text: PKolibriChar; LineBreaks: Integer): LongInt;
|
||||
begin
|
||||
Result := Write(Text) + WriteLn(LineBreaks);
|
||||
end;
|
||||
|
||||
var
|
||||
hConsole: Pointer;
|
||||
ConsoleInitProc: procedure(WndWidth, WndHeight, ScrWidth, ScrHeight: LongInt; Caption: PKolibriChar); stdcall;
|
||||
GetCursorPosProc: procedure(var X, Y: Integer); stdcall;
|
||||
SetCursorPosProc: procedure(X, Y: Integer); stdcall;
|
||||
|
||||
procedure ConsoleInit(Title: PKolibriChar);
|
||||
begin
|
||||
hConsole := LoadLibrary('/sys/lib/console.obj');
|
||||
ConsoleInitProc := GetProcAddress(hConsole, 'con_init');
|
||||
ConsoleExit := GetProcAddress(hConsole, 'con_exit');
|
||||
GetCursorPosProc := GetProcAddress(hConsole, 'con_get_cursor_pos');
|
||||
KeyPressed := GetProcAddress(hConsole, 'con_kbhit');
|
||||
ReadKey := GetProcAddress(hConsole, 'con_getch');
|
||||
SetCursorHeight := GetProcAddress(hConsole, 'con_set_cursor_height');
|
||||
SetCursorPosProc := GetProcAddress(hConsole, 'con_set_cursor_pos');
|
||||
Write := GetProcAddress(hConsole, 'con_printf');
|
||||
WriteText := GetProcAddress(hConsole, 'con_write_string');
|
||||
|
||||
ConsoleInitProc(-1, -1, -1, -1, Title);
|
||||
end;
|
||||
|
||||
function GetCursorPos: TConsolePoint;
|
||||
begin
|
||||
GetCursorPosProc(Result.X, Result.Y);
|
||||
end;
|
||||
|
||||
procedure SetCursorPos(X, Y: Integer);
|
||||
begin
|
||||
SetCursorPosProc(X, Y);
|
||||
end;
|
||||
|
||||
procedure SetCursorPos(const Point: TConsolePoint);
|
||||
begin
|
||||
with Point do
|
||||
SetCursorPosProc(X, Y);
|
||||
end;
|
||||
|
||||
end.
|
Reference in New Issue
Block a user