Ss3Pattern.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #nullable enable
  2. using System.Text.RegularExpressions;
  3. namespace Terminal.Gui.Drivers;
  4. /// <summary>
  5. /// Parser for SS3 terminal escape sequences. These describe specific keys e.g.
  6. /// <c>EscOP</c> is F1.
  7. /// </summary>
  8. public class Ss3Pattern : AnsiKeyboardParserPattern
  9. {
  10. #pragma warning disable IDE1006 // Naming Styles
  11. private static readonly Regex _pattern = new (@"^\u001bO([PQRStDCABOHFwqysu])$");
  12. #pragma warning restore IDE1006 // Naming Styles
  13. /// <inheritdoc/>
  14. public override bool IsMatch (string? input) { return _pattern.IsMatch (input!); }
  15. /// <summary>
  16. /// Returns the ss3 key that corresponds to the provided input escape sequence
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <returns></returns>
  20. protected override Key? GetKeyImpl (string? input)
  21. {
  22. Match match = _pattern.Match (input!);
  23. if (!match.Success)
  24. {
  25. return null;
  26. }
  27. return match.Groups [1].Value.Single () switch
  28. {
  29. 'P' => Key.F1,
  30. 'Q' => Key.F2,
  31. 'R' => Key.F3,
  32. 'S' => Key.F4,
  33. 't' => Key.F5,
  34. 'D' => Key.CursorLeft,
  35. 'C' => Key.CursorRight,
  36. 'A' => Key.CursorUp,
  37. 'B' => Key.CursorDown,
  38. 'H' => Key.Home,
  39. 'F' => Key.End,
  40. 'w' => Key.Home,
  41. 'q' => Key.End,
  42. 'y' => Key.PageUp,
  43. 's' => Key.PageDown,
  44. 'u' => Key.Clear,
  45. _ => null
  46. };
  47. }
  48. }