csi.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import 'dart:collection';
  2. import 'package:xterm/terminal/modes.dart';
  3. import 'package:xterm/terminal/sgr.dart';
  4. import 'package:xterm/terminal/terminal.dart';
  5. import 'package:xterm/utli/lookup_table.dart';
  6. typedef CsiSequenceHandler = void Function(CSI, Terminal);
  7. final _csiHandlers = FastLookupTable({
  8. 'c'.codeUnitAt(0): csiSendDeviceAttributesHandler,
  9. 'd'.codeUnitAt(0): csiLinePositionAbsolute,
  10. 'f'.codeUnitAt(0): csiCursorPositionHandler,
  11. 'g'.codeUnitAt(0): csiTabClearHandler,
  12. 'h'.codeUnitAt(0): csiModeHandler, // SM - Set Mode
  13. 'l'.codeUnitAt(0): csiModeHandler, // RM - Reset Mode
  14. 'm'.codeUnitAt(0): sgrHandler,
  15. 'n'.codeUnitAt(0): csiDeviceStatusReportHandler,
  16. 'r'.codeUnitAt(0): csiSetMarginsHandler, // DECSTBM
  17. 't'.codeUnitAt(0): csiWindowManipulation,
  18. 'A'.codeUnitAt(0): csiCursorUpHandler,
  19. 'B'.codeUnitAt(0): csiCursorDownHandler,
  20. 'C'.codeUnitAt(0): csiCursorForwardHandler,
  21. 'D'.codeUnitAt(0): csiCursorBackwardHandler,
  22. 'E'.codeUnitAt(0): csiCursorNextLineHandler,
  23. 'F'.codeUnitAt(0): csiCursorPrecedingLineHandler,
  24. 'G'.codeUnitAt(0): csiCursorHorizontalAbsoluteHandler,
  25. 'H'.codeUnitAt(0): csiCursorPositionHandler, // CUP - Cursor Position
  26. 'J'.codeUnitAt(0): csiEraseInDisplayHandler, // DECSED - Selective Erase
  27. 'K'.codeUnitAt(0): csiEraseInLineHandler,
  28. 'L'.codeUnitAt(0): csiInsertLinesHandler,
  29. 'M'.codeUnitAt(0): csiDeleteLinesHandler,
  30. 'P'.codeUnitAt(0): csiDeleteHandler,
  31. 'S'.codeUnitAt(0): csiScrollUpHandler,
  32. 'T'.codeUnitAt(0): csiScrollDownHandler,
  33. 'X'.codeUnitAt(0): csiEraseCharactersHandler,
  34. '@'.codeUnitAt(0): csiInsertBlankCharactersHandler,
  35. });
  36. class CSI {
  37. CSI({
  38. required this.params,
  39. required this.finalByte,
  40. required this.intermediates,
  41. });
  42. final List<String> params;
  43. final int finalByte;
  44. final List<int> intermediates;
  45. @override
  46. String toString() {
  47. return params.join(';') + String.fromCharCode(finalByte);
  48. }
  49. }
  50. /// Parse a CSI from the head of the queue. Return null if the CSI isn't
  51. /// complete.
  52. CSI? _parseCsi(Queue<int> queue) {
  53. final paramBuffer = StringBuffer();
  54. final intermediates = <int>[];
  55. // Keep track of how many characters should be taken from the queue.
  56. var readOffset = 0;
  57. while (true) {
  58. // The sequence isn't completed, just ignore it.
  59. if (queue.length <= readOffset) {
  60. return null;
  61. }
  62. // final char = queue.removeFirst();
  63. final char = queue.elementAt(readOffset++);
  64. if (char >= 0x30 && char <= 0x3F) {
  65. paramBuffer.writeCharCode(char);
  66. continue;
  67. }
  68. if (char > 0 && char <= 0x2F) {
  69. intermediates.add(char);
  70. continue;
  71. }
  72. const csiMin = 0x40;
  73. const csiMax = 0x7e;
  74. if (char >= csiMin && char <= csiMax) {
  75. // The sequence is complete. So we consume it from the queue.
  76. for (var i = 0; i < readOffset; i++) {
  77. queue.removeFirst();
  78. }
  79. final params = paramBuffer.toString().split(';');
  80. return CSI(
  81. params: params,
  82. finalByte: char,
  83. intermediates: intermediates,
  84. );
  85. }
  86. }
  87. }
  88. /// CSI - Control Sequence Introducer: sequence starting with ESC [ (7bit) or
  89. /// CSI (\x9B, 8bit)
  90. bool csiHandler(Queue<int> queue, Terminal terminal) {
  91. final csi = _parseCsi(queue);
  92. if (csi == null) {
  93. return false;
  94. }
  95. terminal.debug.onCsi(csi);
  96. final handler = _csiHandlers[csi.finalByte];
  97. if (handler != null) {
  98. handler(csi, terminal);
  99. } else {
  100. terminal.debug.onError('unknown: $csi');
  101. }
  102. return true;
  103. }
  104. /// DECSED - Selective Erase In Display
  105. ///
  106. /// ```text
  107. /// CSI ? P s J
  108. ///
  109. /// Erase in Display (DECSED)
  110. ///
  111. /// P s = 0 → Selective Erase Below (default)
  112. /// P s = 1 → Selective Erase Above
  113. /// P s = 2 → Selective Erase All
  114. /// ```
  115. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  116. var ps = '0';
  117. if (csi.params.isNotEmpty) {
  118. ps = csi.params.first;
  119. }
  120. switch (ps) {
  121. case '':
  122. case '0':
  123. terminal.buffer.eraseDisplayFromCursor();
  124. break;
  125. case '1':
  126. terminal.buffer.eraseDisplayToCursor();
  127. break;
  128. case '2':
  129. case '3':
  130. terminal.buffer.eraseDisplay();
  131. break;
  132. default:
  133. terminal.debug.onError("Unsupported ED: CSI $ps J");
  134. }
  135. }
  136. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  137. var ps = '0';
  138. if (csi.params.isNotEmpty) {
  139. ps = csi.params.first;
  140. }
  141. switch (ps) {
  142. case '':
  143. case '0':
  144. terminal.buffer.eraseLineFromCursor();
  145. break;
  146. case '1':
  147. terminal.buffer.eraseLineToCursor();
  148. break;
  149. case '2':
  150. terminal.buffer.eraseLine();
  151. break;
  152. default:
  153. terminal.debug.onError("Unsupported EL: CSI $ps K");
  154. }
  155. }
  156. /// CUP - Cursor Position
  157. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  158. var x = 1;
  159. var y = 1;
  160. if (csi.params.length == 2) {
  161. y = int.tryParse(csi.params[0]) ?? x;
  162. x = int.tryParse(csi.params[1]) ?? y;
  163. }
  164. terminal.buffer.setPosition(x - 1, y - 1);
  165. }
  166. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  167. var row = 1;
  168. if (csi.params.isNotEmpty) {
  169. row = int.tryParse(csi.params.first) ?? row;
  170. }
  171. terminal.buffer.setCursorY(row - 1);
  172. }
  173. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  174. var x = 1;
  175. if (csi.params.isNotEmpty) {
  176. x = int.tryParse(csi.params.first) ?? x;
  177. }
  178. terminal.buffer.setCursorX(x - 1);
  179. }
  180. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  181. var offset = 1;
  182. if (csi.params.isNotEmpty) {
  183. offset = int.tryParse(csi.params.first) ?? offset;
  184. }
  185. terminal.buffer.movePosition(offset, 0);
  186. }
  187. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  188. var offset = 1;
  189. if (csi.params.isNotEmpty) {
  190. offset = int.tryParse(csi.params.first) ?? offset;
  191. }
  192. terminal.buffer.movePosition(-offset, 0);
  193. }
  194. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  195. var count = 1;
  196. if (csi.params.isNotEmpty) {
  197. count = int.tryParse(csi.params.first) ?? count;
  198. }
  199. terminal.buffer.eraseCharacters(count);
  200. }
  201. void csiModeHandler(CSI csi, Terminal terminal) {
  202. // terminal.ActiveBuffer().ClearSelection()
  203. return csiSetModes(csi, terminal);
  204. }
  205. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  206. if (csi.params.isEmpty) return;
  207. switch (csi.params[0]) {
  208. case "5":
  209. terminal.onInput("\x1b[0n");
  210. break;
  211. case "6": // report cursor position
  212. terminal.onInput("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  213. break;
  214. default:
  215. terminal.debug
  216. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  217. return;
  218. }
  219. }
  220. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  221. var response = '?1;2';
  222. if (csi.params.isNotEmpty && csi.params.first.startsWith('>')) {
  223. response = '>0;0;0';
  224. }
  225. terminal.onInput('\x1b[${response}c');
  226. }
  227. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  228. var distance = 1;
  229. if (csi.params.isNotEmpty) {
  230. distance = int.tryParse(csi.params.first) ?? distance;
  231. }
  232. terminal.buffer.movePosition(0, -distance);
  233. }
  234. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  235. var distance = 1;
  236. if (csi.params.isNotEmpty) {
  237. distance = int.tryParse(csi.params.first) ?? distance;
  238. }
  239. terminal.buffer.movePosition(0, distance);
  240. }
  241. /// DECSTBM – Set Top and Bottom Margins (DEC Private)
  242. ///
  243. /// ESC [ Pn; Pn r
  244. ///
  245. /// This sequence sets the top and bottom margins to define the scrolling
  246. /// region. The first parameter is the line number of the first line in the
  247. /// scrolling region; the second parameter is the line number of the bottom line
  248. /// in the scrolling region. Default is the en tire screen (no margins). The
  249. /// minimum size of the scrolling region allowed is two lines, i.e., the top
  250. /// margin must be less than the bottom margin. The cursor is placed in the home
  251. /// position (see Origin Mode DECOM).
  252. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  253. var top = 1;
  254. var bottom = terminal.viewHeight;
  255. if (csi.params.length > 2) {
  256. return;
  257. }
  258. if (csi.params.isNotEmpty) {
  259. top = int.tryParse(csi.params[0]) ?? top;
  260. if (csi.params.length > 1) {
  261. bottom = int.tryParse(csi.params[1]) ?? bottom;
  262. }
  263. }
  264. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  265. terminal.buffer.setPosition(0, 0);
  266. }
  267. void csiDeleteHandler(CSI csi, Terminal terminal) {
  268. var count = 1;
  269. if (csi.params.isNotEmpty) {
  270. count = int.tryParse(csi.params.first) ?? count;
  271. }
  272. if (count < 1) {
  273. count = 1;
  274. }
  275. terminal.buffer.deleteChars(count);
  276. }
  277. void csiTabClearHandler(CSI csi, Terminal terminal) {
  278. // TODO
  279. }
  280. void csiWindowManipulation(CSI csi, Terminal terminal) {
  281. // not supported
  282. }
  283. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  284. var count = 1;
  285. if (csi.params.isNotEmpty) {
  286. count = int.tryParse(csi.params.first) ?? count;
  287. }
  288. if (count < 1) {
  289. count = 1;
  290. }
  291. terminal.buffer.moveCursorY(count);
  292. terminal.buffer.setCursorX(0);
  293. }
  294. void csiCursorPrecedingLineHandler(CSI csi, Terminal terminal) {
  295. var count = 1;
  296. if (csi.params.isNotEmpty) {
  297. count = int.tryParse(csi.params.first) ?? count;
  298. }
  299. if (count < 1) {
  300. count = 1;
  301. }
  302. terminal.buffer.moveCursorY(-count);
  303. terminal.buffer.setCursorX(0);
  304. }
  305. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  306. var count = 1;
  307. if (csi.params.isNotEmpty) {
  308. count = int.tryParse(csi.params.first) ?? count;
  309. }
  310. if (count < 1) {
  311. count = 1;
  312. }
  313. terminal.buffer.insertLines(count);
  314. }
  315. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  316. var count = 1;
  317. if (csi.params.isNotEmpty) {
  318. count = int.tryParse(csi.params.first) ?? count;
  319. }
  320. if (count < 1) {
  321. count = 1;
  322. }
  323. terminal.buffer.deleteLines(count);
  324. }
  325. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  326. var count = 1;
  327. if (csi.params.isNotEmpty) {
  328. count = int.tryParse(csi.params.first) ?? count;
  329. }
  330. if (count < 1) {
  331. count = 1;
  332. }
  333. terminal.buffer.areaScrollUp(count);
  334. }
  335. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  336. var count = 1;
  337. if (csi.params.isNotEmpty) {
  338. count = int.tryParse(csi.params.first) ?? count;
  339. }
  340. if (count < 1) {
  341. count = 1;
  342. }
  343. terminal.buffer.areaScrollDown(count);
  344. }
  345. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  346. var count = 1;
  347. if (csi.params.isNotEmpty) {
  348. count = int.tryParse(csi.params.first) ?? count;
  349. }
  350. if (count < 1) {
  351. count = 1;
  352. }
  353. terminal.buffer.insertBlankCharacters(count);
  354. }