AnsiKeyboardParser.cs 955 B

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