xterm
[VSoRC/.git] / node_modules / xterm / src / common / buffer / BufferSet.ts
1 /**
2  * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
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';
11
12 /**
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.
15  */
16 export class BufferSet implements IBufferSet {
17   private _normal: Buffer;
18   private _alt: Buffer;
19   private _activeBuffer: Buffer;
20
21
22   private _onBufferActivate = new EventEmitter<{activeBuffer: IBuffer, inactiveBuffer: IBuffer}>();
23   public get onBufferActivate(): IEvent<{activeBuffer: IBuffer, inactiveBuffer: IBuffer}> { return this._onBufferActivate.event; }
24
25   /**
26    * Create a new BufferSet for the given terminal.
27    * @param _terminal - The terminal the BufferSet will belong to
28    */
29   constructor(
30     readonly optionsService: IOptionsService,
31     readonly bufferService: IBufferService
32   ) {
33     this._normal = new Buffer(true, optionsService, bufferService);
34     this._normal.fillViewportRows();
35
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;
40
41     this.setupTabStops();
42   }
43
44   /**
45    * Returns the alt Buffer of the BufferSet
46    */
47   public get alt(): Buffer {
48     return this._alt;
49   }
50
51   /**
52    * Returns the normal Buffer of the BufferSet
53    */
54   public get active(): Buffer {
55     return this._activeBuffer;
56   }
57
58   /**
59    * Returns the currently active Buffer of the BufferSet
60    */
61   public get normal(): Buffer {
62     return this._normal;
63   }
64
65   /**
66    * Sets the normal Buffer of the BufferSet as its currently active Buffer
67    */
68   public activateNormalBuffer(): void {
69     if (this._activeBuffer === this._normal) {
70       return;
71     }
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
76     // when activated.
77     this._alt.clear();
78     this._activeBuffer = this._normal;
79     this._onBufferActivate.fire({
80       activeBuffer: this._normal,
81       inactiveBuffer: this._alt
82     });
83   }
84
85   /**
86    * Sets the alt Buffer of the BufferSet as its currently active Buffer
87    */
88   public activateAltBuffer(fillAttr?: IAttributeData): void {
89     if (this._activeBuffer === this._alt) {
90       return;
91     }
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
101     });
102   }
103
104   /**
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.
108    */
109   public resize(newCols: number, newRows: number): void {
110     this._normal.resize(newCols, newRows);
111     this._alt.resize(newCols, newRows);
112   }
113
114   /**
115    * Setup the tab stops.
116    * @param i The index to start setting up tab stops from.
117    */
118   public setupTabStops(i?: number): void {
119     this._normal.setupTabStops(i);
120     this._alt.setupTabStops(i);
121   }
122 }