csi.dart 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. while (queue.isNotEmpty) {
  53. // TODO: handle special case when queue is empty as this time.
  54. final char = queue.removeFirst();
  55. if (char >= 0x30 && char <= 0x3F) {
  56. paramBuffer.writeCharCode(char);
  57. continue;
  58. }
  59. if (char > 0 && char <= 0x2F) {
  60. intermediates.add(char);
  61. continue;
  62. }
  63. const csiMin = 0x40;
  64. const csiMax = 0x7e;
  65. if (char >= csiMin && char <= csiMax) {
  66. final params = paramBuffer.toString().split(';');
  67. return CSI(
  68. params: params,
  69. finalByte: char,
  70. intermediates: intermediates,
  71. );
  72. }
  73. }
  74. }
  75. void csiHandler(Queue<int> queue, Terminal terminal) {
  76. final csi = _parseCsi(queue);
  77. if (csi == null) {
  78. return;
  79. }
  80. terminal.debug.onCsi(csi);
  81. final handler = _csiHandlers[csi.finalByte];
  82. if (handler != null) {
  83. handler(csi, terminal);
  84. return;
  85. }
  86. terminal.debug.onError('unknown: $csi');
  87. }
  88. /// DECSED - Selective Erase In Display
  89. ///
  90. /// ```text
  91. /// CSI ? P s J
  92. ///
  93. /// Erase in Display (DECSED)
  94. ///
  95. /// P s = 0 → Selective Erase Below (default)
  96. /// P s = 1 → Selective Erase Above
  97. /// P s = 2 → Selective Erase All
  98. /// ```
  99. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  100. var ps = '0';
  101. if (csi.params.isNotEmpty) {
  102. ps = csi.params.first;
  103. }
  104. switch (ps) {
  105. case '':
  106. case '0':
  107. terminal.buffer.eraseDisplayFromCursor();
  108. break;
  109. case '1':
  110. terminal.buffer.eraseDisplayToCursor();
  111. break;
  112. case '2':
  113. case '3':
  114. terminal.buffer.eraseDisplay();
  115. break;
  116. default:
  117. terminal.debug.onError("Unsupported ED: CSI $ps J");
  118. }
  119. }
  120. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  121. var ps = '0';
  122. if (csi.params.isNotEmpty) {
  123. ps = csi.params.first;
  124. }
  125. switch (ps) {
  126. case '':
  127. case '0':
  128. terminal.buffer.eraseLineFromCursor();
  129. break;
  130. case '1':
  131. terminal.buffer.eraseLineToCursor();
  132. break;
  133. case '2':
  134. terminal.buffer.eraseLine();
  135. break;
  136. default:
  137. terminal.debug.onError("Unsupported EL: CSI $ps K");
  138. }
  139. }
  140. /// CUP - Cursor Position
  141. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  142. var x = 1;
  143. var y = 1;
  144. if (csi.params.length == 2) {
  145. y = int.tryParse(csi.params[0]) ?? x;
  146. x = int.tryParse(csi.params[1]) ?? y;
  147. }
  148. terminal.buffer.setPosition(x - 1, y - 1);
  149. }
  150. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  151. var row = 1;
  152. if (csi.params.isNotEmpty) {
  153. row = int.tryParse(csi.params.first) ?? row;
  154. }
  155. terminal.buffer.setCursorY(row - 1);
  156. }
  157. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  158. var x = 1;
  159. if (csi.params.isNotEmpty) {
  160. x = int.tryParse(csi.params.first) ?? x;
  161. }
  162. terminal.buffer.setCursorX(x - 1);
  163. }
  164. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  165. var offset = 1;
  166. if (csi.params.isNotEmpty) {
  167. offset = int.tryParse(csi.params.first) ?? offset;
  168. }
  169. terminal.buffer.movePosition(offset, 0);
  170. }
  171. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  172. var offset = 1;
  173. if (csi.params.isNotEmpty) {
  174. offset = int.tryParse(csi.params.first) ?? offset;
  175. }
  176. terminal.buffer.movePosition(-offset, 0);
  177. }
  178. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  179. var count = 1;
  180. if (csi.params.isNotEmpty) {
  181. count = int.tryParse(csi.params.first) ?? count;
  182. }
  183. terminal.buffer.eraseCharacters(count);
  184. }
  185. void csiModeHandler(CSI csi, Terminal terminal) {
  186. // terminal.ActiveBuffer().ClearSelection()
  187. return csiSetModes(csi, terminal);
  188. }
  189. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  190. if (csi.params.isEmpty) return;
  191. switch (csi.params[0]) {
  192. case "5":
  193. terminal.onInput("\x1b[0n");
  194. break;
  195. case "6": // report cursor position
  196. terminal.onInput("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  197. break;
  198. default:
  199. terminal.debug
  200. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  201. return;
  202. }
  203. }
  204. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  205. var response = '?1;2';
  206. if (csi.params.isNotEmpty && csi.params.first.startsWith('>')) {
  207. response = '>0;0;0';
  208. }
  209. terminal.onInput('\x1b[${response}c');
  210. }
  211. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  212. var distance = 1;
  213. if (csi.params.isNotEmpty) {
  214. distance = int.tryParse(csi.params.first) ?? distance;
  215. }
  216. terminal.buffer.movePosition(0, -distance);
  217. }
  218. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  219. var distance = 1;
  220. if (csi.params.isNotEmpty) {
  221. distance = int.tryParse(csi.params.first) ?? distance;
  222. }
  223. terminal.buffer.movePosition(0, distance);
  224. }
  225. /// DECSTBM – Set Top and Bottom Margins (DEC Private)
  226. ///
  227. /// ESC [ Pn; Pn r
  228. ///
  229. /// This sequence sets the top and bottom margins to define the scrolling
  230. /// region. The first parameter is the line number of the first line in the
  231. /// scrolling region; the second parameter is the line number of the bottom line
  232. /// in the scrolling region. Default is the en tire screen (no margins). The
  233. /// minimum size of the scrolling region allowed is two lines, i.e., the top
  234. /// margin must be less than the bottom margin. The cursor is placed in the home
  235. /// position (see Origin Mode DECOM).
  236. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  237. var top = 1;
  238. var bottom = terminal.viewHeight;
  239. if (csi.params.length > 2) {
  240. return;
  241. }
  242. if (csi.params.isNotEmpty) {
  243. top = int.tryParse(csi.params[0]) ?? top;
  244. if (csi.params.length > 1) {
  245. bottom = int.tryParse(csi.params[1]) ?? bottom;
  246. }
  247. }
  248. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  249. terminal.buffer.setPosition(0, 0);
  250. }
  251. void csiDeleteHandler(CSI csi, Terminal terminal) {
  252. var count = 1;
  253. if (csi.params.isNotEmpty) {
  254. count = int.tryParse(csi.params.first) ?? count;
  255. }
  256. if (count < 1) {
  257. count = 1;
  258. }
  259. terminal.buffer.deleteChars(count);
  260. }
  261. void csiTabClearHandler(CSI csi, Terminal terminal) {
  262. // TODO
  263. }
  264. void csiWindowManipulation(CSI csi, Terminal terminal) {
  265. // not supported
  266. }
  267. void csiCursorNextLineHandler(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.moveCursorY(count);
  276. terminal.buffer.setCursorX(0);
  277. }
  278. void csiCursorPrecedingLineHandler(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 csiInsertLinesHandler(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.insertLines(count);
  298. }
  299. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  300. var count = 1;
  301. if (csi.params.isNotEmpty) {
  302. count = int.tryParse(csi.params.first) ?? count;
  303. }
  304. if (count < 1) {
  305. count = 1;
  306. }
  307. terminal.buffer.deleteLines(count);
  308. }
  309. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  310. var count = 1;
  311. if (csi.params.isNotEmpty) {
  312. count = int.tryParse(csi.params.first) ?? count;
  313. }
  314. if (count < 1) {
  315. count = 1;
  316. }
  317. terminal.buffer.areaScrollUp(count);
  318. }
  319. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  320. var count = 1;
  321. if (csi.params.isNotEmpty) {
  322. count = int.tryParse(csi.params.first) ?? count;
  323. }
  324. if (count < 1) {
  325. count = 1;
  326. }
  327. terminal.buffer.areaScrollDown(count);
  328. }
  329. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  330. var count = 1;
  331. if (csi.params.isNotEmpty) {
  332. count = int.tryParse(csi.params.first) ?? count;
  333. }
  334. if (count < 1) {
  335. count = 1;
  336. }
  337. terminal.buffer.insertBlankCharacters(count);
  338. }