Skip to content

Commit

Permalink
Give AnsiResponseParser a chance to release its content each iteratio…
Browse files Browse the repository at this point in the history
…n if it has been some time since we saw Esc
  • Loading branch information
tznind committed Oct 12, 2024
1 parent c99487f commit dbfdffb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
19 changes: 18 additions & 1 deletion Terminal.Gui/ConsoleDrivers/AnsiResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,23 @@ public IEnumerable<Tuple<char, T>> ProcessInput (params Tuple<char, T> [] input)
return output;
}

public IEnumerable<Tuple<char, T>> Release ()
{
foreach (var h in held)
{
yield return h;
}
ResetState ();
}

public override void ClearHeld () => held.Clear ();

protected override string HeldToString () => new string (held.Select (h => h.Item1).ToArray ());

protected override void AddToHeld (char c) => held.Add (new Tuple<char, T> (c, default!));
}


}

internal class AnsiResponseParser : AnsiResponseParserBase
{
Expand All @@ -235,7 +246,13 @@ public string ProcessInput (string input)
ProcessInputBase (i => input [i], c => output.Append (c), input.Length);
return output.ToString ();
}
public string Release ()
{
var output = held.ToString ();
ResetState ();

return output;
}
public override void ClearHeld () => held.Clear ();

protected override string HeldToString () => held.ToString ();
Expand Down
28 changes: 28 additions & 0 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,11 @@ internal override MainLoop Init ()
}

private bool firstTime = true;

/// <summary>
/// How long after Esc has been pressed before we give up on getting an Ansi escape sequence
/// </summary>
private TimeSpan _escTimeout = TimeSpan.FromMilliseconds (50);
public AnsiResponseParser<WindowsConsole.InputRecord> Parser { get; set; } = new ();

internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
Expand Down Expand Up @@ -1566,13 +1571,31 @@ internal void ProcessInputAfterParsing (WindowsConsole.InputRecord inputEvent)

// TODO: keydown/keyup badness

foreach (var i in ShouldRelease ())
{
yield return i;
}

foreach (Tuple<char, WindowsConsole.InputRecord> output in
Parser.ProcessInput (Tuple.Create(inputEvent.KeyEvent.UnicodeChar,inputEvent)))
{
yield return output.Item2;
}
}

public IEnumerable<WindowsConsole.InputRecord> ShouldRelease ()
{

if (Parser.State == ParserState.ExpectingBracket &&
DateTime.Now - Parser.StateChangedAt > _escTimeout)
{
foreach (Tuple<char, WindowsConsole.InputRecord> output in Parser.Release ())
{
yield return output.Item2;
}
}
}

#if HACK_CHECK_WINCHANGED
private void ChangeWin (object s, SizeChangedEventArgs e)
{
Expand Down Expand Up @@ -2276,6 +2299,11 @@ bool IMainLoopDriver.EventsPending ()

void IMainLoopDriver.Iteration ()
{
foreach(var i in ((WindowsDriver)_consoleDriver).ShouldRelease())
{
((WindowsDriver)_consoleDriver).ProcessInput (i);
}

while (_resultQueue.Count > 0)
{
WindowsConsole.InputRecord [] inputRecords = _resultQueue.Dequeue ();
Expand Down
2 changes: 0 additions & 2 deletions UnitTests/ConsoleDrivers/AnsiResponseParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Terminal.Gui;
using Xunit.Abstractions;

namespace UnitTests.ConsoleDrivers;
Expand Down

0 comments on commit dbfdffb

Please sign in to comment.