terminal.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import 'dart:async';
  2. import 'dart:math' show max;
  3. import 'package:async/async.dart';
  4. import 'package:xterm/buffer/buffer.dart';
  5. import 'package:xterm/buffer/buffer_line.dart';
  6. import 'package:xterm/buffer/cell_attr.dart';
  7. import 'package:xterm/mouse/selection.dart';
  8. import 'package:xterm/color/color_default.dart';
  9. import 'package:xterm/input/keys.dart';
  10. import 'package:xterm/input/keytab/keytab.dart';
  11. import 'package:xterm/input/keytab/keytab_escape.dart';
  12. import 'package:xterm/input/keytab/keytab_record.dart';
  13. import 'package:xterm/mouse/mouse_mode.dart';
  14. import 'package:xterm/terminal/ansi.dart';
  15. import 'package:xterm/terminal/sbc.dart';
  16. import 'package:xterm/terminal/tabs.dart';
  17. import 'package:xterm/utli/debug_handler.dart';
  18. import 'package:xterm/utli/observable.dart';
  19. typedef TerminalInputHandler = void Function(String);
  20. typedef BellHandler = void Function();
  21. typedef TitleChangeHandler = void Function(String);
  22. typedef IconChangeHandler = void Function(String);
  23. class Terminal with Observable {
  24. Terminal({
  25. this.onInput,
  26. this.onBell,
  27. this.onTitleChange,
  28. this.onIconChange,
  29. int maxLines,
  30. }) {
  31. _maxLines = maxLines;
  32. _mainBuffer = Buffer(this);
  33. _altBuffer = Buffer(this);
  34. _buffer = _mainBuffer;
  35. _input = StreamController<int>();
  36. _queue = StreamQueue<int>(_input.stream);
  37. tabs.reset();
  38. _processInput();
  39. // _buffer.write('this is magic!');
  40. }
  41. bool _dirty = false;
  42. bool get dirty {
  43. if (_dirty) {
  44. _dirty = false;
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. int _maxLines;
  51. int get maxLines {
  52. if (_maxLines == null) return null;
  53. return max(viewHeight, _maxLines);
  54. }
  55. int _viewWidth = 80;
  56. int _viewHeight = 25;
  57. int get viewWidth => _viewWidth;
  58. int get viewHeight => _viewHeight;
  59. bool _originMode = false;
  60. bool _replaceMode = false;
  61. bool _lineFeed = true;
  62. bool _screenMode = false; // DECSCNM (black on white background)
  63. bool _autoWrapMode = true;
  64. bool _bracketedPasteMode = false;
  65. bool get originMode => _originMode;
  66. bool get lineFeed => _lineFeed;
  67. bool get newLineMode => !_lineFeed;
  68. bool get bracketedPasteMode => _bracketedPasteMode;
  69. bool _showCursor = true;
  70. bool _applicationCursorKeys = false;
  71. bool _blinkingCursor = true;
  72. bool get showCursor => _showCursor;
  73. bool get applicationCursorKeys => _applicationCursorKeys;
  74. bool get blinkingCursor => _blinkingCursor;
  75. Buffer _buffer;
  76. Buffer _mainBuffer;
  77. Buffer _altBuffer;
  78. StreamController<int> _input;
  79. StreamQueue<int> _queue;
  80. bool _slowMotion = false;
  81. bool get slowMotion => _slowMotion;
  82. MouseMode _mouseMode = MouseMode.none;
  83. MouseMode get mouseMode => _mouseMode;
  84. final colorScheme = defaultColorScheme;
  85. var cellAttr = CellAttr(fgColor: defaultColorScheme.foreground);
  86. final keytab = Keytab.defaultKeytab();
  87. final selection = Selection();
  88. final tabs = Tabs();
  89. final debug = DebugHandler();
  90. final TerminalInputHandler onInput;
  91. final BellHandler onBell;
  92. final TitleChangeHandler onTitleChange;
  93. final IconChangeHandler onIconChange;
  94. void close() {
  95. _input.close();
  96. _queue.cancel();
  97. }
  98. Buffer get buffer {
  99. return _buffer;
  100. }
  101. int get cursorX => buffer.cursorX;
  102. int get cursorY => buffer.cursorY;
  103. int get scrollOffset => buffer.scrollOffset;
  104. void write(String text) async {
  105. for (var char in text.runes) {
  106. writeChar(char);
  107. }
  108. }
  109. void writeChar(int codePoint) {
  110. _input.add(codePoint);
  111. }
  112. List<BufferLine> getVisibleLines() {
  113. return _buffer.getVisibleLines();
  114. }
  115. void _processInput() async {
  116. while (true) {
  117. if (_slowMotion) {
  118. await Future.delayed(Duration(milliseconds: 100));
  119. }
  120. const esc = 0x1b;
  121. final char = await _queue.next;
  122. if (char == esc) {
  123. await ansiHandler(_queue, this);
  124. refresh();
  125. continue;
  126. }
  127. _processChar(char);
  128. }
  129. }
  130. void _processChar(int codePoint) {
  131. final sbcHandler = sbcHandlers[codePoint];
  132. if (sbcHandler != null) {
  133. debug.onSbc(codePoint);
  134. sbcHandler(codePoint, this);
  135. } else {
  136. debug.onChar(codePoint);
  137. _buffer.writeChar(codePoint);
  138. }
  139. refresh();
  140. }
  141. void refresh() {
  142. _dirty = true;
  143. notifyListeners();
  144. }
  145. void setSlowMotion(bool enabled) {
  146. _slowMotion = enabled ?? _slowMotion;
  147. }
  148. void setOriginMode(bool enabled) {
  149. _originMode = enabled ?? _originMode;
  150. buffer.setPosition(0, 0);
  151. }
  152. void setScreenMode(bool enabled) {
  153. _screenMode = true;
  154. }
  155. void setApplicationCursorKeys(bool enabled) {
  156. _applicationCursorKeys = enabled ?? _applicationCursorKeys;
  157. }
  158. void setShowCursor(bool showCursor) {
  159. _showCursor = showCursor ?? _showCursor;
  160. }
  161. void setBlinkingCursor(bool enabled) {
  162. _blinkingCursor = enabled ?? _blinkingCursor;
  163. }
  164. void setAutoWrapMode(bool enabled) {
  165. _autoWrapMode = enabled;
  166. }
  167. void setBracketedPasteMode(bool enabled) {
  168. _bracketedPasteMode = enabled;
  169. }
  170. void setInsertMode() {
  171. _replaceMode = false;
  172. }
  173. void setReplaceMode() {
  174. _replaceMode = true;
  175. }
  176. void setNewLineMode() {
  177. _lineFeed = false;
  178. }
  179. void setLineFeedMode() {
  180. _lineFeed = true;
  181. }
  182. void setMouseMode(MouseMode mode) {
  183. _mouseMode = mode ?? _mouseMode;
  184. }
  185. void useMainBuffer() {
  186. _buffer = _mainBuffer;
  187. }
  188. void useAltBuffer() {
  189. _buffer = _altBuffer;
  190. }
  191. bool isUsingMainBuffer() {
  192. return _buffer == _mainBuffer;
  193. }
  194. bool isUsingAltBuffer() {
  195. return _buffer == _altBuffer;
  196. }
  197. void resize(int width, int heigth) {
  198. buffer.resetVerticalMargins();
  199. final cursorY = buffer.convertViewLineToRawLine(buffer.cursorY);
  200. _viewWidth = max(width, 1);
  201. _viewHeight = max(heigth, 1);
  202. buffer.setCursorY(buffer.convertRawLineToViewLine(cursorY));
  203. if (buffer == _altBuffer) {
  204. buffer.clearScrollback();
  205. }
  206. }
  207. void input(
  208. TerminalKey key, {
  209. bool ctrl,
  210. bool alt,
  211. bool shift,
  212. // bool meta,
  213. }) {
  214. if (onInput == null) {
  215. return;
  216. }
  217. for (var record in keytab.records) {
  218. if (record.key != key) {
  219. continue;
  220. }
  221. if (record.ctrl != null && record.ctrl != ctrl) {
  222. continue;
  223. }
  224. if (record.shift != null && record.shift != shift) {
  225. continue;
  226. }
  227. if (record.alt != null && record.alt != alt) {
  228. continue;
  229. }
  230. if (record.anyModifier == true &&
  231. (ctrl != true && alt != true && shift != true)) {
  232. continue;
  233. }
  234. if (record.anyModifier == false &&
  235. !(ctrl != true && alt != true && shift != true)) {
  236. continue;
  237. }
  238. if (record.appScreen != null && record.appScreen != isUsingAltBuffer()) {
  239. continue;
  240. }
  241. if (record.newLine != null && record.newLine != newLineMode) {
  242. continue;
  243. }
  244. if (record.appCursorKeys != null &&
  245. record.appCursorKeys != applicationCursorKeys) {
  246. continue;
  247. }
  248. // TODO: support VT52
  249. if (record.ansi == false) {
  250. continue;
  251. }
  252. if (record.action.type == KeytabActionType.input) {
  253. debug.onMsg('input: ${record.action.value}');
  254. final input = keytabUnescape(record.action.value);
  255. onInput(input);
  256. return;
  257. }
  258. }
  259. if (ctrl) {
  260. if (key.index >= TerminalKey.keyA.index &&
  261. key.index <= TerminalKey.keyZ.index) {
  262. final input = key.index - TerminalKey.keyA.index + 1;
  263. onInput(String.fromCharCode(input));
  264. return;
  265. }
  266. }
  267. if (alt) {
  268. if (key.index >= TerminalKey.keyA.index &&
  269. key.index <= TerminalKey.keyZ.index) {
  270. final input = [0x1b, key.index - TerminalKey.keyA.index + 65];
  271. onInput(String.fromCharCodes(input));
  272. return;
  273. }
  274. }
  275. }
  276. String getSelectedText() {
  277. if (selection.isEmpty) {
  278. return '';
  279. }
  280. final builder = StringBuffer();
  281. for (var row = selection.start.y; row <= selection.end.y; row++) {
  282. if (row >= buffer.height) {
  283. break;
  284. }
  285. final line = buffer.lines[row];
  286. var xStart = 0;
  287. var xEnd = viewWidth - 1;
  288. if (row == selection.start.y) {
  289. xStart = selection.start.x;
  290. } else if (!line.isWrapped) {
  291. builder.write("\n");
  292. }
  293. if (row == selection.end.y) {
  294. xEnd = selection.end.x;
  295. }
  296. for (var col = xStart; col <= xEnd; col++) {
  297. if (col >= line.length) {
  298. break;
  299. }
  300. final cell = line.getCell(col);
  301. if (cell.width == 0) {
  302. continue;
  303. }
  304. var char = line.getCell(col).codePoint;
  305. if (char == null || char == 0x00) {
  306. const blank = 32;
  307. char = blank;
  308. }
  309. builder.writeCharCode(char);
  310. }
  311. }
  312. return builder.toString();
  313. }
  314. void paste(String data) {
  315. if (bracketedPasteMode) {
  316. data = '\x1b[200~$data\x1b[201~';
  317. }
  318. onInput(data);
  319. }
  320. int get _tabIndexFromCursor {
  321. var index = buffer.cursorX;
  322. if (buffer.cursorX == viewWidth) {
  323. index = 0;
  324. }
  325. return index;
  326. }
  327. void tabSetAtCursor() {
  328. tabs.setAt(_tabIndexFromCursor);
  329. }
  330. void tabClearAtCursor() {
  331. tabs.clearAt(_tabIndexFromCursor);
  332. }
  333. void tab() {
  334. while (buffer.cursorX < viewWidth) {
  335. buffer.write(' ');
  336. if (tabs.isSetAt(buffer.cursorX)) {
  337. break;
  338. }
  339. }
  340. }
  341. }