]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/BufferSet.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / BufferSet.ts
1 /**
2 * @license MIT
3 */
4
5 import { ITerminal, IBufferSet } from './Interfaces';
6 import { Buffer } from './Buffer';
7 import { EventEmitter } from './EventEmitter';
8
9 /**
10 * The BufferSet represents the set of two buffers used by xterm terminals (normal and alt) and
11 * provides also utilities for working with them.
12 */
13 export class BufferSet extends EventEmitter implements IBufferSet {
14 private _normal: Buffer;
15 private _alt: Buffer;
16 private _activeBuffer: Buffer;
17
18 /**
19 * Create a new BufferSet for the given terminal.
20 * @param {Terminal} terminal - The terminal the BufferSet will belong to
21 */
22 constructor(private _terminal: ITerminal) {
23 super();
24 this._normal = new Buffer(this._terminal);
25 this._normal.fillViewportRows();
26 this._alt = new Buffer(this._terminal);
27 this._activeBuffer = this._normal;
28 }
29
30 /**
31 * Returns the alt Buffer of the BufferSet
32 * @returns {Buffer}
33 */
34 public get alt(): Buffer {
35 return this._alt;
36 }
37
38 /**
39 * Returns the normal Buffer of the BufferSet
40 * @returns {Buffer}
41 */
42 public get active(): Buffer {
43 return this._activeBuffer;
44 }
45
46 /**
47 * Returns the currently active Buffer of the BufferSet
48 * @returns {Buffer}
49 */
50 public get normal(): Buffer {
51 return this._normal;
52 }
53
54 /**
55 * Sets the normal Buffer of the BufferSet as its currently active Buffer
56 */
57 public activateNormalBuffer(): void {
58 // The alt buffer should always be cleared when we switch to the normal
59 // buffer. This frees up memory since the alt buffer should always be new
60 // when activated.
61 this._alt.clear();
62
63 this._activeBuffer = this._normal;
64 this.emit('activate', this._normal);
65 }
66
67 /**
68 * Sets the alt Buffer of the BufferSet as its currently active Buffer
69 */
70 public activateAltBuffer(): void {
71 // Since the alt buffer is always cleared when the normal buffer is
72 // activated, we want to fill it when switching to it.
73 this._alt.fillViewportRows();
74
75 this._activeBuffer = this._alt;
76 this.emit('activate', this._alt);
77 }
78
79 /**
80 * Resizes both normal and alt buffers, adjusting their data accordingly.
81 * @param newCols The new number of columns.
82 * @param newRows The new number of rows.
83 */
84 public resize(newCols: number, newRows: number): void {
85 this._normal.resize(newCols, newRows);
86 this._alt.resize(newCols, newRows);
87 }
88 }