-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUANSIW.PAS
188 lines (158 loc) · 4.1 KB
/
UANSIW.PAS
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
uansiw Unit
2022 LRT
}
unit
uansiw;
interface
uses
consts, utils, uexc, uclasses, types, locale, uobject, ustream,
ascii, strings;
type
EAnsiColor = (
fgBlack,
fgRed,
fgGreen,
fgYellow,
fgBlue,
fgMagenta,
fgCyan,
fgWhite,
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite,
none,
bold
);
EAnsiDirection = (
up,
down,
right,
left
);
PAnsiWriter = ^TAnsiWriter;
TAnsiWriter = object (TStream)
public
constructor initWithStream(stream: PStream);
destructor done; virtual;
function read(buffer: pointer; count: word): word; virtual;
procedure write(buffer: pointer; count: word); virtual;
procedure seek(pos: longint); virtual;
procedure clear;
procedure gotoxy(x, y: byte);
procedure setColor(color: EAnsiColor);
procedure showCursor;
procedure hideCursor;
procedure move(direction: EAnsiDirection; n: byte);
procedure setEnabled(enabled: boolean);
function getPosition: longint; virtual;
function isEOF: boolean; virtual;
function getSize: longint; virtual;
function isReadOnly: boolean; virtual;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_stream: PStream;
_enabled: boolean;
end;
implementation
const
C_ANSI_COLORS : array [EAnsiColor] of byte = (
30, 31, 32, 33, 34, 35, 36, 37,
40, 41, 42, 43, 44, 45, 46, 47,
0, 1
);
C_ANSI_DIRECTION : array [EAnsiDirection] of char = (
'A', 'B', 'C', 'D'
);
{ TAnsiWriter public }
constructor TAnsiWriter.initWithStream(stream: PStream);
begin
inherited init;
_stream := stream;
_stream^.retain;
_enabled := true;
end;
destructor TAnsiWriter.done;
begin
_stream^.done;
inherited done;
end;
function TAnsiWriter.read(buffer: pointer; count: word): word;
begin
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
procedure TAnsiWriter.write(buffer: pointer; count: word);
begin
_stream^.write(buffer, count);
end;
procedure TAnsiWriter.seek(pos: longint);
begin
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
procedure TAnsiWriter.clear;
begin
if not _enabled then exit;
_stream^.writestr(C_ESC + '[2J');
end;
procedure TAnsiWriter.gotoxy(x, y: byte);
begin
if not _enabled then exit;
_stream^.writestr(C_ESC + '[' + inttostr(y) + ';' + inttostr(x) + 'H' );
end;
procedure TAnsiWriter.setColor(color: EAnsiColor);
begin
if not _enabled then exit;
_stream^.writestr(C_ESC + '[' + inttostr(C_ANSI_COLORS[color]) + 'm');
end;
procedure TAnsiWriter.showCursor;
begin
if not _enabled then exit;
_stream^.writestr(C_ESC + '[?25h');
end;
procedure TAnsiWriter.hideCursor;
begin
if not _enabled then exit;
_stream^.writestr(C_ESC + '[?25l');
end;
procedure TAnsiWriter.move(direction: EAnsiDirection; n: byte);
begin
if not _enabled then exit;
_stream^.writestr(C_ESC + '[' + inttostr(n) + C_ANSI_DIRECTION[direction]);
end;
procedure TAnsiWriter.setEnabled(enabled: boolean);
begin
_enabled := enabled;
end;
function TAnsiWriter.getPosition: longint;
begin
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
function TAnsiWriter.isEOF: boolean;
begin
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
function TAnsiWriter.getSize: longint;
begin
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
function TAnsiWriter.isReadOnly: boolean;
begin
isReadOnly := false;
end;
function TAnsiWriter.getClassName: string;
begin
getClassName := 'TAnsiWriter';
end;
function TAnsiWriter.getClassId: word;
begin
getClassId := C_CLASS_ID_AnsiWriter;
end;
{ TAnsiWriter private }
{ Other }
end.