ansi.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'dart:collection';
  2. import 'package:xterm/terminal/csi.dart';
  3. import 'package:xterm/terminal/osc.dart';
  4. import 'package:xterm/terminal/terminal.dart';
  5. import 'package:xterm/util/lookup_table.dart';
  6. /// Handler of terminal sequences. Returns true if the sequence is consumed,
  7. /// false to indicate that the sequence is not completed and no charater is
  8. /// consumed from the queue.
  9. typedef AnsiHandler = bool Function(Queue<int>, Terminal);
  10. bool ansiHandler(Queue<int> queue, Terminal terminal) {
  11. // The sequence isn't completed, just ignore it.
  12. if (queue.isEmpty) {
  13. return false;
  14. }
  15. final charAfterEsc = queue.removeFirst();
  16. final handler = _ansiHandlers[charAfterEsc];
  17. if (handler != null) {
  18. // if (handler != csiHandler && handler != oscHandler) {
  19. // terminal.debug.onEsc(charAfterEsc);
  20. // }
  21. final finished = handler(queue, terminal);
  22. if (!finished) {
  23. queue.addFirst(charAfterEsc);
  24. }
  25. return finished;
  26. }
  27. terminal.debug.onError('unsupported ansi sequence: $charAfterEsc');
  28. return true;
  29. }
  30. final _ansiHandlers = FastLookupTable({
  31. '['.codeUnitAt(0): csiHandler,
  32. ']'.codeUnitAt(0): oscHandler,
  33. '7'.codeUnitAt(0): _ansiSaveCursorHandler,
  34. '8'.codeUnitAt(0): _ansiRestoreCursorHandler,
  35. 'D'.codeUnitAt(0): _ansiIndexHandler,
  36. 'E'.codeUnitAt(0): _ansiNextLineHandler,
  37. 'H'.codeUnitAt(0): _ansiTabSetHandler,
  38. 'M'.codeUnitAt(0): _ansiReverseIndexHandler,
  39. 'P'.codeUnitAt(0): _unsupportedHandler, // Sixel
  40. 'c'.codeUnitAt(0): _unsupportedHandler,
  41. '#'.codeUnitAt(0): _unsupportedHandler,
  42. '('.codeUnitAt(0): _scsHandler(0), // SCS - G0
  43. ')'.codeUnitAt(0): _scsHandler(1), // SCS - G1
  44. '*'.codeUnitAt(0): _voidHandler(1), // TODO: G2 (vt220)
  45. '+'.codeUnitAt(0): _voidHandler(1), // TODO: G3 (vt220)
  46. '>'.codeUnitAt(0): _voidHandler(0), // TODO: Normal Keypad
  47. '='.codeUnitAt(0): _voidHandler(0), // TODO: Application Keypad
  48. });
  49. AnsiHandler _voidHandler(int sequenceLength) {
  50. return (queue, terminal) {
  51. if (queue.length < sequenceLength) {
  52. return false;
  53. }
  54. for (var i = 0; i < sequenceLength; i++) {
  55. queue.removeFirst();
  56. }
  57. return true;
  58. };
  59. }
  60. bool _unsupportedHandler(Queue<int> queue, Terminal terminal) {
  61. // print('unimplemented ansi sequence.');
  62. return true;
  63. }
  64. bool _ansiSaveCursorHandler(Queue<int> queue, Terminal terminal) {
  65. terminal.buffer.saveCursor();
  66. return true;
  67. }
  68. bool _ansiRestoreCursorHandler(Queue<int> queue, Terminal terminal) {
  69. terminal.buffer.restoreCursor();
  70. return true;
  71. }
  72. /// https://vt100.net/docs/vt100-ug/chapter3.html#IND IND – Index
  73. ///
  74. /// ESC D
  75. ///
  76. /// This sequence causes the active position to move downward one line without
  77. /// changing the column position. If the active position is at the bottom
  78. /// margin, a scroll up is performed.
  79. bool _ansiIndexHandler(Queue<int> queue, Terminal terminal) {
  80. terminal.buffer.index();
  81. return true;
  82. }
  83. bool _ansiReverseIndexHandler(Queue<int> queue, Terminal terminal) {
  84. terminal.buffer.reverseIndex();
  85. return true;
  86. }
  87. /// SCS – Select Character Set
  88. ///
  89. /// The appropriate G0 and G1 character sets are designated from one of the five
  90. /// possible character sets. The G0 and G1 sets are invoked by the codes SI and
  91. /// SO (shift in and shift out) respectively.
  92. AnsiHandler _scsHandler(int which) {
  93. return (Queue<int> queue, Terminal terminal) {
  94. // The sequence isn't completed, just ignore it.
  95. if (queue.isEmpty) {
  96. return false;
  97. }
  98. final name = queue.removeFirst();
  99. terminal.buffer.charset.designate(which, name);
  100. return true;
  101. };
  102. }
  103. bool _ansiNextLineHandler(Queue<int> queue, Terminal terminal) {
  104. terminal.buffer.newLine();
  105. terminal.buffer.setCursorX(0);
  106. return true;
  107. }
  108. bool _ansiTabSetHandler(Queue<int> queue, Terminal terminal) {
  109. terminal.tabSetAtCursor();
  110. return true;
  111. }