terminal.dart 9.3 KB

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