csi.dart 10 KB

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