| 1 |
/** |
| 2 |
* Copyright (c) 2017 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { Disposable } from 'vs/base/common/lifecycle'; |
| 7 |
import { IAttributeData } from 'common/Types'; |
| 8 |
import { Buffer } from 'common/buffer/Buffer'; |
| 9 |
import { IBuffer, IBufferSet } from 'common/buffer/Types'; |
| 10 |
import { IBufferService, IOptionsService } from 'common/services/Services'; |
| 11 |
import { Emitter } from 'vs/base/common/event'; |
| 12 |
|
| 13 |
/** |
| 14 |
* The BufferSet represents the set of two buffers used by xterm terminals (normal and alt) and |
| 15 |
* provides also utilities for working with them. |
| 16 |
*/ |
| 17 |
export class BufferSet extends Disposable implements IBufferSet { |
| 18 |
private _normal!: Buffer; |
| 19 |
private _alt!: Buffer; |
| 20 |
private _activeBuffer!: Buffer; |
| 21 |
|
| 22 |
private readonly _onBufferActivate = this._register(new Emitter<{ activeBuffer: IBuffer, inactiveBuffer: IBuffer }>()); |
| 23 |
public readonly onBufferActivate = this._onBufferActivate.event; |
| 24 |
|
| 25 |
/** |
| 26 |
* Create a new BufferSet for the given terminal. |
| 27 |
*/ |
| 28 |
constructor( |
| 29 |
private readonly _optionsService: IOptionsService, |
| 30 |
private readonly _bufferService: IBufferService |
| 31 |
) { |
| 32 |
super(); |
| 33 |
this.reset(); |
| 34 |
this._register(this._optionsService.onSpecificOptionChange('scrollback', () => this.resize(this._bufferService.cols, this._bufferService.rows))); |
| 35 |
this._register(this._optionsService.onSpecificOptionChange('tabStopWidth', () => this.setupTabStops())); |
| 36 |
} |
| 37 |
|
| 38 |
public reset(): void { |
| 39 |
this._normal = new Buffer(true, this._optionsService, this._bufferService); |
| 40 |
this._normal.fillViewportRows(); |
| 41 |
|
| 42 |
// The alt buffer should never have scrollback. |
| 43 |
// See http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer |
| 44 |
this._alt = new Buffer(false, this._optionsService, this._bufferService); |
| 45 |
this._activeBuffer = this._normal; |
| 46 |
this._onBufferActivate.fire({ |
| 47 |
activeBuffer: this._normal, |
| 48 |
inactiveBuffer: this._alt |
| 49 |
}); |
| 50 |
|
| 51 |
this.setupTabStops(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns the alt Buffer of the BufferSet |
| 56 |
*/ |
| 57 |
public get alt(): Buffer { |
| 58 |
return this._alt; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the currently active Buffer of the BufferSet |
| 63 |
*/ |
| 64 |
public get active(): Buffer { |
| 65 |
return this._activeBuffer; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the normal Buffer of the BufferSet |
| 70 |
*/ |
| 71 |
public get normal(): Buffer { |
| 72 |
return this._normal; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Sets the normal Buffer of the BufferSet as its currently active Buffer |
| 77 |
*/ |
| 78 |
public activateNormalBuffer(): void { |
| 79 |
if (this._activeBuffer === this._normal) { |
| 80 |
return; |
| 81 |
} |
| 82 |
this._normal.x = this._alt.x; |
| 83 |
this._normal.y = this._alt.y; |
| 84 |
// The alt buffer should always be cleared when we switch to the normal |
| 85 |
// buffer. This frees up memory since the alt buffer should always be new |
| 86 |
// when activated. |
| 87 |
this._alt.clearAllMarkers(); |
| 88 |
this._alt.clear(); |
| 89 |
this._activeBuffer = this._normal; |
| 90 |
this._onBufferActivate.fire({ |
| 91 |
activeBuffer: this._normal, |
| 92 |
inactiveBuffer: this._alt |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Sets the alt Buffer of the BufferSet as its currently active Buffer |
| 98 |
*/ |
| 99 |
public activateAltBuffer(fillAttr?: IAttributeData): void { |
| 100 |
if (this._activeBuffer === this._alt) { |
| 101 |
return; |
| 102 |
} |
| 103 |
// Since the alt buffer is always cleared when the normal buffer is |
| 104 |
// activated, we want to fill it when switching to it. |
| 105 |
this._alt.fillViewportRows(fillAttr); |
| 106 |
this._alt.x = this._normal.x; |
| 107 |
this._alt.y = this._normal.y; |
| 108 |
this._activeBuffer = this._alt; |
| 109 |
this._onBufferActivate.fire({ |
| 110 |
activeBuffer: this._alt, |
| 111 |
inactiveBuffer: this._normal |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Resizes both normal and alt buffers, adjusting their data accordingly. |
| 117 |
* @param newCols The new number of columns. |
| 118 |
* @param newRows The new number of rows. |
| 119 |
*/ |
| 120 |
public resize(newCols: number, newRows: number): void { |
| 121 |
this._normal.resize(newCols, newRows); |
| 122 |
this._alt.resize(newCols, newRows); |
| 123 |
this.setupTabStops(newCols); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Setup the tab stops. |
| 128 |
* @param i The index to start setting up tab stops from. |
| 129 |
*/ |
| 130 |
public setupTabStops(i?: number): void { |
| 131 |
this._normal.setupTabStops(i); |
| 132 |
this._alt.setupTabStops(i); |
| 133 |
} |
| 134 |
} |