keytab.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'package:xterm/core/input/keys.dart';
  2. import 'package:xterm/core/input/keytab/keytab_default.dart';
  3. import 'package:xterm/core/input/keytab/keytab_parse.dart';
  4. import 'package:xterm/core/input/keytab/keytab_record.dart';
  5. import 'package:xterm/core/input/keytab/keytab_token.dart';
  6. class Keytab {
  7. Keytab({
  8. required this.name,
  9. required this.records,
  10. });
  11. factory Keytab.parse(String source) {
  12. final tokens = tokenize(source).toList();
  13. final parser = KeytabParser()..addTokens(tokens);
  14. return parser.result;
  15. }
  16. factory Keytab.defaultKeytab() {
  17. return Keytab.parse(kDefaultKeytab);
  18. }
  19. final String? name;
  20. final List<KeytabRecord> records;
  21. KeytabRecord? find(
  22. TerminalKey key, {
  23. bool ctrl = false,
  24. bool alt = false,
  25. bool shift = false,
  26. bool newLineMode = false,
  27. bool appCursorKeys = false,
  28. bool appKeyPad = false,
  29. bool appScreen = false,
  30. bool macos = false,
  31. // bool meta,
  32. }) {
  33. for (var record in records) {
  34. if (record.key != key) {
  35. continue;
  36. }
  37. if (record.anyModifier == true) {
  38. if (ctrl == false && alt == false && shift == false) {
  39. continue;
  40. }
  41. } else if (record.anyModifier == false) {
  42. if (ctrl != false || alt != false || shift != false) {
  43. continue;
  44. }
  45. } else {
  46. if (record.ctrl != null && record.ctrl != ctrl) {
  47. continue;
  48. }
  49. if (record.shift != null && record.shift != shift) {
  50. continue;
  51. }
  52. if (record.alt != null && record.alt != alt) {
  53. continue;
  54. }
  55. }
  56. if (record.newLine != null && record.newLine != newLineMode) {
  57. continue;
  58. }
  59. if (record.appCursorKeys != null &&
  60. record.appCursorKeys != appCursorKeys) {
  61. continue;
  62. }
  63. if (record.appKeyPad != null && record.appKeyPad != appKeyPad) {
  64. continue;
  65. }
  66. if (record.appScreen != null && record.appScreen != appScreen) {
  67. continue;
  68. }
  69. if (record.macos != null && record.macos != macos) {
  70. continue;
  71. }
  72. // TODO: support VT52
  73. if (record.ansi == false) {
  74. continue;
  75. }
  76. return record;
  77. }
  78. return null;
  79. }
  80. @override
  81. String toString() {
  82. final buffer = StringBuffer();
  83. buffer.writeln('keyboard "$name"');
  84. for (var record in records) {
  85. buffer.writeln(record);
  86. }
  87. return buffer.toString();
  88. }
  89. }