terminal.dart 9.0 KB

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