csi.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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
  184. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  185. return;
  186. }
  187. }
  188. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  189. var response = '?1;2';
  190. if (csi.params.isNotEmpty && csi.params.first.startsWith('>')) {
  191. response = '>0;0;0';
  192. }
  193. terminal.onInput('\x1b[${response}c');
  194. }
  195. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  196. var distance = 1;
  197. if (csi.params.isNotEmpty) {
  198. distance = int.tryParse(csi.params.first) ?? distance;
  199. }
  200. terminal.buffer.movePosition(0, -distance);
  201. }
  202. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  203. var distance = 1;
  204. if (csi.params.isNotEmpty) {
  205. distance = int.tryParse(csi.params.first) ?? distance;
  206. }
  207. terminal.buffer.movePosition(0, distance);
  208. }
  209. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  210. var top = 1;
  211. var bottom = terminal.viewHeight;
  212. if (csi.params.length > 2) {
  213. return;
  214. }
  215. if (csi.params.isNotEmpty) {
  216. top = int.tryParse(csi.params[0]) ?? top;
  217. if (csi.params.length > 1) {
  218. bottom = int.tryParse(csi.params[1]) ?? bottom;
  219. }
  220. }
  221. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  222. terminal.buffer.setPosition(0, 0);
  223. }
  224. void csiDeleteHandler(CSI csi, Terminal terminal) {
  225. var count = 1;
  226. if (csi.params.isNotEmpty) {
  227. count = int.tryParse(csi.params.first) ?? count;
  228. }
  229. if (count < 1) {
  230. count = 1;
  231. }
  232. terminal.buffer.deleteChars(count);
  233. }
  234. void csiTabClearHandler(CSI csi, Terminal terminal) {
  235. // TODO
  236. }
  237. void csiWindowManipulation(CSI csi, Terminal terminal) {
  238. // not supported
  239. }
  240. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  241. var count = 1;
  242. if (csi.params.isNotEmpty) {
  243. count = int.tryParse(csi.params.first) ?? count;
  244. }
  245. if (count < 1) {
  246. count = 1;
  247. }
  248. terminal.buffer.moveCursorY(count);
  249. terminal.buffer.setCursorX(0);
  250. }
  251. void csiCursorPrecedingLineHandler(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.moveCursorY(-count);
  260. terminal.buffer.setCursorX(0);
  261. }
  262. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  263. var count = 1;
  264. if (csi.params.isNotEmpty) {
  265. count = int.tryParse(csi.params.first) ?? count;
  266. }
  267. if (count < 1) {
  268. count = 1;
  269. }
  270. terminal.buffer.insertLines(count);
  271. }
  272. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  273. var count = 1;
  274. if (csi.params.isNotEmpty) {
  275. count = int.tryParse(csi.params.first) ?? count;
  276. }
  277. if (count < 1) {
  278. count = 1;
  279. }
  280. terminal.buffer.deleteLines(count);
  281. }
  282. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  283. var count = 1;
  284. if (csi.params.isNotEmpty) {
  285. count = int.tryParse(csi.params.first) ?? count;
  286. }
  287. if (count < 1) {
  288. count = 1;
  289. }
  290. terminal.buffer.areaScrollUp(count);
  291. }
  292. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  293. var count = 1;
  294. if (csi.params.isNotEmpty) {
  295. count = int.tryParse(csi.params.first) ?? count;
  296. }
  297. if (count < 1) {
  298. count = 1;
  299. }
  300. terminal.buffer.areaScrollDown(count);
  301. }
  302. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  303. var count = 1;
  304. if (csi.params.isNotEmpty) {
  305. count = int.tryParse(csi.params.first) ?? count;
  306. }
  307. if (count < 1) {
  308. count = 1;
  309. }
  310. terminal.buffer.insertBlankCharacters(count);
  311. }