csi.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import 'package:async/async.dart';
  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 = <String, CsiSequenceHandler>{
  7. 'c': csiSendDeviceAttributesHandler,
  8. 'd': csiLinePositionAbsolute,
  9. 'f': csiCursorPositionHandler,
  10. 'g': csiTabClearHandler,
  11. 'h': csiModeHandler,
  12. 'l': csiModeHandler,
  13. 'm': sgrHandler,
  14. 'n': csiDeviceStatusReportHandler,
  15. 'r': csiSetMarginsHandler,
  16. 't': csiWindowManipulation,
  17. 'A': csiCursorUpHandler,
  18. 'B': csiCursorDownHandler,
  19. 'C': csiCursorForwardHandler,
  20. 'D': csiCursorBackwardHandler,
  21. 'E': csiCursorNextLineHandler,
  22. 'F': csiCursorPrecedingLineHandler,
  23. 'G': csiCursorHorizontalAbsoluteHandler,
  24. 'H': csiCursorPositionHandler,
  25. 'J': csiEraseInDisplayHandler,
  26. 'K': csiEraseInLineHandler,
  27. 'L': csiInsertLinesHandler,
  28. 'M': csiDeleteLinesHandler,
  29. 'P': csiDeleteHandler,
  30. 'S': csiScrollUpHandler,
  31. 'T': csiScrollDownHandler,
  32. 'X': csiEraseCharactersHandler,
  33. '@': 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. Future<CSI> _parseCsi(StreamQueue<int> queue) async {
  50. final paramBuffer = StringBuffer();
  51. final intermediates = <int>[];
  52. while (true) {
  53. final char = await queue.next;
  54. if (char >= 0x30 && char <= 0x3F) {
  55. paramBuffer.writeCharCode(char);
  56. continue;
  57. }
  58. if (char > 0 && char <= 0x2F) {
  59. intermediates.add(char);
  60. continue;
  61. }
  62. const csiMin = 0x40;
  63. const csiMax = 0x7e;
  64. if (char >= csiMin && char <= csiMax) {
  65. final params = paramBuffer.toString().split(';');
  66. return CSI(
  67. params: params,
  68. finalByte: char,
  69. intermediates: intermediates,
  70. );
  71. }
  72. }
  73. }
  74. Future<void> csiHandler(StreamQueue<int> queue, Terminal terminal) async {
  75. final csi = await _parseCsi(queue);
  76. terminal.debug.onCsi(csi);
  77. final handler = _csiHandlers[String.fromCharCode(csi.finalByte)];
  78. if (handler != null) {
  79. handler(csi, terminal);
  80. return;
  81. }
  82. terminal.debug.onError('unknown: $csi');
  83. }
  84. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  85. var ps = '0';
  86. if (csi.params.isNotEmpty) {
  87. ps = csi.params.first;
  88. }
  89. switch (ps) {
  90. case '':
  91. case '0':
  92. terminal.buffer.eraseDisplayFromCursor();
  93. break;
  94. case '1':
  95. terminal.buffer.eraseDisplayToCursor();
  96. break;
  97. case '2':
  98. case '3':
  99. terminal.buffer.eraseDisplay();
  100. break;
  101. default:
  102. terminal.debug.onError("Unsupported ED: CSI $ps J");
  103. }
  104. }
  105. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  106. var ps = '0';
  107. if (csi.params.isNotEmpty) {
  108. ps = csi.params.first;
  109. }
  110. switch (ps) {
  111. case '':
  112. case '0':
  113. terminal.buffer.eraseLineFromCursor();
  114. break;
  115. case '1':
  116. terminal.buffer.eraseLineToCursor();
  117. break;
  118. case '2':
  119. terminal.buffer.eraseLine();
  120. break;
  121. default:
  122. terminal.debug.onError("Unsupported EL: CSI $ps K");
  123. }
  124. }
  125. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  126. var x = 1;
  127. var y = 1;
  128. if (csi.params.length == 2) {
  129. y = int.tryParse(csi.params[0]) ?? x;
  130. x = int.tryParse(csi.params[1]) ?? y;
  131. }
  132. terminal.buffer.setPosition(x - 1, y - 1);
  133. }
  134. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  135. var row = 1;
  136. if (csi.params.isNotEmpty) {
  137. row = int.tryParse(csi.params.first) ?? row;
  138. }
  139. terminal.buffer.setCursorY(row - 1);
  140. }
  141. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  142. var x = 1;
  143. if (csi.params.isNotEmpty) {
  144. x = int.tryParse(csi.params.first) ?? x;
  145. }
  146. terminal.buffer.setCursorX(x - 1);
  147. }
  148. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  149. var offset = 1;
  150. if (csi.params.isNotEmpty) {
  151. offset = int.tryParse(csi.params.first) ?? offset;
  152. }
  153. terminal.buffer.movePosition(offset, 0);
  154. }
  155. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  156. var offset = 1;
  157. if (csi.params.isNotEmpty) {
  158. offset = int.tryParse(csi.params.first) ?? offset;
  159. }
  160. terminal.buffer.movePosition(-offset, 0);
  161. }
  162. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  163. var count = 1;
  164. if (csi.params.isNotEmpty) {
  165. count = int.tryParse(csi.params.first) ?? count;
  166. }
  167. terminal.buffer.eraseCharacters(count);
  168. }
  169. void csiModeHandler(CSI csi, Terminal terminal) {
  170. // terminal.ActiveBuffer().ClearSelection()
  171. return csiSetModes(csi, terminal);
  172. }
  173. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  174. if (csi.params.isEmpty) return;
  175. switch (csi.params[0]) {
  176. case "5":
  177. terminal.onInput("\x1b[0n");
  178. break;
  179. case "6": // report cursor position
  180. terminal.onInput("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  181. break;
  182. default:
  183. terminal.debug.onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  184. return;
  185. }
  186. }
  187. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  188. var response = '?1;2';
  189. if (csi.params.isNotEmpty && csi.params.first.startsWith('>')) {
  190. response = '>0;0;0';
  191. }
  192. terminal.onInput('\x1b[${response}c');
  193. }
  194. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  195. var distance = 1;
  196. if (csi.params.isNotEmpty) {
  197. distance = int.tryParse(csi.params.first) ?? distance;
  198. }
  199. terminal.buffer.movePosition(0, -distance);
  200. }
  201. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  202. var distance = 1;
  203. if (csi.params.isNotEmpty) {
  204. distance = int.tryParse(csi.params.first) ?? distance;
  205. }
  206. terminal.buffer.movePosition(0, distance);
  207. }
  208. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  209. var top = 1;
  210. var bottom = terminal.viewHeight;
  211. if (csi.params.length > 2) {
  212. return;
  213. }
  214. if (csi.params.isNotEmpty) {
  215. top = int.tryParse(csi.params[0]) ?? top;
  216. if (csi.params.length > 1) {
  217. bottom = int.tryParse(csi.params[1]) ?? bottom;
  218. }
  219. }
  220. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  221. terminal.buffer.setPosition(0, 0);
  222. }
  223. void csiDeleteHandler(CSI csi, Terminal terminal) {
  224. var count = 1;
  225. if (csi.params.isNotEmpty) {
  226. count = int.tryParse(csi.params.first) ?? count;
  227. }
  228. if (count < 1) {
  229. count = 1;
  230. }
  231. terminal.buffer.deleteChars(count);
  232. }
  233. void csiTabClearHandler(CSI csi, Terminal terminal) {
  234. // TODO
  235. }
  236. void csiWindowManipulation(CSI csi, Terminal terminal) {
  237. // not supported
  238. }
  239. void csiCursorNextLineHandler(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.moveCursorY(count);
  248. terminal.buffer.setCursorX(0);
  249. }
  250. void csiCursorPrecedingLineHandler(CSI csi, Terminal terminal) {
  251. var count = 1;
  252. if (csi.params.isNotEmpty) {
  253. count = int.tryParse(csi.params.first) ?? count;
  254. }
  255. if (count < 1) {
  256. count = 1;
  257. }
  258. terminal.buffer.moveCursorY(-count);
  259. terminal.buffer.setCursorX(0);
  260. }
  261. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  262. var count = 1;
  263. if (csi.params.isNotEmpty) {
  264. count = int.tryParse(csi.params.first) ?? count;
  265. }
  266. if (count < 1) {
  267. count = 1;
  268. }
  269. terminal.buffer.insertLines(count);
  270. }
  271. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  272. var count = 1;
  273. if (csi.params.isNotEmpty) {
  274. count = int.tryParse(csi.params.first) ?? count;
  275. }
  276. if (count < 1) {
  277. count = 1;
  278. }
  279. terminal.buffer.deleteLines(count);
  280. }
  281. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  282. var count = 1;
  283. if (csi.params.isNotEmpty) {
  284. count = int.tryParse(csi.params.first) ?? count;
  285. }
  286. if (count < 1) {
  287. count = 1;
  288. }
  289. terminal.buffer.areaScrollUp(count);
  290. }
  291. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  292. var count = 1;
  293. if (csi.params.isNotEmpty) {
  294. count = int.tryParse(csi.params.first) ?? count;
  295. }
  296. if (count < 1) {
  297. count = 1;
  298. }
  299. terminal.buffer.areaScrollDown(count);
  300. }
  301. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  302. var count = 1;
  303. if (csi.params.isNotEmpty) {
  304. count = int.tryParse(csi.params.first) ?? count;
  305. }
  306. if (count < 1) {
  307. count = 1;
  308. }
  309. terminal.buffer.insertBlankCharacters(count);
  310. }