AnsiKeyboardParserPattern.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Base class for ANSI keyboard parsing patterns.
  5. /// </summary>
  6. public abstract class AnsiKeyboardParserPattern
  7. {
  8. /// <summary>
  9. /// Does this pattern dangerously overlap with other sequences
  10. /// such that it should only be applied at the lsat second after
  11. /// all other sequences have been tried.
  12. /// <remarks>
  13. /// When <see langword="true"/> this pattern will only be used
  14. /// at <see cref="AnsiResponseParser.Release"/> time.
  15. /// </remarks>
  16. /// </summary>
  17. public bool IsLastMinute { get; set; }
  18. /// <summary>
  19. /// Returns <see langword="true"/> if <paramref name="input"/> is one
  20. /// of the terminal sequences recognised by this class.
  21. /// </summary>
  22. /// <param name="input"></param>
  23. /// <returns></returns>
  24. public abstract bool IsMatch (string? input);
  25. private readonly string _name;
  26. /// <summary>
  27. /// Creates a new instance of the class.
  28. /// </summary>
  29. protected AnsiKeyboardParserPattern () { _name = GetType ().Name; }
  30. /// <summary>
  31. /// Returns the <see cref="Key"/> described by the escape sequence.
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <returns></returns>
  35. public Key? GetKey (string? input)
  36. {
  37. Key? key = GetKeyImpl (input);
  38. Logging.Trace ($"{nameof (AnsiKeyboardParser)} interpreted {input} as {key} using {_name}");
  39. return key;
  40. }
  41. /// <summary>
  42. /// When overriden in a derived class, returns the <see cref="Key"/>
  43. /// that matches the input ansi escape sequence.
  44. /// </summary>
  45. /// <param name="input"></param>
  46. /// <returns></returns>
  47. protected abstract Key? GetKeyImpl (string? input);
  48. }