NetInputProcessor.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Input processor for <see cref="NetInput"/>, deals in <see cref="ConsoleKeyInfo"/> stream
  5. /// </summary>
  6. public class NetInputProcessor : InputProcessor<ConsoleKeyInfo>
  7. {
  8. #pragma warning disable CA2211
  9. /// <summary>
  10. /// Set to true to generate code in <see cref="Logging"/> (verbose only) for test cases in NetInputProcessorTests.
  11. /// <remarks>
  12. /// This makes the task of capturing user/language/terminal specific keyboard issues easier to
  13. /// diagnose. By turning this on and searching logs user can send us exactly the input codes that are released
  14. /// to input stream.
  15. /// </remarks>
  16. /// </summary>
  17. public static bool GenerateTestCasesForKeyPresses = false;
  18. #pragma warning restore CA2211
  19. /// <inheritdoc/>
  20. public NetInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer) : base (inputBuffer, new NetKeyConverter ()) { }
  21. /// <inheritdoc/>
  22. protected override void Process (ConsoleKeyInfo consoleKeyInfo)
  23. {
  24. // For building test cases
  25. if (GenerateTestCasesForKeyPresses)
  26. {
  27. Logging.Trace (FormatConsoleKeyInfoForTestCase (consoleKeyInfo));
  28. }
  29. foreach (Tuple<char, ConsoleKeyInfo> released in Parser.ProcessInput (Tuple.Create (consoleKeyInfo.KeyChar, consoleKeyInfo)))
  30. {
  31. ProcessAfterParsing (released.Item2);
  32. }
  33. }
  34. /// <inheritdoc/>
  35. protected override void ProcessAfterParsing (ConsoleKeyInfo input)
  36. {
  37. var key = KeyConverter.ToKey (input);
  38. OnKeyDown (key);
  39. OnKeyUp (key);
  40. }
  41. /* For building test cases */
  42. private static string FormatConsoleKeyInfoForTestCase (ConsoleKeyInfo input)
  43. {
  44. string charLiteral = input.KeyChar == '\0' ? @"'\0'" : $"'{input.KeyChar}'";
  45. return $"new ConsoleKeyInfo({charLiteral}, ConsoleKey.{input.Key}, "
  46. + $"{input.Modifiers.HasFlag (ConsoleModifiers.Shift).ToString ().ToLower ()}, "
  47. + $"{input.Modifiers.HasFlag (ConsoleModifiers.Alt).ToString ().ToLower ()}, "
  48. + $"{input.Modifiers.HasFlag (ConsoleModifiers.Control).ToString ().ToLower ()}),";
  49. }
  50. }