csi.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. 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. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  214. var top = 1;
  215. var bottom = terminal.viewHeight;
  216. if (csi.params.length > 2) {
  217. return;
  218. }
  219. if (csi.params.isNotEmpty) {
  220. top = int.tryParse(csi.params[0]) ?? top;
  221. if (csi.params.length > 1) {
  222. bottom = int.tryParse(csi.params[1]) ?? bottom;
  223. }
  224. }
  225. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  226. terminal.buffer.setPosition(0, 0);
  227. }
  228. void csiDeleteHandler(CSI csi, Terminal terminal) {
  229. var count = 1;
  230. if (csi.params.isNotEmpty) {
  231. count = int.tryParse(csi.params.first) ?? count;
  232. }
  233. if (count < 1) {
  234. count = 1;
  235. }
  236. terminal.buffer.deleteChars(count);
  237. }
  238. void csiTabClearHandler(CSI csi, Terminal terminal) {
  239. // TODO
  240. }
  241. void csiWindowManipulation(CSI csi, Terminal terminal) {
  242. // not supported
  243. }
  244. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  245. var count = 1;
  246. if (csi.params.isNotEmpty) {
  247. count = int.tryParse(csi.params.first) ?? count;
  248. }
  249. if (count < 1) {
  250. count = 1;
  251. }
  252. terminal.buffer.moveCursorY(count);
  253. terminal.buffer.setCursorX(0);
  254. }
  255. void csiCursorPrecedingLineHandler(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 csiInsertLinesHandler(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.insertLines(count);
  275. }
  276. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  277. var count = 1;
  278. if (csi.params.isNotEmpty) {
  279. count = int.tryParse(csi.params.first) ?? count;
  280. }
  281. if (count < 1) {
  282. count = 1;
  283. }
  284. terminal.buffer.deleteLines(count);
  285. }
  286. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  287. var count = 1;
  288. if (csi.params.isNotEmpty) {
  289. count = int.tryParse(csi.params.first) ?? count;
  290. }
  291. if (count < 1) {
  292. count = 1;
  293. }
  294. terminal.buffer.areaScrollUp(count);
  295. }
  296. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  297. var count = 1;
  298. if (csi.params.isNotEmpty) {
  299. count = int.tryParse(csi.params.first) ?? count;
  300. }
  301. if (count < 1) {
  302. count = 1;
  303. }
  304. terminal.buffer.areaScrollDown(count);
  305. }
  306. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  307. var count = 1;
  308. if (csi.params.isNotEmpty) {
  309. count = int.tryParse(csi.params.first) ?? count;
  310. }
  311. if (count < 1) {
  312. count = 1;
  313. }
  314. terminal.buffer.insertBlankCharacters(count);
  315. }