]>
Commit | Line | Data |
---|---|---|
95cb6f30 PK |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | ||
59f75555 | 5 | import { ITerminal, IBufferSet } from './Interfaces'; |
95cb6f30 | 6 | import { Buffer } from './Buffer'; |
8ede1fc9 | 7 | import { EventEmitter } from './EventEmitter'; |
95cb6f30 | 8 | |
58b9f712 PK |
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 | */ | |
59f75555 | 13 | export class BufferSet extends EventEmitter implements IBufferSet { |
95cb6f30 PK |
14 | private _normal: Buffer; |
15 | private _alt: Buffer; | |
16 | private _activeBuffer: Buffer; | |
17 | ||
58b9f712 PK |
18 | /** |
19 | * Create a new BufferSet for the given terminal. | |
20 | * @param {Terminal} terminal - The terminal the BufferSet will belong to | |
21 | */ | |
95cb6f30 | 22 | constructor(private _terminal: ITerminal) { |
8ede1fc9 | 23 | super(); |
95cb6f30 | 24 | this._normal = new Buffer(this._terminal); |
f03d00a4 | 25 | this._normal.fillViewportRows(); |
95cb6f30 PK |
26 | this._alt = new Buffer(this._terminal); |
27 | this._activeBuffer = this._normal; | |
28 | } | |
29 | ||
58b9f712 PK |
30 | /** |
31 | * Returns the alt Buffer of the BufferSet | |
32 | * @returns {Buffer} | |
33 | */ | |
95cb6f30 PK |
34 | public get alt(): Buffer { |
35 | return this._alt; | |
36 | } | |
37 | ||
58b9f712 PK |
38 | /** |
39 | * Returns the normal Buffer of the BufferSet | |
40 | * @returns {Buffer} | |
41 | */ | |
95cb6f30 PK |
42 | public get active(): Buffer { |
43 | return this._activeBuffer; | |
44 | } | |
45 | ||
58b9f712 PK |
46 | /** |
47 | * Returns the currently active Buffer of the BufferSet | |
48 | * @returns {Buffer} | |
49 | */ | |
95cb6f30 PK |
50 | public get normal(): Buffer { |
51 | return this._normal; | |
52 | } | |
53 | ||
58b9f712 PK |
54 | /** |
55 | * Sets the normal Buffer of the BufferSet as its currently active Buffer | |
56 | */ | |
95cb6f30 | 57 | public activateNormalBuffer(): void { |
f03d00a4 DI |
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 | ||
95cb6f30 | 63 | this._activeBuffer = this._normal; |
58b9f712 | 64 | this.emit('activate', this._normal); |
95cb6f30 PK |
65 | } |
66 | ||
58b9f712 PK |
67 | /** |
68 | * Sets the alt Buffer of the BufferSet as its currently active Buffer | |
69 | */ | |
95cb6f30 | 70 | public activateAltBuffer(): void { |
f03d00a4 DI |
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 | ||
8ede1fc9 | 75 | this._activeBuffer = this._alt; |
58b9f712 | 76 | this.emit('activate', this._alt); |
95cb6f30 | 77 | } |
3d20c2f2 | 78 | |
0731f286 DI |
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 | */ | |
3d20c2f2 DI |
84 | public resize(newCols: number, newRows: number): void { |
85 | this._normal.resize(newCols, newRows); | |
86 | this._alt.resize(newCols, newRows); | |
87 | } | |
95cb6f30 | 88 | } |