-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
289 lines (267 loc) · 6.62 KB
/
Copy pathProgram.cs
File metadata and controls
289 lines (267 loc) · 6.62 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Icod.Terminal.RasterAnimation.Sample
Sample application demonstrating Icod.Terminal RasterAnimation features.
Copyright (C) 2026 Timothy J. Bruce <uniblab@hotmail.com>
*/
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Icod.Terminal;
TerminalRasterImage rootImage = CreateFrame(
255, 72, 72,
255, 196, 72,
196, 255, 72,
72, 255, 160
);
TerminalRasterImage secondImage = CreateFrame(
72, 196, 255,
96, 96, 255,
196, 96, 255,
255, 96, 196
);
TerminalRasterImage thirdImage = CreateFrame(
255, 96, 196,
196, 96, 255,
96, 96, 255,
72, 196, 255
);
TerminalRasterImage streamedImage = CreateFrame(
72, 255, 160,
196, 255, 72,
255, 196, 72,
255, 72, 72
);
await using TerminalSession session = await TerminalSession.OpenAsync(
new TerminalSessionOptions {
InputMode = TerminalInputMode.CBreak,
EchoInput = false
}
);
await session.WriteTextAsync(
"Icod.Terminal semantic persistent-raster animation sample.\r\n"
);
await session.WriteTextAsync(
"The application supplies frames and presentation intent; Terminal owns opaque frame identity, sequence certainty, acknowledged control, and cleanup.\r\n"
);
TerminalCapabilityStatus capability = await session.VerifyCapabilityAsync(
TerminalCapability.PersistentRasterAnimation
);
if ( !capability.IsUsable ) {
await session.WriteTextAsync(
string.Concat(
"Persistent raster animation is not currently usable: ",
capability.Support.ToString(),
".\r\n"
)
);
return 1;
}
TerminalControlResult<TerminalRasterResource> resourceResult =
await session.CreateRasterResourceAsync( rootImage );
if ( TerminalControlStatus.Available != resourceResult.Status
|| resourceResult.Value is null ) {
await ReportFailureAsync(
session,
"Raster resource creation",
resourceResult.Status,
resourceResult.Message
);
return 1;
}
await using TerminalRasterResource resource = resourceResult.Value;
TerminalRasterAnimation animation = resource.Animation;
TerminalRasterAnimationFrame rootFrame = animation.RootFrame;
if ( !await RequireSuccessAsync(
session,
"Root-frame timing",
await animation.SetFrameDurationAsync(
rootFrame,
TimeSpan.FromMilliseconds( 180 )
)
) ) {
return 1;
}
TerminalControlResult<TerminalRasterAnimationFrame> secondResult =
await animation.AddFrameAsync(
secondImage,
TimeSpan.FromMilliseconds( 180 )
);
if ( TerminalControlStatus.Available != secondResult.Status
|| secondResult.Value is null ) {
await ReportFailureAsync(
session,
"Second-frame append",
secondResult.Status,
secondResult.Message
);
return 1;
}
TerminalRasterAnimationFrame secondFrame = secondResult.Value;
TerminalControlResult<TerminalRasterAnimationFrame> thirdResult =
await animation.AddFrameAsync(
thirdImage,
TimeSpan.FromMilliseconds( 180 )
);
if ( TerminalControlStatus.Available != thirdResult.Status
|| thirdResult.Value is null ) {
await ReportFailureAsync(
session,
"Third-frame append",
thirdResult.Status,
thirdResult.Message
);
return 1;
}
TerminalControlResult<TerminalRasterPlacement> placementResult =
await resource.CreatePlacementAsync(
new TerminalRasterPlacementOptions {
Columns = 8,
Rows = 4
}
);
if ( TerminalControlStatus.Available != placementResult.Status
|| placementResult.Value is null ) {
await ReportFailureAsync(
session,
"Raster placement creation",
placementResult.Status,
placementResult.Message
);
return 1;
}
await using TerminalRasterPlacement placement = placementResult.Value;
if ( !await RequireSuccessAsync(
session,
"Loading-mode playback",
await animation.RunLoadingAsync()
) ) {
return 1;
}
TerminalControlResult<TerminalRasterAnimationFrame> streamedResult =
await animation.AddFrameAsync(
streamedImage,
TimeSpan.FromMilliseconds( 180 )
);
if ( TerminalControlStatus.Available != streamedResult.Status
|| streamedResult.Value is null ) {
await ReportFailureAsync(
session,
"Loading-mode frame append",
streamedResult.Status,
streamedResult.Message
);
return 1;
}
if ( !await RequireSuccessAsync(
session,
"Loading-mode stop",
await animation.StopAsync()
) ) {
return 1;
}
if ( !await RequireSuccessAsync(
session,
"Explicit frame selection",
await animation.SelectFrameAsync( secondFrame )
) ) {
return 1;
}
if ( !await RequireSuccessAsync(
session,
"Finite normal playback",
await animation.RunAsync(
new TerminalRasterAnimationPlaybackOptions {
RepeatCount = 1
}
)
) ) {
return 1;
}
await Task.Delay( TimeSpan.FromMilliseconds( 900 ) );
if ( !await RequireSuccessAsync(
session,
"Finite playback stop",
await animation.StopAsync()
) ) {
return 1;
}
if ( !await RequireSuccessAsync(
session,
"Indefinite normal playback",
await animation.RunAsync(
new TerminalRasterAnimationPlaybackOptions {
RepeatCount = null
}
)
) ) {
return 1;
}
await Task.Delay( TimeSpan.FromMilliseconds( 900 ) );
if ( !await RequireSuccessAsync(
session,
"Indefinite playback stop",
await animation.StopAsync()
) ) {
return 1;
}
await session.WriteTextAsync(
"Completed loading-mode append, explicit selection, finite playback, indefinite playback, and deterministic resource-owned cleanup.\r\n"
);
return 0;
static TerminalRasterImage CreateFrame(
params byte[] pixels
) {
ArgumentNullException.ThrowIfNull( pixels );
return TerminalRasterImage.CreateRgb24(
2,
2,
pixels
);
}
static async ValueTask<bool> RequireSuccessAsync(
TerminalSession session,
string operation,
TerminalControlMutationResult result
) {
ArgumentNullException.ThrowIfNull( session );
ArgumentException.ThrowIfNullOrWhiteSpace( operation );
if ( result.Succeeded ) {
return true;
}
await ReportFailureAsync(
session,
operation,
result.Status,
result.Message
);
return false;
}
static ValueTask ReportFailureAsync(
TerminalSession session,
string operation,
TerminalControlStatus status,
string? message
) {
ArgumentNullException.ThrowIfNull( session );
ArgumentException.ThrowIfNullOrWhiteSpace( operation );
return session.WriteTextAsync(
string.Concat(
operation,
" was not completed: ",
status.ToString(),
string.IsNullOrEmpty( message )
? string.Empty
: string.Concat( " — ", message ),
".\r\n"
)
);
}