csi.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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,
  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. this.params,
  38. this.finalByte,
  39. 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 (true) {
  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. terminal.debug.onCsi(csi);
  78. final handler = _csiHandlers[csi.finalByte];
  79. if (handler != null) {
  80. handler(csi, terminal);
  81. return;
  82. }
  83. terminal.debug.onError('unknown: $csi');
  84. }
  85. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  86. var ps = '0';
  87. if (csi.params.isNotEmpty) {
  88. ps = csi.params.first;
  89. }
  90. switch (ps) {
  91. case '':
  92. case '0':
  93. terminal.buffer.eraseDisplayFromCursor();
  94. break;
  95. case '1':
  96. terminal.buffer.eraseDisplayToCursor();
  97. break;
  98. case '2':
  99. case '3':
  100. terminal.buffer.eraseDisplay();
  101. break;
  102. default:
  103. terminal.debug.onError("Unsupported ED: CSI $ps J");
  104. }
  105. }
  106. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  107. var ps = '0';
  108. if (csi.params.isNotEmpty) {
  109. ps = csi.params.first;
  110. }
  111. switch (ps) {
  112. case '':
  113. case '0':
  114. terminal.buffer.eraseLineFromCursor();
  115. break;
  116. case '1':
  117. terminal.buffer.eraseLineToCursor();
  118. break;
  119. case '2':
  120. terminal.buffer.eraseLine();
  121. break;
  122. default:
  123. terminal.debug.onError("Unsupported EL: CSI $ps K");
  124. }
  125. }
  126. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  127. var x = 1;
  128. var y = 1;
  129. if (csi.params.length == 2) {
  130. y = int.tryParse(csi.params[0]) ?? x;
  131. x = int.tryParse(csi.params[1]) ?? y;
  132. }
  133. terminal.buffer.setPosition(x - 1, y - 1);
  134. }
  135. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  136. var row = 1;
  137. if (csi.params.isNotEmpty) {
  138. row = int.tryParse(csi.params.first) ?? row;
  139. }
  140. terminal.buffer.setCursorY(row - 1);
  141. }
  142. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  143. var x = 1;
  144. if (csi.params.isNotEmpty) {
  145. x = int.tryParse(csi.params.first) ?? x;
  146. }
  147. terminal.buffer.setCursorX(x - 1);
  148. }
  149. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  150. var offset = 1;
  151. if (csi.params.isNotEmpty) {
  152. offset = int.tryParse(csi.params.first) ?? offset;
  153. }
  154. terminal.buffer.movePosition(offset, 0);
  155. }
  156. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  157. var offset = 1;
  158. if (csi.params.isNotEmpty) {
  159. offset = int.tryParse(csi.params.first) ?? offset;
  160. }
  161. terminal.buffer.movePosition(-offset, 0);
  162. }
  163. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  164. var count = 1;
  165. if (csi.params.isNotEmpty) {
  166. count = int.tryParse(csi.params.first) ?? count;
  167. }
  168. terminal.buffer.eraseCharacters(count);
  169. }
  170. void csiModeHandler(CSI csi, Terminal terminal) {
  171. // terminal.ActiveBuffer().ClearSelection()
  172. return csiSetModes(csi, terminal);
  173. }
  174. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  175. if (csi.params.isEmpty) return;
  176. switch (csi.params[0]) {
  177. case "5":
  178. terminal.onInput("\x1b[0n");
  179. break;
  180. case "6": // report cursor position
  181. terminal.onInput("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  182. break;
  183. default:
  184. terminal.debug
  185. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  186. return;
  187. }
  188. }
  189. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  190. var response = '?1;2';
  191. if (csi.params.isNotEmpty && csi.params.first.startsWith('>')) {
  192. response = '>0;0;0';
  193. }
  194. terminal.onInput('\x1b[${response}c');
  195. }
  196. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  197. var distance = 1;
  198. if (csi.params.isNotEmpty) {
  199. distance = int.tryParse(csi.params.first) ?? distance;
  200. }
  201. terminal.buffer.movePosition(0, -distance);
  202. }
  203. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  204. var distance = 1;
  205. if (csi.params.isNotEmpty) {
  206. distance = int.tryParse(csi.params.first) ?? distance;
  207. }
  208. terminal.buffer.movePosition(0, distance);
  209. }
  210. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  211. var top = 1;
  212. var bottom = terminal.viewHeight;
  213. if (csi.params.length > 2) {
  214. return;
  215. }
  216. if (csi.params.isNotEmpty) {
  217. top = int.tryParse(csi.params[0]) ?? top;
  218. if (csi.params.length > 1) {
  219. bottom = int.tryParse(csi.params[1]) ?? bottom;
  220. }
  221. }
  222. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  223. terminal.buffer.setPosition(0, 0);
  224. }
  225. void csiDeleteHandler(CSI csi, Terminal terminal) {
  226. var count = 1;
  227. if (csi.params.isNotEmpty) {
  228. count = int.tryParse(csi.params.first) ?? count;
  229. }
  230. if (count < 1) {
  231. count = 1;
  232. }
  233. terminal.buffer.deleteChars(count);
  234. }
  235. void csiTabClearHandler(CSI csi, Terminal terminal) {
  236. // TODO
  237. }
  238. void csiWindowManipulation(CSI csi, Terminal terminal) {
  239. // not supported
  240. }
  241. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  242. var count = 1;
  243. if (csi.params.isNotEmpty) {
  244. count = int.tryParse(csi.params.first) ?? count;
  245. }
  246. if (count < 1) {
  247. count = 1;
  248. }
  249. terminal.buffer.moveCursorY(count);
  250. terminal.buffer.setCursorX(0);
  251. }
  252. void csiCursorPrecedingLineHandler(CSI csi, Terminal terminal) {
  253. var count = 1;
  254. if (csi.params.isNotEmpty) {
  255. count = int.tryParse(csi.params.first) ?? count;
  256. }
  257. if (count < 1) {
  258. count = 1;
  259. }
  260. terminal.buffer.moveCursorY(-count);
  261. terminal.buffer.setCursorX(0);
  262. }
  263. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  264. var count = 1;
  265. if (csi.params.isNotEmpty) {
  266. count = int.tryParse(csi.params.first) ?? count;
  267. }
  268. if (count < 1) {
  269. count = 1;
  270. }
  271. terminal.buffer.insertLines(count);
  272. }
  273. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  274. var count = 1;
  275. if (csi.params.isNotEmpty) {
  276. count = int.tryParse(csi.params.first) ?? count;
  277. }
  278. if (count < 1) {
  279. count = 1;
  280. }
  281. terminal.buffer.deleteLines(count);
  282. }
  283. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  284. var count = 1;
  285. if (csi.params.isNotEmpty) {
  286. count = int.tryParse(csi.params.first) ?? count;
  287. }
  288. if (count < 1) {
  289. count = 1;
  290. }
  291. terminal.buffer.areaScrollUp(count);
  292. }
  293. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  294. var count = 1;
  295. if (csi.params.isNotEmpty) {
  296. count = int.tryParse(csi.params.first) ?? count;
  297. }
  298. if (count < 1) {
  299. count = 1;
  300. }
  301. terminal.buffer.areaScrollDown(count);
  302. }
  303. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  304. var count = 1;
  305. if (csi.params.isNotEmpty) {
  306. count = int.tryParse(csi.params.first) ?? count;
  307. }
  308. if (count < 1) {
  309. count = 1;
  310. }
  311. terminal.buffer.insertBlankCharacters(count);
  312. }