| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using Microsoft.Extensions.Logging;
- namespace Terminal.Gui.Drivers;
- /// <summary>
- /// Console input implementation that uses native dotnet methods e.g. <see cref="System.Console"/>.
- /// </summary>
- public class NetInput : ConsoleInput<ConsoleKeyInfo>, INetInput
- {
- private readonly NetWinVTConsole _adjustConsole;
- /// <summary>
- /// Creates a new instance of the class. Implicitly sends
- /// console mode settings that enable virtual input (mouse
- /// reporting etc).
- /// </summary>
- public NetInput ()
- {
- Logging.Logger.LogInformation ($"Creating {nameof (NetInput)}");
- if (ConsoleDriver.RunningUnitTests)
- {
- return;
- }
- PlatformID p = Environment.OSVersion.Platform;
- if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
- {
- try
- {
- _adjustConsole = new ();
- }
- catch (ApplicationException ex)
- {
- // Likely running as a unit test, or in a non-interactive session.
- Logging.Logger.LogCritical (
- ex,
- "NetWinVTConsole could not be constructed i.e. could not configure terminal modes. May indicate running in non-interactive session e.g. unit testing CI");
- }
- }
- //Enable alternative screen buffer.
- Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
- //Set cursor key to application.
- Console.Out.Write (EscSeqUtils.CSI_HideCursor);
- Console.Out.Write (EscSeqUtils.CSI_EnableMouseEvents);
- Console.TreatControlCAsInput = true;
- }
- /// <inheritdoc/>
- protected override bool Peek ()
- {
- if (ConsoleDriver.RunningUnitTests)
- {
- return false;
- }
- return Console.KeyAvailable;
- }
- /// <inheritdoc/>
- protected override IEnumerable<ConsoleKeyInfo> Read ()
- {
- while (Console.KeyAvailable)
- {
- yield return Console.ReadKey (true);
- }
- }
- private void FlushConsoleInput ()
- {
- if (!ConsoleDriver.RunningUnitTests)
- {
- while (Console.KeyAvailable)
- {
- Console.ReadKey (intercept: true);
- }
- }
- }
- /// <inheritdoc/>
- public override void Dispose ()
- {
- base.Dispose ();
- // Disable mouse events first
- Console.Out.Write (EscSeqUtils.CSI_DisableMouseEvents);
- //Disable alternative screen buffer.
- Console.Out.Write (EscSeqUtils.CSI_RestoreCursorAndRestoreAltBufferWithBackscroll);
- //Set cursor key to cursor.
- Console.Out.Write (EscSeqUtils.CSI_ShowCursor);
- _adjustConsole?.Cleanup ();
- // Flush any pending input so no stray events appear
- FlushConsoleInput ();
- }
- }
|