Repository navigation
Expand file tree
/
Copy pathHelper.Console.pas
More file actions
113 lines (92 loc) · 2.63 KB
/
Copy pathHelper.Console.pas
File metadata and controls
113 lines (92 loc) · 2.63 KB
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
{*******************************************************}
{ YAGE: Yet Another Global Encoder }
{ Unit: Helper.Console }
{ Copyright(c) 2021 Alexey Anisimov }
{ Contact email: softlight@ya.ru }
{*******************************************************}
unit Helper.Console;
{$MODE delphiunicode}
interface
uses
Windows,
Helper.Singleton;
type
TConsole = class(TSingleton)
protected
constructor Create; override;
public
destructor Destroy; override;
private
FHandle: Integer;
FInfo: TConsoleScreenBufferInfo;
public
procedure WriteLn(const Msg: string); overload;
procedure WriteLn; overload;
procedure Write(const Msg: string);
procedure Cls(const Attr: Word); overload;
procedure Cls; overload;
procedure SetColor(const Attr: Word); overload;
procedure SetColor; overload;
end;
function Console: TConsole;
implementation
{ TConsole }
constructor TConsole.Create;
begin
inherited Create;
// Init settings here
FHandle := GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(FHandle, FInfo);
end;
destructor TConsole.Destroy;
begin
// Save settings here
inherited Destroy;
end;
procedure TConsole.WriteLn(const Msg: string);
begin
System.WriteLn(MSg);
end;
procedure TConsole.WriteLn;
begin
System.WriteLn;
end;
procedure TConsole.Write(const Msg: string);
begin
System.Write(MSg);
end;
procedure TConsole.Cls(const Attr: Word);
var
CharsWritten: DWORD;
CharsToWrite: DWORD;
begin
GetConsoleScreenBufferInfo(FHandle, FInfo);
CharsToWrite := FInfo.dwSize.X * FInfo.dwSize.Y;
SetColor(Attr);
FInfo.dwCursorPosition.X := 0;
FInfo.dwCursorPosition.Y := 0;
FillConsoleOutputCharacter(FHandle, ' ', CharsToWrite, FInfo.dwCursorPosition, CharsWritten);
Assert(CharsToWrite = CharsWritten);
FillConsoleOutputAttribute(FHandle, Attr, CharsToWrite, FInfo.dwCursorPosition, CharsWritten);
Assert(CharsToWrite = CharsWritten);
FInfo.dwCursorPosition.X := 0;
FInfo.dwCursorPosition.Y := 0;
SetConsoleCursorPosition(FHandle, FInfo.dwCursorPosition);
end;
procedure TConsole.Cls;
begin
Cls(FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE);
end;
procedure TConsole.SetColor(const Attr: Word);
begin
SetConsoleTextAttribute(FHandle, Attr);
end;
procedure TConsole.SetColor;
begin
SetConsoleTextAttribute(FHandle, FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE);
end;
function Console: TConsole;
begin
Result := TConsole.GetInstance;
end;
end.