2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
6 import { IBuffer, IBufferSet } from 'common/buffer/Types';
7 import { IAttributeData } from 'common/Types';
8 import { Buffer } from 'common/buffer/Buffer';
9 import { EventEmitter, IEvent } from 'common/EventEmitter';
10 import { IOptionsService, IBufferService } from 'common/services/Services';
13 * The BufferSet represents the set of two buffers used by xterm terminals (normal and alt) and
14 * provides also utilities for working with them.
16 export class BufferSet implements IBufferSet {
17 private _normal: Buffer;
19 private _activeBuffer: Buffer;
22 private _onBufferActivate = new EventEmitter<{activeBuffer: IBuffer, inactiveBuffer: IBuffer}>();
23 public get onBufferActivate(): IEvent<{activeBuffer: IBuffer, inactiveBuffer: IBuffer}> { return this._onBufferActivate.event; }
26 * Create a new BufferSet for the given terminal.
27 * @param _terminal - The terminal the BufferSet will belong to
30 readonly optionsService: IOptionsService,
31 readonly bufferService: IBufferService
33 this._normal = new Buffer(true, optionsService, bufferService);
34 this._normal.fillViewportRows();
36 // The alt buffer should never have scrollback.
37 // See http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer
38 this._alt = new Buffer(false, optionsService, bufferService);
39 this._activeBuffer = this._normal;
45 * Returns the alt Buffer of the BufferSet
47 public get alt(): Buffer {
52 * Returns the normal Buffer of the BufferSet
54 public get active(): Buffer {
55 return this._activeBuffer;
59 * Returns the currently active Buffer of the BufferSet
61 public get normal(): Buffer {
66 * Sets the normal Buffer of the BufferSet as its currently active Buffer
68 public activateNormalBuffer(): void {
69 if (this._activeBuffer === this._normal) {
72 this._normal.x = this._alt.x;
73 this._normal.y = this._alt.y;
74 // The alt buffer should always be cleared when we switch to the normal
75 // buffer. This frees up memory since the alt buffer should always be new
78 this._activeBuffer = this._normal;
79 this._onBufferActivate.fire({
80 activeBuffer: this._normal,
81 inactiveBuffer: this._alt
86 * Sets the alt Buffer of the BufferSet as its currently active Buffer
88 public activateAltBuffer(fillAttr?: IAttributeData): void {
89 if (this._activeBuffer === this._alt) {
92 // Since the alt buffer is always cleared when the normal buffer is
93 // activated, we want to fill it when switching to it.
94 this._alt.fillViewportRows(fillAttr);
95 this._alt.x = this._normal.x;
96 this._alt.y = this._normal.y;
97 this._activeBuffer = this._alt;
98 this._onBufferActivate.fire({
99 activeBuffer: this._alt,
100 inactiveBuffer: this._normal
105 * Resizes both normal and alt buffers, adjusting their data accordingly.
106 * @param newCols The new number of columns.
107 * @param newRows The new number of rows.
109 public resize(newCols: number, newRows: number): void {
110 this._normal.resize(newCols, newRows);
111 this._alt.resize(newCols, newRows);
115 * Setup the tab stops.
116 * @param i The index to start setting up tab stops from.
118 public setupTabStops(i?: number): void {
119 this._normal.setupTabStops(i);
120 this._alt.setupTabStops(i);