@@ -6,12 +6,32 @@ namespace Terminal.Gui;
6
6
7
7
internal abstract class AnsiResponseParserBase : IAnsiResponseParser
8
8
{
9
- private const char Escape = '\x1B ' ;
9
+ private const char ESCAPE = '\x1B ' ;
10
10
private readonly AnsiMouseParser _mouseParser = new ( ) ;
11
+ #pragma warning disable IDE1006 // Naming Styles
11
12
protected readonly AnsiKeyboardParser _keyboardParser = new ( ) ;
12
13
protected object _lockExpectedResponses = new ( ) ;
13
14
14
15
protected object _lockState = new ( ) ;
16
+ protected readonly IHeld _heldContent ;
17
+
18
+ /// <summary>
19
+ /// Responses we are expecting to come in.
20
+ /// </summary>
21
+ protected readonly List < AnsiResponseExpectation > _expectedResponses = [ ] ;
22
+
23
+ /// <summary>
24
+ /// Collection of responses that we <see cref="StopExpecting"/>.
25
+ /// </summary>
26
+ protected readonly List < AnsiResponseExpectation > _lateResponses = [ ] ;
27
+
28
+ /// <summary>
29
+ /// Responses that you want to look out for that will come in continuously e.g. mouse events.
30
+ /// Key is the terminator.
31
+ /// </summary>
32
+ protected readonly List < AnsiResponseExpectation > _persistentExpectations = [ ] ;
33
+
34
+ #pragma warning restore IDE1006 // Naming Styles
15
35
16
36
/// <summary>
17
37
/// Event raised when mouse events are detected - requires setting <see cref="HandleMouse"/> to true
@@ -35,22 +55,6 @@ internal abstract class AnsiResponseParserBase : IAnsiResponseParser
35
55
/// </summary>
36
56
public bool HandleKeyboard { get ; set ; } = false ;
37
57
38
- /// <summary>
39
- /// Responses we are expecting to come in.
40
- /// </summary>
41
- protected readonly List < AnsiResponseExpectation > _expectedResponses = [ ] ;
42
-
43
- /// <summary>
44
- /// Collection of responses that we <see cref="StopExpecting"/>.
45
- /// </summary>
46
- protected readonly List < AnsiResponseExpectation > _lateResponses = [ ] ;
47
-
48
- /// <summary>
49
- /// Responses that you want to look out for that will come in continuously e.g. mouse events.
50
- /// Key is the terminator.
51
- /// </summary>
52
- protected readonly List < AnsiResponseExpectation > _persistentExpectations = [ ] ;
53
-
54
58
private AnsiResponseParserState _state = AnsiResponseParserState . Normal ;
55
59
56
60
/// <inheritdoc/>
@@ -64,8 +68,6 @@ protected set
64
68
}
65
69
}
66
70
67
- protected readonly IHeld _heldContent ;
68
-
69
71
/// <summary>
70
72
/// When <see cref="State"/> was last changed.
71
73
/// </summary>
@@ -74,17 +76,17 @@ protected set
74
76
// These all are valid terminators on ansi responses,
75
77
// see CSI in https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s
76
78
// No - N or O
77
- protected readonly HashSet < char > _knownTerminators = new (
78
- [
79
- '@' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
80
-
81
- // No - N or O
82
- 'P' , 'Q' , 'R' , 'S' , 'T' , 'W' , 'X' , 'Z' ,
83
- '^' , '`' , '~' ,
84
- 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ,
85
- 'l' , 'm' , 'n' ,
86
- 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
87
- ] ) ;
79
+ protected readonly HashSet < char > _knownTerminators =
80
+ [
81
+ '@' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
82
+
83
+ // No - N or O
84
+ 'P' , 'Q' , 'R' , 'S' , 'T' , 'W' , 'X' , 'Z' ,
85
+ '^' , '`' , '~' ,
86
+ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ,
87
+ 'l' , 'm' , 'n' ,
88
+ 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
89
+ ] ;
88
90
89
91
protected AnsiResponseParserBase ( IHeld heldContent ) { _heldContent = heldContent ; }
90
92
@@ -137,7 +139,7 @@ int inputLength
137
139
char currentChar = getCharAtIndex ( index ) ;
138
140
object currentObj = getObjectAtIndex ( index ) ;
139
141
140
- bool isEscape = currentChar == Escape ;
142
+ bool isEscape = currentChar == ESCAPE ;
141
143
142
144
switch ( State )
143
145
{
@@ -233,7 +235,7 @@ protected void TryLastMinuteSequences ()
233
235
{
234
236
lock ( _lockState )
235
237
{
236
- string cur = _heldContent . HeldToString ( ) ;
238
+ string ? cur = _heldContent . HeldToString ( ) ;
237
239
238
240
if ( HandleKeyboard )
239
241
{
@@ -250,7 +252,7 @@ protected void TryLastMinuteSequences ()
250
252
251
253
// We have something totally unexpected, not a CSI and
252
254
// still Esc+<something>. So give last minute swallow chance
253
- if ( cur . Length >= 2 && cur [ 0 ] == Escape )
255
+ if ( cur ! . Length >= 2 && cur [ 0 ] == ESCAPE )
254
256
{
255
257
// Maybe swallow anyway if user has custom delegate
256
258
bool swallow = ShouldSwallowUnexpectedResponse ( ) ;
@@ -270,7 +272,7 @@ protected bool ShouldReleaseHeldContent ()
270
272
{
271
273
lock ( _lockState )
272
274
{
273
- string cur = _heldContent . HeldToString ( ) ;
275
+ string ? cur = _heldContent . HeldToString ( ) ;
274
276
275
277
if ( HandleMouse && IsMouse ( cur ) )
276
278
{
@@ -328,7 +330,7 @@ protected bool ShouldReleaseHeldContent ()
328
330
329
331
// Finally if it is a valid ansi response but not one we are expect (e.g. its mouse activity)
330
332
// then we can release it back to input processing stream
331
- if ( _knownTerminators . Contains ( cur . Last ( ) ) && cur . StartsWith ( EscSeqUtils . CSI ) )
333
+ if ( _knownTerminators . Contains ( cur ! . Last ( ) ) && cur ! . StartsWith ( EscSeqUtils . CSI ) )
332
334
{
333
335
// We have found a terminator so bail
334
336
State = AnsiResponseParserState . Normal ;
@@ -354,7 +356,7 @@ protected bool ShouldReleaseHeldContent ()
354
356
return false ; // Continue accumulating
355
357
}
356
358
357
- private void RaiseMouseEvent ( string cur )
359
+ private void RaiseMouseEvent ( string ? cur )
358
360
{
359
361
MouseEventArgs ? ev = _mouseParser . ProcessMouseInput ( cur ) ;
360
362
@@ -364,9 +366,9 @@ private void RaiseMouseEvent (string cur)
364
366
}
365
367
}
366
368
367
- private bool IsMouse ( string cur ) { return _mouseParser . IsMouse ( cur ) ; }
369
+ private bool IsMouse ( string ? cur ) { return _mouseParser . IsMouse ( cur ) ; }
368
370
369
- protected void RaiseKeyboardEvent ( AnsiKeyboardParserPattern pattern , string cur )
371
+ protected void RaiseKeyboardEvent ( AnsiKeyboardParserPattern pattern , string ? cur )
370
372
{
371
373
Key ? k = pattern . GetKey ( cur ) ;
372
374
@@ -394,7 +396,7 @@ protected void RaiseKeyboardEvent (AnsiKeyboardParserPattern pattern, string cur
394
396
/// <returns></returns>
395
397
protected abstract bool ShouldSwallowUnexpectedResponse ( ) ;
396
398
397
- private bool MatchResponse ( string cur , List < AnsiResponseExpectation > collection , bool invokeCallback , bool removeExpectation )
399
+ private bool MatchResponse ( string ? cur , List < AnsiResponseExpectation > collection , bool invokeCallback , bool removeExpectation )
398
400
{
399
401
// Check for expected responses
400
402
AnsiResponseExpectation ? matchingResponse = collection . FirstOrDefault ( r => r . Matches ( cur ) ) ;
@@ -422,7 +424,7 @@ private bool MatchResponse (string cur, List<AnsiResponseExpectation> collection
422
424
}
423
425
424
426
/// <inheritdoc/>
425
- public void ExpectResponse ( string terminator , Action < string > response , Action ? abandoned , bool persistent )
427
+ public void ExpectResponse ( string ? terminator , Action < string ? > response , Action ? abandoned , bool persistent )
426
428
{
427
429
lock ( _lockExpectedResponses )
428
430
{
@@ -438,17 +440,17 @@ public void ExpectResponse (string terminator, Action<string> response, Action?
438
440
}
439
441
440
442
/// <inheritdoc/>
441
- public bool IsExpecting ( string terminator )
443
+ public bool IsExpecting ( string ? terminator )
442
444
{
443
445
lock ( _lockExpectedResponses )
444
446
{
445
447
// If any of the new terminator matches any existing terminators characters it's a collision so true.
446
- return _expectedResponses . Any ( r => r . Terminator . Intersect ( terminator ) . Any ( ) ) ;
448
+ return _expectedResponses . Any ( r => r . Terminator ! . Intersect ( terminator ! ) . Any ( ) ) ;
447
449
}
448
450
}
449
451
450
452
/// <inheritdoc/>
451
- public void StopExpecting ( string terminator , bool persistent )
453
+ public void StopExpecting ( string ? terminator , bool persistent )
452
454
{
453
455
lock ( _lockExpectedResponses )
454
456
{
@@ -530,7 +532,7 @@ public Tuple<char, T> [] Release ()
530
532
/// <param name="response"></param>
531
533
/// <param name="abandoned"></param>
532
534
/// <param name="persistent"></param>
533
- public void ExpectResponseT ( string terminator , Action < IEnumerable < Tuple < char , T > > > response , Action ? abandoned , bool persistent )
535
+ public void ExpectResponseT ( string ? terminator , Action < IEnumerable < Tuple < char , T > > > response , Action ? abandoned , bool persistent )
534
536
{
535
537
lock ( _lockExpectedResponses )
536
538
{
@@ -562,7 +564,7 @@ internal class AnsiResponseParser () : AnsiResponseParserBase (new StringHeld ()
562
564
/// keystrokes 'swallowed' (i.e. not returned to input stream).
563
565
/// </para>
564
566
/// </summary>
565
- public Func < string , bool > UnknownResponseHandler { get ; set ; } = _ => false ;
567
+ public Func < string ? , bool > UnknownResponseHandler { get ; set ; } = _ => false ;
566
568
567
569
public string ProcessInput ( string input )
568
570
{
@@ -583,13 +585,13 @@ private void AppendOutput (StringBuilder output, char c)
583
585
output . Append ( c ) ;
584
586
}
585
587
586
- public string Release ( )
588
+ public string ? Release ( )
587
589
{
588
590
lock ( _lockState )
589
591
{
590
592
TryLastMinuteSequences ( ) ;
591
593
592
- string output = _heldContent . HeldToString ( ) ;
594
+ string ? output = _heldContent . HeldToString ( ) ;
593
595
ResetState ( ) ;
594
596
595
597
return output ;
0 commit comments