-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
306 lines (280 loc) · 6.75 KB
/
Copy pathProgram.cs
File metadata and controls
306 lines (280 loc) · 6.75 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System.Globalization;
using Icod.Terminal;
TimeSpan probeTimeout = TimeSpan.FromMilliseconds( 750 );
await using TerminalSession session = await TerminalSession.OpenAsync(
new TerminalSessionOptions {
InputMode = TerminalInputMode.CBreak,
EchoInput = false
}
);
TerminalPresentationLease? presentation = null;
TerminalInputProtocolLease? inputProtocols = null;
try {
TerminalControlResult<TerminalPresentationLease> presentationResult =
await session.AcquirePresentationAsync(
new TerminalPresentationOptions {
CursorVisibility = TerminalCursorVisibility.Normal
}
);
if ( presentationResult.IsAvailable ) {
presentation = presentationResult.GetRequiredValue();
await WriteLineAsync(
session,
"Presentation lease active while explicit probes run."
);
} else {
await WriteLineAsync(
session,
"Presentation lease unavailable; probes will still run explicitly."
);
}
TerminalControlResult<TerminalInputProtocolLease> inputResult =
await session.AcquireInputProtocolsAsync(
new TerminalInputProtocolOptions {
BracketedPaste = true,
FocusReporting = true
}
);
if ( inputResult.IsAvailable ) {
inputProtocols = inputResult.GetRequiredValue();
await WriteLineAsync(
session,
"Rich-input lease active while explicit probes run."
);
} else {
await WriteLineAsync(
session,
"Rich-input lease unavailable; probes will still run explicitly."
);
}
await WriteLineAsync(
session,
"Opening the session performs no automatic interrogation."
);
await WriteLineAsync(
session,
"The following requests are issued only because this sample explicitly asks for them."
);
await WriteLineAsync(
session,
string.Empty
);
await ReportProbeAsync(
session,
"Primary DA",
async () => {
TerminalPrimaryDeviceAttributes value =
await session.QueryPrimaryDeviceAttributesAsync( probeTimeout );
string attributes = string.Join(
",",
value.Attributes.Select(
static attribute => attribute.ToString(
CultureInfo.InvariantCulture
)
)
);
return string.Concat(
"device-code=",
value.DeviceCode.ToString( CultureInfo.InvariantCulture ),
" attributes=[",
attributes,
"]"
);
}
);
await ReportProbeAsync(
session,
"Secondary DA",
async () => {
TerminalSecondaryDeviceAttributes value =
await session.QuerySecondaryDeviceAttributesAsync( probeTimeout );
return string.Concat(
"type=",
value.TerminalTypeCode.ToString( CultureInfo.InvariantCulture ),
" firmware=",
value.FirmwareVersion.ToString( CultureInfo.InvariantCulture ),
" option=",
value.OptionCode.ToString( CultureInfo.InvariantCulture )
);
}
);
await ReportProbeAsync(
session,
"Device status",
async () => {
TerminalDeviceStatus value =
await session.QueryDeviceStatusAsync( probeTimeout );
return value.ToString();
}
);
await ReportProbeAsync(
session,
"Cursor position",
async () => {
TerminalCursorPosition value =
await session.QueryCursorPositionAsync( probeTimeout );
return string.Concat(
"row=",
value.Row.ToString( CultureInfo.InvariantCulture ),
" column=",
value.Column.ToString( CultureInfo.InvariantCulture ),
" (one-based)"
);
}
);
await ReportProbeAsync(
session,
"DECRQSS SGR",
async () => {
TerminalStatusStringResponse value = await session.QueryStatusStringAsync(
TerminalStatusStringKind.SelectGraphicRendition,
probeTimeout
);
return value.IsSupported
? string.Concat(
"supported status-string=\"",
value.StatusString,
"\""
)
: "unsupported"
;
}
);
await ReportProbeAsync(
session,
"XTGETTCAP TN",
async () => {
TerminalCapabilityObservation value =
await session.QueryLiveCapabilityAsync(
"TN",
probeTimeout
);
if ( !value.IsSupported ) {
return "unsupported";
}
IReadOnlyList<byte> bytes = value.ValueBytes
?? throw new InvalidOperationException(
"A supported live capability observation did not contain value bytes."
);
return string.Concat(
"supported value-hex=",
Convert.ToHexString( bytes.ToArray() )
);
}
);
await WriteLineAsync(
session,
string.Empty
);
await WriteLineAsync(
session,
"Generate one input or lifecycle event within 15 seconds to verify the unified event loop remains live."
);
TerminalEvent terminalEvent = await session.ReadEventAsync(
TimeSpan.FromSeconds( 15 )
);
await WriteLineAsync(
session,
FormatEvent( terminalEvent )
);
} finally {
if ( inputProtocols is not null ) {
await inputProtocols.DisposeAsync();
}
if ( presentation is not null ) {
await presentation.DisposeAsync();
}
}
return 0;
static async ValueTask ReportProbeAsync(
TerminalSession session,
string label,
Func<Task<string>> probe
) {
ArgumentNullException.ThrowIfNull( session );
ArgumentException.ThrowIfNullOrWhiteSpace( label );
ArgumentNullException.ThrowIfNull( probe );
try {
string result = await probe();
await WriteLineAsync(
session,
string.Concat(
label,
": ",
result
)
);
} catch ( TimeoutException ) {
await WriteLineAsync(
session,
string.Concat(
label,
": timed out"
)
);
} catch ( FormatException exception ) {
await WriteLineAsync(
session,
string.Concat(
label,
": malformed correlated response: ",
exception.Message
)
);
} catch ( InvalidOperationException exception ) {
await WriteLineAsync(
session,
string.Concat(
label,
": unavailable: ",
exception.Message
)
);
}
}
static string FormatEvent(
TerminalEvent terminalEvent
) {
ArgumentNullException.ThrowIfNull( terminalEvent );
switch ( terminalEvent.Kind ) {
case TerminalEventKind.Input:
TerminalInputEvent input = terminalEvent.Input
?? throw new InvalidOperationException(
"An Input event did not carry an input payload."
);
return string.Concat(
"Unified event loop: input kind=",
input.Kind.ToString()
);
case TerminalEventKind.Lifecycle:
TerminalLifecycleEvent lifecycle = terminalEvent.Lifecycle
?? throw new InvalidOperationException(
"A Lifecycle event did not carry a lifecycle payload."
);
return string.Concat(
"Unified event loop: lifecycle kind=",
lifecycle.Kind.ToString()
);
case TerminalEventKind.Timeout:
return "Unified event loop: no event before timeout.";
case TerminalEventKind.Cancelled:
return "Unified event loop: wait cancelled.";
default:
throw new InvalidOperationException(
$"Unexpected terminal event kind: {terminalEvent.Kind}."
);
}
}
static ValueTask WriteLineAsync(
TerminalSession session,
string text
) {
ArgumentNullException.ThrowIfNull( session );
ArgumentNullException.ThrowIfNull( text );
return session.WriteTextAsync(
string.Concat(
text,
"\r\n"
)
);
}