]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/xterm.js
Merge pull request #535 from Tyriar/534_phantomjs
[mirror_xterm.js.git] / src / xterm.js
index 99540891db06def33abb07305a1328c9de3140bd..f76c01b5941482833ad7e2d58849d12ddeabbee2 100644 (file)
@@ -18,6 +18,7 @@ import { CircularList } from './utils/CircularList';
 import { C0 } from './EscapeSequences';
 import { InputHandler } from './InputHandler';
 import { Parser } from './Parser';
+import { Renderer } from './Renderer';
 import { CharMeasure } from './utils/CharMeasure';
 import * as Browser from './utils/Browser';
 import * as Keyboard from './utils/Keyboard';
@@ -50,13 +51,6 @@ var WRITE_BUFFER_PAUSE_THRESHOLD = 5;
  */
 var WRITE_BATCH_SIZE = 300;
 
-/**
- * The maximum number of refresh frames to skip when the write buffer is non-
- * empty. Note that these frames may be intermingled with frames that are
- * skipped via requestAnimationFrame's mechanism.
- */
-var MAX_REFRESH_FRAME_SKIP = 5;
-
 /**
  * Terminal
  */
@@ -157,9 +151,6 @@ function Terminal(options) {
    */
   this.y = 0;
 
-  /** A queue of the rows to be refreshed */
-  this.refreshRowsQueue = [];
-
   this.cursorState = 0;
   this.cursorHidden = false;
   this.convertEol;
@@ -217,11 +208,12 @@ function Terminal(options) {
 
   this.inputHandler = new InputHandler(this);
   this.parser = new Parser(this.inputHandler, this);
+  // Reuse renderer if the Terminal is being recreated via a Terminal.reset call.
+  this.renderer = this.renderer || null;
 
   // user input states
   this.writeBuffer = [];
   this.writeInProgress = false;
-  this.refreshFramesSkipped = 0;
 
   /**
    * Whether _xterm.js_ sent XOFF in order to catch up with the pty process.
@@ -360,7 +352,9 @@ Terminal.defaults = {
   screenKeys: false,
   debug: false,
   cancelEvents: false,
-  disableStdin: false
+  disableStdin: false,
+  useFlowControl: false,
+  tabStopWidth: 8
   // programFeatures: false,
   // focusKeys: false,
 };
@@ -433,6 +427,7 @@ Terminal.prototype.setOption = function(key, value) {
       this.element.classList.toggle(`xterm-cursor-style-underline`, value === 'underline');
       this.element.classList.toggle(`xterm-cursor-style-bar`, value === 'bar');
       break;
+    case 'tabStopWidth': this.setupStops(); break;
   }
 };
 
@@ -467,7 +462,7 @@ Terminal.prototype.blur = function() {
  */
 Terminal.bindBlur = function (term) {
   on(term.textarea, 'blur', function (ev) {
-    term.queueRefresh(term.y, term.y);
+    term.refresh(term.y, term.y);
     if (term.sendFocus) {
       term.send(C0.ESC + '[O');
     }
@@ -653,10 +648,10 @@ Terminal.prototype.open = function(parent) {
   this.charMeasure.measure();
 
   this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure);
+  this.renderer = new Renderer(this);
 
   // Setup loop that draws to screen
-  this.queueRefresh(0, this.rows - 1);
-  this.refreshLoop();
+  this.refresh(0, this.rows - 1);
 
   // Initialize global actions that
   // need to be taken on the document.
@@ -678,12 +673,6 @@ Terminal.prototype.open = function(parent) {
   // them into terminal mouse protocols.
   this.bindMouse();
 
-  // Figure out whether boldness affects
-  // the character width of monospace fonts.
-  if (Terminal.brokenBold == null) {
-    Terminal.brokenBold = isBoldBroken(this.document);
-  }
-
   /**
    * This event is emitted when terminal has completed opening.
    *
@@ -1056,267 +1045,22 @@ Terminal.prototype.destroy = function() {
   this._events = {};
   this.handler = function() {};
   this.write = function() {};
-  if (this.element.parentNode) {
+  if (this.element && this.element.parentNode) {
     this.element.parentNode.removeChild(this.element);
   }
   //this.emit('close');
 };
 
-
-/**
- * Flags used to render terminal text properly
- */
-Terminal.flags = {
-  BOLD: 1,
-  UNDERLINE: 2,
-  BLINK: 4,
-  INVERSE: 8,
-  INVISIBLE: 16
-}
-
-/**
- * Queues a refresh between two rows (inclusive), to be done on next animation
- * frame.
- * @param {number} start The start row.
- * @param {number} end The end row.
- */
-Terminal.prototype.queueRefresh = function(start, end) {
-  this.refreshRowsQueue.push({ start: start, end: end });
-}
-
-/**
- * Performs the refresh loop callback, calling refresh only if a refresh is
- * necessary before queueing up the next one.
- */
-Terminal.prototype.refreshLoop = function() {
-  // Don't refresh if there were no row changes
-  if (this.refreshRowsQueue.length > 0) {
-    // Skip MAX_REFRESH_FRAME_SKIP frames if the writeBuffer is non-empty as it
-    // will need to be immediately refreshed anyway. This saves a lot of
-    // rendering time as the viewport DOM does not need to be refreshed, no
-    // scroll events, no layouts, etc.
-    var skipFrame = this.writeBuffer.length > 0 && this.refreshFramesSkipped++ <= MAX_REFRESH_FRAME_SKIP;
-
-    if (!skipFrame) {
-      this.refreshFramesSkipped = 0;
-      var start;
-      var end;
-      if (this.refreshRowsQueue.length > 4) {
-        // Just do a full refresh when 5+ refreshes are queued
-        start = 0;
-        end = this.rows - 1;
-      } else {
-        // Get start and end rows that need refreshing
-        start = this.refreshRowsQueue[0].start;
-        end = this.refreshRowsQueue[0].end;
-        for (var i = 1; i < this.refreshRowsQueue.length; i++) {
-          if (this.refreshRowsQueue[i].start < start) {
-            start = this.refreshRowsQueue[i].start;
-          }
-          if (this.refreshRowsQueue[i].end > end) {
-            end = this.refreshRowsQueue[i].end;
-          }
-        }
-      }
-      this.refreshRowsQueue = [];
-      this.refresh(start, end);
-    }
-  }
-  window.requestAnimationFrame(this.refreshLoop.bind(this));
-}
-
 /**
- * Refreshes (re-renders) terminal content within two rows (inclusive)
- *
- * Rendering Engine:
- *
- * In the screen buffer, each character is stored as a an array with a character
- * and a 32-bit integer:
- *   - First value: a utf-16 character.
- *   - Second value:
- *   - Next 9 bits: background color (0-511).
- *   - Next 9 bits: foreground color (0-511).
- *   - Next 14 bits: a mask for misc. flags:
- *     - 1=bold
- *     - 2=underline
- *     - 4=blink
- *     - 8=inverse
- *     - 16=invisible
- *
+ * Tells the renderer to refresh terminal content between two rows (inclusive) at the next
+ * opportunity.
  * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
  * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
  */
 Terminal.prototype.refresh = function(start, end) {
-  var self = this;
-
-  var x, y, i, line, out, ch, ch_width, width, data, attr, bg, fg, flags, row, parent, focused = document.activeElement;
-
-  // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
-  if (end - start >= this.rows / 2) {
-    parent = this.element.parentNode;
-    if (parent) {
-      this.element.removeChild(this.rowContainer);
-    }
+  if (this.renderer) {
+    this.renderer.queueRefresh(start, end);
   }
-
-  width = this.cols;
-  y = start;
-
-  if (end >= this.rows.length) {
-    this.log('`end` is too large. Most likely a bad CSR.');
-    end = this.rows.length - 1;
-  }
-
-  for (; y <= end; y++) {
-    row = y + this.ydisp;
-
-    line = this.lines.get(row);
-    if (!line || !this.children[y]) {
-      // Continue if the line is not available, this means a resize is currently in progress
-      continue;
-    }
-    out = '';
-
-    if (this.y === y - (this.ybase - this.ydisp)
-        && this.cursorState
-        && !this.cursorHidden) {
-      x = this.x;
-    } else {
-      x = -1;
-    }
-
-    attr = this.defAttr;
-    i = 0;
-
-    for (; i < width; i++) {
-      if (!line[i]) {
-        // Continue if the character is not available, this means a resize is currently in progress
-        continue;
-      }
-      data = line[i][0];
-      ch = line[i][1];
-      ch_width = line[i][2];
-      if (!ch_width)
-        continue;
-
-      if (i === x) data = -1;
-
-      if (data !== attr) {
-        if (attr !== this.defAttr) {
-          out += '</span>';
-        }
-        if (data !== this.defAttr) {
-          if (data === -1) {
-            out += '<span class="reverse-video terminal-cursor">';
-          } else {
-            var classNames = [];
-
-            bg = data & 0x1ff;
-            fg = (data >> 9) & 0x1ff;
-            flags = data >> 18;
-
-            if (flags & Terminal.flags.BOLD) {
-              if (!Terminal.brokenBold) {
-                classNames.push('xterm-bold');
-              }
-              // See: XTerm*boldColors
-              if (fg < 8) fg += 8;
-            }
-
-            if (flags & Terminal.flags.UNDERLINE) {
-              classNames.push('xterm-underline');
-            }
-
-            if (flags & Terminal.flags.BLINK) {
-              classNames.push('xterm-blink');
-            }
-
-            // If inverse flag is on, then swap the foreground and background variables.
-            if (flags & Terminal.flags.INVERSE) {
-              /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
-              bg = [fg, fg = bg][0];
-              // Should inverse just be before the
-              // above boldColors effect instead?
-              if ((flags & 1) && fg < 8) fg += 8;
-            }
-
-            if (flags & Terminal.flags.INVISIBLE) {
-              classNames.push('xterm-hidden');
-            }
-
-            /**
-             * Weird situation: Invert flag used black foreground and white background results
-             * in invalid background color, positioned at the 256 index of the 256 terminal
-             * color map. Pin the colors manually in such a case.
-             *
-             * Source: https://github.com/sourcelair/xterm.js/issues/57
-             */
-            if (flags & Terminal.flags.INVERSE) {
-              if (bg == 257) {
-                bg = 15;
-              }
-              if (fg == 256) {
-                fg = 0;
-              }
-            }
-
-            if (bg < 256) {
-              classNames.push('xterm-bg-color-' + bg);
-            }
-
-            if (fg < 256) {
-              classNames.push('xterm-color-' + fg);
-            }
-
-            out += '<span';
-            if (classNames.length) {
-              out += ' class="' + classNames.join(' ') + '"';
-            }
-            out += '>';
-          }
-        }
-      }
-
-      if (ch_width === 2) {
-        out += '<span class="xterm-wide-char">';
-      }
-      switch (ch) {
-        case '&':
-          out += '&amp;';
-          break;
-        case '<':
-          out += '&lt;';
-          break;
-        case '>':
-          out += '&gt;';
-          break;
-        default:
-          if (ch <= ' ') {
-            out += '&nbsp;';
-          } else {
-            out += ch;
-          }
-          break;
-      }
-      if (ch_width === 2) {
-        out += '</span>';
-      }
-
-      attr = data;
-    }
-
-    if (attr !== this.defAttr) {
-      out += '</span>';
-    }
-
-    this.children[y].innerHTML = out;
-  }
-
-  if (parent) {
-    this.element.appendChild(this.rowContainer);
-  }
-
-  this.emit('refresh', {element: this.element, start: start, end: end});
 };
 
 /**
@@ -1325,7 +1069,7 @@ Terminal.prototype.refresh = function(start, end) {
 Terminal.prototype.showCursor = function() {
   if (!this.cursorState) {
     this.cursorState = 1;
-    this.queueRefresh(this.y, this.y);
+    this.refresh(this.y, this.y);
   }
 };
 
@@ -1414,7 +1158,7 @@ Terminal.prototype.scrollDisp = function(disp, suppressScrollEvent) {
     this.emit('scroll', this.ydisp);
   }
 
-  this.queueRefresh(0, this.rows - 1);
+  this.refresh(0, this.rows - 1);
 };
 
 /**
@@ -1449,7 +1193,7 @@ Terminal.prototype.write = function(data) {
   // Send XOFF to pause the pty process if the write buffer becomes too large so
   // xterm.js can catch up before more data is sent. This is necessary in order
   // to keep signals such as ^C responsive.
-  if (!this.xoffSentToCatchUp && this.writeBuffer.length >= WRITE_BUFFER_PAUSE_THRESHOLD) {
+  if (this.options.useFlowControl && !this.xoffSentToCatchUp && this.writeBuffer.length >= WRITE_BUFFER_PAUSE_THRESHOLD) {
     // XOFF - stop pty pipe
     // XON will be triggered by emulator before processing data chunk
     this.send(C0.DC3);
@@ -1486,7 +1230,7 @@ Terminal.prototype.innerWrite = function() {
     this.parser.parse(data);
 
     this.updateRange(this.y);
-    this.queueRefresh(this.refreshStart, this.refreshEnd);
+    this.refresh(this.refreshStart, this.refreshEnd);
   }
   if (this.writeBuffer.length > 0) {
     // Allow renderer to catch up before processing the next batch
@@ -1971,6 +1715,10 @@ Terminal.prototype.error = function() {
  * @param {number} y The number of rows to resize to.
  */
 Terminal.prototype.resize = function(x, y) {
+  if (isNaN(x) || isNaN(y)) {
+    return;
+  }
+
   var line
   , el
   , i
@@ -2071,7 +1819,7 @@ Terminal.prototype.resize = function(x, y) {
 
   this.charMeasure.measure();
 
-  this.queueRefresh(0, this.rows - 1);
+  this.refresh(0, this.rows - 1);
 
   this.normal = null;
 
@@ -2118,7 +1866,7 @@ Terminal.prototype.setupStops = function(i) {
     i = 0;
   }
 
-  for (; i < this.cols; i += 8) {
+  for (; i < this.cols; i += this.getOption('tabStopWidth')) {
     this.tabs[i] = true;
   }
 };
@@ -2156,14 +1904,14 @@ Terminal.prototype.nextStop = function(x) {
  * @param {number} y The line in which to operate.
  */
 Terminal.prototype.eraseRight = function(x, y) {
-  var line = this.lines.get(this.ybase + y)
-  , ch = [this.eraseAttr(), ' ', 1]; // xterm
-
-
+  var line = this.lines.get(this.ybase + y);
+  if (!line) {
+    return;
+  }
+  var ch = [this.eraseAttr(), ' ', 1]; // xterm
   for (; x < this.cols; x++) {
     line[x] = ch;
   }
-
   this.updateRange(y);
 };
 
@@ -2175,12 +1923,15 @@ Terminal.prototype.eraseRight = function(x, y) {
  * @param {number} y The line in which to operate.
  */
 Terminal.prototype.eraseLeft = function(x, y) {
-  var line = this.lines.get(this.ybase + y)
-  , ch = [this.eraseAttr(), ' ', 1]; // xterm
-
+  var line = this.lines.get(this.ybase + y);
+  if (!line) {
+    return;
+  }
+  var ch = [this.eraseAttr(), ' ', 1]; // xterm
   x++;
-  while (x--) line[x] = ch;
-
+  while (x--) {
+    line[x] = ch;
+  }
   this.updateRange(y);
 };
 
@@ -2200,7 +1951,7 @@ Terminal.prototype.clear = function() {
   for (var i = 1; i < this.rows; i++) {
     this.lines.push(this.blankLine());
   }
-  this.queueRefresh(0, this.rows - 1);
+  this.refresh(0, this.rows - 1);
   this.emit('scroll', this.ydisp);
 };
 
@@ -2301,6 +2052,10 @@ Terminal.prototype.index = function() {
     this.y--;
     this.scroll();
   }
+  // If the end of the line is hit, prevent this action from wrapping around to the next line.
+  if (this.x >= this.cols) {
+    this.x--;
+  }
 };
 
 
@@ -2334,7 +2089,7 @@ Terminal.prototype.reset = function() {
   var customKeydownHandler = this.customKeydownHandler;
   Terminal.call(this, this.options);
   this.customKeydownHandler = customKeydownHandler;
-  this.queueRefresh(0, this.rows - 1);
+  this.refresh(0, this.rows - 1);
   this.viewport.syncScrollArea();
 };
 
@@ -2380,20 +2135,6 @@ function inherits(child, parent) {
   child.prototype = new f;
 }
 
-// if bold is broken, we can't
-// use it in the terminal.
-function isBoldBroken(document) {
-  var body = document.getElementsByTagName('body')[0];
-  var el = document.createElement('span');
-  el.innerHTML = 'hello world';
-  body.appendChild(el);
-  var w1 = el.scrollWidth;
-  el.style.fontWeight = 'bold';
-  var w2 = el.scrollWidth;
-  body.removeChild(el);
-  return w1 !== w2;
-}
-
 function indexOf(obj, el) {
   var i = obj.length;
   while (i--) {
@@ -2496,6 +2237,9 @@ function keys(obj) {
 Terminal.EventEmitter = EventEmitter;
 Terminal.inherits = inherits;
 
+// Expose for Phantom.JS tests
+Terminal.CharMeasure = CharMeasure;
+
 /**
  * Adds an event listener to the terminal.
  *