]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/BufferSet.ts
Clean up buffer clean up/fill logic
[mirror_xterm.js.git] / src / BufferSet.ts
index e115fe001c7d8a87da509eca2c2dcd1171e79825..345191cfa2116d3e5483ec0f4b1670478e427f1e 100644 (file)
@@ -2,47 +2,82 @@
  * @license MIT
  */
 
-import { ITerminal } from './Interfaces';
+import { ITerminal, IBufferSet } from './Interfaces';
 import { Buffer } from './Buffer';
 import { EventEmitter } from './EventEmitter';
 
-export class BufferSet extends EventEmitter {
+/**
+ * The BufferSet represents the set of two buffers used by xterm terminals (normal and alt) and
+ * provides also utilities for working with them.
+ */
+export class BufferSet extends EventEmitter implements IBufferSet {
   private _normal: Buffer;
   private _alt: Buffer;
   private _activeBuffer: Buffer;
 
+  /**
+   * Create a new BufferSet for the given terminal.
+   * @param {Terminal} terminal - The terminal the BufferSet will belong to
+   */
   constructor(private _terminal: ITerminal) {
     super();
     this._normal = new Buffer(this._terminal);
+    this._normal.fillViewportRows();
     this._alt = new Buffer(this._terminal);
     this._activeBuffer = this._normal;
   }
 
+  /**
+   * Returns the alt Buffer of the BufferSet
+   * @returns {Buffer}
+   */
   public get alt(): Buffer {
     return this._alt;
   }
 
+  /**
+   * Returns the normal Buffer of the BufferSet
+   * @returns {Buffer}
+   */
   public get active(): Buffer {
     return this._activeBuffer;
   }
 
+  /**
+   * Returns the currently active Buffer of the BufferSet
+   * @returns {Buffer}
+   */
   public get normal(): Buffer {
     return this._normal;
   }
 
-  private resetTerminal() {
-    this._terminal.reset();
-    this._terminal.viewport.syncScrollArea();
-    this._terminal.showCursor();
-  }
-
+  /**
+   * Sets the normal Buffer of the BufferSet as its currently active Buffer
+   */
   public activateNormalBuffer(): void {
+    // The alt buffer should always be cleared when we switch to the normal
+    // buffer. This frees up memory since the alt buffer should always be new
+    // when activated.
+    this._alt.clear();
+
     this._activeBuffer = this._normal;
     this.emit('activate', this._normal);
   }
 
+  /**
+   * Sets the alt Buffer of the BufferSet as its currently active Buffer
+   */
   public activateAltBuffer(): void {
+    // Since the alt buffer is always cleared when the normal buffer is
+    // activated, we want to fill it when switching to it.
+    this._alt.fillViewportRows();
+
     this._activeBuffer = this._alt;
     this.emit('activate', this._alt);
   }
+
+  public resize(newCols: number, newRows: number): void {
+    this._normal.resize(newCols, newRows);
+    this._alt.resize(newCols, newRows);
+  }
 }