AnsiKeyboardParser.cs 967 B

12345678910111213141516171819202122232425262728
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Parses ANSI escape sequence strings that describe keyboard activity into <see cref="Key"/>.
  5. /// </summary>
  6. public class AnsiKeyboardParser
  7. {
  8. private readonly List<AnsiKeyboardParserPattern> _patterns = new ()
  9. {
  10. new Ss3Pattern (),
  11. new CsiKeyPattern (),
  12. new CsiCursorPattern(),
  13. new EscAsAltPattern { IsLastMinute = true }
  14. };
  15. /// <summary>
  16. /// Looks for any pattern that matches the <paramref name="input"/> and returns
  17. /// the matching pattern or <see langword="null"/> if no matches.
  18. /// </summary>
  19. /// <param name="input"></param>
  20. /// <param name="isLastMinute"></param>
  21. /// <returns></returns>
  22. public AnsiKeyboardParserPattern? IsKeyboard (string? input, bool isLastMinute = false)
  23. {
  24. return _patterns.FirstOrDefault (pattern => pattern.IsLastMinute == isLastMinute && pattern.IsMatch (input));
  25. }
  26. }