terminal.dart 9.4 KB

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