Ss3Pattern.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #nullable enable
  2. using System.Text.RegularExpressions;
  3. namespace Terminal.Gui;
  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([PQRStDCAB])$");
  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. _ => null
  39. };
  40. }
  41. }