PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/butterfly/static/main.js

https://github.com/PKUbuntu/butterfly
JavaScript | 2891 lines | 2730 code | 160 blank | 1 comment | 560 complexity | 4cac6a01a99d89cee09c0c422076a746 MD5 | raw file
  1. (function() {
  2. var $, Selection, State, Terminal, alt, bench, cancel, cbench, cols, ctl, ctrl, first, next_leaf, open_ts, previous_leaf, quit, rows, s, selection, send, term, virtual_input, ws, ws_url,
  3. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  4. __slice = [].slice;
  5. cancel = function(ev) {
  6. if (ev.preventDefault) {
  7. ev.preventDefault();
  8. }
  9. if (ev.stopPropagation) {
  10. ev.stopPropagation();
  11. }
  12. ev.cancelBubble = true;
  13. return false;
  14. };
  15. s = 0;
  16. State = {
  17. normal: s++,
  18. escaped: s++,
  19. csi: s++,
  20. osc: s++,
  21. charset: s++,
  22. dcs: s++,
  23. ignore: s++
  24. };
  25. Terminal = (function() {
  26. function Terminal(parent, out, ctl) {
  27. var div, i, term_size;
  28. this.parent = parent;
  29. this.out = out;
  30. this.ctl = ctl != null ? ctl : function() {};
  31. this.context = this.parent.ownerDocument.defaultView;
  32. this.document = this.parent.ownerDocument;
  33. this.body = this.document.getElementsByTagName('body')[0];
  34. this.element = this.document.createElement('div');
  35. this.element.className = 'terminal focus';
  36. this.element.style.outline = 'none';
  37. this.element.setAttribute('tabindex', 0);
  38. this.parent.appendChild(this.element);
  39. div = this.document.createElement('div');
  40. div.className = 'line';
  41. this.element.appendChild(div);
  42. this.children = [div];
  43. this.compute_char_size();
  44. div.style.height = this.char_size.height + 'px';
  45. term_size = this.parent.getBoundingClientRect();
  46. this.cols = Math.floor(term_size.width / this.char_size.width);
  47. this.rows = Math.floor(term_size.height / this.char_size.height);
  48. i = this.rows - 1;
  49. while (i--) {
  50. div = this.document.createElement('div');
  51. div.style.height = this.char_size.height + 'px';
  52. div.className = 'line';
  53. this.element.appendChild(div);
  54. this.children.push(div);
  55. }
  56. this.scrollback = 100000;
  57. this.visualBell = 100;
  58. this.convertEol = false;
  59. this.termName = 'xterm';
  60. this.cursorBlink = true;
  61. this.cursorState = 0;
  62. this.last_cc = 0;
  63. this.reset_vars();
  64. this.refresh(0, this.rows - 1);
  65. this.focus();
  66. this.startBlink();
  67. addEventListener('keydown', this.keyDown.bind(this));
  68. addEventListener('keypress', this.keyPress.bind(this));
  69. addEventListener('focus', this.focus.bind(this));
  70. addEventListener('blur', this.blur.bind(this));
  71. addEventListener('paste', this.paste.bind(this));
  72. addEventListener('resize', this.resize.bind(this));
  73. if (typeof InstallTrigger !== "undefined") {
  74. this.element.contentEditable = 'true';
  75. this.element.addEventListener("mouseup", function() {
  76. var sel;
  77. sel = getSelection().getRangeAt(0);
  78. if (sel.startOffset === sel.endOffset) {
  79. return getSelection().removeAllRanges();
  80. }
  81. });
  82. }
  83. this.initmouse();
  84. setTimeout(this.resize.bind(this), 100);
  85. }
  86. Terminal.prototype.reset_vars = function() {
  87. var i;
  88. this.ybase = 0;
  89. this.ydisp = 0;
  90. this.x = 0;
  91. this.y = 0;
  92. this.cursorHidden = false;
  93. this.state = State.normal;
  94. this.queue = '';
  95. this.scrollTop = 0;
  96. this.scrollBottom = this.rows - 1;
  97. this.applicationKeypad = false;
  98. this.applicationCursor = false;
  99. this.originMode = false;
  100. this.wraparoundMode = false;
  101. this.normal = null;
  102. this.charset = null;
  103. this.gcharset = null;
  104. this.glevel = 0;
  105. this.charsets = [null];
  106. this.defAttr = (0 << 18) | (257 << 9) | (256 << 0);
  107. this.curAttr = this.defAttr;
  108. this.params = [];
  109. this.currentParam = 0;
  110. this.prefix = "";
  111. this.lines = [];
  112. i = this.rows;
  113. while (i--) {
  114. this.lines.push(this.blankLine());
  115. }
  116. this.setupStops();
  117. return this.skipNextKey = null;
  118. };
  119. Terminal.prototype.compute_char_size = function() {
  120. var test_span;
  121. test_span = document.createElement('span');
  122. test_span.textContent = '0123456789';
  123. this.children[0].appendChild(test_span);
  124. this.char_size = {
  125. width: test_span.getBoundingClientRect().width / 10,
  126. height: this.children[0].getBoundingClientRect().height
  127. };
  128. return this.children[0].removeChild(test_span);
  129. };
  130. Terminal.prototype.eraseAttr = function() {
  131. return (this.defAttr & ~0x1ff) | (this.curAttr & 0x1ff);
  132. };
  133. Terminal.prototype.focus = function() {
  134. if (this.sendFocus) {
  135. this.send('\x1b[I');
  136. }
  137. this.showCursor();
  138. this.element.classList.add('focus');
  139. return this.element.classList.remove('blur');
  140. };
  141. Terminal.prototype.blur = function() {
  142. this.cursorState = 1;
  143. this.refresh(this.y, this.y);
  144. if (this.sendFocus) {
  145. this.send('\x1b[O');
  146. }
  147. this.element.classList.add('blur');
  148. return this.element.classList.remove('focus');
  149. };
  150. Terminal.prototype.paste = function(ev) {
  151. if (ev.clipboardData) {
  152. this.send(ev.clipboardData.getData('text/plain'));
  153. } else if (this.context.clipboardData) {
  154. this.send(this.context.clipboardData.getData('Text'));
  155. }
  156. return cancel(ev);
  157. };
  158. Terminal.prototype.initmouse = function() {
  159. var encode, getButton, getCoords, pressed, sendButton, sendEvent, sendMove;
  160. pressed = 32;
  161. sendButton = function(ev) {
  162. var button, pos;
  163. button = getButton(ev);
  164. pos = getCoords(ev);
  165. if (!pos) {
  166. return;
  167. }
  168. sendEvent(button, pos);
  169. switch (ev.type) {
  170. case "mousedown":
  171. return pressed = button;
  172. case "mouseup":
  173. return pressed = 32;
  174. }
  175. };
  176. sendMove = function(ev) {
  177. var button, pos;
  178. button = pressed;
  179. pos = getCoords(ev);
  180. if (!pos) {
  181. return;
  182. }
  183. button += 32;
  184. return sendEvent(button, pos);
  185. };
  186. encode = (function(_this) {
  187. return function(data, ch) {
  188. if (!_this.utfMouse) {
  189. if (ch === 255) {
  190. return data.push(0);
  191. }
  192. if (ch > 127) {
  193. ch = 127;
  194. }
  195. return data.push(ch);
  196. } else {
  197. if (ch === 2047) {
  198. return data.push(0);
  199. }
  200. if (ch < 127) {
  201. return data.push(ch);
  202. } else {
  203. if (ch > 2047) {
  204. ch = 2047;
  205. }
  206. data.push(0xC0 | (ch >> 6));
  207. return data.push(0x80 | (ch & 0x3F));
  208. }
  209. }
  210. };
  211. })(this);
  212. sendEvent = (function(_this) {
  213. return function(button, pos) {
  214. var data;
  215. if (_this.urxvtMouse) {
  216. pos.x -= 32;
  217. pos.y -= 32;
  218. pos.x++;
  219. pos.y++;
  220. _this.send("\x1b[" + button + ";" + pos.x + ";" + pos.y + "M");
  221. return;
  222. }
  223. if (_this.sgrMouse) {
  224. pos.x -= 32;
  225. pos.y -= 32;
  226. _this.send("\x1b[<" + ((button & 3) === 3 ? button & ~3 : button) + ";" + pos.x + ";" + pos.y + ((button & 3) === 3 ? "m" : "M"));
  227. return;
  228. }
  229. data = [];
  230. encode(data, button);
  231. encode(data, pos.x);
  232. encode(data, pos.y);
  233. return _this.send("\x1b[M" + String.fromCharCode.apply(String, data));
  234. };
  235. })(this);
  236. getButton = (function(_this) {
  237. return function(ev) {
  238. var button, ctrl, meta, mod, shift;
  239. switch (ev.type) {
  240. case "mousedown":
  241. button = ev.button != null ? +ev.button : (ev.which != null ? ev.which - 1 : null);
  242. break;
  243. case "mouseup":
  244. button = 3;
  245. break;
  246. case "wheel":
  247. button = ev.deltaY < 0 ? 64 : 65;
  248. }
  249. shift = ev.shiftKey ? 4 : 0;
  250. meta = ev.metaKey ? 8 : 0;
  251. ctrl = ev.ctrlKey ? 16 : 0;
  252. mod = shift | meta | ctrl;
  253. if (_this.vt200Mouse) {
  254. mod &= ctrl;
  255. } else {
  256. if (!_this.normalMouse) {
  257. mod = 0;
  258. }
  259. }
  260. return (32 + (mod << 2)) + button;
  261. };
  262. })(this);
  263. getCoords = (function(_this) {
  264. return function(ev) {
  265. var el, h, w, x, y;
  266. x = ev.pageX;
  267. y = ev.pageY;
  268. el = _this.element;
  269. while (el && el !== _this.document.documentElement) {
  270. x -= el.offsetLeft;
  271. y -= el.offsetTop;
  272. el = "offsetParent" in el ? el.offsetParent : el.parentNode;
  273. }
  274. w = _this.element.clientWidth;
  275. h = _this.element.clientHeight;
  276. x = Math.ceil((x / w) * _this.cols);
  277. y = Math.ceil((y / h) * _this.rows);
  278. if (x < 0) {
  279. x = 0;
  280. }
  281. if (x > _this.cols) {
  282. x = _this.cols;
  283. }
  284. if (y < 0) {
  285. y = 0;
  286. }
  287. if (y > _this.rows) {
  288. y = _this.rows;
  289. }
  290. x += 32;
  291. y += 32;
  292. return {
  293. x: x,
  294. y: y,
  295. type: ev.type
  296. };
  297. };
  298. })(this);
  299. addEventListener("mousedown", (function(_this) {
  300. return function(ev) {
  301. var sm, up;
  302. if (!_this.mouseEvents) {
  303. return;
  304. }
  305. sendButton(ev);
  306. if (_this.vt200Mouse) {
  307. sendButton({
  308. __proto__: ev,
  309. type: "mouseup"
  310. });
  311. return cancel(ev);
  312. }
  313. sm = sendMove.bind(_this);
  314. if (_this.normalMouse) {
  315. addEventListener("mousemove", sm);
  316. }
  317. if (!_this.x10Mouse) {
  318. addEventListener("mouseup", up = function(ev) {
  319. sendButton(ev);
  320. if (_this.normalMouse) {
  321. removeEventListener("mousemove", sm);
  322. }
  323. removeEventListener("mouseup", up);
  324. return cancel(ev);
  325. });
  326. }
  327. return cancel(ev);
  328. };
  329. })(this));
  330. return addEventListener("wheel", (function(_this) {
  331. return function(ev) {
  332. if (_this.mouseEvents) {
  333. if (_this.x10Mouse) {
  334. return;
  335. }
  336. sendButton(ev);
  337. } else {
  338. if (_this.applicationKeypad) {
  339. return;
  340. }
  341. _this.scrollDisp(ev.deltaY > 0 ? 5 : -5);
  342. }
  343. return cancel(ev);
  344. };
  345. })(this));
  346. };
  347. Terminal.prototype.refresh = function(start, end) {
  348. var attr, bg, ch, classes, data, fg, flags, i, line, out, parent, row, width, x, y;
  349. if (end - start >= this.rows / 3) {
  350. parent = this.element.parentNode;
  351. if (parent != null) {
  352. parent.removeChild(this.element);
  353. }
  354. }
  355. width = this.cols;
  356. y = start;
  357. if (end >= this.lines.length) {
  358. end = this.lines.length - 1;
  359. }
  360. while (y <= end) {
  361. row = y + this.ydisp;
  362. line = this.lines[row];
  363. out = "";
  364. if (y === this.y && (this.ydisp === this.ybase || this.selectMode) && !this.cursorHidden) {
  365. x = this.x;
  366. } else {
  367. x = -Infinity;
  368. }
  369. attr = this.defAttr;
  370. i = 0;
  371. while (i < width) {
  372. data = line[i][0];
  373. ch = line[i][1];
  374. if (data !== attr) {
  375. if (attr !== this.defAttr) {
  376. out += "</span>";
  377. }
  378. if (data !== this.defAttr) {
  379. classes = [];
  380. out += "<span ";
  381. bg = data & 0x1ff;
  382. fg = (data >> 9) & 0x1ff;
  383. flags = data >> 18;
  384. if (flags & 1) {
  385. classes.push("bold");
  386. }
  387. if (flags & 2) {
  388. classes.push("underline");
  389. }
  390. if (flags & 4) {
  391. classes.push("blink");
  392. }
  393. if (flags & 8) {
  394. classes.push("reverse-video");
  395. }
  396. if (flags & 16) {
  397. classes.push("invisible");
  398. }
  399. if (flags & 1 && fg < 8) {
  400. fg += 8;
  401. }
  402. classes.push("bg-color-" + bg);
  403. classes.push("fg-color-" + fg);
  404. out += "class=\"";
  405. out += classes.join(" ");
  406. out += "\">";
  407. }
  408. }
  409. if (i === x) {
  410. out += "<span class=\"" + (this.cursorState ? "reverse-video " : "") + "cursor\">";
  411. }
  412. if (ch.length > 1) {
  413. out += ch;
  414. } else {
  415. switch (ch) {
  416. case "&":
  417. out += "&amp;";
  418. break;
  419. case "<":
  420. out += "&lt;";
  421. break;
  422. case ">":
  423. out += "&gt;";
  424. break;
  425. default:
  426. if (ch <= " ") {
  427. out += "&nbsp;";
  428. } else {
  429. if (("\uff00" < ch && ch < "\uffef")) {
  430. i++;
  431. }
  432. out += ch;
  433. }
  434. }
  435. }
  436. if (i === x) {
  437. out += "</span>";
  438. }
  439. attr = data;
  440. i++;
  441. }
  442. if (attr !== this.defAttr) {
  443. out += "</span>";
  444. }
  445. this.children[y].innerHTML = out;
  446. y++;
  447. }
  448. return parent != null ? parent.appendChild(this.element) : void 0;
  449. };
  450. Terminal.prototype._cursorBlink = function() {
  451. var cursor;
  452. this.cursorState ^= 1;
  453. cursor = this.element.querySelector(".cursor");
  454. if (!cursor) {
  455. return;
  456. }
  457. if (cursor.classList.contains("reverse-video")) {
  458. return cursor.classList.remove("reverse-video");
  459. } else {
  460. return cursor.classList.add("reverse-video");
  461. }
  462. };
  463. Terminal.prototype.showCursor = function() {
  464. if (!this.cursorState) {
  465. this.cursorState = 1;
  466. return this.refresh(this.y, this.y);
  467. }
  468. };
  469. Terminal.prototype.startBlink = function() {
  470. if (!this.cursorBlink) {
  471. return;
  472. }
  473. this._blinker = (function(_this) {
  474. return function() {
  475. return _this._cursorBlink();
  476. };
  477. })(this);
  478. return this.t_blink = setInterval(this._blinker, 500);
  479. };
  480. Terminal.prototype.refreshBlink = function() {
  481. if (!this.cursorBlink) {
  482. return;
  483. }
  484. clearInterval(this.t_blink);
  485. return this.t_blink = setInterval(this._blinker, 500);
  486. };
  487. Terminal.prototype.scroll = function() {
  488. var row;
  489. if (++this.ybase === this.scrollback) {
  490. this.ybase = this.ybase / 2 | 0;
  491. this.lines = this.lines.slice(-(this.ybase + this.rows) + 1);
  492. }
  493. this.ydisp = this.ybase;
  494. row = this.ybase + this.rows - 1;
  495. row -= this.rows - 1 - this.scrollBottom;
  496. if (row === this.lines.length) {
  497. this.lines.push(this.blankLine());
  498. } else {
  499. this.lines.splice(row, 0, this.blankLine());
  500. }
  501. if (this.scrollTop !== 0) {
  502. if (this.ybase !== 0) {
  503. this.ybase--;
  504. this.ydisp = this.ybase;
  505. }
  506. this.lines.splice(this.ybase + this.scrollTop, 1);
  507. }
  508. this.updateRange(this.scrollTop);
  509. return this.updateRange(this.scrollBottom);
  510. };
  511. Terminal.prototype.scrollDisp = function(disp) {
  512. this.ydisp += disp;
  513. if (this.ydisp > this.ybase) {
  514. this.ydisp = this.ybase;
  515. } else {
  516. if (this.ydisp < 0) {
  517. this.ydisp = 0;
  518. }
  519. }
  520. return this.refresh(0, this.rows - 1);
  521. };
  522. Terminal.prototype.write = function(data) {
  523. var ch, cs, i, j, l, pt, valid, _ref;
  524. this.refreshStart = this.y;
  525. this.refreshEnd = this.y;
  526. if (this.ybase !== this.ydisp) {
  527. this.ydisp = this.ybase;
  528. this.maxRange();
  529. }
  530. i = 0;
  531. l = data.length;
  532. while (i < l) {
  533. ch = data[i];
  534. switch (this.state) {
  535. case State.normal:
  536. switch (ch) {
  537. case "\x07":
  538. this.bell();
  539. break;
  540. case "\n":
  541. case "\x0b":
  542. case "\x0c":
  543. if (this.convertEol) {
  544. this.x = 0;
  545. }
  546. this.y++;
  547. if (this.y > this.scrollBottom) {
  548. this.y--;
  549. this.scroll();
  550. }
  551. break;
  552. case "\r":
  553. this.x = 0;
  554. break;
  555. case "\b":
  556. if (this.x > 0) {
  557. this.x--;
  558. }
  559. break;
  560. case "\t":
  561. this.x = this.nextStop();
  562. break;
  563. case "\x0e":
  564. this.setgLevel(1);
  565. break;
  566. case "\x0f":
  567. this.setgLevel(0);
  568. break;
  569. case "\x1b":
  570. this.state = State.escaped;
  571. break;
  572. default:
  573. if (ch >= " ") {
  574. if ((_ref = this.charset) != null ? _ref[ch] : void 0) {
  575. ch = this.charset[ch];
  576. }
  577. if (this.x >= this.cols) {
  578. this.x = 0;
  579. this.y++;
  580. if (this.y > this.scrollBottom) {
  581. this.y--;
  582. this.scroll();
  583. }
  584. }
  585. this.lines[this.y + this.ybase][this.x] = [this.curAttr, ch];
  586. this.x++;
  587. this.updateRange(this.y);
  588. if (("\uff00" < ch && ch < "\uffef")) {
  589. j = this.y + this.ybase;
  590. if (this.cols < 2 || this.x >= this.cols) {
  591. this.lines[j][this.x - 1] = [this.curAttr, " "];
  592. break;
  593. }
  594. this.lines[j][this.x] = [this.curAttr, " "];
  595. this.x++;
  596. }
  597. }
  598. }
  599. break;
  600. case State.escaped:
  601. switch (ch) {
  602. case "[":
  603. this.params = [];
  604. this.currentParam = 0;
  605. this.state = State.csi;
  606. break;
  607. case "]":
  608. this.params = [];
  609. this.currentParam = 0;
  610. this.state = State.osc;
  611. break;
  612. case "P":
  613. this.params = [];
  614. this.currentParam = 0;
  615. this.state = State.dcs;
  616. break;
  617. case "_":
  618. this.state = State.ignore;
  619. break;
  620. case "^":
  621. this.state = State.ignore;
  622. break;
  623. case "c":
  624. this.reset();
  625. break;
  626. case "E":
  627. this.x = 0;
  628. this.index();
  629. break;
  630. case "D":
  631. this.index();
  632. break;
  633. case "M":
  634. this.reverseIndex();
  635. break;
  636. case "%":
  637. this.setgLevel(0);
  638. this.setgCharset(0, Terminal.prototype.charsets.US);
  639. this.state = State.normal;
  640. i++;
  641. break;
  642. case "(":
  643. case ")":
  644. case "*":
  645. case "+":
  646. case "-":
  647. case ".":
  648. switch (ch) {
  649. case "(":
  650. this.gcharset = 0;
  651. break;
  652. case ")":
  653. case "-":
  654. this.gcharset = 1;
  655. break;
  656. case "*":
  657. case ".":
  658. this.gcharset = 2;
  659. break;
  660. case "+":
  661. this.gcharset = 3;
  662. }
  663. this.state = State.charset;
  664. break;
  665. case "/":
  666. this.gcharset = 3;
  667. this.state = State.charset;
  668. i--;
  669. break;
  670. case "n":
  671. this.setgLevel(2);
  672. break;
  673. case "o":
  674. this.setgLevel(3);
  675. break;
  676. case "|":
  677. this.setgLevel(3);
  678. break;
  679. case "}":
  680. this.setgLevel(2);
  681. break;
  682. case "~":
  683. this.setgLevel(1);
  684. break;
  685. case "7":
  686. this.saveCursor();
  687. this.state = State.normal;
  688. break;
  689. case "8":
  690. this.restoreCursor();
  691. this.state = State.normal;
  692. break;
  693. case "#":
  694. this.state = State.normal;
  695. i++;
  696. break;
  697. case "H":
  698. this.tabSet();
  699. break;
  700. case "=":
  701. this.applicationKeypad = true;
  702. this.state = State.normal;
  703. break;
  704. case ">":
  705. this.applicationKeypad = false;
  706. this.state = State.normal;
  707. break;
  708. default:
  709. this.state = State.normal;
  710. console.log("Unknown ESC control:", ch);
  711. }
  712. break;
  713. case State.charset:
  714. switch (ch) {
  715. case "0":
  716. cs = Terminal.prototype.charsets.SCLD;
  717. break;
  718. case "A":
  719. cs = Terminal.prototype.charsets.UK;
  720. break;
  721. case "B":
  722. cs = Terminal.prototype.charsets.US;
  723. break;
  724. case "4":
  725. cs = Terminal.prototype.charsets.Dutch;
  726. break;
  727. case "C":
  728. case "5":
  729. cs = Terminal.prototype.charsets.Finnish;
  730. break;
  731. case "R":
  732. cs = Terminal.prototype.charsets.French;
  733. break;
  734. case "Q":
  735. cs = Terminal.prototype.charsets.FrenchCanadian;
  736. break;
  737. case "K":
  738. cs = Terminal.prototype.charsets.German;
  739. break;
  740. case "Y":
  741. cs = Terminal.prototype.charsets.Italian;
  742. break;
  743. case "E":
  744. case "6":
  745. cs = Terminal.prototype.charsets.NorwegianDanish;
  746. break;
  747. case "Z":
  748. cs = Terminal.prototype.charsets.Spanish;
  749. break;
  750. case "H":
  751. case "7":
  752. cs = Terminal.prototype.charsets.Swedish;
  753. break;
  754. case "=":
  755. cs = Terminal.prototype.charsets.Swiss;
  756. break;
  757. case "/":
  758. cs = Terminal.prototype.charsets.ISOLatin;
  759. i++;
  760. break;
  761. default:
  762. cs = Terminal.prototype.charsets.US;
  763. }
  764. this.setgCharset(this.gcharset, cs);
  765. this.gcharset = null;
  766. this.state = State.normal;
  767. break;
  768. case State.osc:
  769. if (ch === "\x1b" || ch === "\x07") {
  770. if (ch === "\x1b") {
  771. i++;
  772. }
  773. this.params.push(this.currentParam);
  774. switch (this.params[0]) {
  775. case 0:
  776. case 1:
  777. case 2:
  778. if (this.params[1]) {
  779. this.title = this.params[1] + " - ƸӜƷ butterfly";
  780. this.handleTitle(this.title);
  781. }
  782. }
  783. this.params = [];
  784. this.currentParam = 0;
  785. this.state = State.normal;
  786. } else {
  787. if (!this.params.length) {
  788. if (ch >= "0" && ch <= "9") {
  789. this.currentParam = this.currentParam * 10 + ch.charCodeAt(0) - 48;
  790. } else if (ch === ";") {
  791. this.params.push(this.currentParam);
  792. this.currentParam = "";
  793. }
  794. } else {
  795. this.currentParam += ch;
  796. }
  797. }
  798. break;
  799. case State.csi:
  800. if (ch === "?" || ch === ">" || ch === "!") {
  801. this.prefix = ch;
  802. break;
  803. }
  804. if (ch >= "0" && ch <= "9") {
  805. this.currentParam = this.currentParam * 10 + ch.charCodeAt(0) - 48;
  806. break;
  807. }
  808. if (ch === "$" || ch === "\"" || ch === " " || ch === "'") {
  809. break;
  810. }
  811. this.params.push(this.currentParam);
  812. this.currentParam = 0;
  813. if (ch === ";") {
  814. break;
  815. }
  816. this.state = State.normal;
  817. switch (ch) {
  818. case "A":
  819. this.cursorUp(this.params);
  820. break;
  821. case "B":
  822. this.cursorDown(this.params);
  823. break;
  824. case "C":
  825. this.cursorForward(this.params);
  826. break;
  827. case "D":
  828. this.cursorBackward(this.params);
  829. break;
  830. case "H":
  831. this.cursorPos(this.params);
  832. break;
  833. case "J":
  834. this.eraseInDisplay(this.params);
  835. break;
  836. case "K":
  837. this.eraseInLine(this.params);
  838. break;
  839. case "m":
  840. if (!this.prefix) {
  841. this.charAttributes(this.params);
  842. }
  843. break;
  844. case "n":
  845. if (!this.prefix) {
  846. this.deviceStatus(this.params);
  847. }
  848. break;
  849. case "@":
  850. this.insertChars(this.params);
  851. break;
  852. case "E":
  853. this.cursorNextLine(this.params);
  854. break;
  855. case "F":
  856. this.cursorPrecedingLine(this.params);
  857. break;
  858. case "G":
  859. this.cursorCharAbsolute(this.params);
  860. break;
  861. case "L":
  862. this.insertLines(this.params);
  863. break;
  864. case "M":
  865. this.deleteLines(this.params);
  866. break;
  867. case "P":
  868. this.deleteChars(this.params);
  869. break;
  870. case "X":
  871. this.eraseChars(this.params);
  872. break;
  873. case "`":
  874. this.charPosAbsolute(this.params);
  875. break;
  876. case "a":
  877. this.HPositionRelative(this.params);
  878. break;
  879. case "c":
  880. this.sendDeviceAttributes(this.params);
  881. break;
  882. case "d":
  883. this.linePosAbsolute(this.params);
  884. break;
  885. case "e":
  886. this.VPositionRelative(this.params);
  887. break;
  888. case "f":
  889. this.HVPosition(this.params);
  890. break;
  891. case "h":
  892. this.setMode(this.params);
  893. break;
  894. case "l":
  895. this.resetMode(this.params);
  896. break;
  897. case "r":
  898. this.setScrollRegion(this.params);
  899. break;
  900. case "s":
  901. this.saveCursor(this.params);
  902. break;
  903. case "u":
  904. this.restoreCursor(this.params);
  905. break;
  906. case "I":
  907. this.cursorForwardTab(this.params);
  908. break;
  909. case "S":
  910. this.scrollUp(this.params);
  911. break;
  912. case "T":
  913. if (this.params.length < 2 && !this.prefix) {
  914. this.scrollDown(this.params);
  915. }
  916. break;
  917. case "Z":
  918. this.cursorBackwardTab(this.params);
  919. break;
  920. case "b":
  921. this.repeatPrecedingCharacter(this.params);
  922. break;
  923. case "g":
  924. this.tabClear(this.params);
  925. break;
  926. case "p":
  927. if (this.prefix === '!') {
  928. this.softReset(this.params);
  929. }
  930. break;
  931. default:
  932. console.error("Unknown CSI code: %s.", ch);
  933. }
  934. this.prefix = "";
  935. break;
  936. case State.dcs:
  937. if (ch === "\x1b" || ch === "\x07") {
  938. if (ch === "\x1b") {
  939. i++;
  940. }
  941. switch (this.prefix) {
  942. case "":
  943. break;
  944. case "$q":
  945. pt = this.currentParam;
  946. valid = false;
  947. switch (pt) {
  948. case "\"q":
  949. pt = "0\"q";
  950. break;
  951. case "\"p":
  952. pt = "61\"p";
  953. break;
  954. case "r":
  955. pt = "" + (this.scrollTop + 1) + ";" + (this.scrollBottom + 1) + "r";
  956. break;
  957. case "m":
  958. pt = "0m";
  959. break;
  960. default:
  961. console.error("Unknown DCS Pt: %s.", pt);
  962. pt = "";
  963. }
  964. this.send("\x1bP" + +valid + "$r" + pt + "\x1b\\");
  965. break;
  966. case "+q":
  967. pt = this.currentParam;
  968. valid = false;
  969. this.send("\x1bP" + +valid + "+r" + pt + "\x1b\\");
  970. break;
  971. default:
  972. console.error("Unknown DCS prefix: %s.", this.prefix);
  973. }
  974. this.currentParam = 0;
  975. this.prefix = "";
  976. this.state = State.normal;
  977. } else if (!this.currentParam) {
  978. if (!this.prefix && ch !== "$" && ch !== "+") {
  979. this.currentParam = ch;
  980. } else if (this.prefix.length === 2) {
  981. this.currentParam = ch;
  982. } else {
  983. this.prefix += ch;
  984. }
  985. } else {
  986. this.currentParam += ch;
  987. }
  988. break;
  989. case State.ignore:
  990. if (ch === "\x1b" || ch === "\x07") {
  991. if (ch === "\x1b") {
  992. i++;
  993. }
  994. this.state = State.normal;
  995. }
  996. }
  997. i++;
  998. }
  999. this.updateRange(this.y);
  1000. return this.refresh(this.refreshStart, this.refreshEnd);
  1001. };
  1002. Terminal.prototype.writeln = function(data) {
  1003. return this.write("" + data + "\r\n");
  1004. };
  1005. Terminal.prototype.keyDown = function(ev) {
  1006. var id, key, t, _ref;
  1007. if (ev.keyCode > 15 && ev.keyCode < 19) {
  1008. return true;
  1009. }
  1010. if ((ev.shiftKey || ev.ctrlKey) && ev.keyCode === 45) {
  1011. return true;
  1012. }
  1013. if ((ev.shiftKey && ev.ctrlKey) && ((_ref = ev.keyCode) === 67 || _ref === 86)) {
  1014. return true;
  1015. }
  1016. if (ev.altKey && ev.keyCode === 90 && !this.skipNextKey) {
  1017. this.skipNextKey = true;
  1018. this.element.classList.add('skip');
  1019. return cancel(ev);
  1020. }
  1021. if (this.skipNextKey) {
  1022. this.skipNextKey = false;
  1023. this.element.classList.remove('skip');
  1024. return true;
  1025. }
  1026. switch (ev.keyCode) {
  1027. case 8:
  1028. key = ev.altKey ? "\x1b" : "";
  1029. if (ev.shiftKey) {
  1030. key += "\x08";
  1031. break;
  1032. }
  1033. key += "\x7f";
  1034. break;
  1035. case 9:
  1036. if (ev.shiftKey) {
  1037. key = "\x1b[Z";
  1038. break;
  1039. }
  1040. key = "\t";
  1041. break;
  1042. case 13:
  1043. key = "\r";
  1044. break;
  1045. case 27:
  1046. key = "\x1b";
  1047. break;
  1048. case 37:
  1049. if (this.applicationCursor) {
  1050. key = "\x1bOD";
  1051. break;
  1052. }
  1053. key = "\x1b[D";
  1054. break;
  1055. case 39:
  1056. if (this.applicationCursor) {
  1057. key = "\x1bOC";
  1058. break;
  1059. }
  1060. key = "\x1b[C";
  1061. break;
  1062. case 38:
  1063. if (this.applicationCursor) {
  1064. key = "\x1bOA";
  1065. break;
  1066. }
  1067. if (ev.ctrlKey) {
  1068. this.scrollDisp(-1);
  1069. return cancel(ev);
  1070. } else {
  1071. key = "\x1b[A";
  1072. }
  1073. break;
  1074. case 40:
  1075. if (this.applicationCursor) {
  1076. key = "\x1bOB";
  1077. break;
  1078. }
  1079. if (ev.ctrlKey) {
  1080. this.scrollDisp(1);
  1081. return cancel(ev);
  1082. } else {
  1083. key = "\x1b[B";
  1084. }
  1085. break;
  1086. case 46:
  1087. key = "\x1b[3~";
  1088. break;
  1089. case 45:
  1090. key = "\x1b[2~";
  1091. break;
  1092. case 36:
  1093. if (this.applicationKeypad) {
  1094. key = "\x1bOH";
  1095. break;
  1096. }
  1097. key = "\x1bOH";
  1098. break;
  1099. case 35:
  1100. if (this.applicationKeypad) {
  1101. key = "\x1bOF";
  1102. break;
  1103. }
  1104. key = "\x1bOF";
  1105. break;
  1106. case 33:
  1107. if (ev.shiftKey) {
  1108. this.scrollDisp(-(this.rows - 1));
  1109. return cancel(ev);
  1110. } else {
  1111. key = "\x1b[5~";
  1112. }
  1113. break;
  1114. case 34:
  1115. if (ev.shiftKey) {
  1116. this.scrollDisp(this.rows - 1);
  1117. return cancel(ev);
  1118. } else {
  1119. key = "\x1b[6~";
  1120. }
  1121. break;
  1122. case 112:
  1123. key = "\x1bOP";
  1124. break;
  1125. case 113:
  1126. key = "\x1bOQ";
  1127. break;
  1128. case 114:
  1129. key = "\x1bOR";
  1130. break;
  1131. case 115:
  1132. key = "\x1bOS";
  1133. break;
  1134. case 116:
  1135. key = "\x1b[15~";
  1136. break;
  1137. case 117:
  1138. key = "\x1b[17~";
  1139. break;
  1140. case 118:
  1141. key = "\x1b[18~";
  1142. break;
  1143. case 119:
  1144. key = "\x1b[19~";
  1145. break;
  1146. case 120:
  1147. key = "\x1b[20~";
  1148. break;
  1149. case 121:
  1150. key = "\x1b[21~";
  1151. break;
  1152. case 122:
  1153. key = "\x1b[23~";
  1154. break;
  1155. case 123:
  1156. key = "\x1b[24~";
  1157. break;
  1158. default:
  1159. if (ev.ctrlKey) {
  1160. if (ev.keyCode >= 65 && ev.keyCode <= 90) {
  1161. if (ev.keyCode === 67) {
  1162. t = (new Date()).getTime();
  1163. if ((t - this.last_cc) < 75) {
  1164. id = (setTimeout(function() {})) - 6;
  1165. this.write('\r\n --8<------8<-- Sectioned --8<------8<-- \r\n\r\n');
  1166. while (id--) {
  1167. if (id !== this.t_bell && id !== this.t_queue && id !== this.t_blink) {
  1168. clearTimeout(id);
  1169. }
  1170. }
  1171. }
  1172. this.last_cc = t;
  1173. }
  1174. key = String.fromCharCode(ev.keyCode - 64);
  1175. } else if (ev.keyCode === 32) {
  1176. key = String.fromCharCode(0);
  1177. } else if (ev.keyCode >= 51 && ev.keyCode <= 55) {
  1178. key = String.fromCharCode(ev.keyCode - 51 + 27);
  1179. } else if (ev.keyCode === 56) {
  1180. key = String.fromCharCode(127);
  1181. } else if (ev.keyCode === 219) {
  1182. key = String.fromCharCode(27);
  1183. } else {
  1184. if (ev.keyCode === 221) {
  1185. key = String.fromCharCode(29);
  1186. }
  1187. }
  1188. } else if ((ev.altKey && __indexOf.call(navigator.platform, 'Mac') < 0) || (ev.metaKey && __indexOf.call(navigator.platform, 'Mac') >= 0)) {
  1189. if (ev.keyCode >= 65 && ev.keyCode <= 90) {
  1190. key = "\x1b" + String.fromCharCode(ev.keyCode + 32);
  1191. } else if (ev.keyCode === 192) {
  1192. key = "\x1b`";
  1193. } else {
  1194. if (ev.keyCode >= 48 && ev.keyCode <= 57) {
  1195. key = "\x1b" + (ev.keyCode - 48);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. if (ev.keyCode >= 37 && ev.keyCode <= 40) {
  1201. if (ev.ctrlKey) {
  1202. key = key.slice(0, -1) + "1;5" + key.slice(-1);
  1203. } else if (ev.altKey) {
  1204. key = key.slice(0, -1) + "1;3" + key.slice(-1);
  1205. } else if (ev.shiftKey) {
  1206. key = key.slice(0, -1) + "1;4" + key.slice(-1);
  1207. }
  1208. }
  1209. if (!key) {
  1210. return true;
  1211. }
  1212. if (this.prefixMode) {
  1213. this.leavePrefix();
  1214. return cancel(ev);
  1215. }
  1216. if (this.selectMode) {
  1217. this.keySelect(ev, key);
  1218. return cancel(ev);
  1219. }
  1220. this.showCursor();
  1221. this.handler(key);
  1222. return cancel(ev);
  1223. };
  1224. Terminal.prototype.setgLevel = function(g) {
  1225. this.glevel = g;
  1226. return this.charset = this.charsets[g];
  1227. };
  1228. Terminal.prototype.setgCharset = function(g, charset) {
  1229. this.charsets[g] = charset;
  1230. if (this.glevel === g) {
  1231. return this.charset = charset;
  1232. }
  1233. };
  1234. Terminal.prototype.keyPress = function(ev) {
  1235. var key;
  1236. if (this.skipNextKey === false) {
  1237. this.skipNextKey = null;
  1238. return true;
  1239. }
  1240. cancel(ev);
  1241. if (ev.charCode) {
  1242. key = ev.charCode;
  1243. } else if (ev.which == null) {
  1244. key = ev.keyCode;
  1245. } else if (ev.which !== 0 && ev.charCode !== 0) {
  1246. key = ev.which;
  1247. } else {
  1248. return false;
  1249. }
  1250. if (!key || ev.ctrlKey || ev.altKey || ev.metaKey) {
  1251. return false;
  1252. }
  1253. key = String.fromCharCode(key);
  1254. this.showCursor();
  1255. this.handler(key);
  1256. return false;
  1257. };
  1258. Terminal.prototype.send = function(data) {
  1259. if (!this.queue) {
  1260. this.t_queue = setTimeout(((function(_this) {
  1261. return function() {
  1262. _this.handler(_this.queue);
  1263. return _this.queue = "";
  1264. };
  1265. })(this)), 1);
  1266. }
  1267. return this.queue += data;
  1268. };
  1269. Terminal.prototype.bell = function() {
  1270. if (!this.visualBell) {
  1271. return;
  1272. }
  1273. this.element.classList.add("bell");
  1274. return this.t_bell = setTimeout(((function(_this) {
  1275. return function() {
  1276. return _this.element.classList.remove("bell");
  1277. };
  1278. })(this)), this.visualBell);
  1279. };
  1280. Terminal.prototype.resize = function() {
  1281. var ch, el, i, j, line, old_cols, old_rows, term_size;
  1282. old_cols = this.cols;
  1283. old_rows = this.rows;
  1284. this.compute_char_size();
  1285. term_size = this.parent.getBoundingClientRect();
  1286. this.cols = Math.floor(term_size.width / this.char_size.width);
  1287. this.rows = Math.floor(term_size.height / this.char_size.height);
  1288. if (old_cols === this.cols && old_rows === this.rows) {
  1289. return;
  1290. }
  1291. this.ctl('Resize', this.cols, this.rows);
  1292. if (old_cols < this.cols) {
  1293. ch = [this.defAttr, " "];
  1294. i = this.lines.length;
  1295. while (i--) {
  1296. while (this.lines[i].length < this.cols) {
  1297. this.lines[i].push(ch);
  1298. }
  1299. }
  1300. } else if (old_cols > this.cols) {
  1301. i = this.lines.length;
  1302. while (i--) {
  1303. while (this.lines[i].length > this.cols) {
  1304. this.lines[i].pop();
  1305. }
  1306. }
  1307. }
  1308. this.setupStops(old_cols);
  1309. j = old_rows;
  1310. if (j < this.rows) {
  1311. el = this.element;
  1312. while (j++ < this.rows) {
  1313. if (this.lines.length < this.rows + this.ybase) {
  1314. this.lines.push(this.blankLine());
  1315. }
  1316. if (this.children.length < this.rows) {
  1317. line = this.document.createElement("div");
  1318. line.className = 'line';
  1319. line.style.height = this.char_size.height + 'px';
  1320. el.appendChild(line);
  1321. this.children.push(line);
  1322. }
  1323. }
  1324. } else if (j > this.rows) {
  1325. while (j-- > this.rows) {
  1326. if (this.lines.length > this.rows + this.ybase) {
  1327. this.lines.pop();
  1328. }
  1329. if (this.children.length > this.rows) {
  1330. el = this.children.pop();
  1331. if (!el) {
  1332. continue;
  1333. }
  1334. el.parentNode.removeChild(el);
  1335. }
  1336. }
  1337. }
  1338. if (this.y >= this.rows) {
  1339. this.y = this.rows - 1;
  1340. }
  1341. if (this.x >= this.cols) {
  1342. this.x = this.cols - 1;
  1343. }
  1344. this.scrollTop = 0;
  1345. this.scrollBottom = this.rows - 1;
  1346. this.refresh(0, this.rows - 1);
  1347. return this.normal = null;
  1348. };
  1349. Terminal.prototype.updateRange = function(y) {
  1350. if (y < this.refreshStart) {
  1351. this.refreshStart = y;
  1352. }
  1353. if (y > this.refreshEnd) {
  1354. return this.refreshEnd = y;
  1355. }
  1356. };
  1357. Terminal.prototype.maxRange = function() {
  1358. this.refreshStart = 0;
  1359. return this.refreshEnd = this.rows - 1;
  1360. };
  1361. Terminal.prototype.setupStops = function(i) {
  1362. var _results;
  1363. if (i != null) {
  1364. if (!this.tabs[i]) {
  1365. i = this.prevStop(i);
  1366. }
  1367. } else {
  1368. this.tabs = {};
  1369. i = 0;
  1370. }
  1371. _results = [];
  1372. while (i < this.cols) {
  1373. this.tabs[i] = true;
  1374. _results.push(i += 8);
  1375. }
  1376. return _results;
  1377. };
  1378. Terminal.prototype.prevStop = function(x) {
  1379. if (x == null) {
  1380. x = this.x;
  1381. }
  1382. while (!this.tabs[--x] && x > 0) {
  1383. 1;
  1384. }
  1385. if (x >= this.cols) {
  1386. return this.cols - 1;
  1387. } else {
  1388. if (x < 0) {
  1389. return 0;
  1390. } else {
  1391. return x;
  1392. }
  1393. }
  1394. };
  1395. Terminal.prototype.nextStop = function(x) {
  1396. if (x == null) {
  1397. x = this.x;
  1398. }
  1399. while (!this.tabs[++x] && x < this.cols) {
  1400. 1;
  1401. }
  1402. if (x >= this.cols) {
  1403. return this.cols - 1;
  1404. } else {
  1405. if (x < 0) {
  1406. return 0;
  1407. } else {
  1408. return x;
  1409. }
  1410. }
  1411. };
  1412. Terminal.prototype.eraseRight = function(x, y) {
  1413. var ch, line;
  1414. line = this.lines[this.ybase + y];
  1415. ch = [this.eraseAttr(), " "];
  1416. while (x < this.cols) {
  1417. line[x] = ch;
  1418. x++;
  1419. }
  1420. return this.updateRange(y);
  1421. };
  1422. Terminal.prototype.eraseLeft = function(x, y) {
  1423. var ch, line;
  1424. line = this.lines[this.ybase + y];
  1425. ch = [this.eraseAttr(), " "];
  1426. x++;
  1427. while (x--) {
  1428. line[x] = ch;
  1429. }
  1430. return this.updateRange(y);
  1431. };
  1432. Terminal.prototype.eraseLine = function(y) {
  1433. return this.eraseRight(0, y);
  1434. };
  1435. Terminal.prototype.blankLine = function(cur) {
  1436. var attr, ch, i, line;
  1437. attr = (cur ? this.eraseAttr() : this.defAttr);
  1438. ch = [attr, " "];
  1439. line = [];
  1440. i = 0;
  1441. while (i < this.cols) {
  1442. line[i] = ch;
  1443. i++;
  1444. }
  1445. return line;
  1446. };
  1447. Terminal.prototype.ch = function(cur) {
  1448. if (cur) {
  1449. return [this.eraseAttr(), " "];
  1450. } else {
  1451. return [this.defAttr, " "];
  1452. }
  1453. };
  1454. Terminal.prototype.isterm = function(term) {
  1455. return ("" + this.termName).indexOf(term) === 0;
  1456. };
  1457. Terminal.prototype.handler = function(data) {
  1458. return this.out(data);
  1459. };
  1460. Terminal.prototype.handleTitle = function(title) {
  1461. return document.title = title;
  1462. };
  1463. Terminal.prototype.index = function() {
  1464. this.y++;
  1465. if (this.y > this.scrollBottom) {
  1466. this.y--;
  1467. this.scroll();
  1468. }
  1469. return this.state = State.normal;
  1470. };
  1471. Terminal.prototype.reverseIndex = function() {
  1472. var j;
  1473. this.y--;
  1474. if (this.y < this.scrollTop) {
  1475. this.y++;
  1476. this.lines.splice(this.y + this.ybase, 0, this.blankLine(true));
  1477. j = this.rows - 1 - this.scrollBottom;
  1478. this.lines.splice(this.rows - 1 + this.ybase - j + 1, 1);
  1479. this.updateRange(this.scrollTop);
  1480. this.updateRange(this.scrollBottom);
  1481. }
  1482. return this.state = State.normal;
  1483. };
  1484. Terminal.prototype.reset = function() {
  1485. this.reset_vars();
  1486. return this.refresh(0, this.rows - 1);
  1487. };
  1488. Terminal.prototype.tabSet = function() {
  1489. this.tabs[this.x] = true;
  1490. return this.state = State.normal;
  1491. };
  1492. Terminal.prototype.cursorUp = function(params) {
  1493. var param;
  1494. param = params[0];
  1495. if (param < 1) {
  1496. param = 1;
  1497. }
  1498. this.y -= param;
  1499. if (this.y < 0) {
  1500. return this.y = 0;
  1501. }
  1502. };
  1503. Terminal.prototype.cursorDown = function(params) {
  1504. var param;
  1505. param = params[0];
  1506. if (param < 1) {
  1507. param = 1;
  1508. }
  1509. this.y += param;
  1510. if (this.y >= this.rows) {
  1511. return this.y = this.rows - 1;
  1512. }
  1513. };
  1514. Terminal.prototype.cursorForward = function(params) {
  1515. var param;
  1516. param = params[0];
  1517. if (param < 1) {
  1518. param = 1;
  1519. }
  1520. this.x += param;
  1521. if (this.x >= this.cols) {
  1522. return this.x = this.cols - 1;
  1523. }
  1524. };
  1525. Terminal.prototype.cursorBackward = function(params) {
  1526. var param;
  1527. param = params[0];
  1528. if (param < 1) {
  1529. param = 1;
  1530. }
  1531. this.x -= param;
  1532. if (this.x < 0) {
  1533. return this.x = 0;
  1534. }
  1535. };
  1536. Terminal.prototype.cursorPos = function(params) {
  1537. var col, row;
  1538. row = params[0] - 1;
  1539. if (params.length >= 2) {
  1540. col = params[1] - 1;
  1541. } else {
  1542. col = 0;
  1543. }
  1544. if (row < 0) {
  1545. row = 0;
  1546. } else {
  1547. if (row >= this.rows) {
  1548. row = this.rows - 1;
  1549. }
  1550. }
  1551. if (col < 0) {
  1552. col = 0;
  1553. } else {
  1554. if (col >= this.cols) {
  1555. col = this.cols - 1;
  1556. }
  1557. }
  1558. this.x = col;
  1559. return this.y = row;
  1560. };
  1561. Terminal.prototype.eraseInDisplay = function(params) {
  1562. var j, _results, _results1, _results2;
  1563. switch (params[0]) {
  1564. case 0:
  1565. this.eraseRight(this.x, this.y);
  1566. j = this.y + 1;
  1567. _results = [];
  1568. while (j < this.rows) {
  1569. this.eraseLine(j);
  1570. _results.push(j++);
  1571. }
  1572. return _results;
  1573. break;
  1574. case 1:
  1575. this.eraseLeft(this.x, this.y);
  1576. j = this.y;
  1577. _results1 = [];
  1578. while (j--) {
  1579. _results1.push(this.eraseLine(j));
  1580. }
  1581. return _results1;
  1582. break;
  1583. case 2:
  1584. j = this.rows;
  1585. _results2 = [];
  1586. while (j--) {
  1587. _results2.push(this.eraseLine(j));
  1588. }
  1589. return _results2;
  1590. }
  1591. };
  1592. Terminal.prototype.eraseInLine = function(params) {
  1593. switch (params[0]) {
  1594. case 0:
  1595. return this.eraseRight(this.x, this.y);
  1596. case 1:
  1597. return this.eraseLeft(this.x, this.y);
  1598. case 2:
  1599. return this.eraseLine(this.y);
  1600. }
  1601. };
  1602. Terminal.prototype.charAttributes = function(params) {
  1603. var bg, fg, flags, i, l, p;
  1604. if (params.length === 1 && params[0] === 0) {
  1605. this.curAttr = this.defAttr;
  1606. return;
  1607. }
  1608. flags = this.curAttr >> 18;
  1609. fg = (this.curAttr >> 9) & 0x1ff;
  1610. bg = this.curAttr & 0x1ff;
  1611. l = params.length;
  1612. i = 0;
  1613. while (i < l) {
  1614. p = params[i];
  1615. if (p >= 30 && p <= 37) {
  1616. fg = p - 30;
  1617. } else if (p >= 40 && p <= 47) {
  1618. bg = p - 40;
  1619. } else if (p >= 90 && p <= 97) {
  1620. p += 8;
  1621. fg = p - 90;
  1622. } else if (p >= 100 && p <= 107) {
  1623. p += 8;
  1624. bg = p - 100;
  1625. } else if (p === 0) {
  1626. flags = this.defAttr >> 18;
  1627. fg = (this.defAttr >> 9) & 0x1ff;
  1628. bg = this.defAttr & 0x1ff;
  1629. } else if (p === 1) {
  1630. flags |= 1;
  1631. } else if (p === 4) {
  1632. flags |= 2;
  1633. } else if (p === 5) {
  1634. flags |= 4;
  1635. } else if (p === 7) {
  1636. flags |= 8;
  1637. } else if (p === 8) {
  1638. flags |= 16;
  1639. } else if (p === 22) {
  1640. flags &= ~1;
  1641. } else if (p === 24) {
  1642. flags &= ~2;
  1643. } else if (p === 25) {
  1644. flags &= ~4;
  1645. } else if (p === 27) {
  1646. flags &= ~8;
  1647. } else if (p === 28) {
  1648. flags &= ~16;
  1649. } else if (p === 39) {
  1650. fg = (this.defAttr >> 9) & 0x1ff;
  1651. } else if (p === 49) {
  1652. bg = this.defAttr & 0x1ff;
  1653. } else if (p === 38) {
  1654. if (params[i + 1] === 2) {
  1655. i += 2;
  1656. fg = "#" + params[i] & 0xff + params[i + 1] & 0xff + params[i + 2] & 0xff;
  1657. i += 2;
  1658. } else if (params[i + 1] === 5) {
  1659. i += 2;
  1660. fg = params[i] & 0xff;
  1661. }
  1662. } else if (p === 48) {
  1663. if (params[i + 1] === 2) {
  1664. i += 2;
  1665. bg = "#" + params[i] & 0xff + params[i + 1] & 0xff + params[i + 2] & 0xff;
  1666. i += 2;
  1667. } else if (params[i + 1] === 5) {
  1668. i += 2;
  1669. bg = params[i] & 0xff;
  1670. }
  1671. } else if (p === 100) {
  1672. fg = (this.defAttr >> 9) & 0x1ff;
  1673. bg = this.defAttr & 0x1ff;
  1674. } else {
  1675. console.error("Unknown SGR attribute: %d.", p);
  1676. }
  1677. i++;
  1678. }
  1679. return this.curAttr = (flags << 18) | (fg << 9) | bg;
  1680. };
  1681. Terminal.prototype.deviceStatus = function(params) {
  1682. if (!this.prefix) {
  1683. switch (params[0]) {
  1684. case 5:
  1685. return this.send("\x1b[0n");
  1686. case 6:
  1687. return this.send("\x1b[" + (this.y + 1) + ";" + (this.x + 1) + "R");
  1688. }
  1689. } else if (this.prefix === "?") {
  1690. if (params[0] === 6) {
  1691. return this.send("\x1b[?" + (this.y + 1) + ";" + (this.x + 1) + "R");
  1692. }
  1693. }
  1694. };
  1695. Terminal.prototype.insertChars = function(params) {
  1696. var ch, j, param, row, _results;
  1697. param = params[0];
  1698. if (param < 1) {
  1699. param = 1;
  1700. }
  1701. row = this.y + this.ybase;
  1702. j = this.x;
  1703. ch = [this.eraseAttr(), " "];
  1704. _results = [];
  1705. while (param-- && j < this.cols) {
  1706. this.lines[row].splice(j++, 0, ch);
  1707. _results.push(this.lines[row].pop());
  1708. }
  1709. return _results;
  1710. };
  1711. Terminal.prototype.cursorNextLine = function(params) {
  1712. var param;
  1713. param = params[0];
  1714. if (param < 1) {
  1715. param = 1;
  1716. }
  1717. this.y += param;
  1718. if (this.y >= this.rows) {
  1719. this.y = this.rows - 1;
  1720. }
  1721. return this.x = 0;
  1722. };
  1723. Terminal.prototype.cursorPrecedingLine = function(params) {
  1724. var param;
  1725. param = params[0];
  1726. if (param < 1) {
  1727. param = 1;
  1728. }
  1729. this.y -= param;
  1730. if (this.y < 0) {
  1731. this.y = 0;
  1732. }
  1733. return this.x = 0;
  1734. };
  1735. Terminal.prototype.cursorCharAbsolute = function(params) {
  1736. var param;
  1737. param = params[0];
  1738. if (param < 1) {
  1739. param = 1;
  1740. }
  1741. return this.x = param - 1;
  1742. };
  1743. Terminal.prototype.insertLines = function(params) {
  1744. var j, param, row;
  1745. param = params[0];
  1746. if (param < 1) {
  1747. param = 1;
  1748. }
  1749. row = this.y + this.ybase;
  1750. j = this.rows - 1 - this.scrollBottom;
  1751. j = this.rows - 1 + this.ybase - j + 1;
  1752. while (param--) {
  1753. this.lines.splice(row, 0, this.blankLine(true));
  1754. this.lines.splice(j, 1);
  1755. }
  1756. this.updateRange(this.y);
  1757. return this.updateRange(this.scrollBottom);
  1758. };
  1759. Terminal.prototype.deleteLines = function(params) {
  1760. var j, param, row;
  1761. param = params[0];
  1762. if (param < 1) {
  1763. param = 1;
  1764. }
  1765. row = this.y + this.ybase;
  1766. j = this.rows - 1 - this.scrollBottom;
  1767. j = this.rows - 1 + this.ybase - j;
  1768. while (param--) {
  1769. this.lines.splice(j + 1, 0, this.blankLine(true));
  1770. this.lines.splice(row, 1);
  1771. }
  1772. this.updateRange(this.y);
  1773. return this.updateRange(this.scrollBottom);
  1774. };
  1775. Terminal.prototype.deleteChars = function(params) {
  1776. var ch, param, row, _results;
  1777. param = params[0];
  1778. if (param < 1) {
  1779. param = 1;
  1780. }
  1781. row = this.y + this.ybase;
  1782. ch = [this.eraseAttr(), " "];
  1783. _results = [];
  1784. while (param--) {
  1785. this.lines[row].splice(this.x, 1);
  1786. _results.push(this.lines[row].push(ch));
  1787. }
  1788. return _results;
  1789. };
  1790. Terminal.prototype.eraseChars = function(params) {
  1791. var ch, j, param, row, _results;
  1792. param = params[0];
  1793. if (param < 1) {
  1794. param = 1;
  1795. }
  1796. row = this.y + this.ybase;
  1797. j = this.x;
  1798. ch = [this.eraseAttr(), " "];
  1799. _results = [];
  1800. while (param-- && j < this.cols) {
  1801. _results.push(this.lines[row][j++] = ch);
  1802. }
  1803. return _results;
  1804. };
  1805. Terminal.prototype.charPosAbsolute = function(params) {
  1806. var param;
  1807. param = params[0];
  1808. if (param < 1) {
  1809. param = 1;
  1810. }
  1811. this.x = param - 1;
  1812. if (this.x >= this.cols) {
  1813. return this.x = this.cols - 1;
  1814. }
  1815. };
  1816. Terminal.prototype.HPositionRelative = function(params) {
  1817. var param;
  1818. param = params[0];
  1819. if (param < 1) {
  1820. param = 1;
  1821. }
  1822. this.x += param;
  1823. if (this.x >= this.cols) {
  1824. return this.x = this.cols - 1;
  1825. }
  1826. };
  1827. Terminal.prototype.sendDeviceAttributes = function(params) {
  1828. if (params[0] > 0) {
  1829. return;
  1830. }
  1831. if (!this.prefix) {
  1832. if (this.isterm("xterm") || this.isterm("rxvt-unicode") || this.isterm("screen")) {
  1833. return this.send("\x1b[?1;2c");
  1834. } else {
  1835. if (this.isterm("linux")) {
  1836. return this.send("\x1b[?6c");
  1837. }
  1838. }
  1839. } else if (this.prefix === ">") {
  1840. if (this.isterm("xterm")) {
  1841. return this.send("\x1b[>0;276;0c");
  1842. } else if (this.isterm("rxvt-unicode")) {
  1843. return this.send("\x1b[>85;95;0c");
  1844. } else if (this.isterm("linux")) {
  1845. return this.send(params[0] + "c");
  1846. } else {
  1847. if (this.isterm("screen")) {
  1848. return this.send("\x1b[>83;40003;0c");
  1849. }
  1850. }
  1851. }
  1852. };
  1853. Terminal.prototype.linePosAbsolute = function(params) {
  1854. var param;
  1855. param = params[0];
  1856. if (param < 1) {
  1857. param = 1;
  1858. }
  1859. this.y = param - 1;
  1860. if (this.y >= this.rows) {
  1861. return this.y = this.rows - 1;
  1862. }
  1863. };
  1864. Terminal.prototype.VPositionRelative = function(params) {
  1865. var param;
  1866. param = params[0];
  1867. if (param < 1) {
  1868. param = 1;
  1869. }
  1870. this.y += param;
  1871. if (this.y >= this.rows) {
  1872. return this.y = this.rows - 1;
  1873. }
  1874. };
  1875. Terminal.prototype.HVPosition = function(params) {
  1876. if (params[0] < 1) {
  1877. params[0] = 1;
  1878. }
  1879. if (params[1] < 1) {
  1880. params[1] = 1;
  1881. }
  1882. this.y = params[0] - 1;
  1883. if (this.y >= this.rows) {
  1884. this.y = this.rows - 1;
  1885. }
  1886. this.x = params[1] - 1;
  1887. if (this.x >= this.cols) {
  1888. return this.x = this.cols - 1;
  1889. }
  1890. };
  1891. Terminal.prototype.setMode = function(params) {
  1892. var i, l, normal;
  1893. if (typeof params === "object") {
  1894. l = params.length;
  1895. i = 0;
  1896. while (i < l) {
  1897. this.setMode(params[i]);
  1898. i++;
  1899. }
  1900. return;
  1901. }
  1902. if (this.prefix === "?") {
  1903. switch (params) {
  1904. case 1:
  1905. return this.applicationCursor = true;
  1906. case 2:
  1907. this.setgCharset(0, Terminal.prototype.charsets.US);
  1908. this.setgCharset(1, Terminal.prototype.charsets.US);
  1909. this.setgCharset(2, Terminal.prototype.charsets.US);
  1910. return this.setgCharset(3, Terminal.prototype.charsets.US);
  1911. case 3:
  1912. this.savedCols = this.cols;
  1913. return this.resize(132, this.rows);
  1914. case 6:
  1915. return this.originMode = true;
  1916. case 7:
  1917. return this.wraparoundMode = true;
  1918. case 66:
  1919. return this.applicationKeypad = true;
  1920. case 9:
  1921. case 1000:
  1922. case 1002:
  1923. case 1003:
  1924. this.x10Mouse = params === 9;
  1925. this.vt200Mouse = params === 1000;
  1926. this.normalMouse = params > 1000;
  1927. this.mouseEvents = true;
  1928. return this.element.style.cursor = 'pointer';
  1929. case 1004:
  1930. return this.sendFocus = true;
  1931. case 1005:
  1932. return this.utfMouse = true;
  1933. case 1006:
  1934. return this.sgrMouse = true;
  1935. case 1015:
  1936. return this.urxvtMouse = true;
  1937. case 25:
  1938. return this.cursorHidden = false;
  1939. case 1049:
  1940. case 47:
  1941. case 1047:
  1942. if (!this.normal) {
  1943. normal = {
  1944. lines: this.lines,
  1945. ybase: this.ybase,
  1946. ydisp: this.ydisp,
  1947. x: this.x,
  1948. y: this.y,
  1949. scrollTop: this.scrollTop,
  1950. scrollBottom: this.scrollBottom,
  1951. tabs: this.tabs
  1952. };
  1953. this.reset();
  1954. this.normal = normal;
  1955. return this.showCursor();
  1956. }
  1957. }
  1958. }
  1959. };
  1960. Terminal.prototype.resetMode = function(params) {
  1961. var i, l;
  1962. if (typeof params === "object") {
  1963. l = params.length;
  1964. i = 0;
  1965. while (i < l) {
  1966. this.resetMode(params[i]);
  1967. i++;
  1968. }
  1969. return;
  1970. }
  1971. if (this.prefix === "?") {
  1972. switch (params) {
  1973. case 1:
  1974. return this.applicationCursor = false;
  1975. case 3:
  1976. if (this.cols === 132 && this.savedCols) {
  1977. this.resize(this.savedCols, this.rows);
  1978. }
  1979. return delete this.savedCols;
  1980. case 6:
  1981. return this.originMode = false;
  1982. case 7:
  1983. return this.wraparoundMode = false;
  1984. case 66:
  1985. return this.applicationKeypad = false;
  1986. case 9:
  1987. case 1000:
  1988. case 1002:
  1989. case 1003:
  1990. this.x10Mouse = false;
  1991. this.vt200Mouse = false;
  1992. this.normalMouse = false;
  1993. this.mouseEvents = false;
  1994. return this.element.style.cursor = "";
  1995. case 1004:
  1996. return this.sendFocus = false;
  1997. case 1005:
  1998. return this.utfMouse = false;
  1999. case 1006:
  2000. return this.sgrMouse = false;
  2001. case 1015:
  2002. return this.urxvtMouse = false;
  2003. case 25:
  2004. return this.cursorHidden = true;
  2005. case 1049:
  2006. case 47:
  2007. case 1047:
  2008. if (this.normal) {
  2009. this.lines = this.normal.lines;
  2010. this.ybase = this.normal.ybase;
  2011. this.ydisp = this.normal.ydisp;
  2012. this.x = this.normal.x;
  2013. this.y = this.normal.y;
  2014. this.scrollTop = this.normal.scrollTop;
  2015. this.scrollBottom = this.normal.scrollBottom;
  2016. this.tabs = this.normal.tabs;
  2017. this.normal = null;
  2018. this.refresh(0, this.rows - 1);
  2019. return this.showCursor();
  2020. }
  2021. }
  2022. }
  2023. };
  2024. Terminal.prototype.setScrollRegion = function(params) {
  2025. if (this.prefix) {
  2026. return;
  2027. }
  2028. this.scrollTop = (params[0] || 1) - 1;
  2029. this.scrollBottom = (params[1] || this.rows) - 1;
  2030. this.x = 0;
  2031. return this.y = 0;
  2032. };
  2033. Terminal.prototype.saveCursor = function(params) {
  2034. this.savedX = this.x;
  2035. return this.savedY = this.y;
  2036. };
  2037. Terminal.prototype.restoreCursor = function(params) {
  2038. this.x = this.savedX || 0;
  2039. return this.y = this.savedY || 0;
  2040. };
  2041. Terminal.prototype.cursorForwardTab = function(params) {
  2042. var param, _results;
  2043. param = params[0] || 1;
  2044. _results = [];
  2045. while (param--) {
  2046. _results.push(this.x = this.nextStop());
  2047. }
  2048. return _results;
  2049. };
  2050. Terminal.prototype.scrollUp = function(params) {
  2051. var param;
  2052. param = params[0] || 1;
  2053. while (param--) {
  2054. this.lines.splice(this.ybase + this.scrollTop, 1);
  2055. this.lines.splice(this.ybase + this.scrollBottom, 0, this.blankLine());
  2056. }
  2057. this.updateRange(this.scrollTop);
  2058. return this.updateRange(this.scrollBottom);
  2059. };
  2060. Terminal.prototype.scrollDown = function(params) {
  2061. var param;
  2062. param = params[0] || 1;
  2063. while (param--) {
  2064. this.lines.splice(this.ybase + this.scrollBottom, 1);
  2065. this.lines.splice(this.ybase + this.scrollTop, 0, this.blankLine());
  2066. }
  2067. this.updateRange(this.scrollTop);
  2068. return this.updateRange(this.scrollBottom);
  2069. };
  2070. Terminal.prototype.initMouseTracking = function(params) {};
  2071. Terminal.prototype.resetTitleModes = function(params) {};
  2072. Terminal.prototype.cursorBackwardTab = function(params) {
  2073. var param, _results;
  2074. param = params[0] || 1;
  2075. _results = [];
  2076. while (param--) {
  2077. _results.push(this.x = this.prevStop());
  2078. }
  2079. return _results;
  2080. };
  2081. Terminal.prototype.repeatPrecedingCharacter = function(params) {
  2082. var ch, line, param, _results;
  2083. param = params[0] || 1;
  2084. line = this.lines[this.ybase + this.y];
  2085. ch = line[this.x - 1] || [this.defAttr, " "];
  2086. _results = [];
  2087. while (param--) {
  2088. _results.push(line[this.x++] = ch);
  2089. }
  2090. return _results;
  2091. };
  2092. Terminal.prototype.tabClear = function(params) {
  2093. var param;
  2094. param = params[0];
  2095. if (param <= 0) {
  2096. return delete this.tabs[this.x];
  2097. } else {
  2098. if (param === 3) {
  2099. return this.tabs = {};
  2100. }
  2101. }
  2102. };
  2103. Terminal.prototype.mediaCopy = function(params) {};
  2104. Terminal.prototype.setResources = function(params) {};
  2105. Terminal.prototype.disableModifiers = function(params) {};
  2106. Terminal.prototype.setPointerMode = function(params) {};
  2107. Terminal.prototype.softReset = function(params) {
  2108. this.cursorHidden = false;
  2109. this.insertMode = false;
  2110. this.originMode = false;
  2111. this.wraparoundMode = false;
  2112. this.applicationKeypad = false;
  2113. this.applicationCursor = false;
  2114. this.scrollTop = 0;
  2115. this.scrollBottom = this.rows - 1;
  2116. this.curAttr = this.defAttr;
  2117. this.x = this.y = 0;
  2118. this.charset = null;
  2119. this.glevel = 0;
  2120. return this.charsets = [null];
  2121. };
  2122. Terminal.prototype.requestAnsiMode = function(params) {};
  2123. Terminal.prototype.requestPrivateMode = function(params) {};
  2124. Terminal.prototype.setConformanceLevel = function(params) {};
  2125. Terminal.prototype.loadLEDs = function(params) {};
  2126. Terminal.prototype.setCursorStyle = function(params) {};
  2127. Terminal.prototype.setCharProtectionAttr = function(params) {};
  2128. Terminal.prototype.restorePrivateValues = function(params) {};
  2129. Terminal.prototype.setAttrInRectangle = function(params) {
  2130. var attr, b, i, l, line, r, t;
  2131. t = params[0];
  2132. l = params[1];
  2133. b = params[2];
  2134. r = params[3];
  2135. attr = params[4];
  2136. while (t < b + 1) {
  2137. line = this.lines[this.ybase + t];
  2138. i = l;
  2139. while (i < r) {
  2140. line[i] = [attr, line[i][1]];
  2141. i++;
  2142. }
  2143. t++;
  2144. }
  2145. this.updateRange(params[0]);
  2146. return this.updateRange(params[2]);
  2147. };
  2148. Terminal.prototype.savePrivateValues = function(params) {};
  2149. Terminal.prototype.manipulateWindow = function(params) {};
  2150. Terminal.prototype.reverseAttrInRectangle = function(params) {};
  2151. Terminal.prototype.setTitleModeFeature = function(params) {};
  2152. Terminal.prototype.setWarningBellVolume = function(params) {};
  2153. Terminal.prototype.setMarginBellVolume = function(params) {};
  2154. Terminal.prototype.copyRectangle = function(params) {};
  2155. Terminal.prototype.enableFilterRectangle = function(params) {};
  2156. Terminal.prototype.requestParameters = function(params) {};
  2157. Terminal.prototype.selectChangeExtent = function(params) {};
  2158. Terminal.prototype.fillRectangle = function(params) {
  2159. var b, ch, i, l, line, r, t;
  2160. ch = params[0];
  2161. t = params[1];
  2162. l = params[2];
  2163. b = params[3];
  2164. r = params[4];
  2165. while (t < b + 1) {
  2166. line = this.lines[this.ybase + t];
  2167. i = l;
  2168. while (i < r) {
  2169. line[i] = [line[i][0], String.fromCharCode(ch)];
  2170. i++;
  2171. }
  2172. t++;
  2173. }
  2174. this.updateRange(params[1]);
  2175. return this.updateRange(params[3]);
  2176. };
  2177. Terminal.prototype.enableLocatorReporting = function(params) {
  2178. var val;
  2179. return val = params[0] > 0;
  2180. };
  2181. Terminal.prototype.eraseRectangle = function(params) {
  2182. var b, ch, i, l, line, r, t;
  2183. t = params[0];
  2184. l = params[1];
  2185. b = params[2];
  2186. r = params[3];
  2187. ch = [this.eraseAttr(), " "];
  2188. while (t < b + 1) {
  2189. line = this.lines[this.ybase + t];
  2190. i = l;
  2191. while (i < r) {
  2192. line[i] = ch;
  2193. i++;
  2194. }
  2195. t++;
  2196. }
  2197. this.updateRange(params[0]);
  2198. return this.updateRange(params[2]);
  2199. };
  2200. Terminal.prototype.setLocatorEvents = function(params) {};
  2201. Terminal.prototype.selectiveEraseRectangle = function(params) {};
  2202. Terminal.prototype.requestLocatorPosition = function(params) {};
  2203. Terminal.prototype.insertColumns = function() {
  2204. var ch, i, l, param;
  2205. param = params[0];
  2206. l = this.ybase + this.rows;
  2207. ch = [this.eraseAttr(), " "];
  2208. while (param--) {
  2209. i = this.ybase;
  2210. while (i < l) {
  2211. this.lines[i].splice(this.x + 1, 0, ch);
  2212. this.lines[i].pop();
  2213. i++;
  2214. }
  2215. }
  2216. return this.maxRange();
  2217. };
  2218. Terminal.prototype.deleteColumns = function() {
  2219. var ch, i, l, param;
  2220. param = params[0];
  2221. l = this.ybase + this.rows;
  2222. ch = [this.eraseAttr(), " "];
  2223. while (param--) {
  2224. i = this.ybase;
  2225. while (i < l) {
  2226. this.lines[i].splice(this.x, 1);
  2227. this.lines[i].push(ch);
  2228. i++;
  2229. }
  2230. }
  2231. return this.maxRange();
  2232. };
  2233. Terminal.prototype.get_html_height_in_lines = function(html) {
  2234. var html_height, temp_node;
  2235. temp_node = document.createElement("div");
  2236. temp_node.innerHTML = html;
  2237. this.element.appendChild(temp_node);
  2238. html_height = temp_node.getBoundingClientRect().height;
  2239. this.element.removeChild(temp_node);
  2240. return Math.ceil(html_height / this.char_size.height);
  2241. };
  2242. Terminal.prototype.charsets = {
  2243. SCLD: {
  2244. "`": "◆",
  2245. a: "▒",
  2246. b: "\t",
  2247. c: "\f",
  2248. d: "\r",
  2249. e: "\n",
  2250. f: "°",
  2251. g: "±",
  2252. h: "␤",
  2253. i: "\x0b",
  2254. j: "┘",
  2255. k: "┐",
  2256. l: "┌",
  2257. m: "└",
  2258. n: "┼",
  2259. o: "⎺",
  2260. p: "⎻",
  2261. q: "─",
  2262. r: "⎼",
  2263. s: "⎽",
  2264. t: "├",
  2265. u: "┤",
  2266. v: "┴",
  2267. w: "┬",
  2268. x: "│",
  2269. y: "≤",
  2270. z: "≥",
  2271. "{": "π",
  2272. "|": "≠",
  2273. "}": "£",
  2274. "~": "·"
  2275. },
  2276. UK: null,
  2277. US: null,
  2278. Dutch: null,
  2279. Finnish: null,
  2280. French: null,
  2281. FrenchCanadian: null,
  2282. German: null,
  2283. Italian: null,
  2284. NorwegianDanish: null,
  2285. Spanish: null,
  2286. Swedish: null,
  2287. Swiss: null,
  2288. ISOLatin: null
  2289. };
  2290. return Terminal;
  2291. })();
  2292. selection = null;
  2293. previous_leaf = function(node) {
  2294. var previous;
  2295. previous = node.previousSibling;
  2296. if (!previous) {
  2297. previous = node.parentNode.previousSibling;
  2298. }
  2299. if (!previous) {
  2300. previous = node.parentNode.parentNode.previousSibling;
  2301. }
  2302. while (previous.lastChild) {
  2303. previous = previous.lastChild;
  2304. }
  2305. return previous;
  2306. };
  2307. next_leaf = function(node) {
  2308. var next;
  2309. next = node.nextSibling;
  2310. if (!next) {
  2311. next = node.parentNode.nextSibling;
  2312. }
  2313. if (!next) {
  2314. next = node.parentNode.parentNode.nextSibling;
  2315. }
  2316. while (next.firstChild) {
  2317. next = next.firstChild;
  2318. }
  2319. return next;
  2320. };
  2321. Selection = (function() {
  2322. function Selection() {
  2323. term.element.classList.add('selection');
  2324. this.selection = getSelection();
  2325. }
  2326. Selection.prototype.reset = function() {
  2327. var fake_range, _ref, _results;
  2328. this.selection = getSelection();
  2329. fake_range = document.createRange();
  2330. fake_range.setStart(this.selection.anchorNode, this.selection.anchorOffset);
  2331. fake_range.setEnd(this.selection.focusNode, this.selection.focusOffset);
  2332. this.start = {
  2333. node: this.selection.anchorNode,
  2334. offset: this.selection.anchorOffset
  2335. };
  2336. this.end = {
  2337. node: this.selection.focusNode,
  2338. offset: this.selection.focusOffset
  2339. };
  2340. if (fake_range.collapsed) {
  2341. _ref = [this.end, this.start], this.start = _ref[0], this.end = _ref[1];
  2342. }
  2343. this.start_line = this.start.node;
  2344. while (!this.start_line.classList || __indexOf.call(this.start_line.classList, 'line') < 0) {
  2345. this.start_line = this.start_line.parentNode;
  2346. }
  2347. this.end_line = this.end.node;
  2348. _results = [];
  2349. while (!this.end_line.classList || __indexOf.call(this.end_line.classList, 'line') < 0) {
  2350. _results.push(this.end_line = this.end_line.parentNode);
  2351. }
  2352. return _results;
  2353. };
  2354. Selection.prototype.clear = function() {
  2355. return this.selection.removeAllRanges();
  2356. };
  2357. Selection.prototype.destroy = function() {
  2358. term.element.classList.remove('selection');
  2359. return this.clear();
  2360. };
  2361. Selection.prototype.text = function() {
  2362. return this.selection.toString();
  2363. };
  2364. Selection.prototype.up = function() {
  2365. return this.go(-1);
  2366. };
  2367. Selection.prototype.down = function() {
  2368. return this.go(+1);
  2369. };
  2370. Selection.prototype.go = function(n) {
  2371. var index;
  2372. index = term.children.indexOf(this.start_line) + n;
  2373. if (!((0 <= index && index < term.children.length))) {
  2374. return;
  2375. }
  2376. while (!term.children[index].textContent.match(/\S/)) {
  2377. index += n;
  2378. if (!((0 <= index && index < term.children.length))) {
  2379. return;
  2380. }
  2381. }
  2382. return this.select_line(index);
  2383. };
  2384. Selection.prototype.apply = function() {
  2385. var range;
  2386. this.clear();
  2387. range = document.createRange();
  2388. range.setStart(this.start.node, this.start.offset);
  2389. range.setEnd(this.end.node, this.end.offset);
  2390. return this.selection.addRange(range);
  2391. };
  2392. Selection.prototype.select_line = function(index) {
  2393. var line, line_end, line_start;
  2394. line = term.children[index];
  2395. line_start = {
  2396. node: line.firstChild,
  2397. offset: 0
  2398. };
  2399. line_end = {
  2400. node: line.lastChild,
  2401. offset: line.lastChild.textContent.length
  2402. };
  2403. this.start = this.walk(line_start, /\S/);
  2404. return this.end = this.walk(line_end, /\S/, true);
  2405. };
  2406. Selection.prototype.collapsed = function(start, end) {
  2407. var fake_range;
  2408. fake_range = document.createRange();
  2409. fake_range.setStart(start.node, start.offset);
  2410. fake_range.setEnd(end.node, end.offset);
  2411. return fake_range.collapsed;
  2412. };
  2413. Selection.prototype.shrink_right = function() {
  2414. var end, node;
  2415. node = this.walk(this.end, /\s/, true);
  2416. end = this.walk(node, /\S/, true);
  2417. if (!this.collapsed(this.start, end)) {
  2418. return this.end = end;
  2419. }
  2420. };
  2421. Selection.prototype.shrink_left = function() {
  2422. var node, start;
  2423. node = this.walk(this.start, /\s/);
  2424. start = this.walk(node, /\S/);
  2425. if (!this.collapsed(start, this.end)) {
  2426. return this.start = start;
  2427. }
  2428. };
  2429. Selection.prototype.expand_right = function() {
  2430. var node;
  2431. node = this.walk(this.end, /\S/);
  2432. return this.end = this.walk(node, /\s/);
  2433. };
  2434. Selection.prototype.expand_left = function() {
  2435. var node;
  2436. node = this.walk(this.start, /\S/, true);
  2437. return this.start = this.walk(node, /\s/, true);
  2438. };
  2439. Selection.prototype.walk = function(needle, til, backward) {
  2440. var i, node, text;
  2441. if (backward == null) {
  2442. backward = false;
  2443. }
  2444. if (needle.node.firstChild) {
  2445. node = needle.node.firstChild;
  2446. } else {
  2447. node = needle.node;
  2448. }
  2449. text = node.textContent;
  2450. i = needle.offset;
  2451. if (backward) {
  2452. while (node) {
  2453. while (i > 0) {
  2454. if (text[--i].match(til)) {
  2455. return {
  2456. node: node,
  2457. offset: i + 1
  2458. };
  2459. }
  2460. }
  2461. node = previous_leaf(node);
  2462. text = node.textContent;
  2463. i = text.length;
  2464. }
  2465. } else {
  2466. while (node) {
  2467. while (i < text.length) {
  2468. if (text[i++].match(til)) {
  2469. return {
  2470. node: node,
  2471. offset: i - 1
  2472. };
  2473. }
  2474. }
  2475. node = next_leaf(node);
  2476. text = node.textContent;
  2477. i = 0;
  2478. }
  2479. }
  2480. return needle;
  2481. };
  2482. return Selection;
  2483. })();
  2484. document.addEventListener('keydown', function(e) {
  2485. var _ref, _ref1;
  2486. if (_ref = e.keyCode, __indexOf.call([16, 17, 18, 19], _ref) >= 0) {
  2487. return true;
  2488. }
  2489. if (e.shiftKey && e.keyCode === 13 && !selection && !getSelection().isCollapsed) {
  2490. term.handler(getSelection().toString());
  2491. getSelection().removeAllRanges();
  2492. return cancel(e);
  2493. }
  2494. if (selection) {
  2495. selection.reset();
  2496. if (!e.ctrlKey && e.shiftKey && (37 <= (_ref1 = e.keyCode) && _ref1 <= 40)) {
  2497. return true;
  2498. }
  2499. if (e.shiftKey && e.ctrlKey) {
  2500. if (e.keyCode === 38) {
  2501. selection.up();
  2502. } else if (e.keyCode === 40) {
  2503. selection.down();
  2504. }
  2505. } else if (e.keyCode === 39) {
  2506. selection.shrink_left();
  2507. } else if (e.keyCode === 38) {
  2508. selection.expand_left();
  2509. } else if (e.keyCode === 37) {
  2510. selection.shrink_right();
  2511. } else if (e.keyCode === 40) {
  2512. selection.expand_right();
  2513. } else {
  2514. return cancel(e);
  2515. }
  2516. if (selection != null) {
  2517. selection.apply();
  2518. }
  2519. return cancel(e);
  2520. }
  2521. if (!selection && e.ctrlKey && e.shiftKey && e.keyCode === 38) {
  2522. selection = new Selection();
  2523. selection.select_line(term.y - 1);
  2524. selection.apply();
  2525. return cancel(e);
  2526. }
  2527. return true;
  2528. });
  2529. document.addEventListener('keyup', function(e) {
  2530. var _ref, _ref1;
  2531. if (_ref = e.keyCode, __indexOf.call([16, 17, 18, 19], _ref) >= 0) {
  2532. return true;
  2533. }
  2534. if (selection) {
  2535. if (e.keyCode === 13) {
  2536. term.handler(selection.text());
  2537. selection.destroy();
  2538. selection = null;
  2539. return cancel(e);
  2540. }
  2541. if (_ref1 = e.keyCode, __indexOf.call([37, 38, 39, 40], _ref1) < 0) {
  2542. selection.destroy();
  2543. selection = null;
  2544. return true;
  2545. }
  2546. }
  2547. return true;
  2548. });
  2549. document.addEventListener('dblclick', function(e) {
  2550. var anchorNode, anchorOffset, new_range, range, sel;
  2551. if (e.ctrlKey || e.altkey) {
  2552. return;
  2553. }
  2554. sel = getSelection();
  2555. if (sel.isCollapsed || sel.toString().match(/\s/)) {
  2556. return;
  2557. }
  2558. range = document.createRange();
  2559. range.setStart(sel.anchorNode, sel.anchorOffset);
  2560. range.setEnd(sel.focusNode, sel.focusOffset);
  2561. if (range.collapsed) {
  2562. sel.removeAllRanges();
  2563. new_range = document.createRange();
  2564. new_range.setStart(sel.focusNode, sel.focusOffset);
  2565. new_range.setEnd(sel.anchorNode, sel.anchorOffset);
  2566. sel.addRange(new_range);
  2567. }
  2568. range.detach();
  2569. while (!(sel.toString().match(/\s/) || !sel.toString())) {
  2570. sel.modify('extend', 'forward', 'character');
  2571. }
  2572. sel.modify('extend', 'backward', 'character');
  2573. anchorNode = sel.anchorNode;
  2574. anchorOffset = sel.anchorOffset;
  2575. sel.collapseToEnd();
  2576. sel.extend(anchorNode, anchorOffset);
  2577. while (!(sel.toString().match(/\s/) || !sel.toString())) {
  2578. sel.modify('extend', 'backward', 'character');
  2579. }
  2580. return sel.modify('extend', 'forward', 'character');
  2581. });
  2582. if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
  2583. ctrl = false;
  2584. alt = false;
  2585. first = true;
  2586. virtual_input = document.createElement('input');
  2587. virtual_input.type = 'password';
  2588. virtual_input.style.position = 'fixed';
  2589. virtual_input.style.top = 0;
  2590. virtual_input.style.left = 0;
  2591. virtual_input.style.border = 'none';
  2592. virtual_input.style.outline = 'none';
  2593. virtual_input.style.opacity = 0;
  2594. virtual_input.value = '0';
  2595. document.body.appendChild(virtual_input);
  2596. virtual_input.addEventListener('blur', function() {
  2597. return setTimeout(((function(_this) {
  2598. return function() {
  2599. return _this.focus();
  2600. };
  2601. })(this)), 10);
  2602. });
  2603. addEventListener('click', function() {
  2604. return virtual_input.focus();
  2605. });
  2606. addEventListener('touchstart', function(e) {
  2607. if (e.touches.length === 2) {
  2608. return ctrl = true;
  2609. } else if (e.touches.length === 3) {
  2610. ctrl = false;
  2611. return alt = true;
  2612. } else if (e.touches.length === 4) {
  2613. ctrl = true;
  2614. return alt = true;
  2615. }
  2616. });
  2617. virtual_input.addEventListener('keydown', function(e) {
  2618. term.keyDown(e);
  2619. return true;
  2620. });
  2621. virtual_input.addEventListener('input', function(e) {
  2622. var len;
  2623. len = this.value.length;
  2624. if (len === 0) {
  2625. e.keyCode = 8;
  2626. term.keyDown(e);
  2627. this.value = '0';
  2628. return true;
  2629. }
  2630. e.keyCode = this.value.charAt(1).charCodeAt(0);
  2631. if ((ctrl || alt) && !first) {
  2632. e.keyCode = this.value.charAt(1).charCodeAt(0);
  2633. e.ctrlKey = ctrl;
  2634. e.altKey = alt;
  2635. if (e.keyCode >= 97 && e.keyCode <= 122) {
  2636. e.keyCode -= 32;
  2637. }
  2638. term.keyDown(e);
  2639. this.value = '0';
  2640. ctrl = alt = false;
  2641. return true;
  2642. }
  2643. term.keyPress(e);
  2644. first = false;
  2645. this.value = '0';
  2646. return true;
  2647. });
  2648. }
  2649. cols = rows = null;
  2650. quit = false;
  2651. open_ts = (new Date()).getTime();
  2652. $ = document.querySelectorAll.bind(document);
  2653. send = function(data) {
  2654. return ws.send('S' + data);
  2655. };
  2656. ctl = function() {
  2657. var args, params, type;
  2658. type = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  2659. params = args.join(',');
  2660. if (type === 'Resize') {
  2661. return ws.send('R' + params);
  2662. }
  2663. };
  2664. if (location.protocol === 'https:') {
  2665. ws_url = 'wss://';
  2666. } else {
  2667. ws_url = 'ws://';
  2668. }
  2669. ws_url += document.location.host + '/ws' + location.pathname;
  2670. ws = new WebSocket(ws_url);
  2671. ws.addEventListener('open', function() {
  2672. console.log("WebSocket open", arguments);
  2673. ws.send('R' + term.cols + ',' + term.rows);
  2674. return open_ts = (new Date()).getTime();
  2675. });
  2676. ws.addEventListener('error', function() {
  2677. return console.log("WebSocket error", arguments);
  2678. });
  2679. ws.addEventListener('message', function(e) {
  2680. return setTimeout(function() {
  2681. return term.write(e.data);
  2682. }, 1);
  2683. });
  2684. ws.addEventListener('close', function() {
  2685. console.log("WebSocket closed", arguments);
  2686. setTimeout(function() {
  2687. term.write('Closed');
  2688. term.skipNextKey = true;
  2689. return term.element.classList.add('dead');
  2690. }, 1);
  2691. quit = true;
  2692. if ((new Date()).getTime() - open_ts > 60 * 1000) {
  2693. return open('', '_self').close();
  2694. }
  2695. });
  2696. term = new Terminal($('#wrapper')[0], send, ctl);
  2697. addEventListener('beforeunload', function() {
  2698. if (!quit) {
  2699. return 'This will exit the terminal session';
  2700. }
  2701. });
  2702. bench = function(n) {
  2703. var rnd, t0;
  2704. if (n == null) {
  2705. n = 100000000;
  2706. }
  2707. rnd = '';
  2708. while (rnd.length < n) {
  2709. rnd += Math.random().toString(36).substring(2);
  2710. }
  2711. t0 = (new Date()).getTime();
  2712. term.write(rnd);
  2713. return console.log("" + n + " chars in " + ((new Date()).getTime() - t0) + " ms");
  2714. };
  2715. cbench = function(n) {
  2716. var rnd, t0;
  2717. if (n == null) {
  2718. n = 100000000;
  2719. }
  2720. rnd = '';
  2721. while (rnd.length < n) {
  2722. rnd += "\x1b[" + (30 + parseInt(Math.random() * 20)) + "m";
  2723. rnd += Math.random().toString(36).substring(2);
  2724. }
  2725. t0 = (new Date()).getTime();
  2726. term.write(rnd);
  2727. return console.log("" + n + " chars + colors in " + ((new Date()).getTime() - t0) + " ms");
  2728. };
  2729. window.butterfly = term;
  2730. }).call(this);
  2731. //# sourceMappingURL=main.js.map