csi.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. while (true) {
  95. // The sequence isn't completed, just ignore it.
  96. if (queue.length <= readOffset) {
  97. return null;
  98. }
  99. // final char = queue.removeFirst();
  100. final char = queue.elementAt(readOffset++);
  101. if (char == _semicolon) {
  102. _csi.params.add(param);
  103. param = 0;
  104. continue;
  105. }
  106. // '0' <= char <= '9'
  107. if (char >= 48 && char <= 57) {
  108. param *= 10;
  109. param += char - 48;
  110. continue;
  111. }
  112. if (char > 0 && char <= 0x2F) {
  113. // intermediates.add(char);
  114. continue;
  115. }
  116. const csiMin = 0x40;
  117. const csiMax = 0x7e;
  118. if (char >= csiMin && char <= csiMax) {
  119. // The sequence is complete. So we consume it from the queue.
  120. for (var i = 0; i < readOffset; i++) {
  121. queue.removeFirst();
  122. }
  123. // final params = paramBuffer.toString().split(';');
  124. _csi.params.add(param);
  125. _csi.finalByte = char;
  126. return _csi;
  127. }
  128. }
  129. }
  130. /// CSI - Control Sequence Introducer: sequence starting with ESC [ (7bit) or
  131. /// CSI (\x9B, 8bit)
  132. bool csiHandler(Queue<int> queue, Terminal terminal) {
  133. final csi = _parseCsi(queue);
  134. if (csi == null) {
  135. return false;
  136. }
  137. // terminal.debug.onCsi(csi);
  138. final handler = _csiHandlers[csi.finalByte];
  139. if (handler != null) {
  140. handler(csi, terminal);
  141. } else {
  142. terminal.debug.onError('unknown: $csi');
  143. }
  144. return true;
  145. }
  146. /// DECSED - Selective Erase In Display
  147. ///
  148. /// ```text
  149. /// CSI ? P s J
  150. ///
  151. /// Erase in Display (DECSED)
  152. ///
  153. /// P s = 0 → Selective Erase Below (default)
  154. /// P s = 1 → Selective Erase Above
  155. /// P s = 2 → Selective Erase All
  156. /// ```
  157. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  158. var ps = 0;
  159. if (csi.params.isNotEmpty) {
  160. ps = csi.params.first;
  161. }
  162. switch (ps) {
  163. case 0:
  164. terminal.buffer.eraseDisplayFromCursor();
  165. break;
  166. case 1:
  167. terminal.buffer.eraseDisplayToCursor();
  168. break;
  169. case 2:
  170. case 3:
  171. terminal.buffer.eraseDisplay();
  172. break;
  173. default:
  174. terminal.debug.onError("Unsupported ED: CSI $ps J");
  175. }
  176. }
  177. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  178. var ps = 0;
  179. if (csi.params.isNotEmpty) {
  180. ps = csi.params.first;
  181. }
  182. switch (ps) {
  183. case 0:
  184. terminal.buffer.eraseLineFromCursor();
  185. break;
  186. case 1:
  187. terminal.buffer.eraseLineToCursor();
  188. break;
  189. case 2:
  190. terminal.buffer.eraseLine();
  191. break;
  192. default:
  193. terminal.debug.onError("Unsupported EL: CSI $ps K");
  194. }
  195. }
  196. /// CUP - Cursor Position
  197. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  198. var x = 1;
  199. var y = 1;
  200. if (csi.params.length == 2) {
  201. y = csi.params[0];
  202. x = csi.params[1];
  203. }
  204. terminal.buffer.setPosition(x - 1, y - 1);
  205. }
  206. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  207. var row = 1;
  208. if (csi.params.isNotEmpty) {
  209. row = csi.params.first;
  210. }
  211. terminal.buffer.setCursorY(row - 1);
  212. }
  213. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  214. var x = 1;
  215. if (csi.params.isNotEmpty) {
  216. x = csi.params.first;
  217. }
  218. terminal.buffer.setCursorX(x - 1);
  219. }
  220. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  221. var offset = 1;
  222. if (csi.params.isNotEmpty) {
  223. offset = csi.params.first;
  224. }
  225. terminal.buffer.movePosition(offset, 0);
  226. }
  227. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  228. var offset = 1;
  229. if (csi.params.isNotEmpty) {
  230. offset = csi.params.first;
  231. }
  232. terminal.buffer.movePosition(-offset, 0);
  233. }
  234. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  235. var count = 1;
  236. if (csi.params.isNotEmpty) {
  237. count = csi.params.first;
  238. }
  239. terminal.buffer.eraseCharacters(count);
  240. }
  241. void csiModeHandler(CSI csi, Terminal terminal) {
  242. // terminal.ActiveBuffer().ClearSelection()
  243. return csiSetModes(csi, terminal);
  244. }
  245. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  246. if (csi.params.isEmpty) return;
  247. switch (csi.params[0]) {
  248. case 5:
  249. terminal.onInput("\x1b[0n");
  250. break;
  251. case 6: // report cursor position
  252. terminal.onInput("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  253. break;
  254. default:
  255. terminal.debug
  256. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  257. return;
  258. }
  259. }
  260. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  261. var response = '?1;2';
  262. if (csi.prefix == 62 /* '>' */) {
  263. response = '>0;0;0';
  264. }
  265. terminal.onInput('\x1b[${response}c');
  266. }
  267. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  268. var distance = 1;
  269. if (csi.params.isNotEmpty) {
  270. distance = csi.params.first;
  271. }
  272. terminal.buffer.movePosition(0, -distance);
  273. }
  274. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  275. var distance = 1;
  276. if (csi.params.isNotEmpty) {
  277. distance = csi.params.first;
  278. }
  279. terminal.buffer.movePosition(0, distance);
  280. }
  281. /// DECSTBM – Set Top and Bottom Margins (DEC Private)
  282. ///
  283. /// ESC [ Pn; Pn r
  284. ///
  285. /// This sequence sets the top and bottom margins to define the scrolling
  286. /// region. The first parameter is the line number of the first line in the
  287. /// scrolling region; the second parameter is the line number of the bottom line
  288. /// in the scrolling region. Default is the en tire screen (no margins). The
  289. /// minimum size of the scrolling region allowed is two lines, i.e., the top
  290. /// margin must be less than the bottom margin. The cursor is placed in the home
  291. /// position (see Origin Mode DECOM).
  292. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  293. var top = 1;
  294. var bottom = terminal.viewHeight;
  295. if (csi.params.length > 2) {
  296. return;
  297. }
  298. if (csi.params.isNotEmpty) {
  299. top = csi.params[0];
  300. if (csi.params.length > 1) {
  301. bottom = csi.params[1];
  302. }
  303. }
  304. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  305. terminal.buffer.setPosition(0, 0);
  306. }
  307. void csiDeleteHandler(CSI csi, Terminal terminal) {
  308. var count = 1;
  309. if (csi.params.isNotEmpty) {
  310. count = csi.params.first;
  311. }
  312. if (count < 1) {
  313. count = 1;
  314. }
  315. terminal.buffer.deleteChars(count);
  316. }
  317. void csiTabClearHandler(CSI csi, Terminal terminal) {
  318. // TODO
  319. }
  320. void csiWindowManipulation(CSI csi, Terminal terminal) {
  321. // not supported
  322. }
  323. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  324. var count = 1;
  325. if (csi.params.isNotEmpty) {
  326. count = csi.params.first;
  327. }
  328. if (count < 1) {
  329. count = 1;
  330. }
  331. terminal.buffer.moveCursorY(count);
  332. terminal.buffer.setCursorX(0);
  333. }
  334. void csiCursorPrecedingLineHandler(CSI csi, Terminal terminal) {
  335. var count = 1;
  336. if (csi.params.isNotEmpty) {
  337. count = csi.params.first;
  338. }
  339. if (count < 1) {
  340. count = 1;
  341. }
  342. terminal.buffer.moveCursorY(-count);
  343. terminal.buffer.setCursorX(0);
  344. }
  345. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  346. var count = 1;
  347. if (csi.params.isNotEmpty) {
  348. count = csi.params.first;
  349. }
  350. if (count < 1) {
  351. count = 1;
  352. }
  353. terminal.buffer.insertLines(count);
  354. }
  355. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  356. var count = 1;
  357. if (csi.params.isNotEmpty) {
  358. count = csi.params.first;
  359. }
  360. if (count < 1) {
  361. count = 1;
  362. }
  363. terminal.buffer.deleteLines(count);
  364. }
  365. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  366. var count = 1;
  367. if (csi.params.isNotEmpty) {
  368. count = csi.params.first;
  369. }
  370. if (count < 1) {
  371. count = 1;
  372. }
  373. terminal.buffer.areaScrollUp(count);
  374. }
  375. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  376. var count = 1;
  377. if (csi.params.isNotEmpty) {
  378. count = csi.params.first;
  379. }
  380. if (count < 1) {
  381. count = 1;
  382. }
  383. terminal.buffer.areaScrollDown(count);
  384. }
  385. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  386. var count = 1;
  387. if (csi.params.isNotEmpty) {
  388. count = csi.params.first;
  389. }
  390. if (count < 1) {
  391. count = 1;
  392. }
  393. terminal.buffer.insertBlankCharacters(count);
  394. }