AnsiKeyboardParserPattern.cs 1.8 KB

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