Remove unneeded abstract ConsoleDriver objects.

This commit is contained in:
BDisp
2024-10-31 19:10:46 +00:00
parent ee0f93d543
commit c1063a00fe
5 changed files with 24 additions and 85 deletions

View File

@@ -61,23 +61,14 @@ public class AnsiEscapeSequenceRequest
public static bool TryExecuteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest, out AnsiEscapeSequenceResponse result)
{
var error = new StringBuilder ();
var savedIsReportingMouseMoves = false;
ConsoleDriver? driver = null;
var values = new string? [] { null };
try
{
driver = Application.Driver;
savedIsReportingMouseMoves = driver!.IsReportingMouseMoves;
if (savedIsReportingMouseMoves)
{
driver.StopReportingMouseMoves ();
}
ConsoleDriver? driver = Application.Driver;
// Send the ANSI escape sequence
ansiRequest.Response = driver.WriteAnsiRequest (ansiRequest);
ansiRequest.Response = driver?.WriteAnsiRequest (ansiRequest)!;
if (!string.IsNullOrEmpty (ansiRequest.Response) && !ansiRequest.Response.StartsWith (EscSeqUtils.KeyEsc))
{
@@ -104,12 +95,7 @@ public class AnsiEscapeSequenceRequest
{
if (string.IsNullOrEmpty (error.ToString ()))
{
(string? c1Control, string? code, values, string? terminator) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
}
if (savedIsReportingMouseMoves)
{
driver?.StartReportingMouseMoves ();
(string? _, string? _, values, string? _) = EscSeqUtils.GetEscapeResult (ansiRequest.Response.ToCharArray ());
}
}

View File

@@ -144,7 +144,7 @@ public abstract class ConsoleDriver
// are correctly combined with the base char, but are ALSO treated as 1 column
// width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
//
// Until this is addressed (see Issue #), we do our best by
// Until this is addressed (see Issue #), we do our best by
// a) Attempting to normalize any CM with the base char to it's left
// b) Ignoring any CMs that don't normalize
if (Col > 0)
@@ -167,7 +167,7 @@ public abstract class ConsoleDriver
if (normalized.Length == 1)
{
// It normalized! We can just set the Cell to the left with the
// normalized codepoint
// normalized codepoint
Contents [Row, Col - 1].Rune = (Rune)normalized [0];
// Ignore. Don't move to next column because we're already there
@@ -377,7 +377,7 @@ public abstract class ConsoleDriver
{
Contents [r, c] = new Cell
{
Rune = (rune != default ? rune : (Rune)' '),
Rune = rune != default ? rune : (Rune)' ',
Attribute = CurrentAttribute, IsDirty = true
};
_dirtyLines! [r] = true;
@@ -562,11 +562,6 @@ public abstract class ConsoleDriver
#region Mouse and Keyboard
/// <summary>
/// Gets whether the mouse is reporting move events.
/// </summary>
public abstract bool IsReportingMouseMoves { get; internal set; }
/// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="KeyUp"/>.</summary>
public event EventHandler<Key>? KeyDown;
@@ -613,16 +608,6 @@ public abstract class ConsoleDriver
/// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
/// <summary>
/// Provide handling for the terminal start reporting mouse events.
/// </summary>
public abstract void StartReportingMouseMoves ();
/// <summary>
/// Provide handling for the terminal stop reporting mouse events.
/// </summary>
public abstract void StopReportingMouseMoves ();
/// <summary>
/// Provide handling for the terminal write ANSI escape sequence request.
/// </summary>
@@ -630,22 +615,29 @@ public abstract class ConsoleDriver
/// <returns>The request response.</returns>
public abstract string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest);
/// <summary>
/// Provide proper writing to send escape sequence recognized by the <see cref="ConsoleDriver"/>.
/// </summary>
/// <param name="ansi"></param>
public abstract void WriteRaw (string ansi);
internal string ReadAnsiResponseDefault (AnsiEscapeSequenceRequest ansiRequest)
{
var response = new StringBuilder ();
int index = 0;
var index = 0;
while (Console.KeyAvailable)
{
// Peek the next key
ConsoleKeyInfo keyInfo = Console.ReadKey (true); // true to not display on the console
if ((index == 0 && keyInfo.KeyChar == EscSeqUtils.KeyEsc) || (index > 0 && keyInfo.KeyChar != EscSeqUtils.KeyEsc))
if (index == 0 && keyInfo.KeyChar != EscSeqUtils.KeyEsc)
{
// Append the current key to the response
response.Append (keyInfo.KeyChar);
break;
}
response.Append (keyInfo.KeyChar);
// Read until no key is available if no terminator was specified or
// check if the key is terminator (ANSI escape sequence ends)
if (!string.IsNullOrEmpty (ansiRequest.Terminator) && keyInfo.KeyChar == ansiRequest.Terminator [^1])

View File

@@ -177,33 +177,19 @@ internal class CursesDriver : ConsoleDriver
return true;
}
public override bool IsReportingMouseMoves { get; internal set; }
public override void StartReportingMouseMoves ()
public void StartReportingMouseMoves ()
{
if (!RunningUnitTests)
{
Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
IsReportingMouseMoves = true;
}
}
public override void StopReportingMouseMoves ()
public void StopReportingMouseMoves ()
{
if (!RunningUnitTests)
{
Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
IsReportingMouseMoves = false;
Thread.Sleep (100); // Allow time for mouse stopping and to flush the input buffer
// Flush the input buffer to avoid reading stale input
while (Console.KeyAvailable)
{
Console.ReadKey (true);
}
}
}

View File

@@ -40,9 +40,6 @@ public class FakeDriver : ConsoleDriver
public static Behaviors FakeBehaviors = new ();
public override bool SupportsTrueColor => false;
/// <inheritdoc />
public override bool IsReportingMouseMoves { get; internal set; }
public FakeDriver ()
{
Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
@@ -395,15 +392,12 @@ public class FakeDriver : ConsoleDriver
MockKeyPressedHandler (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
}
/// <inheritdoc />
public override void StartReportingMouseMoves () { throw new NotImplementedException (); }
/// <inheritdoc />
public override void StopReportingMouseMoves () { throw new NotImplementedException (); }
/// <inheritdoc />
public override string WriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest) { throw new NotImplementedException (); }
/// <inheritdoc />
public override void WriteRaw (string ansi) { throw new NotImplementedException (); }
public void SetBufferSize (int width, int height)
{
FakeConsole.SetBufferSize (width, height);

View File

@@ -1414,38 +1414,19 @@ internal class NetDriver : ConsoleDriver
#region Mouse Handling
public override bool IsReportingMouseMoves { get; internal set; }
public override void StartReportingMouseMoves ()
public void StartReportingMouseMoves ()
{
if (!RunningUnitTests)
{
Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
IsReportingMouseMoves = true;
}
}
public override void StopReportingMouseMoves ()
public void StopReportingMouseMoves ()
{
if (!RunningUnitTests)
{
Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
IsReportingMouseMoves = false;
while (_mainLoopDriver is { _netEvents: { } } && Console.KeyAvailable)
{
_mainLoopDriver._netEvents._waitForStart.Set ();
_mainLoopDriver._netEvents._waitForStart.Reset ();
_mainLoopDriver._netEvents._forceRead = true;
}
if (_mainLoopDriver is { _netEvents: { } })
{
_mainLoopDriver._netEvents._forceRead = false;
}
}
}