csi.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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,
  12. 'l'.codeUnitAt(0): csiModeHandler,
  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,
  25. 'J'.codeUnitAt(0): csiEraseInDisplayHandler,
  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. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  89. var ps = '0';
  90. if (csi.params.isNotEmpty) {
  91. ps = csi.params.first;
  92. }
  93. switch (ps) {
  94. case '':
  95. case '0':
  96. terminal.buffer.eraseDisplayFromCursor();
  97. break;
  98. case '1':
  99. terminal.buffer.eraseDisplayToCursor();
  100. break;
  101. case '2':
  102. case '3':
  103. terminal.buffer.eraseDisplay();
  104. break;
  105. default:
  106. terminal.debug.onError("Unsupported ED: CSI $ps J");
  107. }
  108. }
  109. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  110. var ps = '0';
  111. if (csi.params.isNotEmpty) {
  112. ps = csi.params.first;
  113. }
  114. switch (ps) {
  115. case '':
  116. case '0':
  117. terminal.buffer.eraseLineFromCursor();
  118. break;
  119. case '1':
  120. terminal.buffer.eraseLineToCursor();
  121. break;
  122. case '2':
  123. terminal.buffer.eraseLine();
  124. break;
  125. default:
  126. terminal.debug.onError("Unsupported EL: CSI $ps K");
  127. }
  128. }
  129. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  130. var x = 1;
  131. var y = 1;
  132. if (csi.params.length == 2) {
  133. y = int.tryParse(csi.params[0]) ?? x;
  134. x = int.tryParse(csi.params[1]) ?? y;
  135. }
  136. terminal.buffer.setPosition(x - 1, y - 1);
  137. }
  138. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  139. var row = 1;
  140. if (csi.params.isNotEmpty) {
  141. row = int.tryParse(csi.params.first) ?? row;
  142. }
  143. terminal.buffer.setCursorY(row - 1);
  144. }
  145. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  146. var x = 1;
  147. if (csi.params.isNotEmpty) {
  148. x = int.tryParse(csi.params.first) ?? x;
  149. }
  150. terminal.buffer.setCursorX(x - 1);
  151. }
  152. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  153. var offset = 1;
  154. if (csi.params.isNotEmpty) {
  155. offset = int.tryParse(csi.params.first) ?? offset;
  156. }
  157. terminal.buffer.movePosition(offset, 0);
  158. }
  159. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  160. var offset = 1;
  161. if (csi.params.isNotEmpty) {
  162. offset = int.tryParse(csi.params.first) ?? offset;
  163. }
  164. terminal.buffer.movePosition(-offset, 0);
  165. }
  166. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  167. var count = 1;
  168. if (csi.params.isNotEmpty) {
  169. count = int.tryParse(csi.params.first) ?? count;
  170. }
  171. terminal.buffer.eraseCharacters(count);
  172. }
  173. void csiModeHandler(CSI csi, Terminal terminal) {
  174. // terminal.ActiveBuffer().ClearSelection()
  175. return csiSetModes(csi, terminal);
  176. }
  177. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  178. if (csi.params.isEmpty) return;
  179. switch (csi.params[0]) {
  180. case "5":
  181. terminal.onInput("\x1b[0n");
  182. break;
  183. case "6": // report cursor position
  184. terminal.onInput("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  185. break;
  186. default:
  187. terminal.debug
  188. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  189. return;
  190. }
  191. }
  192. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  193. var response = '?1;2';
  194. if (csi.params.isNotEmpty && csi.params.first.startsWith('>')) {
  195. response = '>0;0;0';
  196. }
  197. terminal.onInput('\x1b[${response}c');
  198. }
  199. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  200. var distance = 1;
  201. if (csi.params.isNotEmpty) {
  202. distance = int.tryParse(csi.params.first) ?? distance;
  203. }
  204. terminal.buffer.movePosition(0, -distance);
  205. }
  206. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  207. var distance = 1;
  208. if (csi.params.isNotEmpty) {
  209. distance = int.tryParse(csi.params.first) ?? distance;
  210. }
  211. terminal.buffer.movePosition(0, distance);
  212. }
  213. /// DECSTBM – Set Top and Bottom Margins (DEC Private)
  214. ///
  215. /// ESC [ Pn; Pn r
  216. ///
  217. /// This sequence sets the top and bottom margins to define the scrolling
  218. /// region. The first parameter is the line number of the first line in the
  219. /// scrolling region; the second parameter is the line number of the bottom line
  220. /// in the scrolling region. Default is the en tire screen (no margins). The
  221. /// minimum size of the scrolling region allowed is two lines, i.e., the top
  222. /// margin must be less than the bottom margin. The cursor is placed in the home
  223. /// position (see Origin Mode DECOM).
  224. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  225. var top = 1;
  226. var bottom = terminal.viewHeight;
  227. if (csi.params.length > 2) {
  228. return;
  229. }
  230. if (csi.params.isNotEmpty) {
  231. top = int.tryParse(csi.params[0]) ?? top;
  232. if (csi.params.length > 1) {
  233. bottom = int.tryParse(csi.params[1]) ?? bottom;
  234. }
  235. }
  236. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  237. terminal.buffer.setPosition(0, 0);
  238. }
  239. void csiDeleteHandler(CSI csi, Terminal terminal) {
  240. var count = 1;
  241. if (csi.params.isNotEmpty) {
  242. count = int.tryParse(csi.params.first) ?? count;
  243. }
  244. if (count < 1) {
  245. count = 1;
  246. }
  247. terminal.buffer.deleteChars(count);
  248. }
  249. void csiTabClearHandler(CSI csi, Terminal terminal) {
  250. // TODO
  251. }
  252. void csiWindowManipulation(CSI csi, Terminal terminal) {
  253. // not supported
  254. }
  255. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  256. var count = 1;
  257. if (csi.params.isNotEmpty) {
  258. count = int.tryParse(csi.params.first) ?? count;
  259. }
  260. if (count < 1) {
  261. count = 1;
  262. }
  263. terminal.buffer.moveCursorY(count);
  264. terminal.buffer.setCursorX(0);
  265. }
  266. void csiCursorPrecedingLineHandler(CSI csi, Terminal terminal) {
  267. var count = 1;
  268. if (csi.params.isNotEmpty) {
  269. count = int.tryParse(csi.params.first) ?? count;
  270. }
  271. if (count < 1) {
  272. count = 1;
  273. }
  274. terminal.buffer.moveCursorY(-count);
  275. terminal.buffer.setCursorX(0);
  276. }
  277. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  278. var count = 1;
  279. if (csi.params.isNotEmpty) {
  280. count = int.tryParse(csi.params.first) ?? count;
  281. }
  282. if (count < 1) {
  283. count = 1;
  284. }
  285. terminal.buffer.insertLines(count);
  286. }
  287. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  288. var count = 1;
  289. if (csi.params.isNotEmpty) {
  290. count = int.tryParse(csi.params.first) ?? count;
  291. }
  292. if (count < 1) {
  293. count = 1;
  294. }
  295. terminal.buffer.deleteLines(count);
  296. }
  297. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  298. var count = 1;
  299. if (csi.params.isNotEmpty) {
  300. count = int.tryParse(csi.params.first) ?? count;
  301. }
  302. if (count < 1) {
  303. count = 1;
  304. }
  305. terminal.buffer.areaScrollUp(count);
  306. }
  307. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  308. var count = 1;
  309. if (csi.params.isNotEmpty) {
  310. count = int.tryParse(csi.params.first) ?? count;
  311. }
  312. if (count < 1) {
  313. count = 1;
  314. }
  315. terminal.buffer.areaScrollDown(count);
  316. }
  317. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  318. var count = 1;
  319. if (csi.params.isNotEmpty) {
  320. count = int.tryParse(csi.params.first) ?? count;
  321. }
  322. if (count < 1) {
  323. count = 1;
  324. }
  325. terminal.buffer.insertBlankCharacters(count);
  326. }