csi.dart 11 KB

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