]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/xterm.js
Clean up buffer clean up/fill logic
[mirror_xterm.js.git] / src / xterm.js
index d4cb00158c776e2933038d27b187fd041752e7cb..e9e3465e6b83b98255f67ef1b64dc30036cae1a3 100644 (file)
@@ -227,11 +227,6 @@ function Terminal(options) {
     this._terminal.buffer = buffer;
   });
 
-  var i = this.rows;
-
-  while (i--) {
-    this.buffer.lines.push(this.blankLine());
-  }
   // Ensure the selection manager has the correct buffer
   if (this.selectionManager) {
     this.selectionManager.setBuffer(this.buffer.lines);
@@ -429,7 +424,7 @@ Terminal.prototype.setOption = function(key, value) {
   switch (key) {
     case 'cursorBlink': this.setCursorBlinking(value); break;
     case 'cursorStyle':
-      // Style 'block' applies with no class
+      this.element.classList.toggle(`xterm-cursor-style-block`, value === 'block');
       this.element.classList.toggle(`xterm-cursor-style-underline`, value === 'underline');
       this.element.classList.toggle(`xterm-cursor-style-bar`, value === 'bar');
       break;
@@ -639,6 +634,7 @@ Terminal.prototype.open = function(parent, focus) {
   this.element.classList.add('terminal');
   this.element.classList.add('xterm');
   this.element.classList.add('xterm-theme-' + this.theme);
+  this.element.classList.add(`xterm-cursor-style-${this.options.cursorStyle}`);
   this.setCursorBlinking(this.options.cursorBlink);
 
   this.element.setAttribute('tabindex', 0);
@@ -1016,7 +1012,10 @@ Terminal.prototype.bindMouse = function() {
   }
 
   on(el, 'mousedown', function(ev) {
-    // ensure focus
+
+    // Prevent the focus on the textarea from getting lost
+    // and make sure we get focused on mousedown
+    ev.preventDefault();
     self.focus();
 
     if (!self.mouseEvents) return;
@@ -1926,86 +1925,21 @@ Terminal.prototype.resize = function(x, y) {
   if (x < 1) x = 1;
   if (y < 1) y = 1;
 
-  // resize cols
-  j = this.cols;
-  if (j < x) {
-    ch = [this.defAttr, ' ', 1]; // does xterm use the default attr?
-    i = this.buffer.lines.length;
-    while (i--) {
-      if (this.buffer.lines.get(i) === undefined) {
-        this.buffer.lines.set(i, this.blankLine());
-      }
-      while (this.buffer.lines.get(i).length < x) {
-        this.buffer.lines.get(i).push(ch);
-      }
-    }
-  }
-
-  this.cols = x;
-  this.setupStops(this.cols);
-
-  // resize rows
-  j = this.rows;
-  addToY = 0;
-  if (j < y) {
-    el = this.element;
-    while (j++ < y) {
-      // y is rows, not this.buffer.y
-      if (this.buffer.lines.length < y + this.buffer.ybase) {
-        if (this.buffer.ybase > 0 && this.buffer.lines.length <= this.buffer.ybase + this.buffer.y + addToY + 1) {
-          // There is room above the buffer and there are no empty elements below the line,
-          // scroll up
-          this.buffer.ybase--;
-          addToY++;
-          if (this.buffer.ydisp > 0) {
-            // Viewport is at the top of the buffer, must increase downwards
-            this.buffer.ydisp--;
-          }
-        } else {
-          // Add a blank line if there is no buffer left at the top to scroll to, or if there
-          // are blank lines after the cursor
-          this.buffer.lines.push(this.blankLine());
-        }
-      }
-      if (this.children.length < y) {
-        this.insertRow();
-      }
-    }
-  } else { // (j > y)
-    while (j-- > y) {
-      if (this.buffer.lines.length > y + this.buffer.ybase) {
-        if (this.buffer.lines.length > this.buffer.ybase + this.buffer.y + 1) {
-          // The line is a blank line below the cursor, remove it
-          this.buffer.lines.pop();
-        } else {
-          // The line is the cursor, scroll down
-          this.buffer.ybase++;
-          this.buffer.ydisp++;
-        }
-      }
-      if (this.children.length > y) {
-        el = this.children.shift();
-        if (!el) continue;
-        el.parentNode.removeChild(el);
-      }
-    }
-  }
-  this.rows = y;
+  this.buffers.resize(x, y);
 
-  // Make sure that the cursor stays on screen
-  if (this.buffer.y >= y) {
-    this.buffer.y = y - 1;
-  }
-  if (addToY) {
-    this.buffer.y += addToY;
+  // Adjust rows in the DOM to accurately reflect the new dimensions
+  while (this.children.length < y) {
+    this.insertRow();
   }
-
-  if (this.buffer.x >= x) {
-    this.buffer.x = x - 1;
+  while (this.children.length > y) {
+    el = this.children.shift();
+    if (!el) continue;
+    el.parentNode.removeChild(el);
   }
 
-  this.buffer.scrollTop = 0;
-  this.buffer.scrollBottom = y - 1;
+  this.cols = x;
+  this.rows = y;
+  this.setupStops(this.cols);
 
   this.charMeasure.measure();
 
@@ -2289,12 +2223,10 @@ Terminal.prototype.reset = function() {
   var customKeyEventHandler = this.customKeyEventHandler;
   var cursorBlinkInterval = this.cursorBlinkInterval;
   var inputHandler = this.inputHandler;
-  var buffers = this.buffers;
   Terminal.call(this, this.options);
   this.customKeyEventHandler = customKeyEventHandler;
   this.cursorBlinkInterval = cursorBlinkInterval;
   this.inputHandler = inputHandler;
-  this.buffers = buffers;
   this.refresh(0, this.rows - 1);
   this.viewport.syncScrollArea();
 };