Skip to content

Instantly share code, notes, and snippets.

@Irwin1985
Created August 2, 2022 18:29
Show Gist options
  • Save Irwin1985/0179e19d7497bcdebf19f50b2b7bd687 to your computer and use it in GitHub Desktop.
Save Irwin1985/0179e19d7497bcdebf19f50b2b7bd687 to your computer and use it in GitHub Desktop.
{
uTerm is a small unit to print text and background with color in your
terminal window.
Usage:
// forecolor
uTerm.Blue('This text should be printed in blue');
// backcolor
uTerm.BgBlue('This text should be printed in white with blue background');
**********************************************************************}
unit uTerm;
{$mode ObjFPC}{$H+}
{$modeswitch advancedrecords}
interface
uses
Classes, SysUtils, Crt;
type
TColor = (
{ Foreground and background color constants }
cBlack,
cBlue,
cGreen,
cCyan,
cRed,
cMagenta,
cBrown,
cLightGray,
{ Foreground color constants }
cDarkGray,
cLightBlue,
cLightGreen,
cLightCyan,
cLightRed,
cLightMagenta,
cYellow,
cWhite
);
TTerm = record
private
class procedure WriteColor(const Color: TColor; const Msg: string); static;
class procedure WriteBgColor(const Color: TColor; const Msg: string); static;
public
{ ForeColor }
class procedure Black(const Msg: string); static;
class procedure Blue(const Msg: string); static;
class procedure Green(const Msg: string); static;
class procedure Cyan(const Msg: string); static;
class procedure Red(const Msg: string); static;
class procedure Magenta(const Msg: string); static;
class procedure Brown(const Msg: string); static;
class procedure LightGray(const Msg: string); static;
{ BackgroundColor }
class procedure BgBlack(const Msg: string); static;
class procedure BgBlue(const Msg: string); static;
class procedure BgGreen(const Msg: string); static;
class procedure BgCyan(const Msg: string); static;
class procedure BgRed(const Msg: string); static;
class procedure BgMagenta(const Msg: string); static;
class procedure BgBrown(const Msg: string); static;
class procedure BgLightGray(const Msg: string); static;
end;
implementation
class procedure TTerm.WriteColor(const Color: TColor; const Msg: string);
begin
AssignCrt(stdout);
ReWrite(stdout);
TextColor(ord(Color));
writeln(stdout, Msg);
TextColor(ord(cLightGray)); // Reset
end;
class procedure TTerm.WriteBgColor(const Color: TColor; const Msg: string);
begin
AssignCrt(stdout);
ReWrite(stdout);
TextBackground(ord(Color));
writeln(stdout, Msg);
TextBackground(ord(cBlack)); // Reset
end;
class procedure TTerm.Black(const Msg: string);
begin
WriteColor(cBlack, Msg);
end;
class procedure TTerm.Blue(const Msg: string);
begin
WriteColor(cBlue, Msg);
end;
class procedure TTerm.Green(const Msg: string);
begin
WriteColor(cGreen, Msg);
end;
class procedure TTerm.Cyan(const Msg: string);
begin
WriteColor(cCyan, Msg);
end;
class procedure TTerm.Red(const Msg: string);
begin
WriteColor(cRed, Msg);
end;
class procedure TTerm.Magenta(const Msg: string);
begin
WriteColor(cMagenta, Msg);
end;
class procedure TTerm.Brown(const Msg: string);
begin
WriteColor(cBrown, Msg);
end;
class procedure TTerm.LightGray(const Msg: string);
begin
WriteColor(cLightGray, Msg);
end;
class procedure TTerm.BgBlack(const Msg: string);
begin
WriteBgColor(cBlack, Msg);
end;
class procedure TTerm.BgBlue(const Msg: string);
begin
WriteBgColor(cBlue, Msg);
end;
class procedure TTerm.BgGreen(const Msg: string);
begin
WriteBgColor(cGreen, Msg);
end;
class procedure TTerm.BgCyan(const Msg: string);
begin
WriteBgColor(cCyan, Msg);
end;
class procedure TTerm.BgRed(const Msg: string);
begin
WriteBgColor(cRed, Msg);
end;
class procedure TTerm.BgMagenta(const Msg: string);
begin
WriteBgColor(cMagenta, Msg);
end;
class procedure TTerm.BgBrown(const Msg: string);
begin
WriteBgColor(cBrown, Msg);
end;
class procedure TTerm.BgLightGray(const Msg: string);
begin
WriteBgColor(cLightGray, Msg);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment