Ss3Pattern.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. private static readonly Regex _pattern = new (@"^\u001bO([PQRStDCAB])$");
  11. /// <inheritdoc/>
  12. public override bool IsMatch (string input) { return _pattern.IsMatch (input); }
  13. /// <summary>
  14. /// Returns the ss3 key that corresponds to the provided input escape sequence
  15. /// </summary>
  16. /// <param name="input"></param>
  17. /// <returns></returns>
  18. protected override Key? GetKeyImpl (string input)
  19. {
  20. Match match = _pattern.Match (input);
  21. if (!match.Success)
  22. {
  23. return null;
  24. }
  25. return match.Groups [1].Value.Single () switch
  26. {
  27. 'P' => Key.F1,
  28. 'Q' => Key.F2,
  29. 'R' => Key.F3,
  30. 'S' => Key.F4,
  31. 't' => Key.F5,
  32. 'D' => Key.CursorLeft,
  33. 'C' => Key.CursorRight,
  34. 'A' => Key.CursorUp,
  35. 'B' => Key.CursorDown,
  36. _ => null
  37. };
  38. }
  39. }