From 4d660deff3db9f3eea3923dcaa970fde0e9deaf8 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Tue, 4 Dec 2018 19:00:44 -0800 Subject: [PATCH 01/61] hooks for custom control sequences This fixes (at least partially) issue #1176 "Add a way to plugin a custom control sequence handler". --- src/EscapeSequenceParser.ts | 74 +++++++++++++++++++++++++++++++++---- src/InputHandler.ts | 17 ++++++++- src/Terminal.ts | 6 ++- src/Types.ts | 14 +++++++ src/public/Terminal.ts | 5 ++- 5 files changed, 104 insertions(+), 12 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index b38c50f543..7a5da2c851 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -301,9 +301,39 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._executeHandlerFb = callback; } + private _removeHandler(array: any[], callback: any): void { + if (array) { + for (let i = array.length; --i >= 0; ) { + if (array[i] == callback) { + array.splice(i, 1); + return; + } + } + } + } + + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { + let index = flag.charCodeAt(0); + let array = this._csiHandlers[index]; + if (! array) { this._csiHandlers[index] = array = new Array(); } + array.push(callback); + } + + removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { + let index = flag.charCodeAt(0); + let array = this._csiHandlers[index]; + this._removeHandler(array, callback); + if (array && array.length == 0) + delete this._csiHandlers[index]; + } + /* deprecated */ setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { - this._csiHandlers[flag.charCodeAt(0)] = callback; + this.clearCsiHandler(flag); + this.addCsiHandler(flag, (params: number[], collect: string): boolean => { + callback(params, collect); return true; + }); } + /* deprecated */ clearCsiHandler(flag: string): void { if (this._csiHandlers[flag.charCodeAt(0)]) delete this._csiHandlers[flag.charCodeAt(0)]; } @@ -321,9 +351,25 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._escHandlerFb = callback; } + addOscHandler(ident: number, callback: (data: string) => boolean): void { + let array = this._oscHandlers[ident]; + if (! array) { this._oscHandlers[ident] = array = new Array(); } + array.push(callback); + } + removeOscHandler(ident: number, callback: (data: string) => boolean): void { + let array = this._oscHandlers[ident]; + this._removeHandler(array, callback); + if (array && array.length == 0) + delete this._oscHandlers[ident]; + } + /* deprecated */ setOscHandler(ident: number, callback: (data: string) => void): void { - this._oscHandlers[ident] = callback; + this.clearOscHandler(ident); + this.addOscHandler(ident, (data: string): boolean => { + callback(data); return true; + }); } + /* deprecated */ clearOscHandler(ident: number): void { if (this._oscHandlers[ident]) delete this._oscHandlers[ident]; } @@ -463,9 +509,15 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } break; case ParserAction.CSI_DISPATCH: - callback = this._csiHandlers[code]; - if (callback) callback(params, collect); - else this._csiHandlerFb(collect, params, code); + let cHandler = this._csiHandlers[code]; + if (cHandler) { + for (let i = cHandler.length; ;) { + if (--i < 0) { cHandler = null; break; } + if ((cHandler[i])(params, collect)) + break; + } + } + if (! cHandler) this._csiHandlerFb(collect, params, code); break; case ParserAction.PARAM: if (code === 0x3b) params.push(0); @@ -532,9 +584,15 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP // or with an explicit NaN OSC handler const identifier = parseInt(osc.substring(0, idx)); const content = osc.substring(idx + 1); - callback = this._oscHandlers[identifier]; - if (callback) callback(content); - else this._oscHandlerFb(identifier, content); + let oHandler = this._oscHandlers[identifier]; + if (oHandler) { + for (let i = oHandler.length; ;) { + if (--i < 0) { oHandler = null; break; } + if ((oHandler[i])(content)) + break; + } + } + if (! oHandler) this._oscHandlerFb(identifier, content); } } if (code === 0x1b) transition |= ParserState.ESCAPE; diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 7604b01f69..2c931a2bb0 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -4,7 +4,7 @@ * @license MIT */ -import { IInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; +import { IVtInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; import { C0, C1 } from './common/data/EscapeSequences'; import { CHARSETS, DEFAULT_CHARSET } from './core/data/Charsets'; import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer'; @@ -112,7 +112,7 @@ class DECRQSS implements IDcsHandler { * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand * each function's header comment. */ -export class InputHandler extends Disposable implements IInputHandler { +export class InputHandler extends Disposable implements IVtInputHandler { private _surrogateFirst: string; constructor( @@ -465,6 +465,19 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.updateRange(buffer.y); } + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { + this._parser.addCsiHandler(flag, callback); + } + removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { + this._parser.removeCsiHandler(flag, callback); + } + addOscHandler(ident: number, callback: (data: string) => boolean): void { + this._parser.setOscHandler(ident, callback); + } + removeOscHandler(ident: number, callback: (data: string) => boolean): void { + this._parser.removeOscHandler(ident, callback); + } + /** * BEL * Bell (Ctrl-G). diff --git a/src/Terminal.ts b/src/Terminal.ts index 2cfc1ca88b..fea0cb699d 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -21,7 +21,7 @@ * http://linux.die.net/man/7/urxvt */ -import { IInputHandlingTerminal, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; +import { IInputHandlingTerminal, IInputHandler, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; import { IMouseZoneManager } from './ui/Types'; import { IRenderer } from './renderer/Types'; import { BufferSet } from './BufferSet'; @@ -1286,6 +1286,10 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.refresh(0, this.rows - 1); } + public get inputHandler(): IInputHandler { + return this._inputHandler; + } + /** * Scroll the display of the terminal by a number of pages. * @param pageCount The number of pages to scroll (negative scrolls up). diff --git a/src/Types.ts b/src/Types.ts index 430c6575bd..93dd7c8aa8 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -182,6 +182,16 @@ export interface IInputHandler { ESC ~ */ setgLevel(level: number): void; } +/* + * An InputHandler for VT-style terminals + */ +export interface IVtInputHandler extends IInputHandler { + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; + removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; + addOscHandler(ident: number, callback: (data: string) => boolean): void; + removeOscHandler(ident: number, callback: (data: string) => boolean): void; +} + export interface ILinkMatcher { id: number; regex: RegExp; @@ -492,6 +502,10 @@ export interface IEscapeSequenceParser extends IDisposable { setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void; clearCsiHandler(flag: string): void; setCsiHandlerFallback(callback: (collect: string, params: number[], flag: number) => void): void; + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; + removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; + addOscHandler(ident: number, callback: (data: string) => boolean): void; + removeOscHandler(ident: number, callback: (data: string) => boolean): void; setEscHandler(collectAndFlag: string, callback: () => void): void; clearEscHandler(collectAndFlag: string): void; diff --git a/src/public/Terminal.ts b/src/public/Terminal.ts index 8ff7cf2b3b..de15fad08c 100644 --- a/src/public/Terminal.ts +++ b/src/public/Terminal.ts @@ -4,7 +4,7 @@ */ import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings } from 'xterm'; -import { ITerminal } from '../Types'; +import { ITerminal, IInputHandler } from '../Types'; import { Terminal as TerminalCore } from '../Terminal'; import * as Strings from '../Strings'; @@ -15,6 +15,9 @@ export class Terminal implements ITerminalApi { this._core = new TerminalCore(options); } + public get inputHandler(): IInputHandler { + return (this._core as TerminalCore).inputHandler; + } public get element(): HTMLElement { return this._core.element; } public get textarea(): HTMLTextAreaElement { return this._core.textarea; } public get rows(): number { return this._core.rows; } From b68974517e2f9f02b0304db064020e25afc527fd Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Wed, 5 Dec 2018 14:45:57 -0800 Subject: [PATCH 02/61] Cleanups required by tslink. --- src/EscapeSequenceParser.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 7a5da2c851..5a2c6cd37a 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -304,7 +304,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP private _removeHandler(array: any[], callback: any): void { if (array) { for (let i = array.length; --i >= 0; ) { - if (array[i] == callback) { + if (array[i] === callback) { array.splice(i, 1); return; } @@ -313,18 +313,19 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - let index = flag.charCodeAt(0); + const index = flag.charCodeAt(0); let array = this._csiHandlers[index]; if (! array) { this._csiHandlers[index] = array = new Array(); } array.push(callback); } removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - let index = flag.charCodeAt(0); - let array = this._csiHandlers[index]; + const index = flag.charCodeAt(0); + const array = this._csiHandlers[index]; this._removeHandler(array, callback); - if (array && array.length == 0) + if (array && array.length === 0) { delete this._csiHandlers[index]; + } } /* deprecated */ setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { @@ -357,10 +358,11 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP array.push(callback); } removeOscHandler(ident: number, callback: (data: string) => boolean): void { - let array = this._oscHandlers[ident]; + const array = this._oscHandlers[ident]; this._removeHandler(array, callback); - if (array && array.length == 0) + if (array && array.length === 0) { delete this._oscHandlers[ident]; + } } /* deprecated */ setOscHandler(ident: number, callback: (data: string) => void): void { @@ -513,8 +515,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (cHandler) { for (let i = cHandler.length; ;) { if (--i < 0) { cHandler = null; break; } - if ((cHandler[i])(params, collect)) - break; + if ((cHandler[i])(params, collect)) { break; } } } if (! cHandler) this._csiHandlerFb(collect, params, code); @@ -588,8 +589,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (oHandler) { for (let i = oHandler.length; ;) { if (--i < 0) { oHandler = null; break; } - if ((oHandler[i])(content)) - break; + if ((oHandler[i])(content)) { break; } } } if (! oHandler) this._oscHandlerFb(identifier, content); From 03115639a959c19ad9a5b6e112cab2066cb46c7f Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sun, 9 Dec 2018 10:56:34 -0800 Subject: [PATCH 03/61] Optimize parsing of OSC_STRING to minimize string concatenation. --- src/EscapeSequenceParser.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 5a2c6cd37a..28a946d1dc 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -439,7 +439,11 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } // normal transition & action lookup - transition = (code < 0xa0) ? (table[currentState << 8 | code]) : DEFAULT_TRANSITION; + transition = (code < 0xa0 + ? (table[currentState << 8 | code]) + : currentState === ParserState.OSC_STRING + ? (ParserAction.OSC_PUT << 4) | ParserState.OSC_STRING + : DEFAULT_TRANSITION); switch (transition >> 4) { case ParserAction.PRINT: print = (~print) ? print : i; @@ -471,10 +475,6 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP case ParserState.GROUND: print = (~print) ? print : i; break; - case ParserState.OSC_STRING: - osc += String.fromCharCode(code); - transition |= ParserState.OSC_STRING; - break; case ParserState.CSI_IGNORE: transition |= ParserState.CSI_IGNORE; break; @@ -570,7 +570,16 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP osc = ''; break; case ParserAction.OSC_PUT: - osc += data.charAt(i); + for (let j = i + 1; ; j++) { + if (j >= l + || ((code = data.charCodeAt(j)) <= 0x9f + && (table[ParserState.OSC_STRING << 8 | code] >> 4 + !== ParserAction.OSC_PUT))) { + osc += data.substring(i, j); + i = j - 1; + break; + } + } break; case ParserAction.OSC_END: if (osc && code !== 0x18 && code !== 0x1a) { From 30a667c3be32cbccc57405b58a787a41668491e4 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sun, 9 Dec 2018 13:11:17 -0800 Subject: [PATCH 04/61] Revert "Optimize parsing of OSC_STRING to minimize string concatenation." This reverts commit 03115639a959c19ad9a5b6e112cab2066cb46c7f. --- src/EscapeSequenceParser.ts | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 28a946d1dc..5a2c6cd37a 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -439,11 +439,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } // normal transition & action lookup - transition = (code < 0xa0 - ? (table[currentState << 8 | code]) - : currentState === ParserState.OSC_STRING - ? (ParserAction.OSC_PUT << 4) | ParserState.OSC_STRING - : DEFAULT_TRANSITION); + transition = (code < 0xa0) ? (table[currentState << 8 | code]) : DEFAULT_TRANSITION; switch (transition >> 4) { case ParserAction.PRINT: print = (~print) ? print : i; @@ -475,6 +471,10 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP case ParserState.GROUND: print = (~print) ? print : i; break; + case ParserState.OSC_STRING: + osc += String.fromCharCode(code); + transition |= ParserState.OSC_STRING; + break; case ParserState.CSI_IGNORE: transition |= ParserState.CSI_IGNORE; break; @@ -570,16 +570,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP osc = ''; break; case ParserAction.OSC_PUT: - for (let j = i + 1; ; j++) { - if (j >= l - || ((code = data.charCodeAt(j)) <= 0x9f - && (table[ParserState.OSC_STRING << 8 | code] >> 4 - !== ParserAction.OSC_PUT))) { - osc += data.substring(i, j); - i = j - 1; - break; - } - } + osc += data.charAt(i); break; case ParserAction.OSC_END: if (osc && code !== 0x18 && code !== 0x1a) { From ffb2708a8128a0cf1637c1f57de3c35ceca6029b Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Tue, 11 Dec 2018 16:50:14 -0800 Subject: [PATCH 05/61] Revert "Cleanups required by tslink." This reverts commit b68974517e2f9f02b0304db064020e25afc527fd. --- src/EscapeSequenceParser.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 76b9dd686f..90405a4275 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -306,7 +306,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP private _removeHandler(array: any[], callback: any): void { if (array) { for (let i = array.length; --i >= 0; ) { - if (array[i] === callback) { + if (array[i] == callback) { array.splice(i, 1); return; } @@ -315,19 +315,18 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - const index = flag.charCodeAt(0); + let index = flag.charCodeAt(0); let array = this._csiHandlers[index]; if (! array) { this._csiHandlers[index] = array = new Array(); } array.push(callback); } removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - const index = flag.charCodeAt(0); - const array = this._csiHandlers[index]; + let index = flag.charCodeAt(0); + let array = this._csiHandlers[index]; this._removeHandler(array, callback); - if (array && array.length === 0) { + if (array && array.length == 0) delete this._csiHandlers[index]; - } } /* deprecated */ setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { @@ -360,11 +359,10 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP array.push(callback); } removeOscHandler(ident: number, callback: (data: string) => boolean): void { - const array = this._oscHandlers[ident]; + let array = this._oscHandlers[ident]; this._removeHandler(array, callback); - if (array && array.length === 0) { + if (array && array.length == 0) delete this._oscHandlers[ident]; - } } /* deprecated */ setOscHandler(ident: number, callback: (data: string) => void): void { @@ -513,7 +511,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (cHandler) { for (let i = cHandler.length; ;) { if (--i < 0) { cHandler = null; break; } - if ((cHandler[i])(params, collect)) { break; } + if ((cHandler[i])(params, collect)) + break; } } if (! cHandler) this._csiHandlerFb(collect, params, code); @@ -595,7 +594,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (oHandler) { for (let i = oHandler.length; ;) { if (--i < 0) { oHandler = null; break; } - if ((oHandler[i])(content)) { break; } + if ((oHandler[i])(content)) + break; } } if (! oHandler) this._oscHandlerFb(identifier, content); From 53fd04a8867a9e67519df38600d2a4c08b0e3065 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Tue, 11 Dec 2018 16:50:40 -0800 Subject: [PATCH 06/61] Revert "hooks for custom control sequences" This reverts commit 4d660deff3db9f3eea3923dcaa970fde0e9deaf8. --- src/EscapeSequenceParser.ts | 74 ++++--------------------------------- src/InputHandler.ts | 17 +-------- src/Terminal.ts | 6 +-- src/Types.ts | 14 ------- src/public/Terminal.ts | 5 +-- 5 files changed, 12 insertions(+), 104 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 90405a4275..f489884177 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -303,39 +303,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._executeHandlerFb = callback; } - private _removeHandler(array: any[], callback: any): void { - if (array) { - for (let i = array.length; --i >= 0; ) { - if (array[i] == callback) { - array.splice(i, 1); - return; - } - } - } - } - - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - let index = flag.charCodeAt(0); - let array = this._csiHandlers[index]; - if (! array) { this._csiHandlers[index] = array = new Array(); } - array.push(callback); - } - - removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - let index = flag.charCodeAt(0); - let array = this._csiHandlers[index]; - this._removeHandler(array, callback); - if (array && array.length == 0) - delete this._csiHandlers[index]; - } - /* deprecated */ setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { - this.clearCsiHandler(flag); - this.addCsiHandler(flag, (params: number[], collect: string): boolean => { - callback(params, collect); return true; - }); + this._csiHandlers[flag.charCodeAt(0)] = callback; } - /* deprecated */ clearCsiHandler(flag: string): void { if (this._csiHandlers[flag.charCodeAt(0)]) delete this._csiHandlers[flag.charCodeAt(0)]; } @@ -353,25 +323,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._escHandlerFb = callback; } - addOscHandler(ident: number, callback: (data: string) => boolean): void { - let array = this._oscHandlers[ident]; - if (! array) { this._oscHandlers[ident] = array = new Array(); } - array.push(callback); - } - removeOscHandler(ident: number, callback: (data: string) => boolean): void { - let array = this._oscHandlers[ident]; - this._removeHandler(array, callback); - if (array && array.length == 0) - delete this._oscHandlers[ident]; - } - /* deprecated */ setOscHandler(ident: number, callback: (data: string) => void): void { - this.clearOscHandler(ident); - this.addOscHandler(ident, (data: string): boolean => { - callback(data); return true; - }); + this._oscHandlers[ident] = callback; } - /* deprecated */ clearOscHandler(ident: number): void { if (this._oscHandlers[ident]) delete this._oscHandlers[ident]; } @@ -507,15 +461,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } break; case ParserAction.CSI_DISPATCH: - let cHandler = this._csiHandlers[code]; - if (cHandler) { - for (let i = cHandler.length; ;) { - if (--i < 0) { cHandler = null; break; } - if ((cHandler[i])(params, collect)) - break; - } - } - if (! cHandler) this._csiHandlerFb(collect, params, code); + callback = this._csiHandlers[code]; + if (callback) callback(params, collect); + else this._csiHandlerFb(collect, params, code); break; case ParserAction.PARAM: if (code === 0x3b) params.push(0); @@ -590,15 +538,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP // or with an explicit NaN OSC handler const identifier = parseInt(osc.substring(0, idx)); const content = osc.substring(idx + 1); - let oHandler = this._oscHandlers[identifier]; - if (oHandler) { - for (let i = oHandler.length; ;) { - if (--i < 0) { oHandler = null; break; } - if ((oHandler[i])(content)) - break; - } - } - if (! oHandler) this._oscHandlerFb(identifier, content); + callback = this._oscHandlers[identifier]; + if (callback) callback(content); + else this._oscHandlerFb(identifier, content); } } if (code === 0x1b) transition |= ParserState.ESCAPE; diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 2c931a2bb0..7604b01f69 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -4,7 +4,7 @@ * @license MIT */ -import { IVtInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; +import { IInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; import { C0, C1 } from './common/data/EscapeSequences'; import { CHARSETS, DEFAULT_CHARSET } from './core/data/Charsets'; import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer'; @@ -112,7 +112,7 @@ class DECRQSS implements IDcsHandler { * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand * each function's header comment. */ -export class InputHandler extends Disposable implements IVtInputHandler { +export class InputHandler extends Disposable implements IInputHandler { private _surrogateFirst: string; constructor( @@ -465,19 +465,6 @@ export class InputHandler extends Disposable implements IVtInputHandler { this._terminal.updateRange(buffer.y); } - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - this._parser.addCsiHandler(flag, callback); - } - removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void { - this._parser.removeCsiHandler(flag, callback); - } - addOscHandler(ident: number, callback: (data: string) => boolean): void { - this._parser.setOscHandler(ident, callback); - } - removeOscHandler(ident: number, callback: (data: string) => boolean): void { - this._parser.removeOscHandler(ident, callback); - } - /** * BEL * Bell (Ctrl-G). diff --git a/src/Terminal.ts b/src/Terminal.ts index 8f66ebe905..bc8fb103a2 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -21,7 +21,7 @@ * http://linux.die.net/man/7/urxvt */ -import { IInputHandlingTerminal, IInputHandler, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; +import { IInputHandlingTerminal, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; import { IMouseZoneManager } from './ui/Types'; import { IRenderer } from './renderer/Types'; import { BufferSet } from './BufferSet'; @@ -1287,10 +1287,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.refresh(0, this.rows - 1); } - public get inputHandler(): IInputHandler { - return this._inputHandler; - } - /** * Scroll the display of the terminal by a number of pages. * @param pageCount The number of pages to scroll (negative scrolls up). diff --git a/src/Types.ts b/src/Types.ts index 22eff1c853..a5aa8add70 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -182,16 +182,6 @@ export interface IInputHandler { ESC ~ */ setgLevel(level: number): void; } -/* - * An InputHandler for VT-style terminals - */ -export interface IVtInputHandler extends IInputHandler { - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; - removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; - addOscHandler(ident: number, callback: (data: string) => boolean): void; - removeOscHandler(ident: number, callback: (data: string) => boolean): void; -} - export interface ILinkMatcher { id: number; regex: RegExp; @@ -502,10 +492,6 @@ export interface IEscapeSequenceParser extends IDisposable { setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void; clearCsiHandler(flag: string): void; setCsiHandlerFallback(callback: (collect: string, params: number[], flag: number) => void): void; - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; - removeCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): void; - addOscHandler(ident: number, callback: (data: string) => boolean): void; - removeOscHandler(ident: number, callback: (data: string) => boolean): void; setEscHandler(collectAndFlag: string, callback: () => void): void; clearEscHandler(collectAndFlag: string): void; diff --git a/src/public/Terminal.ts b/src/public/Terminal.ts index de15fad08c..8ff7cf2b3b 100644 --- a/src/public/Terminal.ts +++ b/src/public/Terminal.ts @@ -4,7 +4,7 @@ */ import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings } from 'xterm'; -import { ITerminal, IInputHandler } from '../Types'; +import { ITerminal } from '../Types'; import { Terminal as TerminalCore } from '../Terminal'; import * as Strings from '../Strings'; @@ -15,9 +15,6 @@ export class Terminal implements ITerminalApi { this._core = new TerminalCore(options); } - public get inputHandler(): IInputHandler { - return (this._core as TerminalCore).inputHandler; - } public get element(): HTMLElement { return this._core.element; } public get textarea(): HTMLTextAreaElement { return this._core.textarea; } public get rows(): number { return this._core.rows; } From 8ceea112f7a4d11532b03d39a06d319c08c13f18 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Tue, 11 Dec 2018 17:18:10 -0800 Subject: [PATCH 07/61] hooks for custom control sequences This re-implements addCsiHandler/addOscHandler to return an IDisposable. --- src/EscapeSequenceParser.ts | 53 ++++++++++++++++++++++++++++++++++++- src/InputHandler.ts | 12 +++++++-- src/Terminal.ts | 6 ++++- src/Types.ts | 10 +++++++ src/public/Terminal.ts | 5 +++- 5 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index f489884177..285629391e 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -4,6 +4,7 @@ */ import { ParserState, ParserAction, IParsingState, IDcsHandler, IEscapeSequenceParser } from './Types'; +import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; /** @@ -41,7 +42,7 @@ export class TransitionTable { * @param action parser action to be done * @param next next parser state */ - add(code: number, state: number, action: number | null, next: number | null): void { + add(code: number, state: number, action: number | null, next: number | null): void { this.table[state << 8 | code] = ((action | 0) << 4) | ((next === undefined) ? state : next); } @@ -303,6 +304,32 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._executeHandlerFb = callback; } + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + const index = flag.charCodeAt(0); + const oldHead = this._csiHandlers[index]; + const newHead = Object.assign( + (params: number[], collect: string): void => { + if (callback(params, collect)) { } + else if (newHead.nextHandler) { newHead.nextHandler(params, collect); } + else { this._csiHandlerFb(collect, params, index); } + }, + { nextHandler: oldHead, + dispose(): void { + let previous = null; let cur = this._csiHandlers[index]; + for (; cur && cur.nextHandler; + previous = cur, cur = cur.nextHandler) { + if (cur === newHead) { + if (previous) { previous.nextHandler = cur.nextHandler; } + else { this._csiHandlers[index] = cur.nextHandler; } + break; + } + } + } + }); + this._csiHandlers[index] = newHead; + return newHead; + } + setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { this._csiHandlers[flag.charCodeAt(0)] = callback; } @@ -323,6 +350,30 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._escHandlerFb = callback; } + addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { + const oldHead = this._oscHandlers[ident]; + const newHead = Object.assign( + (data: string): void => { + if (callback(data)) { } + else if (newHead.nextHandler) { newHead.nextHandler(data); } + else { this._oscHandlerFb(ident, data); } + }, + { nextHandler: oldHead, + dispose(): void { + let previous = null; let cur = this._oscHandlers[ident]; + for (; cur && cur.nextHandler; + previous = cur, cur = cur.nextHandler) { + if (cur === newHead) { + if (previous) { previous.nextHandler = cur.nextHandler; } + else { this._oscHandlers[ident] = cur.nextHandler; } + break; + } + } + } + }); + this._oscHandlers[ident] = newHead; + return newHead; + } setOscHandler(ident: number, callback: (data: string) => void): void { this._oscHandlers[ident] = callback; } diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 7604b01f69..2e016296f7 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -4,7 +4,7 @@ * @license MIT */ -import { IInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; +import { IVtInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; import { C0, C1 } from './common/data/EscapeSequences'; import { CHARSETS, DEFAULT_CHARSET } from './core/data/Charsets'; import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer'; @@ -12,6 +12,7 @@ import { FLAGS } from './renderer/Types'; import { wcwidth } from './CharWidth'; import { EscapeSequenceParser } from './EscapeSequenceParser'; import { ICharset } from './core/Types'; +import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; /** @@ -112,7 +113,7 @@ class DECRQSS implements IDcsHandler { * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand * each function's header comment. */ -export class InputHandler extends Disposable implements IInputHandler { +export class InputHandler extends Disposable implements IVtInputHandler { private _surrogateFirst: string; constructor( @@ -465,6 +466,13 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.updateRange(buffer.y); } + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + return this._parser.addCsiHandler(flag, callback); + } + addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { + return this._parser.addOscHandler(ident, callback); + } + /** * BEL * Bell (Ctrl-G). diff --git a/src/Terminal.ts b/src/Terminal.ts index bc8fb103a2..8f66ebe905 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -21,7 +21,7 @@ * http://linux.die.net/man/7/urxvt */ -import { IInputHandlingTerminal, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; +import { IInputHandlingTerminal, IInputHandler, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; import { IMouseZoneManager } from './ui/Types'; import { IRenderer } from './renderer/Types'; import { BufferSet } from './BufferSet'; @@ -1287,6 +1287,10 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.refresh(0, this.rows - 1); } + public get inputHandler(): IInputHandler { + return this._inputHandler; + } + /** * Scroll the display of the terminal by a number of pages. * @param pageCount The number of pages to scroll (negative scrolls up). diff --git a/src/Types.ts b/src/Types.ts index a5aa8add70..e150b90b55 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -182,6 +182,14 @@ export interface IInputHandler { ESC ~ */ setgLevel(level: number): void; } +/* + * An InputHandler for VT-style terminals + */ +export interface IVtInputHandler extends IInputHandler { + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable; + addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable; +} + export interface ILinkMatcher { id: number; regex: RegExp; @@ -492,6 +500,8 @@ export interface IEscapeSequenceParser extends IDisposable { setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void; clearCsiHandler(flag: string): void; setCsiHandlerFallback(callback: (collect: string, params: number[], flag: number) => void): void; + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable; + addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable; setEscHandler(collectAndFlag: string, callback: () => void): void; clearEscHandler(collectAndFlag: string): void; diff --git a/src/public/Terminal.ts b/src/public/Terminal.ts index 8ff7cf2b3b..de15fad08c 100644 --- a/src/public/Terminal.ts +++ b/src/public/Terminal.ts @@ -4,7 +4,7 @@ */ import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings } from 'xterm'; -import { ITerminal } from '../Types'; +import { ITerminal, IInputHandler } from '../Types'; import { Terminal as TerminalCore } from '../Terminal'; import * as Strings from '../Strings'; @@ -15,6 +15,9 @@ export class Terminal implements ITerminalApi { this._core = new TerminalCore(options); } + public get inputHandler(): IInputHandler { + return (this._core as TerminalCore).inputHandler; + } public get element(): HTMLElement { return this._core.element; } public get textarea(): HTMLTextAreaElement { return this._core.textarea; } public get rows(): number { return this._core.rows; } From 5af4626ec7d44f0536024a7a9af66b463ee5d7d9 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Thu, 13 Dec 2018 17:33:33 -0800 Subject: [PATCH 08/61] Change addCsiHandler/addOscHandler to not use Object.assign. --- src/EscapeSequenceParser.ts | 47 ++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 285629391e..a104d58362 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -307,25 +307,26 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { const index = flag.charCodeAt(0); const oldHead = this._csiHandlers[index]; - const newHead = Object.assign( + const parser = this; + const newHead = (params: number[], collect: string): void => { - if (callback(params, collect)) { } - else if (newHead.nextHandler) { newHead.nextHandler(params, collect); } - else { this._csiHandlerFb(collect, params, index); } - }, - { nextHandler: oldHead, - dispose(): void { - let previous = null; let cur = this._csiHandlers[index]; + if (! callback(params, collect)) { + if (newHead.nextHandler) { newHead.nextHandler(params, collect); } + else { this._csiHandlerFb(collect, params, index); } + } + }; + newHead.nextHandler = oldHead; + newHead.dispose = function (): void { + let previous = null; let cur = parser._csiHandlers[index]; for (; cur && cur.nextHandler; previous = cur, cur = cur.nextHandler) { if (cur === newHead) { if (previous) { previous.nextHandler = cur.nextHandler; } - else { this._csiHandlers[index] = cur.nextHandler; } + else { parser._csiHandlers[index] = cur.nextHandler; } break; } } - } - }); + }; this._csiHandlers[index] = newHead; return newHead; } @@ -352,25 +353,27 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { const oldHead = this._oscHandlers[ident]; - const newHead = Object.assign( + const parser = this; + const newHead = (data: string): void => { - if (callback(data)) { } - else if (newHead.nextHandler) { newHead.nextHandler(data); } - else { this._oscHandlerFb(ident, data); } - }, - { nextHandler: oldHead, - dispose(): void { - let previous = null; let cur = this._oscHandlers[ident]; + if (! callback(data)) { + if (newHead.nextHandler) { newHead.nextHandler(data); } + else { this._oscHandlerFb(ident, data); } + } + }; + newHead.nextHandler = oldHead; + newHead.dispose = + function (): void { + let previous = null; let cur = parser._oscHandlers[ident]; for (; cur && cur.nextHandler; previous = cur, cur = cur.nextHandler) { if (cur === newHead) { if (previous) { previous.nextHandler = cur.nextHandler; } - else { this._oscHandlers[ident] = cur.nextHandler; } + else { parser._oscHandlers[ident] = cur.nextHandler; } break; } } - } - }); + }; this._oscHandlers[ident] = newHead; return newHead; } From 8a5a03238fedded95e4cedfc9e80e3e0aec2ecc4 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sat, 15 Dec 2018 09:42:19 -0800 Subject: [PATCH 09/61] New method _linkHandler used by both addCsiHandler and addOscHandler. --- src/EscapeSequenceParser.ts | 60 +++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index a104d58362..c85f670d4c 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -7,6 +7,10 @@ import { ParserState, ParserAction, IParsingState, IDcsHandler, IEscapeSequenceP import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; +interface IHandlerLink extends IDisposable { + nextHandler: IHandlerLink | null; +} + /** * Returns an array filled with numbers between the low and high parameters (right exclusive). * @param low The low number. @@ -304,33 +308,38 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._executeHandlerFb = callback; } - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { - const index = flag.charCodeAt(0); - const oldHead = this._csiHandlers[index]; - const parser = this; - const newHead = - (params: number[], collect: string): void => { - if (! callback(params, collect)) { - if (newHead.nextHandler) { newHead.nextHandler(params, collect); } - else { this._csiHandlerFb(collect, params, index); } - } - }; - newHead.nextHandler = oldHead; + private _linkHandler(handlers: object[], index: number, newCallback: object): IDisposable { + const newHead: any = newCallback; + newHead.nextHandler = handlers[index] as IHandlerLink; newHead.dispose = function (): void { - let previous = null; let cur = parser._csiHandlers[index]; + let previous = null; + let cur = handlers[index] as IHandlerLink; for (; cur && cur.nextHandler; previous = cur, cur = cur.nextHandler) { if (cur === newHead) { if (previous) { previous.nextHandler = cur.nextHandler; } - else { parser._csiHandlers[index] = cur.nextHandler; } + else { handlers[index] = cur.nextHandler; } break; } } }; - this._csiHandlers[index] = newHead; + handlers[index] = newHead; return newHead; } + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + const index = flag.charCodeAt(0); + const newHead = + (params: number[], collect: string): void => { + if (! callback(params, collect)) { + const next = (newHead as unknown as IHandlerLink).nextHandler; + if (next) { (next as any)(params, collect); } + else { this._csiHandlerFb(collect, params, index); } + } + }; + return this._linkHandler(this._csiHandlers, index, newHead); + } + setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { this._csiHandlers[flag.charCodeAt(0)] = callback; } @@ -352,30 +361,15 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { - const oldHead = this._oscHandlers[ident]; - const parser = this; const newHead = (data: string): void => { if (! callback(data)) { - if (newHead.nextHandler) { newHead.nextHandler(data); } + const next = (newHead as unknown as IHandlerLink).nextHandler; + if (next) { (next as any)(data); } else { this._oscHandlerFb(ident, data); } } }; - newHead.nextHandler = oldHead; - newHead.dispose = - function (): void { - let previous = null; let cur = parser._oscHandlers[ident]; - for (; cur && cur.nextHandler; - previous = cur, cur = cur.nextHandler) { - if (cur === newHead) { - if (previous) { previous.nextHandler = cur.nextHandler; } - else { parser._oscHandlers[ident] = cur.nextHandler; } - break; - } - } - }; - this._oscHandlers[ident] = newHead; - return newHead; + return this._linkHandler(this._oscHandlers, ident, newHead); } setOscHandler(ident: number, callback: (data: string) => void): void { this._oscHandlers[ident] = callback; From 6b65ebd4aace41ddf0f7d663e5ff6c92a4a37948 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sat, 15 Dec 2018 11:34:15 -0800 Subject: [PATCH 10/61] Various typing and API fixes, doc comments, typing test etc. --- fixtures/typings-test/typings-test.ts | 8 +++++++- src/InputHandler.ts | 4 ++-- src/Terminal.ts | 15 ++++++++++----- src/Types.ts | 8 -------- src/public/Terminal.ts | 11 +++++++---- src/ui/TestUtils.test.ts | 6 ++++++ typings/xterm.d.ts | 25 +++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 20 deletions(-) diff --git a/fixtures/typings-test/typings-test.ts b/fixtures/typings-test/typings-test.ts index 13da69616b..87d911c2a4 100644 --- a/fixtures/typings-test/typings-test.ts +++ b/fixtures/typings-test/typings-test.ts @@ -4,7 +4,7 @@ /// -import { Terminal } from 'xterm'; +import { Terminal, IDisposable } from 'xterm'; namespace constructor { { @@ -119,6 +119,12 @@ namespace methods_core { const t: Terminal = new Terminal(); t.attachCustomKeyEventHandler((e: KeyboardEvent) => true); t.attachCustomKeyEventHandler((e: KeyboardEvent) => false); + const d1: IDisposable = t.addCsiHandler("x", + (params: number[], collect: string): boolean => params[0]===1); + d1.dispose(); + const d2: IDisposable = t.addOscHandler(199, + (data: string): boolean => true); + d2.dispose(); } namespace options { { diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 2e016296f7..eb1ca1050b 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -4,7 +4,7 @@ * @license MIT */ -import { IVtInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; +import { IInputHandler, IDcsHandler, IEscapeSequenceParser, IBuffer, IInputHandlingTerminal } from './Types'; import { C0, C1 } from './common/data/EscapeSequences'; import { CHARSETS, DEFAULT_CHARSET } from './core/data/Charsets'; import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer'; @@ -113,7 +113,7 @@ class DECRQSS implements IDcsHandler { * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand * each function's header comment. */ -export class InputHandler extends Disposable implements IVtInputHandler { +export class InputHandler extends Disposable implements IInputHandler { private _surrogateFirst: string; constructor( diff --git a/src/Terminal.ts b/src/Terminal.ts index 8f66ebe905..bed45e46ec 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -21,7 +21,7 @@ * http://linux.die.net/man/7/urxvt */ -import { IInputHandlingTerminal, IInputHandler, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; +import { IInputHandlingTerminal, IViewport, ICompositionHelper, ITerminalOptions, ITerminal, IBrowser, ILinkifier, ILinkMatcherOptions, CustomKeyEventHandler, LinkMatcherHandler, CharData, CharacterJoinerHandler, IBufferLine } from './Types'; import { IMouseZoneManager } from './ui/Types'; import { IRenderer } from './renderer/Types'; import { BufferSet } from './BufferSet'; @@ -1287,10 +1287,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.refresh(0, this.rows - 1); } - public get inputHandler(): IInputHandler { - return this._inputHandler; - } - /** * Scroll the display of the terminal by a number of pages. * @param pageCount The number of pages to scroll (negative scrolls up). @@ -1417,6 +1413,15 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this._customKeyEventHandler = customKeyEventHandler; } + /** Add handler for CSI escape sequence. See xterm.d.ts for details. */ + public addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + return this._inputHandler.addCsiHandler(flag, callback); + } + /** Add handler for OSC escape sequence. See xterm.d.ts for details. */ + public addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { + return this._inputHandler.addOscHandler(ident, callback); + } + /** * Registers a link matcher, allowing custom link patterns to be matched and * handled. diff --git a/src/Types.ts b/src/Types.ts index e150b90b55..93ba01f64b 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -182,14 +182,6 @@ export interface IInputHandler { ESC ~ */ setgLevel(level: number): void; } -/* - * An InputHandler for VT-style terminals - */ -export interface IVtInputHandler extends IInputHandler { - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable; - addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable; -} - export interface ILinkMatcher { id: number; regex: RegExp; diff --git a/src/public/Terminal.ts b/src/public/Terminal.ts index de15fad08c..87fcfaef99 100644 --- a/src/public/Terminal.ts +++ b/src/public/Terminal.ts @@ -4,7 +4,7 @@ */ import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings } from 'xterm'; -import { ITerminal, IInputHandler } from '../Types'; +import { ITerminal } from '../Types'; import { Terminal as TerminalCore } from '../Terminal'; import * as Strings from '../Strings'; @@ -15,9 +15,6 @@ export class Terminal implements ITerminalApi { this._core = new TerminalCore(options); } - public get inputHandler(): IInputHandler { - return (this._core as TerminalCore).inputHandler; - } public get element(): HTMLElement { return this._core.element; } public get textarea(): HTMLTextAreaElement { return this._core.textarea; } public get rows(): number { return this._core.rows; } @@ -62,6 +59,12 @@ export class Terminal implements ITerminalApi { public attachCustomKeyEventHandler(customKeyEventHandler: (event: KeyboardEvent) => boolean): void { this._core.attachCustomKeyEventHandler(customKeyEventHandler); } + public addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + return this._core.addCsiHandler(flag, callback); + } + public addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { + return this._core.addOscHandler(ident, callback); + } public registerLinkMatcher(regex: RegExp, handler: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions): number { return this._core.registerLinkMatcher(regex, handler, options); } diff --git a/src/ui/TestUtils.test.ts b/src/ui/TestUtils.test.ts index 10033a3355..e6e4aaa32f 100644 --- a/src/ui/TestUtils.test.ts +++ b/src/ui/TestUtils.test.ts @@ -54,6 +54,12 @@ export class MockTerminal implements ITerminal { attachCustomKeyEventHandler(customKeyEventHandler: (event: KeyboardEvent) => boolean): void { throw new Error('Method not implemented.'); } + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + throw new Error('Method not implemented.'); + } + addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { + throw new Error('Method not implemented.'); + } registerLinkMatcher(regex: RegExp, handler: (event: MouseEvent, uri: string) => boolean | void, options?: ILinkMatcherOptions): number { throw new Error('Method not implemented.'); } diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 7528bb5540..cf489a610c 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -481,6 +481,31 @@ declare module 'xterm' { */ attachCustomKeyEventHandler(customKeyEventHandler: (event: KeyboardEvent) => boolean): void; + /** + * (EXPERIMENTAL) Adds a handler for CSI escape sequences. + * @param flag The flag should be one-character string, which specifies + * the final character (e.g "m" for SGR) of the CSI sequence. + * @param callback The function to handle the escape sequence. + * The callback is called with the numerical params, + * as well as the special characters (e.g. "$" for DECSCPP). + * Return true if the sequence was handled; false if we should + * try a previous handler (set by addCsiHandler or setCsiHandler). + * The most recently-added handler is tried first. + * @return An IDisposable you can call to remove this handler. + */ + addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable; + + /** + * (EXPERIMENTAL) Adds a handler for OSC escape sequences. + * @param ident The number (first parameter) of the sequence. + * @param callback The function to handle the escape sequence. + * The callback is called with OSC data string. + * Return true if the sequence was handled; false if we should + * try a previous handler (set by addOscHandler or setOscHandler). + * The most recently-added handler is tried first. + * @return An IDisposable you can call to remove this handler. + */ + addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable; /** * (EXPERIMENTAL) Registers a link matcher, allowing custom link patterns to * be matched and handled. From c6e6c519291be333792f0469b205f4522abd1814 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 23 Dec 2018 10:51:41 -0800 Subject: [PATCH 11/61] Use textBaseline middle to draw instead of top Chrome and Firefox behavior differs, see: - https://bugzilla.mozilla.org/show_bug.cgi?id=737852 - https://bugs.chromium.org/p/chromium/issues/detail?id=607053 Fixes #1858 --- src/renderer/BaseRenderLayer.ts | 8 ++++---- src/renderer/atlas/CharAtlasGenerator.ts | 10 +++++----- src/renderer/atlas/DynamicCharAtlas.ts | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 2afdebb54e..3e0b864307 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -236,12 +236,12 @@ export abstract class BaseRenderLayer implements IRenderLayer { */ protected fillCharTrueColor(terminal: ITerminal, charData: CharData, x: number, y: number): void { this._ctx.font = this._getFont(terminal, false, false); - this._ctx.textBaseline = 'top'; + this._ctx.textBaseline = 'middle'; this._clipRow(terminal, y); this._ctx.fillText( charData[CHAR_DATA_CHAR_INDEX], x * this._scaledCellWidth + this._scaledCharLeft, - y * this._scaledCellHeight + this._scaledCharTop); + (y + 0.5) * this._scaledCellHeight + this._scaledCharTop); } /** @@ -295,7 +295,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { private _drawUncachedChars(terminal: ITerminal, chars: string, width: number, fg: number, x: number, y: number, bold: boolean, dim: boolean, italic: boolean): void { this._ctx.save(); this._ctx.font = this._getFont(terminal, bold, italic); - this._ctx.textBaseline = 'top'; + this._ctx.textBaseline = 'middle'; if (fg === INVERTED_DEFAULT_COLOR) { this._ctx.fillStyle = this._colors.background.css; @@ -316,7 +316,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { this._ctx.fillText( chars, x * this._scaledCellWidth + this._scaledCharLeft, - y * this._scaledCellHeight + this._scaledCharTop); + (y + 0.5) * this._scaledCellHeight + this._scaledCharTop); this._ctx.restore(); } diff --git a/src/renderer/atlas/CharAtlasGenerator.ts b/src/renderer/atlas/CharAtlasGenerator.ts index e40215cfe8..cadcce2ec1 100644 --- a/src/renderer/atlas/CharAtlasGenerator.ts +++ b/src/renderer/atlas/CharAtlasGenerator.ts @@ -29,7 +29,7 @@ export function generateStaticCharAtlasTexture(context: Window, canvasFactory: ( ctx.save(); ctx.fillStyle = config.colors.foreground.css; ctx.font = getFont(config.fontWeight, config); - ctx.textBaseline = 'top'; + ctx.textBaseline = 'middle'; // Default color for (let i = 0; i < 256; i++) { @@ -37,7 +37,7 @@ export function generateStaticCharAtlasTexture(context: Window, canvasFactory: ( ctx.beginPath(); ctx.rect(i * cellWidth, 0, cellWidth, cellHeight); ctx.clip(); - ctx.fillText(String.fromCharCode(i), i * cellWidth, 0); + ctx.fillText(String.fromCharCode(i), i * cellWidth, cellHeight / 2); ctx.restore(); } // Default color bold @@ -48,7 +48,7 @@ export function generateStaticCharAtlasTexture(context: Window, canvasFactory: ( ctx.beginPath(); ctx.rect(i * cellWidth, cellHeight, cellWidth, cellHeight); ctx.clip(); - ctx.fillText(String.fromCharCode(i), i * cellWidth, cellHeight); + ctx.fillText(String.fromCharCode(i), i * cellWidth, cellHeight * 1.5); ctx.restore(); } ctx.restore(); @@ -64,7 +64,7 @@ export function generateStaticCharAtlasTexture(context: Window, canvasFactory: ( ctx.rect(i * cellWidth, y, cellWidth, cellHeight); ctx.clip(); ctx.fillStyle = config.colors.ansi[colorIndex].css; - ctx.fillText(String.fromCharCode(i), i * cellWidth, y); + ctx.fillText(String.fromCharCode(i), i * cellWidth, y + cellHeight / 2); ctx.restore(); } } @@ -80,7 +80,7 @@ export function generateStaticCharAtlasTexture(context: Window, canvasFactory: ( ctx.rect(i * cellWidth, y, cellWidth, cellHeight); ctx.clip(); ctx.fillStyle = config.colors.ansi[colorIndex].css; - ctx.fillText(String.fromCharCode(i), i * cellWidth, y); + ctx.fillText(String.fromCharCode(i), i * cellWidth, y + cellHeight / 2); ctx.restore(); } } diff --git a/src/renderer/atlas/DynamicCharAtlas.ts b/src/renderer/atlas/DynamicCharAtlas.ts index 72010768a3..e311c369a3 100644 --- a/src/renderer/atlas/DynamicCharAtlas.ts +++ b/src/renderer/atlas/DynamicCharAtlas.ts @@ -250,7 +250,7 @@ export default class DynamicCharAtlas extends BaseCharAtlas { const fontStyle = glyph.italic ? 'italic' : ''; this._tmpCtx.font = `${fontStyle} ${fontWeight} ${this._config.fontSize * this._config.devicePixelRatio}px ${this._config.fontFamily}`; - this._tmpCtx.textBaseline = 'top'; + this._tmpCtx.textBaseline = 'middle'; this._tmpCtx.fillStyle = this._getForegroundColor(glyph).css; @@ -259,7 +259,7 @@ export default class DynamicCharAtlas extends BaseCharAtlas { this._tmpCtx.globalAlpha = DIM_OPACITY; } // Draw the character - this._tmpCtx.fillText(glyph.chars, 0, 0); + this._tmpCtx.fillText(glyph.chars, 0, this._config.scaledCharHeight / 2); this._tmpCtx.restore(); // clear the background from the character to avoid issues with drawing over the previous From 48ff841d6ab23744570326201c4294a25ec16474 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sun, 23 Dec 2018 12:27:26 -0800 Subject: [PATCH 12/61] Be more paranoid about cleaning up escape sequence handlers. --- src/EscapeSequenceParser.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index c85f670d4c..3215513065 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -283,8 +283,17 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._errorHandlerFb = null; this._printHandler = null; this._executeHandlers = null; - this._csiHandlers = null; this._escHandlers = null; + let handlers; + while ((handlers = this._csiHandlers) && handlers.dispose !== undefined) { + handlers.dispose(); + if (handlers === this._csiHandlers) { break; } // sanity check + } + this._csiHandlers = null; + while ((handlers = this._oscHandlers) && handlers.dispose !== undefined) { + handlers.dispose(); + if (handlers === this._oscHandlers) { break; } // sanity check + } this._oscHandlers = null; this._dcsHandlers = null; this._activeDcsHandler = null; @@ -319,6 +328,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (cur === newHead) { if (previous) { previous.nextHandler = cur.nextHandler; } else { handlers[index] = cur.nextHandler; } + cur.nextHandler = null; + handlers = null; newCallback = null; // just in case break; } } From 9e3e724f63f0de83a86997041395bea58ad8009e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 10:53:40 -0800 Subject: [PATCH 13/61] Add sanity checks to dom renderer underline code Fixes #1860 --- src/renderer/dom/DomRenderer.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/renderer/dom/DomRenderer.ts b/src/renderer/dom/DomRenderer.ts index a0cefd677c..c5ef212da6 100644 --- a/src/renderer/dom/DomRenderer.ts +++ b/src/renderer/dom/DomRenderer.ts @@ -364,9 +364,11 @@ export class DomRenderer extends EventEmitter implements IRenderer { return; } const span = row.children[x]; - span.style.textDecoration = enabled ? 'underline' : 'none'; - x = (x + 1) % cols; - if (x === 0) { + if (span) { + span.style.textDecoration = enabled ? 'underline' : 'none'; + } + if (++x >= cols) { + x = 0; y++; } } From d01efdda270f0d9c2a0c55ec78bc40e7213e1d11 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 14:08:53 -0800 Subject: [PATCH 14/61] Use array instead of linkedlist, add typings --- src/EscapeSequenceParser.ts | 110 ++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 61 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 3215513065..52007244d6 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -11,6 +11,13 @@ interface IHandlerLink extends IDisposable { nextHandler: IHandlerLink | null; } +interface IHandlerCollection { + [key: string]: T[]; +} + +type CsiHandler = (params: number[], collect: string) => boolean | void; +type OscHandler = (data: string) => boolean | void; + /** * Returns an array filled with numbers between the low and high parameters (right exclusive). * @param low The low number. @@ -46,7 +53,7 @@ export class TransitionTable { * @param action parser action to be done * @param next next parser state */ - add(code: number, state: number, action: number | null, next: number | null): void { + add(code: number, state: number, action: number | null, next: number | null): void { this.table[state << 8 | code] = ((action | 0) << 4) | ((next === undefined) ? state : next); } @@ -227,9 +234,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP // handler lookup containers protected _printHandler: (data: string, start: number, end: number) => void; protected _executeHandlers: any; - protected _csiHandlers: any; + protected _csiHandlers: IHandlerCollection; protected _escHandlers: any; - protected _oscHandlers: any; + protected _oscHandlers: IHandlerCollection; protected _dcsHandlers: any; protected _activeDcsHandler: IDcsHandler | null; protected _errorHandler: (state: IParsingState) => IParsingState; @@ -284,16 +291,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._printHandler = null; this._executeHandlers = null; this._escHandlers = null; - let handlers; - while ((handlers = this._csiHandlers) && handlers.dispose !== undefined) { - handlers.dispose(); - if (handlers === this._csiHandlers) { break; } // sanity check - } this._csiHandlers = null; - while ((handlers = this._oscHandlers) && handlers.dispose !== undefined) { - handlers.dispose(); - if (handlers === this._oscHandlers) { break; } // sanity check - } this._oscHandlers = null; this._dcsHandlers = null; this._activeDcsHandler = null; @@ -317,42 +315,18 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._executeHandlerFb = callback; } - private _linkHandler(handlers: object[], index: number, newCallback: object): IDisposable { - const newHead: any = newCallback; - newHead.nextHandler = handlers[index] as IHandlerLink; - newHead.dispose = function (): void { - let previous = null; - let cur = handlers[index] as IHandlerLink; - for (; cur && cur.nextHandler; - previous = cur, cur = cur.nextHandler) { - if (cur === newHead) { - if (previous) { previous.nextHandler = cur.nextHandler; } - else { handlers[index] = cur.nextHandler; } - cur.nextHandler = null; - handlers = null; newCallback = null; // just in case - break; - } - } - }; - handlers[index] = newHead; - return newHead; - } - - addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable { + addCsiHandler(flag: string, callback: CsiHandler): IDisposable { const index = flag.charCodeAt(0); - const newHead = - (params: number[], collect: string): void => { - if (! callback(params, collect)) { - const next = (newHead as unknown as IHandlerLink).nextHandler; - if (next) { (next as any)(params, collect); } - else { this._csiHandlerFb(collect, params, index); } - } - }; - return this._linkHandler(this._csiHandlers, index, newHead); + if (this._csiHandlers[index] === undefined) { + this._csiHandlers[index] = []; + } + this._csiHandlers[index].push(callback); + return { + dispose: () => this._csiHandlers[index].splice(this._csiHandlers[index].indexOf(callback)) + }; } - setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { - this._csiHandlers[flag.charCodeAt(0)] = callback; + this._csiHandlers[flag.charCodeAt(0)] = [callback]; } clearCsiHandler(flag: string): void { if (this._csiHandlers[flag.charCodeAt(0)]) delete this._csiHandlers[flag.charCodeAt(0)]; @@ -372,18 +346,16 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable { - const newHead = - (data: string): void => { - if (! callback(data)) { - const next = (newHead as unknown as IHandlerLink).nextHandler; - if (next) { (next as any)(data); } - else { this._oscHandlerFb(ident, data); } - } - }; - return this._linkHandler(this._oscHandlers, ident, newHead); + if (this._oscHandlers[ident] === undefined) { + this._oscHandlers[ident] = []; + } + this._oscHandlers[ident].push(callback); + return { + dispose: () => this._oscHandlers[ident].splice(this._oscHandlers[ident].indexOf(callback)) + }; } setOscHandler(ident: number, callback: (data: string) => void): void { - this._oscHandlers[ident] = callback; + this._oscHandlers[ident] = [callback]; } clearOscHandler(ident: number): void { if (this._oscHandlers[ident]) delete this._oscHandlers[ident]; @@ -520,9 +492,17 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP } break; case ParserAction.CSI_DISPATCH: - callback = this._csiHandlers[code]; - if (callback) callback(params, collect); - else this._csiHandlerFb(collect, params, code); + // Trigger CSI Handler + const handlers = this._csiHandlers[code]; + let j: number; + for (j = handlers.length - 1; j >= 0; j--) { + if (handlers[j](params, collect)) { + break; + } + } + if (j < 0) { + this._csiHandlerFb(collect, params, code); + } break; case ParserAction.PARAM: if (code === 0x3b) params.push(0); @@ -597,9 +577,17 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP // or with an explicit NaN OSC handler const identifier = parseInt(osc.substring(0, idx)); const content = osc.substring(idx + 1); - callback = this._oscHandlers[identifier]; - if (callback) callback(content); - else this._oscHandlerFb(identifier, content); + // Trigger OSC Handler + const handlers = this._oscHandlers[identifier]; + let j: number; + for (j = handlers.length - 1; j >= 0; j--) { + if (handlers[j](content)) { + break; + } + } + if (j < 0) { + this._oscHandlerFb(identifier, content); + } } } if (code === 0x1b) transition |= ParserState.ESCAPE; From 38796a0f748340044f875580870764f80b5535f6 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 14:40:23 -0800 Subject: [PATCH 15/61] Add tests, fix NPE --- src/EscapeSequenceParser.test.ts | 96 ++++++++++++++++++++++++++++++++ src/EscapeSequenceParser.ts | 12 ++-- 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index e92f412574..3b01cb8a17 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -1169,6 +1169,54 @@ describe('EscapeSequenceParser', function (): void { parser2.parse(INPUT); chai.expect(csi).eql([]); }); + describe('CSI custom handlers', () => { + it('Prevent fallback', () => { + const csiCustom: [string, number[], string][] = []; + parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); + parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); + parser2.parse(INPUT); + chai.expect(csi).eql([], 'Should not fallback to original handler'); + chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]); + }); + it('Allow fallback', () => { + const csiCustom: [string, number[], string][] = []; + parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); + parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return false; }); + parser2.parse(INPUT); + chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']], 'Should fallback to original handler'); + chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]); + }); + it('Multiple custom handlers fallback once', () => { + const csiCustom: [string, number[], string][] = []; + const csiCustom2: [string, number[], string][] = []; + parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); + parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); + parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params, collect]); return false; }); + parser2.parse(INPUT); + chai.expect(csi).eql([], 'Should not fallback to original handler'); + chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]); + chai.expect(csiCustom2).eql([['m', [1, 31], ''], ['m', [0], '']]); + }); + it('Multiple custom handlers no fallback', () => { + const csiCustom: [string, number[], string][] = []; + const csiCustom2: [string, number[], string][] = []; + parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); + parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); + parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params, collect]); return true; }); + parser2.parse(INPUT); + chai.expect(csi).eql([], 'Should not fallback to original handler'); + chai.expect(csiCustom).eql([], 'Should not fallback once'); + chai.expect(csiCustom2).eql([['m', [1, 31], ''], ['m', [0], '']]); + }); + it('Execution order should go from latest handler down to the original', () => { + const order: number[] = []; + parser2.setCsiHandler('m', () => order.push(1)); + parser2.addCsiHandler('m', () => { order.push(2); return false; }); + parser2.addCsiHandler('m', () => { order.push(3); return false; }); + parser2.parse('\x1b[0m'); + chai.expect(order).eql([3, 2, 1]); + }); + }); it('EXECUTE handler', function (): void { parser2.setExecuteHandler('\n', function (): void { exe.push('\n'); @@ -1196,6 +1244,54 @@ describe('EscapeSequenceParser', function (): void { parser2.parse(INPUT); chai.expect(osc).eql([]); }); + describe('OSC custom handlers', () => { + it('Prevent fallback', () => { + const oscCustom: [number, string][] = []; + parser2.setOscHandler(1, data => osc.push([1, data])); + parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); + parser2.parse(INPUT); + chai.expect(osc).eql([], 'Should not fallback to original handler'); + chai.expect(oscCustom).eql([[1, 'foo=bar']]); + }); + it('Allow fallback', () => { + const oscCustom: [number, string][] = []; + parser2.setOscHandler(1, data => osc.push([1, data])); + parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return false; }); + parser2.parse(INPUT); + chai.expect(osc).eql([[1, 'foo=bar']], 'Should fallback to original handler'); + chai.expect(oscCustom).eql([[1, 'foo=bar']]); + }); + it('Multiple custom handlers fallback once', () => { + const oscCustom: [number, string][] = []; + const oscCustom2: [number, string][] = []; + parser2.setOscHandler(1, data => osc.push([1, data])); + parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); + parser2.addOscHandler(1, data => { oscCustom2.push([1, data]); return false; }); + parser2.parse(INPUT); + chai.expect(osc).eql([], 'Should not fallback to original handler'); + chai.expect(oscCustom).eql([[1, 'foo=bar']]); + chai.expect(oscCustom2).eql([[1, 'foo=bar']]); + }); + it('Multiple custom handlers no fallback', () => { + const oscCustom: [number, string][] = []; + const oscCustom2: [number, string][] = []; + parser2.setOscHandler(1, data => osc.push([1, data])); + parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); + parser2.addOscHandler(1, data => { oscCustom2.push([1, data]); return true; }); + parser2.parse(INPUT); + chai.expect(osc).eql([], 'Should not fallback to original handler'); + chai.expect(oscCustom).eql([], 'Should not fallback once'); + chai.expect(oscCustom2).eql([[1, 'foo=bar']]); + }); + it('Execution order should go from latest handler down to the original', () => { + const order: number[] = []; + parser2.setOscHandler(1, () => order.push(1)); + parser2.addOscHandler(1, () => { order.push(2); return false; }); + parser2.addOscHandler(1, () => { order.push(3); return false; }); + parser2.parse('\x1b]1;foo=bar\x1b\\'); + chai.expect(order).eql([3, 2, 1]); + }); + }); it('DCS handler', function (): void { parser2.setDcsHandler('+p', { hook: function (collect: string, params: number[], flag: number): void { diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 52007244d6..acb5a42d29 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -7,10 +7,6 @@ import { ParserState, ParserAction, IParsingState, IDcsHandler, IEscapeSequenceP import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; -interface IHandlerLink extends IDisposable { - nextHandler: IHandlerLink | null; -} - interface IHandlerCollection { [key: string]: T[]; } @@ -494,8 +490,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP case ParserAction.CSI_DISPATCH: // Trigger CSI Handler const handlers = this._csiHandlers[code]; - let j: number; - for (j = handlers.length - 1; j >= 0; j--) { + let j = handlers ? handlers.length - 1 : -1; + for (; j >= 0; j--) { if (handlers[j](params, collect)) { break; } @@ -579,8 +575,8 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP const content = osc.substring(idx + 1); // Trigger OSC Handler const handlers = this._oscHandlers[identifier]; - let j: number; - for (j = handlers.length - 1; j >= 0; j--) { + let j = handlers ? handlers.length - 1 : -1; + for (; j >= 0; j--) { if (handlers[j](content)) { break; } From c045c803d60dc569baab8efd53b6722faebd3e81 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 14:48:35 -0800 Subject: [PATCH 16/61] Add tests for dispose --- src/EscapeSequenceParser.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index 3b01cb8a17..9216dd4cc2 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -1216,6 +1216,15 @@ describe('EscapeSequenceParser', function (): void { parser2.parse('\x1b[0m'); chai.expect(order).eql([3, 2, 1]); }); + it('Dispose', () => { + const csiCustom: [string, number[], string][] = []; + parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); + const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); + customHandler.dispose(); + parser2.parse(INPUT); + chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]); + chai.expect(csiCustom).eql([], 'Should not use custom handler as it was disposed'); + }); }); it('EXECUTE handler', function (): void { parser2.setExecuteHandler('\n', function (): void { @@ -1291,6 +1300,15 @@ describe('EscapeSequenceParser', function (): void { parser2.parse('\x1b]1;foo=bar\x1b\\'); chai.expect(order).eql([3, 2, 1]); }); + it('Dispose', () => { + const oscCustom: [number, string][] = []; + parser2.setOscHandler(1, data => osc.push([1, data])); + const customHandler = parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); + customHandler.dispose(); + parser2.parse(INPUT); + chai.expect(osc).eql([[1, 'foo=bar']]); + chai.expect(oscCustom).eql([], 'Should not use custom handler as it was disposed'); + }); }); it('DCS handler', function (): void { parser2.setDcsHandler('+p', { From adbb929f7881d6f678cdde8b8736bfd113565204 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 14:49:49 -0800 Subject: [PATCH 17/61] Wrap .d.ts comments to 80 chars --- typings/xterm.d.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index cf489a610c..0ceab01dcc 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -483,14 +483,13 @@ declare module 'xterm' { /** * (EXPERIMENTAL) Adds a handler for CSI escape sequences. - * @param flag The flag should be one-character string, which specifies - * the final character (e.g "m" for SGR) of the CSI sequence. - * @param callback The function to handle the escape sequence. - * The callback is called with the numerical params, - * as well as the special characters (e.g. "$" for DECSCPP). - * Return true if the sequence was handled; false if we should - * try a previous handler (set by addCsiHandler or setCsiHandler). - * The most recently-added handler is tried first. + * @param flag The flag should be one-character string, which specifies the + * final character (e.g "m" for SGR) of the CSI sequence. + * @param callback The function to handle the escape sequence. The callback + * is called with the numerical params, as well as the special characters + * (e.g. "$" for DECSCPP). Return true if the sequence was handled; false if + * we should try a previous handler (set by addCsiHandler or setCsiHandler). + * The most recently-added handler is tried first. * @return An IDisposable you can call to remove this handler. */ addCsiHandler(flag: string, callback: (params: number[], collect: string) => boolean): IDisposable; @@ -498,11 +497,10 @@ declare module 'xterm' { /** * (EXPERIMENTAL) Adds a handler for OSC escape sequences. * @param ident The number (first parameter) of the sequence. - * @param callback The function to handle the escape sequence. - * The callback is called with OSC data string. - * Return true if the sequence was handled; false if we should - * try a previous handler (set by addOscHandler or setOscHandler). - * The most recently-added handler is tried first. + * @param callback The function to handle the escape sequence. The callback + * is called with OSC data string. Return true if the sequence was handled; + * false if we should try a previous handler (set by addOscHandler or + * setOscHandler). The most recently-added handler is tried first. * @return An IDisposable you can call to remove this handler. */ addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable; From 51e1f49bf36085e46d58ce98c07e115d74c112a8 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 27 Dec 2018 10:26:40 -0800 Subject: [PATCH 18/61] Make dispose more resilient --- src/EscapeSequenceParser.test.ts | 24 ++++++++++++++++++++++-- src/EscapeSequenceParser.ts | 20 ++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index 9216dd4cc2..0f1d63cc0d 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -1216,7 +1216,7 @@ describe('EscapeSequenceParser', function (): void { parser2.parse('\x1b[0m'); chai.expect(order).eql([3, 2, 1]); }); - it('Dispose', () => { + it('Dispose should work', () => { const csiCustom: [string, number[], string][] = []; parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); @@ -1225,6 +1225,16 @@ describe('EscapeSequenceParser', function (): void { chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]); chai.expect(csiCustom).eql([], 'Should not use custom handler as it was disposed'); }); + it('Should not corrupt the parser when dispose is called twice', () => { + const csiCustom: [string, number[], string][] = []; + parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); + const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); + customHandler.dispose(); + customHandler.dispose(); + parser2.parse(INPUT); + chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]); + chai.expect(csiCustom).eql([], 'Should not use custom handler as it was disposed'); + }); }); it('EXECUTE handler', function (): void { parser2.setExecuteHandler('\n', function (): void { @@ -1300,7 +1310,7 @@ describe('EscapeSequenceParser', function (): void { parser2.parse('\x1b]1;foo=bar\x1b\\'); chai.expect(order).eql([3, 2, 1]); }); - it('Dispose', () => { + it('Dispose should work', () => { const oscCustom: [number, string][] = []; parser2.setOscHandler(1, data => osc.push([1, data])); const customHandler = parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); @@ -1309,6 +1319,16 @@ describe('EscapeSequenceParser', function (): void { chai.expect(osc).eql([[1, 'foo=bar']]); chai.expect(oscCustom).eql([], 'Should not use custom handler as it was disposed'); }); + it('Should not corrupt the parser when dispose is called twice', () => { + const oscCustom: [number, string][] = []; + parser2.setOscHandler(1, data => osc.push([1, data])); + const customHandler = parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); + customHandler.dispose(); + customHandler.dispose(); + parser2.parse(INPUT); + chai.expect(osc).eql([[1, 'foo=bar']]); + chai.expect(oscCustom).eql([], 'Should not use custom handler as it was disposed'); + }); }); it('DCS handler', function (): void { parser2.setDcsHandler('+p', { diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index acb5a42d29..70c1c6c8ef 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -316,9 +316,15 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (this._csiHandlers[index] === undefined) { this._csiHandlers[index] = []; } - this._csiHandlers[index].push(callback); + const handlerList = this._csiHandlers[index]; + handlerList.push(callback); return { - dispose: () => this._csiHandlers[index].splice(this._csiHandlers[index].indexOf(callback)) + dispose: () => { + const handlerIndex = handlerList.indexOf(callback); + if (handlerIndex !== -1) { + handlerList.splice(handlerIndex); + } + } }; } setCsiHandler(flag: string, callback: (params: number[], collect: string) => void): void { @@ -345,9 +351,15 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (this._oscHandlers[ident] === undefined) { this._oscHandlers[ident] = []; } - this._oscHandlers[ident].push(callback); + const handlerList = this._oscHandlers[ident]; + handlerList.push(callback); return { - dispose: () => this._oscHandlers[ident].splice(this._oscHandlers[ident].indexOf(callback)) + dispose: () => { + const handlerIndex = handlerList.indexOf(callback); + if (handlerIndex !== -1) { + handlerList.splice(handlerIndex); + } + } }; } setOscHandler(ident: number, callback: (data: string) => void): void { From 8a50729a98b3c9ea4daf249e35d96ab709e6b859 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 27 Dec 2018 21:58:54 -0800 Subject: [PATCH 19/61] Reflow wider --- src/Buffer.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++++ src/BufferLine.ts | 10 ++++++ 2 files changed, 87 insertions(+) diff --git a/src/Buffer.ts b/src/Buffer.ts index 74750a8b4e..6b247afee0 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -233,6 +233,83 @@ export class Buffer implements IBuffer { } this.scrollBottom = newRows - 1; + + if (this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { + this._reflow(newCols, newRows); + } + } + + private _reflow(newCols: number, newRows: number): void { + if (this._terminal.cols === newCols) { + return; + } + + // Iterate through rows, ignore the last one as it cannot be wrapped + for (let y = 0; y < this.lines.length - 1; y++) { + // Check if this row is wrapped + let i = y; + let nextLine = this.lines.get(++i) as BufferLine; + if (!nextLine.isWrapped) { + continue; + } + + // Check how many lines it's wrapped for + const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; + while (nextLine.isWrapped) { + wrappedLines.push(nextLine); + nextLine = this.lines.get(++i) as BufferLine; + } + + if (newCols > this._terminal.cols) { + let destLineIndex = 0; + let destCol = this._terminal.cols; + let srcLineIndex = 1; + let srcCol = 0; + while (srcLineIndex < wrappedLines.length) { + const srcRemainingCells = this._terminal.cols - srcCol; + const destRemainingCells = newCols - destCol; + const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy); + destCol += cellsToCopy; + if (destCol === newCols) { + destLineIndex++; + destCol = 0; + } + srcCol += cellsToCopy; + if (srcCol === this._terminal.cols) { + srcLineIndex++; + srcCol = 0; + } + } + + // Work backwards and remove any rows at the end that only contain null cells + let countToRemove = 0; + for (let i = wrappedLines.length - 1; i > 0; i--) { + if (wrappedLines[i].getTrimmedLength() === 0) { + countToRemove++; + } else { + break; + } + } + + // Remove rows and adjust cursor + if (countToRemove > 0) { + this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); + while (countToRemove-- > 0) { + if (this.ybase === 0) { + this.y--; + } else { + if (this.ydisp === this.ybase) { + this.ydisp--; + } + this.ybase--; + } + } + } + } else { + + } + } } /** diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 3f93af626a..aafc9051c4 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -304,6 +304,16 @@ export class BufferLine implements IBufferLine { return 0; } + public copyCellsFrom(src: BufferLine, srcCol: number, destCol: number, length: number): void { + console.log(' copyCellsFrom', srcCol, destCol, length); + const srcData = src._data; + for (let cell = 0; cell < length; cell++) { + for (let i = 0; i < CELL_SIZE; i++) { + this._data[(destCol + cell) * CELL_SIZE + i] = srcData[(srcCol + cell) * CELL_SIZE + i]; + } + } + } + public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = this.length): string { if (trimRight) { endCol = Math.min(endCol, this.getTrimmedLength()); From 314f98f2a63cd100f1084df46aab0bdbe5c82624 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 27 Dec 2018 23:35:52 -0800 Subject: [PATCH 20/61] Mostly working for reflowing to smaller --- src/Buffer.ts | 215 ++++++++++++++++++++++++++++++++++------------ src/BufferLine.ts | 17 ++-- 2 files changed, 171 insertions(+), 61 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 6b247afee0..fc8e008e60 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -235,81 +235,185 @@ export class Buffer implements IBuffer { this.scrollBottom = newRows - 1; if (this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { - this._reflow(newCols, newRows); + this._reflow(newCols); } } - private _reflow(newCols: number, newRows: number): void { + private _reflow(newCols: number): void { if (this._terminal.cols === newCols) { return; } // Iterate through rows, ignore the last one as it cannot be wrapped for (let y = 0; y < this.lines.length - 1; y++) { - // Check if this row is wrapped - let i = y; - let nextLine = this.lines.get(++i) as BufferLine; - if (!nextLine.isWrapped) { - continue; + if (newCols > this._terminal.cols) { + y += this._reflowLarger(y, newCols); + } else { + y += this._reflowSmaller(y, newCols); } + } + } + + private _reflowLarger(y: number, newCols: number): number { + // Check if this row is wrapped + let i = y; + let nextLine = this.lines.get(++i) as BufferLine; + if (!nextLine.isWrapped) { + return 0; + } - // Check how many lines it's wrapped for - const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; - while (nextLine.isWrapped) { - wrappedLines.push(nextLine); - nextLine = this.lines.get(++i) as BufferLine; + // Check how many lines it's wrapped for + const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; + while (nextLine.isWrapped) { + wrappedLines.push(nextLine); + nextLine = this.lines.get(++i) as BufferLine; + } + + // Copy buffer data to new locations + let destLineIndex = 0; + let destCol = this._terminal.cols; + let srcLineIndex = 1; + let srcCol = 0; + while (srcLineIndex < wrappedLines.length) { + const srcRemainingCells = this._terminal.cols - srcCol; + const destRemainingCells = newCols - destCol; + const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); + destCol += cellsToCopy; + if (destCol === newCols) { + destLineIndex++; + destCol = 0; + } + srcCol += cellsToCopy; + if (srcCol === this._terminal.cols) { + srcLineIndex++; + srcCol = 0; } + } - if (newCols > this._terminal.cols) { - let destLineIndex = 0; - let destCol = this._terminal.cols; - let srcLineIndex = 1; - let srcCol = 0; - while (srcLineIndex < wrappedLines.length) { - const srcRemainingCells = this._terminal.cols - srcCol; - const destRemainingCells = newCols - destCol; - const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy); - destCol += cellsToCopy; - if (destCol === newCols) { - destLineIndex++; - destCol = 0; - } - srcCol += cellsToCopy; - if (srcCol === this._terminal.cols) { - srcLineIndex++; - srcCol = 0; - } - } + // Clear out remaining cells or fragments could remain + // TODO: @jerch can this be a const? + const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; + wrappedLines[destLineIndex].replaceCells(destCol, newCols, fillCharData); + + // Work backwards and remove any rows at the end that only contain null cells + let countToRemove = 0; + for (let i = wrappedLines.length - 1; i > 0; i--) { + if (wrappedLines[i].getTrimmedLength() === 0) { + countToRemove++; + } else { + break; + } + } - // Work backwards and remove any rows at the end that only contain null cells - let countToRemove = 0; - for (let i = wrappedLines.length - 1; i > 0; i--) { - if (wrappedLines[i].getTrimmedLength() === 0) { - countToRemove++; - } else { - break; + // Remove rows and adjust cursor + if (countToRemove > 0) { + this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); + let removing = countToRemove; + while (removing-- > 0) { + if (this.ybase === 0) { + this.y--; + // Add an extra row at the bottom of the viewport + this.lines.push(new this._bufferLineConstructor(newCols, fillCharData)); + } else { + if (this.ydisp === this.ybase) { + this.ydisp--; } + this.ybase--; } + } + } + // TODO: Handle list trimming - // Remove rows and adjust cursor - if (countToRemove > 0) { - this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); - while (countToRemove-- > 0) { - if (this.ybase === 0) { - this.y--; - } else { - if (this.ydisp === this.ybase) { - this.ydisp--; - } - this.ybase--; - } - } + return wrappedLines.length - countToRemove - 1; + } + + private _reflowSmaller(y: number, newCols: number): number { + // Check whether this line is a problem + const line = this.lines.get(y) as BufferLine; + if (line.getTrimmedLength() <= newCols) { + return 0; + } + + // TODO: How is the cursor x handled if it's wrapped? Do something special when the cursor is this line? + + + // Gather wrapped lines if it's wrapped + let lineIndex = y; + let nextLine = this.lines.get(++lineIndex) as BufferLine; + const wrappedLines: BufferLine[] = [line]; + while (nextLine.isWrapped) { + wrappedLines.push(nextLine); + nextLine = this.lines.get(++lineIndex) as BufferLine; + } + + // Determine how many lines need to be inserted at the end, based on the trimmed length of + // the last wrapped line + if (wrappedLines[wrappedLines.length - 1].getTrimmedLength() === undefined) { + debugger; + } + const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); + const cellsNeeded = (wrappedLines.length - 1) * this._terminal.cols + lastLineLength; + const linesNeeded = Math.ceil(cellsNeeded / newCols); + const linesToAdd = linesNeeded - wrappedLines.length; + + // Add the new lines + const newLines: BufferLine[] = []; + for (let i = 0; i < linesToAdd; i++) { + // TODO: Remove any! + const newLine = this.getBlankLine((this._terminal as any).eraseAttr(), true) as BufferLine; + newLines.push(newLine); + } + this.lines.splice(y + wrappedLines.length, 0, ...newLines); + wrappedLines.push(...newLines); + + // Copy buffer data to new locations, this needs to happen backwards to do in-place + let destLineIndex = Math.floor(cellsNeeded / newCols); + let destCol = cellsNeeded % newCols; + if (destCol === 0) { + destLineIndex--; + destCol = newCols; + } + let srcLineIndex = wrappedLines.length - linesToAdd - 1; + let srcCol = lastLineLength; + while (srcLineIndex >= 0) { // Don't need to copy any from the first line + const cellsToCopy = Math.min(srcCol, destCol); + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true); + destCol -= cellsToCopy; + if (destCol === 0) { + destLineIndex--; + destCol = newCols; + } + srcCol -= cellsToCopy; + if (srcCol === 0) { + srcLineIndex--; + srcCol = this._terminal.cols; + } + } + + // Adjust viewport as needed + let viewportAdjustments = linesToAdd; + while (viewportAdjustments-- > 0) { + if (this.ybase === 0) { + if (this.y < this._terminal.rows) { + this.y++; + this.lines.pop(); + } else { + this.ybase++; + this.ydisp++; } } else { - + if (this.ybase === this.ydisp) { + this.ybase++; + this.ydisp++; + } } } + + // TODO: Adjust viewport if needed (remove rows on end if ybase === 0? etc. + // TODO: Handle list trimming + + return wrappedLines.length - 1; } /** @@ -339,10 +443,9 @@ export class Buffer implements IBuffer { } lineIndex++; } - return [lineIndex, 0]; } - /** + /** // TODO: Handle list trimming * Translates a buffer line to a string, with optional start and end columns. * Wide characters will count as two columns in the resulting string. This * function is useful for getting the actual text underneath the raw selection diff --git a/src/BufferLine.ts b/src/BufferLine.ts index aafc9051c4..ea44de8770 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -304,12 +304,19 @@ export class BufferLine implements IBufferLine { return 0; } - public copyCellsFrom(src: BufferLine, srcCol: number, destCol: number, length: number): void { - console.log(' copyCellsFrom', srcCol, destCol, length); + public copyCellsFrom(src: BufferLine, srcCol: number, destCol: number, length: number, applyInReverse: boolean): void { const srcData = src._data; - for (let cell = 0; cell < length; cell++) { - for (let i = 0; i < CELL_SIZE; i++) { - this._data[(destCol + cell) * CELL_SIZE + i] = srcData[(srcCol + cell) * CELL_SIZE + i]; + if (applyInReverse) { + for (let cell = length - 1; cell >= 0; cell--) { + for (let i = 0; i < CELL_SIZE; i++) { + this._data[(destCol + cell) * CELL_SIZE + i] = srcData[(srcCol + cell) * CELL_SIZE + i]; + } + } + } else { + for (let cell = 0; cell < length; cell++) { + for (let i = 0; i < CELL_SIZE; i++) { + this._data[(destCol + cell) * CELL_SIZE + i] = srcData[(srcCol + cell) * CELL_SIZE + i]; + } } } } From fa47036982cb8aecfb10d9c86427198b5fc91982 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 27 Dec 2018 23:49:59 -0800 Subject: [PATCH 21/61] Fix row removal in reflowLarger --- src/Buffer.ts | 13 ++----------- src/BufferLine.ts | 6 +++--- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index fc8e008e60..961032d9d9 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -292,21 +292,12 @@ export class Buffer implements IBuffer { } // Clear out remaining cells or fragments could remain - // TODO: @jerch can this be a const? + // TODO: @jerch can fillCharData be a const? const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; wrappedLines[destLineIndex].replaceCells(destCol, newCols, fillCharData); - // Work backwards and remove any rows at the end that only contain null cells - let countToRemove = 0; - for (let i = wrappedLines.length - 1; i > 0; i--) { - if (wrappedLines[i].getTrimmedLength() === 0) { - countToRemove++; - } else { - break; - } - } - // Remove rows and adjust cursor + const countToRemove = wrappedLines.length - destLineIndex - 1; if (countToRemove > 0) { this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); let removing = countToRemove; diff --git a/src/BufferLine.ts b/src/BufferLine.ts index ea44de8770..fd28af846a 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -228,8 +228,8 @@ export class BufferLine implements IBufferLine { } } - public resize(cols: number, fillCharData: CharData, shrink: boolean = false): void { - if (cols === this.length || (!shrink && cols < this.length)) { + public resize(cols: number, fillCharData: CharData): void { + if (cols === this.length) { return; } if (cols > this.length) { @@ -245,7 +245,7 @@ export class BufferLine implements IBufferLine { for (let i = this.length; i < cols; ++i) { this.set(i, fillCharData); } - } else if (shrink) { + } else { if (cols) { const data = new Uint32Array(cols * CELL_SIZE); data.set(this._data.subarray(0, cols * CELL_SIZE)); From 8bc04c2fa73206f2e4ba3404297ebe187578d96c Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 27 Dec 2018 23:58:57 -0800 Subject: [PATCH 22/61] Tidy up --- src/Buffer.ts | 14 +++----------- src/Types.ts | 1 + 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 961032d9d9..5c1d8e458e 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -300,8 +300,8 @@ export class Buffer implements IBuffer { const countToRemove = wrappedLines.length - destLineIndex - 1; if (countToRemove > 0) { this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); - let removing = countToRemove; - while (removing-- > 0) { + let viewportAdjustments = countToRemove; + while (viewportAdjustments-- > 0) { if (this.ybase === 0) { this.y--; // Add an extra row at the bottom of the viewport @@ -314,7 +314,6 @@ export class Buffer implements IBuffer { } } } - // TODO: Handle list trimming return wrappedLines.length - countToRemove - 1; } @@ -326,9 +325,6 @@ export class Buffer implements IBuffer { return 0; } - // TODO: How is the cursor x handled if it's wrapped? Do something special when the cursor is this line? - - // Gather wrapped lines if it's wrapped let lineIndex = y; let nextLine = this.lines.get(++lineIndex) as BufferLine; @@ -340,9 +336,6 @@ export class Buffer implements IBuffer { // Determine how many lines need to be inserted at the end, based on the trimmed length of // the last wrapped line - if (wrappedLines[wrappedLines.length - 1].getTrimmedLength() === undefined) { - debugger; - } const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); const cellsNeeded = (wrappedLines.length - 1) * this._terminal.cols + lastLineLength; const linesNeeded = Math.ceil(cellsNeeded / newCols); @@ -351,8 +344,7 @@ export class Buffer implements IBuffer { // Add the new lines const newLines: BufferLine[] = []; for (let i = 0; i < linesToAdd; i++) { - // TODO: Remove any! - const newLine = this.getBlankLine((this._terminal as any).eraseAttr(), true) as BufferLine; + const newLine = this.getBlankLine(this._terminal.eraseAttr(), true) as BufferLine; newLines.push(newLine); } this.lines.splice(y + wrappedLines.length, 0, ...newLines); diff --git a/src/Types.ts b/src/Types.ts index 8ebb28d3e3..b1fea90357 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -230,6 +230,7 @@ export interface ITerminal extends PublicTerminal, IElementAccessor, IBufferAcce cancel(ev: Event, force?: boolean): boolean | void; log(text: string): void; showCursor(): void; + eraseAttr(): number; } export interface IBufferAccessor { From 3311ed509aaca3372062b1b59457d487b6b59bd4 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 00:40:30 -0800 Subject: [PATCH 23/61] Do shrink in reverse, fix up row remove count again --- src/Buffer.ts | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 5c1d8e458e..dcb0504908 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -245,11 +245,14 @@ export class Buffer implements IBuffer { } // Iterate through rows, ignore the last one as it cannot be wrapped - for (let y = 0; y < this.lines.length - 1; y++) { - if (newCols > this._terminal.cols) { + if (newCols > this._terminal.cols) { + for (let y = 0; y < this.lines.length - 1; y++) { y += this._reflowLarger(y, newCols); - } else { - y += this._reflowSmaller(y, newCols); + } + } else { + // Go backwards as many lines may be trimmed and this will avoid considering them + for (let y = this.lines.length - 1; y >= 0; y--) { + y -= this._reflowSmaller(y, newCols); } } } @@ -296,8 +299,16 @@ export class Buffer implements IBuffer { const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; wrappedLines[destLineIndex].replaceCells(destCol, newCols, fillCharData); - // Remove rows and adjust cursor - const countToRemove = wrappedLines.length - destLineIndex - 1; + // Work backwards and remove any rows at the end that only contain null cells + let countToRemove = 0; + for (let i = wrappedLines.length - 1; i > 0; i--) { + if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) { + countToRemove++; + } else { + break; + } + } + if (countToRemove > 0) { this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); let viewportAdjustments = countToRemove; @@ -320,18 +331,22 @@ export class Buffer implements IBuffer { private _reflowSmaller(y: number, newCols: number): number { // Check whether this line is a problem - const line = this.lines.get(y) as BufferLine; - if (line.getTrimmedLength() <= newCols) { + let nextLine = this.lines.get(y) as BufferLine; + if (!nextLine.isWrapped && nextLine.getTrimmedLength() <= newCols) { return 0; } - // Gather wrapped lines if it's wrapped - let lineIndex = y; - let nextLine = this.lines.get(++lineIndex) as BufferLine; - const wrappedLines: BufferLine[] = [line]; - while (nextLine.isWrapped) { - wrappedLines.push(nextLine); - nextLine = this.lines.get(++lineIndex) as BufferLine; + // Gather wrapped lines and adjust y to be the starting line + const wrappedLines: BufferLine[] = [nextLine]; + if (nextLine.isWrapped) { + while (true) { + nextLine = this.lines.get(--y) as BufferLine; + // TODO: unshift is expensive + wrappedLines.unshift(nextLine); + if (!nextLine.isWrapped || y === 0) { + break; + } + } } // Determine how many lines need to be inserted at the end, based on the trimmed length of @@ -396,7 +411,7 @@ export class Buffer implements IBuffer { // TODO: Adjust viewport if needed (remove rows on end if ybase === 0? etc. // TODO: Handle list trimming - return wrappedLines.length - 1; + return wrappedLines.length - 1 - linesToAdd; } /** From ae29dbb133dc18348a70ab7af81a3f099d886782 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 01:26:49 -0800 Subject: [PATCH 24/61] Fix scrollbar when wrapping beyond single viewport of data --- src/Buffer.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index dcb0504908..ef91055d66 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -355,6 +355,9 @@ export class Buffer implements IBuffer { const cellsNeeded = (wrappedLines.length - 1) * this._terminal.cols + lastLineLength; const linesNeeded = Math.ceil(cellsNeeded / newCols); const linesToAdd = linesNeeded - wrappedLines.length; + const trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); + console.log('linesToAdd', linesToAdd); + console.log('trimmedLines', trimmedLines); // Add the new lines const newLines: BufferLine[] = []; @@ -374,7 +377,7 @@ export class Buffer implements IBuffer { } let srcLineIndex = wrappedLines.length - linesToAdd - 1; let srcCol = lastLineLength; - while (srcLineIndex >= 0) { // Don't need to copy any from the first line + while (srcLineIndex >= 0) { const cellsToCopy = Math.min(srcCol, destCol); wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true); destCol -= cellsToCopy; @@ -393,11 +396,12 @@ export class Buffer implements IBuffer { let viewportAdjustments = linesToAdd; while (viewportAdjustments-- > 0) { if (this.ybase === 0) { - if (this.y < this._terminal.rows) { + if (this.y < this._terminal.rows - 1) { this.y++; this.lines.pop(); } else { this.ybase++; + // TODO: Use this? if (this._terminal._userScrolling) { this.ydisp++; } } else { @@ -411,7 +415,7 @@ export class Buffer implements IBuffer { // TODO: Adjust viewport if needed (remove rows on end if ybase === 0? etc. // TODO: Handle list trimming - return wrappedLines.length - 1 - linesToAdd; + return wrappedLines.length - 1 - linesToAdd + trimmedLines; } /** From 7684f93773fbde3bd9c16e9d56230c17ab312fc0 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 01:32:19 -0800 Subject: [PATCH 25/61] Fix ydisp/ybase after trimming buffer --- src/Buffer.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index ef91055d66..acba0c3297 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -356,8 +356,6 @@ export class Buffer implements IBuffer { const linesNeeded = Math.ceil(cellsNeeded / newCols); const linesToAdd = linesNeeded - wrappedLines.length; const trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); - console.log('linesToAdd', linesToAdd); - console.log('trimmedLines', trimmedLines); // Add the new lines const newLines: BufferLine[] = []; @@ -393,7 +391,7 @@ export class Buffer implements IBuffer { } // Adjust viewport as needed - let viewportAdjustments = linesToAdd; + let viewportAdjustments = linesToAdd - trimmedLines; while (viewportAdjustments-- > 0) { if (this.ybase === 0) { if (this.y < this._terminal.rows - 1) { @@ -412,9 +410,6 @@ export class Buffer implements IBuffer { } } - // TODO: Adjust viewport if needed (remove rows on end if ybase === 0? etc. - // TODO: Handle list trimming - return wrappedLines.length - 1 - linesToAdd + trimmedLines; } From 358898daf9ca63e3da8ff649bad00c2b6a9129fa Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 11:02:08 -0800 Subject: [PATCH 26/61] Fix some tests --- src/Buffer.ts | 3 ++- src/BufferLine.test.ts | 51 +++------------------------------------- src/ui/TestUtils.test.ts | 3 +++ 3 files changed, 8 insertions(+), 49 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index acba0c3297..143e2b2bf1 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -440,9 +440,10 @@ export class Buffer implements IBuffer { } lineIndex++; } + return [lineIndex, 0]; } - /** // TODO: Handle list trimming + /** * Translates a buffer line to a string, with optional start and end columns. * Wide characters will count as two columns in the resulting string. This * function is useful for getting the actual text underneath the raw selection diff --git a/src/BufferLine.test.ts b/src/BufferLine.test.ts index fbf8b0517b..c652ff4e77 100644 --- a/src/BufferLine.test.ts +++ b/src/BufferLine.test.ts @@ -141,64 +141,19 @@ describe('BufferLine', function(): void { }); it('enlarge(true)', function(): void { const line = new TestBufferLine(5, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], true); + line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)]); chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); }); it('shrink(true) - should apply new size', function(): void { const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], true); + line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)]); chai.expect(line.toArray()).eql(Array(5).fill([1, 'a', 0, 'a'.charCodeAt(0)])); }); - it('shrink(false) - should not apply new size', function(): void { - const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false); - chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); - it('shrink(false) + shrink(false) - should not apply new size', function(): void { - const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false); - chai.expect(line.toArray()).eql(Array(20).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); - it('shrink(false) + enlarge(false) to smaller than before', function(): void { - const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(15, [1, 'a', 0, 'a'.charCodeAt(0)]); - chai.expect(line.toArray()).eql(Array(20).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); - it('shrink(false) + enlarge(false) to bigger than before', function(): void { - const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(25, [1, 'a', 0, 'a'.charCodeAt(0)]); - chai.expect(line.toArray()).eql(Array(25).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); - it('shrink(false) + resize shrink=true should enforce shrinking', function(): void { - const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], true); - chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); - it('enlarge from 0 length', function(): void { - const line = new TestBufferLine(0, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); it('shrink to 0 length', function(): void { const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)], true); + line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)]); chai.expect(line.toArray()).eql(Array(0).fill([1, 'a', 0, 'a'.charCodeAt(0)])); }); - it('shrink(false) to 0 and enlarge to different sizes', function(): void { - const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); - line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)], false); - chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false); - chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - line.resize(7, [1, 'a', 0, 'a'.charCodeAt(0)], false); - chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - line.resize(7, [1, 'a', 0, 'a'.charCodeAt(0)], true); - chai.expect(line.toArray()).eql(Array(7).fill([1, 'a', 0, 'a'.charCodeAt(0)])); - }); }); describe('getTrimLength', function(): void { it('empty line', function(): void { diff --git a/src/ui/TestUtils.test.ts b/src/ui/TestUtils.test.ts index 10033a3355..3b59ee5d69 100644 --- a/src/ui/TestUtils.test.ts +++ b/src/ui/TestUtils.test.ts @@ -19,6 +19,9 @@ export class TestTerminal extends Terminal { } export class MockTerminal implements ITerminal { + eraseAttr(): number { + throw new Error('Method not implemented.'); + } markers: IMarker[]; addMarker(cursorYOffset: number): IMarker { throw new Error('Method not implemented.'); From a33e8a6af79b9f2aa9838f300b5e8366ddd010ba Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 12:57:10 -0800 Subject: [PATCH 27/61] Properly shrink rows to cols every time --- src/Buffer.ts | 8 +++++++- src/Types.ts | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 143e2b2bf1..41a76ccc48 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -162,7 +162,7 @@ export class Buffer implements IBuffer { // The following adjustments should only happen if the buffer has been // initialized/filled. if (this.lines.length > 0) { - // Deal with columns increasing (we don't do anything when columns reduce) + // Deal with columns increasing (reducing needs to happen after reflow) if (this._terminal.cols < newCols) { const ch: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; // does xterm use the default attr? for (let i = 0; i < this.lines.length; i++) { @@ -236,6 +236,12 @@ export class Buffer implements IBuffer { if (this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { this._reflow(newCols); + + if (this._terminal.cols > newCols) { + for (let i = 0; i < this.lines.length; i++) { + this.lines.get(i).resize(newCols, null); + } + } } } diff --git a/src/Types.ts b/src/Types.ts index b1fea90357..d7715f8199 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -521,7 +521,7 @@ export interface IBufferLine { insertCells(pos: number, n: number, ch: CharData): void; deleteCells(pos: number, n: number, fill: CharData): void; replaceCells(start: number, end: number, fill: CharData): void; - resize(cols: number, fill: CharData, shrink?: boolean): void; + resize(cols: number, fill: CharData): void; fill(fillCharData: CharData): void; copyFrom(line: IBufferLine): void; clone(): IBufferLine; From 2a0da173f200b006f292b0592d2d47334e78bbc9 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 14:55:15 -0800 Subject: [PATCH 28/61] Add a bunch of reflow tests --- src/Buffer.test.ts | 115 +++++++++++++++++++++++++++++++++++++++++++++ src/Buffer.ts | 18 +++++-- 2 files changed, 128 insertions(+), 5 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 0546dfe809..189b3ad674 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -233,6 +233,121 @@ describe('Buffer', () => { } }); }); + + describe('reflow', () => { + beforeEach(() => { + terminal.eraseAttr = () => DEFAULT_ATTR; + // Needed until the setting is removed + terminal.options.experimentalBufferLineImpl = 'TypedArray'; + }); + it('should not wrap empty lines', () => { + buffer.fillViewportRows(); + assert.equal(buffer.lines.length, INIT_ROWS); + buffer.resize(INIT_COLS - 5, INIT_ROWS); + assert.equal(buffer.lines.length, INIT_ROWS); + }); + it('should shrink row length', () => { + buffer.fillViewportRows(); + buffer.resize(5, 10); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).length, 5); + assert.equal(buffer.lines.get(1).length, 5); + assert.equal(buffer.lines.get(2).length, 5); + assert.equal(buffer.lines.get(3).length, 5); + assert.equal(buffer.lines.get(4).length, 5); + assert.equal(buffer.lines.get(5).length, 5); + assert.equal(buffer.lines.get(6).length, 5); + assert.equal(buffer.lines.get(7).length, 5); + assert.equal(buffer.lines.get(8).length, 5); + assert.equal(buffer.lines.get(9).length, 5); + }); + it('should wrap and unwrap lines', () => { + buffer.fillViewportRows(); + buffer.resize(5, 10); + terminal.cols = 5; + const firstLine = buffer.lines.get(0); + for (let i = 0; i < 5; i++) { + const code = 'a'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + firstLine.set(i, [null, char, 1, code]); + } + assert.equal(buffer.lines.get(0).length, 5); + assert.equal(buffer.lines.get(0).translateToString(), 'abcde'); + buffer.resize(1, 10); + terminal.cols = 1; + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'a'); + assert.equal(buffer.lines.get(1).translateToString(), 'b'); + assert.equal(buffer.lines.get(2).translateToString(), 'c'); + assert.equal(buffer.lines.get(3).translateToString(), 'd'); + assert.equal(buffer.lines.get(4).translateToString(), 'e'); + assert.equal(buffer.lines.get(5).translateToString(), ' '); + assert.equal(buffer.lines.get(6).translateToString(), ' '); + assert.equal(buffer.lines.get(7).translateToString(), ' '); + assert.equal(buffer.lines.get(8).translateToString(), ' '); + assert.equal(buffer.lines.get(9).translateToString(), ' '); + buffer.resize(5, 10); + terminal.cols = 5; + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcde'); + assert.equal(buffer.lines.get(1).translateToString(), ' '); + assert.equal(buffer.lines.get(2).translateToString(), ' '); + assert.equal(buffer.lines.get(3).translateToString(), ' '); + assert.equal(buffer.lines.get(4).translateToString(), ' '); + assert.equal(buffer.lines.get(5).translateToString(), ' '); + assert.equal(buffer.lines.get(6).translateToString(), ' '); + assert.equal(buffer.lines.get(7).translateToString(), ' '); + assert.equal(buffer.lines.get(8).translateToString(), ' '); + assert.equal(buffer.lines.get(9).translateToString(), ' '); + }); + it('should discard parts of wrapped lines that go out of the scrollback', () => { + buffer.fillViewportRows(); + terminal.options.scrollback = 1; + buffer.resize(10, 5); + terminal.cols = 10; + terminal.rows = 5; + const lastLine = buffer.lines.get(4); + for (let i = 0; i < 10; i++) { + const code = 'a'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + lastLine.set(i, [null, char, 1, code]); + } + assert.equal(buffer.lines.length, 5); + buffer.y = 4; + buffer.resize(2, 5); + terminal.cols = 2; + assert.equal(buffer.y, 4); + assert.equal(buffer.ybase, 1); + assert.equal(buffer.lines.length, 6); + assert.equal(buffer.lines.get(0).translateToString(), ' '); + assert.equal(buffer.lines.get(1).translateToString(), 'ab'); + assert.equal(buffer.lines.get(2).translateToString(), 'cd'); + assert.equal(buffer.lines.get(3).translateToString(), 'ef'); + assert.equal(buffer.lines.get(4).translateToString(), 'gh'); + assert.equal(buffer.lines.get(5).translateToString(), 'ij'); + buffer.resize(1, 5); + terminal.cols = 1; + assert.equal(buffer.y, 4); + assert.equal(buffer.ybase, 1); + assert.equal(buffer.lines.length, 6); + assert.equal(buffer.lines.get(0).translateToString(), 'e'); + assert.equal(buffer.lines.get(1).translateToString(), 'f'); + assert.equal(buffer.lines.get(2).translateToString(), 'g'); + assert.equal(buffer.lines.get(3).translateToString(), 'h'); + assert.equal(buffer.lines.get(4).translateToString(), 'i'); + assert.equal(buffer.lines.get(5).translateToString(), 'j'); + buffer.resize(10, 5); + terminal.cols = 10; + assert.equal(buffer.y, 0); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 5); + assert.equal(buffer.lines.get(0).translateToString(), 'efghij '); + assert.equal(buffer.lines.get(1).translateToString(), ' '); + assert.equal(buffer.lines.get(2).translateToString(), ' '); + assert.equal(buffer.lines.get(3).translateToString(), ' '); + assert.equal(buffer.lines.get(4).translateToString(), ' '); + }); + }); }); describe('buffer marked to have no scrollback', () => { diff --git a/src/Buffer.ts b/src/Buffer.ts index 41a76ccc48..5f6791d8f2 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -164,9 +164,9 @@ export class Buffer implements IBuffer { if (this.lines.length > 0) { // Deal with columns increasing (reducing needs to happen after reflow) if (this._terminal.cols < newCols) { - const ch: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; // does xterm use the default attr? + const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; for (let i = 0; i < this.lines.length; i++) { - this.lines.get(i).resize(newCols, ch); + this.lines.get(i).resize(newCols, fillCharData); } } @@ -237,9 +237,11 @@ export class Buffer implements IBuffer { if (this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { this._reflow(newCols); + // Trim the end of the line off if cols shrunk if (this._terminal.cols > newCols) { + const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; for (let i = 0; i < this.lines.length; i++) { - this.lines.get(i).resize(newCols, null); + this.lines.get(i).resize(newCols, fillCharData); } } } @@ -273,7 +275,7 @@ export class Buffer implements IBuffer { // Check how many lines it's wrapped for const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; - while (nextLine.isWrapped) { + while (nextLine.isWrapped && i < this.lines.length) { wrappedLines.push(nextLine); nextLine = this.lines.get(++i) as BufferLine; } @@ -361,7 +363,13 @@ export class Buffer implements IBuffer { const cellsNeeded = (wrappedLines.length - 1) * this._terminal.cols + lastLineLength; const linesNeeded = Math.ceil(cellsNeeded / newCols); const linesToAdd = linesNeeded - wrappedLines.length; - const trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); + let trimmedLines: number; + if (this.ybase === 0 && this.y !== this.lines.length - 1) { + // If the top section of the buffer is not yet filled + trimmedLines = Math.max(0, this.y - this.lines.maxLength + linesToAdd); + } else { + trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); + } // Add the new lines const newLines: BufferLine[] = []; From 72369e060e602152709956dc6deae6aa2295d606 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 14:55:58 -0800 Subject: [PATCH 29/61] Only enable reflow on the normal buffer --- src/Buffer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 5f6791d8f2..0b660321df 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -234,7 +234,7 @@ export class Buffer implements IBuffer { this.scrollBottom = newRows - 1; - if (this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { + if (this.hasScrollback && this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { this._reflow(newCols); // Trim the end of the line off if cols shrunk From 4a9f10d062c2f92b08dbbc862e7a5279ed5f2a3e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 19:22:05 -0800 Subject: [PATCH 30/61] Remove some of Buffer's dependency on Terminal --- src/Buffer.test.ts | 9 ++------- src/Buffer.ts | 4 ++-- src/Types.ts | 1 - 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 189b3ad674..cee0fbad43 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -108,12 +108,12 @@ describe('Buffer', () => { describe('resize', () => { describe('column size is reduced', () => { - it('should not trim the data in the buffer', () => { + it('should trim the data in the buffer', () => { buffer.fillViewportRows(); buffer.resize(INIT_COLS / 2, INIT_ROWS); assert.equal(buffer.lines.length, INIT_ROWS); for (let i = 0; i < INIT_ROWS; i++) { - assert.equal(buffer.lines.get(i).length, INIT_COLS); + assert.equal(buffer.lines.get(i).length, INIT_COLS / 2); } }); }); @@ -235,11 +235,6 @@ describe('Buffer', () => { }); describe('reflow', () => { - beforeEach(() => { - terminal.eraseAttr = () => DEFAULT_ATTR; - // Needed until the setting is removed - terminal.options.experimentalBufferLineImpl = 'TypedArray'; - }); it('should not wrap empty lines', () => { buffer.fillViewportRows(); assert.equal(buffer.lines.length, INIT_ROWS); diff --git a/src/Buffer.ts b/src/Buffer.ts index 0b660321df..feb3a1ee38 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -234,7 +234,7 @@ export class Buffer implements IBuffer { this.scrollBottom = newRows - 1; - if (this.hasScrollback && this._terminal.options.experimentalBufferLineImpl === 'TypedArray') { + if (this.hasScrollback && this._bufferLineConstructor === BufferLine) { this._reflow(newCols); // Trim the end of the line off if cols shrunk @@ -374,7 +374,7 @@ export class Buffer implements IBuffer { // Add the new lines const newLines: BufferLine[] = []; for (let i = 0; i < linesToAdd; i++) { - const newLine = this.getBlankLine(this._terminal.eraseAttr(), true) as BufferLine; + const newLine = this.getBlankLine(DEFAULT_ATTR, true) as BufferLine; newLines.push(newLine); } this.lines.splice(y + wrappedLines.length, 0, ...newLines); diff --git a/src/Types.ts b/src/Types.ts index d7715f8199..d48362b8ef 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -230,7 +230,6 @@ export interface ITerminal extends PublicTerminal, IElementAccessor, IBufferAcce cancel(ev: Event, force?: boolean): boolean | void; log(text: string): void; showCursor(): void; - eraseAttr(): number; } export interface IBufferAccessor { From dde96187fa7b6502b357c1a614d0b2cacd6d79e5 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 28 Dec 2018 19:27:38 -0800 Subject: [PATCH 31/61] Keep track of cols/rows inside Buffer --- src/Buffer.test.ts | 8 ------- src/Buffer.ts | 56 +++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index cee0fbad43..bece8c226a 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -259,7 +259,6 @@ describe('Buffer', () => { it('should wrap and unwrap lines', () => { buffer.fillViewportRows(); buffer.resize(5, 10); - terminal.cols = 5; const firstLine = buffer.lines.get(0); for (let i = 0; i < 5; i++) { const code = 'a'.charCodeAt(0) + i; @@ -269,7 +268,6 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(0).length, 5); assert.equal(buffer.lines.get(0).translateToString(), 'abcde'); buffer.resize(1, 10); - terminal.cols = 1; assert.equal(buffer.lines.length, 10); assert.equal(buffer.lines.get(0).translateToString(), 'a'); assert.equal(buffer.lines.get(1).translateToString(), 'b'); @@ -282,7 +280,6 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(8).translateToString(), ' '); assert.equal(buffer.lines.get(9).translateToString(), ' '); buffer.resize(5, 10); - terminal.cols = 5; assert.equal(buffer.lines.length, 10); assert.equal(buffer.lines.get(0).translateToString(), 'abcde'); assert.equal(buffer.lines.get(1).translateToString(), ' '); @@ -299,8 +296,6 @@ describe('Buffer', () => { buffer.fillViewportRows(); terminal.options.scrollback = 1; buffer.resize(10, 5); - terminal.cols = 10; - terminal.rows = 5; const lastLine = buffer.lines.get(4); for (let i = 0; i < 10; i++) { const code = 'a'.charCodeAt(0) + i; @@ -310,7 +305,6 @@ describe('Buffer', () => { assert.equal(buffer.lines.length, 5); buffer.y = 4; buffer.resize(2, 5); - terminal.cols = 2; assert.equal(buffer.y, 4); assert.equal(buffer.ybase, 1); assert.equal(buffer.lines.length, 6); @@ -321,7 +315,6 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(4).translateToString(), 'gh'); assert.equal(buffer.lines.get(5).translateToString(), 'ij'); buffer.resize(1, 5); - terminal.cols = 1; assert.equal(buffer.y, 4); assert.equal(buffer.ybase, 1); assert.equal(buffer.lines.length, 6); @@ -332,7 +325,6 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(4).translateToString(), 'i'); assert.equal(buffer.lines.get(5).translateToString(), 'j'); buffer.resize(10, 5); - terminal.cols = 10; assert.equal(buffer.y, 0); assert.equal(buffer.ybase, 0); assert.equal(buffer.lines.length, 5); diff --git a/src/Buffer.ts b/src/Buffer.ts index feb3a1ee38..ba3353093d 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -46,6 +46,8 @@ export class Buffer implements IBuffer { public savedCurAttr: number; public markers: Marker[] = []; private _bufferLineConstructor: IBufferLineConstructor; + private _cols: number; + private _rows: number; /** * Create a new Buffer. @@ -57,6 +59,8 @@ export class Buffer implements IBuffer { private _terminal: ITerminal, private _hasScrollback: boolean ) { + this._cols = this._terminal.cols; + this._rows = this._terminal.rows; this.clear(); } @@ -88,17 +92,17 @@ export class Buffer implements IBuffer { public getBlankLine(attr: number, isWrapped?: boolean): IBufferLine { const fillCharData: CharData = [attr, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - return new this._bufferLineConstructor(this._terminal.cols, fillCharData, isWrapped); + return new this._bufferLineConstructor(this._cols, fillCharData, isWrapped); } public get hasScrollback(): boolean { - return this._hasScrollback && this.lines.maxLength > this._terminal.rows; + return this._hasScrollback && this.lines.maxLength > this._rows; } public get isCursorInViewport(): boolean { const absoluteY = this.ybase + this.y; const relativeY = absoluteY - this.ydisp; - return (relativeY >= 0 && relativeY < this._terminal.rows); + return (relativeY >= 0 && relativeY < this._rows); } /** @@ -124,7 +128,7 @@ export class Buffer implements IBuffer { if (fillAttr === undefined) { fillAttr = DEFAULT_ATTR; } - let i = this._terminal.rows; + let i = this._rows; while (i--) { this.lines.push(this.getBlankLine(fillAttr)); } @@ -140,9 +144,9 @@ export class Buffer implements IBuffer { this.ybase = 0; this.y = 0; this.x = 0; - this.lines = new CircularList(this._getCorrectBufferLength(this._terminal.rows)); + this.lines = new CircularList(this._getCorrectBufferLength(this._rows)); this.scrollTop = 0; - this.scrollBottom = this._terminal.rows - 1; + this.scrollBottom = this._rows - 1; this.setupTabStops(); } @@ -163,7 +167,7 @@ export class Buffer implements IBuffer { // initialized/filled. if (this.lines.length > 0) { // Deal with columns increasing (reducing needs to happen after reflow) - if (this._terminal.cols < newCols) { + if (this._cols < newCols) { const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; for (let i = 0; i < this.lines.length; i++) { this.lines.get(i).resize(newCols, fillCharData); @@ -172,8 +176,8 @@ export class Buffer implements IBuffer { // Resize rows in both directions as needed let addToY = 0; - if (this._terminal.rows < newRows) { - for (let y = this._terminal.rows; y < newRows; y++) { + if (this._rows < newRows) { + for (let y = this._rows; y < newRows; y++) { if (this.lines.length < newRows + this.ybase) { if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) { // There is room above the buffer and there are no empty elements below the line, @@ -192,8 +196,8 @@ export class Buffer implements IBuffer { } } } - } else { // (this._terminal.rows >= newRows) - for (let y = this._terminal.rows; y > newRows; y--) { + } else { // (this._rows >= newRows) + for (let y = this._rows; y > newRows; y--) { if (this.lines.length > newRows + this.ybase) { if (this.lines.length > this.ybase + this.y + 1) { // The line is a blank line below the cursor, remove it @@ -238,22 +242,25 @@ export class Buffer implements IBuffer { this._reflow(newCols); // Trim the end of the line off if cols shrunk - if (this._terminal.cols > newCols) { + if (this._cols > newCols) { const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; for (let i = 0; i < this.lines.length; i++) { this.lines.get(i).resize(newCols, fillCharData); } } } + + this._cols = newCols; + this._rows = newRows; } private _reflow(newCols: number): void { - if (this._terminal.cols === newCols) { + if (this._cols === newCols) { return; } // Iterate through rows, ignore the last one as it cannot be wrapped - if (newCols > this._terminal.cols) { + if (newCols > this._cols) { for (let y = 0; y < this.lines.length - 1; y++) { y += this._reflowLarger(y, newCols); } @@ -282,11 +289,11 @@ export class Buffer implements IBuffer { // Copy buffer data to new locations let destLineIndex = 0; - let destCol = this._terminal.cols; + let destCol = this._cols; let srcLineIndex = 1; let srcCol = 0; while (srcLineIndex < wrappedLines.length) { - const srcRemainingCells = this._terminal.cols - srcCol; + const srcRemainingCells = this._cols - srcCol; const destRemainingCells = newCols - destCol; const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); @@ -296,7 +303,7 @@ export class Buffer implements IBuffer { destCol = 0; } srcCol += cellsToCopy; - if (srcCol === this._terminal.cols) { + if (srcCol === this._cols) { srcLineIndex++; srcCol = 0; } @@ -360,7 +367,7 @@ export class Buffer implements IBuffer { // Determine how many lines need to be inserted at the end, based on the trimmed length of // the last wrapped line const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); - const cellsNeeded = (wrappedLines.length - 1) * this._terminal.cols + lastLineLength; + const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; const linesNeeded = Math.ceil(cellsNeeded / newCols); const linesToAdd = linesNeeded - wrappedLines.length; let trimmedLines: number; @@ -400,7 +407,7 @@ export class Buffer implements IBuffer { srcCol -= cellsToCopy; if (srcCol === 0) { srcLineIndex--; - srcCol = this._terminal.cols; + srcCol = this._cols; } } @@ -408,12 +415,11 @@ export class Buffer implements IBuffer { let viewportAdjustments = linesToAdd - trimmedLines; while (viewportAdjustments-- > 0) { if (this.ybase === 0) { - if (this.y < this._terminal.rows - 1) { + if (this.y < this._rows - 1) { this.y++; this.lines.pop(); } else { this.ybase++; - // TODO: Use this? if (this._terminal._userScrolling) { this.ydisp++; } } else { @@ -503,7 +509,7 @@ export class Buffer implements IBuffer { i = 0; } - for (; i < this._terminal.cols; i += this._terminal.options.tabStopWidth) { + for (; i < this._cols; i += this._terminal.options.tabStopWidth) { this.tabs[i] = true; } } @@ -517,7 +523,7 @@ export class Buffer implements IBuffer { x = this.x; } while (!this.tabs[--x] && x > 0); - return x >= this._terminal.cols ? this._terminal.cols - 1 : x < 0 ? 0 : x; + return x >= this._cols ? this._cols - 1 : x < 0 ? 0 : x; } /** @@ -528,8 +534,8 @@ export class Buffer implements IBuffer { if (x === null || x === undefined) { x = this.x; } - while (!this.tabs[++x] && x < this._terminal.cols); - return x >= this._terminal.cols ? this._terminal.cols - 1 : x < 0 ? 0 : x; + while (!this.tabs[++x] && x < this._cols); + return x >= this._cols ? this._cols - 1 : x < 0 ? 0 : x; } public addMarker(y: number): Marker { From 478742a22e7c3c8aacfa0ea81757a7f6d9121504 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 30 Dec 2018 12:14:48 -0800 Subject: [PATCH 32/61] Make reflow small crazy fast This messy but this drops 100000 scrollback reflow from 87 cols to 40 cols go from ~18 seconds to < 1 second, 10000 takes around 70ms --- src/Buffer.ts | 223 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 150 insertions(+), 73 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index ba3353093d..acb462ab2d 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -265,10 +265,7 @@ export class Buffer implements IBuffer { y += this._reflowLarger(y, newCols); } } else { - // Go backwards as many lines may be trimmed and this will avoid considering them - for (let y = this.lines.length - 1; y >= 0; y--) { - y -= this._reflowSmaller(y, newCols); - } + this._reflowSmaller(newCols); } } @@ -344,93 +341,173 @@ export class Buffer implements IBuffer { return wrappedLines.length - countToRemove - 1; } - private _reflowSmaller(y: number, newCols: number): number { - // Check whether this line is a problem - let nextLine = this.lines.get(y) as BufferLine; - if (!nextLine.isWrapped && nextLine.getTrimmedLength() <= newCols) { - return 0; - } + private _reflowSmaller(newCols: number): void { + // Gather all BufferLines that need to be inserted into the Buffer here so that they can be + // batched up and only committed once + const toInsert = []; + let countToInsert = 0; + // Go backwards as many lines may be trimmed and this will avoid considering them + for (let y = this.lines.length - 1; y >= 0; y--) { + // Check whether this line is a problem + let nextLine = this.lines.get(y) as BufferLine; + if (!nextLine.isWrapped && nextLine.getTrimmedLength() <= newCols) { + continue; + } - // Gather wrapped lines and adjust y to be the starting line - const wrappedLines: BufferLine[] = [nextLine]; - if (nextLine.isWrapped) { - while (true) { + // Gather wrapped lines and adjust y to be the starting line + const wrappedLines: BufferLine[] = [nextLine]; + while (nextLine.isWrapped && y > 0) { nextLine = this.lines.get(--y) as BufferLine; // TODO: unshift is expensive wrappedLines.unshift(nextLine); - if (!nextLine.isWrapped || y === 0) { - break; - } } - } - // Determine how many lines need to be inserted at the end, based on the trimmed length of - // the last wrapped line - const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); - const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; - const linesNeeded = Math.ceil(cellsNeeded / newCols); - const linesToAdd = linesNeeded - wrappedLines.length; - let trimmedLines: number; - if (this.ybase === 0 && this.y !== this.lines.length - 1) { - // If the top section of the buffer is not yet filled - trimmedLines = Math.max(0, this.y - this.lines.maxLength + linesToAdd); - } else { - trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); - } + // Determine how many lines need to be inserted at the end, based on the trimmed length of + // the last wrapped line + const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); + const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; + const linesNeeded = Math.ceil(cellsNeeded / newCols); + const linesToAdd = linesNeeded - wrappedLines.length; + let trimmedLines: number; + if (this.ybase === 0 && this.y !== this.lines.length - 1) { + // If the top section of the buffer is not yet filled + trimmedLines = Math.max(0, this.y - this.lines.maxLength + linesToAdd); + } else { + trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); + } - // Add the new lines - const newLines: BufferLine[] = []; - for (let i = 0; i < linesToAdd; i++) { - const newLine = this.getBlankLine(DEFAULT_ATTR, true) as BufferLine; - newLines.push(newLine); - } - this.lines.splice(y + wrappedLines.length, 0, ...newLines); - wrappedLines.push(...newLines); - - // Copy buffer data to new locations, this needs to happen backwards to do in-place - let destLineIndex = Math.floor(cellsNeeded / newCols); - let destCol = cellsNeeded % newCols; - if (destCol === 0) { - destLineIndex--; - destCol = newCols; - } - let srcLineIndex = wrappedLines.length - linesToAdd - 1; - let srcCol = lastLineLength; - while (srcLineIndex >= 0) { - const cellsToCopy = Math.min(srcCol, destCol); - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true); - destCol -= cellsToCopy; + // Add the new lines + const newLines: BufferLine[] = []; + for (let i = 0; i < linesToAdd; i++) { + const newLine = this.getBlankLine(DEFAULT_ATTR, true) as BufferLine; + newLines.push(newLine); + } + if (newLines.length > 0) { + toInsert.push({ + // countToInsert here gets the actual index, taking into account other inserted items. + // using this we can iterate through the list forwards + start: y + wrappedLines.length + countToInsert, + newLines + }); + countToInsert += newLines.length; + } + // this.lines.splice(y + wrappedLines.length, 0, ...newLines); + wrappedLines.push(...newLines); + + // Copy buffer data to new locations, this needs to happen backwards to do in-place + let destLineIndex = Math.floor(cellsNeeded / newCols); + let destCol = cellsNeeded % newCols; if (destCol === 0) { destLineIndex--; destCol = newCols; } - srcCol -= cellsToCopy; - if (srcCol === 0) { - srcLineIndex--; - srcCol = this._cols; + let srcLineIndex = wrappedLines.length - linesToAdd - 1; + let srcCol = lastLineLength; + while (srcLineIndex >= 0) { + const cellsToCopy = Math.min(srcCol, destCol); + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true); + destCol -= cellsToCopy; + if (destCol === 0) { + destLineIndex--; + destCol = newCols; + } + srcCol -= cellsToCopy; + if (srcCol === 0) { + srcLineIndex--; + srcCol = this._cols; + } } - } - // Adjust viewport as needed - let viewportAdjustments = linesToAdd - trimmedLines; - while (viewportAdjustments-- > 0) { - if (this.ybase === 0) { - if (this.y < this._rows - 1) { - this.y++; - this.lines.pop(); + // Adjust viewport as needed + let viewportAdjustments = linesToAdd - trimmedLines; + while (viewportAdjustments-- > 0) { + if (this.ybase === 0) { + if (this.y < this._rows - 1) { + this.y++; + this.lines.pop(); + } else { + this.ybase++; + this.ydisp++; + } } else { - this.ybase++; - this.ydisp++; + if (this.ybase === this.ydisp) { + this.ybase++; + this.ydisp++; + } } - } else { - if (this.ybase === this.ydisp) { - this.ybase++; - this.ydisp++; + } + + // y -= wrappedLines.length - 1 /*- linesToAdd*/ /*+ trimmedLines */; + } + + // Record original lines so they don't get overridden when we rearrange the list + const originalLines: BufferLine[] = []; + for (let i = 0; i < this.lines.length; i++) { + originalLines.push(this.lines.get(i) as BufferLine); + } + // if (toInsert.length) { + // let insertIndex = toInsert.length - 1; + // let nextToInsert = toInsert[insertIndex]; + // let originalLineIndex = 0; + // for (let i = 0; i < Math.min(this.lines.maxLength - 1, this.lines.length + countToInsert); i++) { + // if (nextToInsert && nextToInsert.start === i) { + // this.lines.set(i, nextToInsert.newLines.shift()); + // if (nextToInsert.newLines.length === 0) { + // nextToInsert = toInsert[--insertIndex]; + // } + // } else { + // this.lines.set(i, originalLines[originalLineIndex++]); + // } + // } + // } + + if (toInsert.length > 0) { + let nextToInsertIndex = 0; + let nextToInsert = toInsert[nextToInsertIndex]; + let originalLineIndex = originalLines.length - 1; + const originalLinesLength = this.lines.length; + this.lines.length = Math.min(this.lines.maxLength, this.lines.length + countToInsert); + // let countToBeInserted = countToInsert; + let countInsertedSoFar = 0; + for (let i = Math.min(this.lines.maxLength - 1, originalLinesLength + countToInsert - 1); i >= 0; i--) { + if (nextToInsert && nextToInsert.start > originalLineIndex + countInsertedSoFar) { + for (let nextI = nextToInsert.newLines.length - 1; nextI >= 0; nextI--) { + this.lines.set(i--, nextToInsert.newLines[nextI]); + } + i++; // Don't skip for the first row + // this.lines.set(i, nextToInsert.newLines.pop()); + countInsertedSoFar += nextToInsert.newLines.length; + // countToBeInserted--; + // if (nextToInsert.newLines.length === 0) { + nextToInsert = toInsert[++nextToInsertIndex]; + // } + + + + // this.lines.set(i, nextToInsert.newLines.pop()); + // countInsertedSoFar++; + // // countToBeInserted--; + // if (nextToInsert.newLines.length === 0) { + // nextToInsert = toInsert[++nextToInsertIndex]; + // } + } else { + this.lines.set(i, originalLines[originalLineIndex--]); } } - } + // TODO: Throw trim event + } - return wrappedLines.length - 1 - linesToAdd + trimmedLines; + // let offset = 0; + // const listener = (countToTrim: number) => { + // offset -= countToTrim; + // }; + // this.lines.on('trim', listener); + // toInsert.forEach(value => { + // console.log('Insert at ', value.start + offset, value.newLines); + // this.lines.splice(value.start + offset, 0, ...value.newLines); + // // offset -= value.start; + // }); + // this.lines.off('trim', listener); } /** From c9f4a650c6c24c350e526d8bb8ed063fb0d8f8d3 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 07:26:08 -0800 Subject: [PATCH 33/61] Clean up comments and todos --- src/Buffer.ts | 72 ++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 58 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index acb462ab2d..1dfc3ef63b 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -25,6 +25,8 @@ export const WHITESPACE_CELL_CHAR = ' '; export const WHITESPACE_CELL_WIDTH = 1; export const WHITESPACE_CELL_CODE = 32; +const FILL_CHAR_DATA: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; + /** * This class represents a terminal buffer (an internal state of the terminal), where the * following information is stored (in high-level): @@ -168,9 +170,8 @@ export class Buffer implements IBuffer { if (this.lines.length > 0) { // Deal with columns increasing (reducing needs to happen after reflow) if (this._cols < newCols) { - const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; for (let i = 0; i < this.lines.length; i++) { - this.lines.get(i).resize(newCols, fillCharData); + this.lines.get(i).resize(newCols, FILL_CHAR_DATA); } } @@ -191,8 +192,7 @@ export class Buffer implements IBuffer { } else { // Add a blank line if there is no buffer left at the top to scroll to, or if there // are blank lines after the cursor - const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - this.lines.push(new this._bufferLineConstructor(newCols, fillCharData)); + this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); } } } @@ -243,9 +243,8 @@ export class Buffer implements IBuffer { // Trim the end of the line off if cols shrunk if (this._cols > newCols) { - const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; for (let i = 0; i < this.lines.length; i++) { - this.lines.get(i).resize(newCols, fillCharData); + this.lines.get(i).resize(newCols, FILL_CHAR_DATA); } } } @@ -306,10 +305,8 @@ export class Buffer implements IBuffer { } } - // Clear out remaining cells or fragments could remain - // TODO: @jerch can fillCharData be a const? - const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - wrappedLines[destLineIndex].replaceCells(destCol, newCols, fillCharData); + // Clear out remaining cells or fragments could remain; + wrappedLines[destLineIndex].replaceCells(destCol, newCols, FILL_CHAR_DATA); // Work backwards and remove any rows at the end that only contain null cells let countToRemove = 0; @@ -328,7 +325,7 @@ export class Buffer implements IBuffer { if (this.ybase === 0) { this.y--; // Add an extra row at the bottom of the viewport - this.lines.push(new this._bufferLineConstructor(newCols, fillCharData)); + this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); } else { if (this.ydisp === this.ybase) { this.ydisp--; @@ -358,7 +355,6 @@ export class Buffer implements IBuffer { const wrappedLines: BufferLine[] = [nextLine]; while (nextLine.isWrapped && y > 0) { nextLine = this.lines.get(--y) as BufferLine; - // TODO: unshift is expensive wrappedLines.unshift(nextLine); } @@ -391,7 +387,6 @@ export class Buffer implements IBuffer { }); countToInsert += newLines.length; } - // this.lines.splice(y + wrappedLines.length, 0, ...newLines); wrappedLines.push(...newLines); // Copy buffer data to new locations, this needs to happen backwards to do in-place @@ -436,8 +431,6 @@ export class Buffer implements IBuffer { } } } - - // y -= wrappedLines.length - 1 /*- linesToAdd*/ /*+ trimmedLines */; } // Record original lines so they don't get overridden when we rearrange the list @@ -445,69 +438,32 @@ export class Buffer implements IBuffer { for (let i = 0; i < this.lines.length; i++) { originalLines.push(this.lines.get(i) as BufferLine); } - // if (toInsert.length) { - // let insertIndex = toInsert.length - 1; - // let nextToInsert = toInsert[insertIndex]; - // let originalLineIndex = 0; - // for (let i = 0; i < Math.min(this.lines.maxLength - 1, this.lines.length + countToInsert); i++) { - // if (nextToInsert && nextToInsert.start === i) { - // this.lines.set(i, nextToInsert.newLines.shift()); - // if (nextToInsert.newLines.length === 0) { - // nextToInsert = toInsert[--insertIndex]; - // } - // } else { - // this.lines.set(i, originalLines[originalLineIndex++]); - // } - // } - // } + // Rearrange lines in the buffer if there are any insertions, this is done at the end rather + // than earlier so that it's a single O(n) pass through the buffer, instead of O(n^2) from many + // costly calls to CircularList.splice. if (toInsert.length > 0) { let nextToInsertIndex = 0; let nextToInsert = toInsert[nextToInsertIndex]; let originalLineIndex = originalLines.length - 1; const originalLinesLength = this.lines.length; this.lines.length = Math.min(this.lines.maxLength, this.lines.length + countToInsert); - // let countToBeInserted = countToInsert; let countInsertedSoFar = 0; for (let i = Math.min(this.lines.maxLength - 1, originalLinesLength + countToInsert - 1); i >= 0; i--) { if (nextToInsert && nextToInsert.start > originalLineIndex + countInsertedSoFar) { + // Insert extra lines here, adjusting i as needed for (let nextI = nextToInsert.newLines.length - 1; nextI >= 0; nextI--) { this.lines.set(i--, nextToInsert.newLines[nextI]); } i++; // Don't skip for the first row - // this.lines.set(i, nextToInsert.newLines.pop()); countInsertedSoFar += nextToInsert.newLines.length; - // countToBeInserted--; - // if (nextToInsert.newLines.length === 0) { - nextToInsert = toInsert[++nextToInsertIndex]; - // } - - - - // this.lines.set(i, nextToInsert.newLines.pop()); - // countInsertedSoFar++; - // // countToBeInserted--; - // if (nextToInsert.newLines.length === 0) { - // nextToInsert = toInsert[++nextToInsertIndex]; - // } + nextToInsert = toInsert[++nextToInsertIndex]; } else { this.lines.set(i, originalLines[originalLineIndex--]); } } // TODO: Throw trim event - } - - // let offset = 0; - // const listener = (countToTrim: number) => { - // offset -= countToTrim; - // }; - // this.lines.on('trim', listener); - // toInsert.forEach(value => { - // console.log('Insert at ', value.start + offset, value.newLines); - // this.lines.splice(value.start + offset, 0, ...value.newLines); - // // offset -= value.start; - // }); - // this.lines.off('trim', listener); + } } /** From b7081abfceb41b82dda4921e2f96b59cde26f0f4 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 07:54:16 -0800 Subject: [PATCH 34/61] Move loop into reflowLarger (adjust indent) --- src/Buffer.ts | 120 +++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 1dfc3ef63b..65e42de056 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -260,82 +260,82 @@ export class Buffer implements IBuffer { // Iterate through rows, ignore the last one as it cannot be wrapped if (newCols > this._cols) { - for (let y = 0; y < this.lines.length - 1; y++) { - y += this._reflowLarger(y, newCols); - } + this._reflowLarger(newCols); } else { this._reflowSmaller(newCols); } } - private _reflowLarger(y: number, newCols: number): number { - // Check if this row is wrapped - let i = y; - let nextLine = this.lines.get(++i) as BufferLine; - if (!nextLine.isWrapped) { - return 0; - } - - // Check how many lines it's wrapped for - const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; - while (nextLine.isWrapped && i < this.lines.length) { - wrappedLines.push(nextLine); - nextLine = this.lines.get(++i) as BufferLine; - } + private _reflowLarger(newCols: number): void { + for (let y = 0; y < this.lines.length - 1; y++) { + // Check if this row is wrapped + let i = y; + let nextLine = this.lines.get(++i) as BufferLine; + if (!nextLine.isWrapped) { + continue; + } - // Copy buffer data to new locations - let destLineIndex = 0; - let destCol = this._cols; - let srcLineIndex = 1; - let srcCol = 0; - while (srcLineIndex < wrappedLines.length) { - const srcRemainingCells = this._cols - srcCol; - const destRemainingCells = newCols - destCol; - const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); - destCol += cellsToCopy; - if (destCol === newCols) { - destLineIndex++; - destCol = 0; + // Check how many lines it's wrapped for + const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; + while (nextLine.isWrapped && i < this.lines.length) { + wrappedLines.push(nextLine); + nextLine = this.lines.get(++i) as BufferLine; } - srcCol += cellsToCopy; - if (srcCol === this._cols) { - srcLineIndex++; - srcCol = 0; + + // Copy buffer data to new locations + let destLineIndex = 0; + let destCol = this._cols; + let srcLineIndex = 1; + let srcCol = 0; + while (srcLineIndex < wrappedLines.length) { + const srcRemainingCells = this._cols - srcCol; + const destRemainingCells = newCols - destCol; + const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); + destCol += cellsToCopy; + if (destCol === newCols) { + destLineIndex++; + destCol = 0; + } + srcCol += cellsToCopy; + if (srcCol === this._cols) { + srcLineIndex++; + srcCol = 0; + } } - } - // Clear out remaining cells or fragments could remain; - wrappedLines[destLineIndex].replaceCells(destCol, newCols, FILL_CHAR_DATA); + // Clear out remaining cells or fragments could remain; + wrappedLines[destLineIndex].replaceCells(destCol, newCols, FILL_CHAR_DATA); - // Work backwards and remove any rows at the end that only contain null cells - let countToRemove = 0; - for (let i = wrappedLines.length - 1; i > 0; i--) { - if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) { - countToRemove++; - } else { - break; + // Work backwards and remove any rows at the end that only contain null cells + let countToRemove = 0; + for (let i = wrappedLines.length - 1; i > 0; i--) { + if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) { + countToRemove++; + } else { + break; + } } - } - if (countToRemove > 0) { - this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); - let viewportAdjustments = countToRemove; - while (viewportAdjustments-- > 0) { - if (this.ybase === 0) { - this.y--; - // Add an extra row at the bottom of the viewport - this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); - } else { - if (this.ydisp === this.ybase) { - this.ydisp--; + if (countToRemove > 0) { + this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); + let viewportAdjustments = countToRemove; + while (viewportAdjustments-- > 0) { + if (this.ybase === 0) { + this.y--; + // Add an extra row at the bottom of the viewport + this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); + } else { + if (this.ydisp === this.ybase) { + this.ydisp--; + } + this.ybase--; } - this.ybase--; } } - } - return wrappedLines.length - countToRemove - 1; + y += wrappedLines.length - countToRemove - 1; + } } private _reflowSmaller(newCols: number): void { From 135e31f2ca653932d5b85c78780326b5a4776179 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 08:58:04 -0800 Subject: [PATCH 35/61] Speed up reflow larger by batching removals --- src/Buffer.ts | 82 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 65e42de056..fc74f679b7 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -267,6 +267,9 @@ export class Buffer implements IBuffer { } private _reflowLarger(newCols: number): void { + // Gather all BufferLines that need to be removed from the Buffer here so that they can be + // batched up and only committed once + const toRemove: number[] = []; for (let y = 0; y < this.lines.length - 1; y++) { // Check if this row is wrapped let i = y; @@ -318,24 +321,59 @@ export class Buffer implements IBuffer { } if (countToRemove > 0) { - this.lines.splice(y + wrappedLines.length - countToRemove, countToRemove); - let viewportAdjustments = countToRemove; - while (viewportAdjustments-- > 0) { - if (this.ybase === 0) { - this.y--; - // Add an extra row at the bottom of the viewport - this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); - } else { - if (this.ydisp === this.ybase) { - this.ydisp--; - } - this.ybase--; - } - } + toRemove.push(y + wrappedLines.length - countToRemove); // index + toRemove.push(countToRemove); } y += wrappedLines.length - countToRemove - 1; } + + if (toRemove.length > 0) { + // First iterate through the list and get the actual indexes to use for rows + const newLayout: number[] = []; + + let nextToRemoveIndex = 0; + let nextToRemoveStart = toRemove[nextToRemoveIndex]; + let countRemovedSoFar = 0; + for (let i = 0; i < this.lines.length; i++) { + if (nextToRemoveStart === i) { + const countToRemove = toRemove[++nextToRemoveIndex]; + i += countToRemove - 1; + countRemovedSoFar += countToRemove; + nextToRemoveStart = toRemove[++nextToRemoveIndex]; + } else { + newLayout.push(i); + } + } + + // TODO: THis and the next loop could be improved, only gather the new layout lines, not the original lines + // Record original lines so they don't get overridden when we rearrange the list + const originalLines: BufferLine[] = []; + for (let i = 0; i < this.lines.length; i++) { + originalLines.push(this.lines.get(i) as BufferLine); + } + + // Rearrange the list + for (let i = 0; i < newLayout.length; i++) { + this.lines.set(i, originalLines[newLayout[i]]); + } + this.lines.length = newLayout.length; + + // Adjust viewport based on number of items removed + let viewportAdjustments = countRemovedSoFar; + while (viewportAdjustments-- > 0) { + if (this.ybase === 0) { + this.y--; + // Add an extra row at the bottom of the viewport + this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); + } else { + if (this.ydisp === this.ybase) { + this.ydisp--; + } + this.ybase--; + } + } + } } private _reflowSmaller(newCols: number): void { @@ -433,20 +471,20 @@ export class Buffer implements IBuffer { } } - // Record original lines so they don't get overridden when we rearrange the list - const originalLines: BufferLine[] = []; - for (let i = 0; i < this.lines.length; i++) { - originalLines.push(this.lines.get(i) as BufferLine); - } - // Rearrange lines in the buffer if there are any insertions, this is done at the end rather // than earlier so that it's a single O(n) pass through the buffer, instead of O(n^2) from many // costly calls to CircularList.splice. if (toInsert.length > 0) { + // Record original lines so they don't get overridden when we rearrange the list + const originalLines: BufferLine[] = []; + for (let i = 0; i < this.lines.length; i++) { + originalLines.push(this.lines.get(i) as BufferLine); + } + const originalLinesLength = this.lines.length; + + let originalLineIndex = originalLinesLength - 1; let nextToInsertIndex = 0; let nextToInsert = toInsert[nextToInsertIndex]; - let originalLineIndex = originalLines.length - 1; - const originalLinesLength = this.lines.length; this.lines.length = Math.min(this.lines.maxLength, this.lines.length + countToInsert); let countInsertedSoFar = 0; for (let i = Math.min(this.lines.maxLength - 1, originalLinesLength + countToInsert - 1); i >= 0; i--) { From 1612cec3e093861f6f46da4101876d32c635affa Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 09:54:19 -0800 Subject: [PATCH 36/61] Fix reflow larger bug, add regression test --- src/Buffer.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Buffer.ts | 15 +++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index bece8c226a..faed180833 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -334,6 +334,49 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(3).translateToString(), ' '); assert.equal(buffer.lines.get(4).translateToString(), ' '); }); + it('should remove the correct amount of rows when reflowing larger', () => { + // This is a regression test to ensure that successive wrapped lines that are getting + // 3+ lines removed on a reflow actually remove the right lines + buffer.fillViewportRows(); + buffer.resize(10, 10); + const firstLine = buffer.lines.get(0); + const secondLine = buffer.lines.get(1); + for (let i = 0; i < 10; i++) { + const code = 'a'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + firstLine.set(i, [null, char, 1, code]); + } + for (let i = 0; i < 10; i++) { + const code = '0'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + secondLine.set(i, [null, char, 1, code]); + } + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + for (let i = 2; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + buffer.resize(2, 10); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'cd'); + assert.equal(buffer.lines.get(2).translateToString(), 'ef'); + assert.equal(buffer.lines.get(3).translateToString(), 'gh'); + assert.equal(buffer.lines.get(4).translateToString(), 'ij'); + assert.equal(buffer.lines.get(5).translateToString(), '01'); + assert.equal(buffer.lines.get(6).translateToString(), '23'); + assert.equal(buffer.lines.get(7).translateToString(), '45'); + assert.equal(buffer.lines.get(8).translateToString(), '67'); + assert.equal(buffer.lines.get(9).translateToString(), '89'); + buffer.resize(10, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + for (let i = 2; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + }); }); }); diff --git a/src/Buffer.ts b/src/Buffer.ts index fc74f679b7..27662708df 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -280,7 +280,7 @@ export class Buffer implements IBuffer { // Check how many lines it's wrapped for const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; - while (nextLine.isWrapped && i < this.lines.length) { + while (i < this.lines.length && nextLine.isWrapped) { wrappedLines.push(nextLine); nextLine = this.lines.get(++i) as BufferLine; } @@ -325,7 +325,7 @@ export class Buffer implements IBuffer { toRemove.push(countToRemove); } - y += wrappedLines.length - countToRemove - 1; + y += wrappedLines.length - 1; } if (toRemove.length > 0) { @@ -346,16 +346,15 @@ export class Buffer implements IBuffer { } } - // TODO: THis and the next loop could be improved, only gather the new layout lines, not the original lines // Record original lines so they don't get overridden when we rearrange the list - const originalLines: BufferLine[] = []; - for (let i = 0; i < this.lines.length; i++) { - originalLines.push(this.lines.get(i) as BufferLine); + const newLayoutLines: BufferLine[] = []; + for (let i = 0; i < newLayout.length; i++) { + newLayoutLines.push(this.lines.get(newLayout[i]) as BufferLine); } // Rearrange the list - for (let i = 0; i < newLayout.length; i++) { - this.lines.set(i, originalLines[newLayout[i]]); + for (let i = 0; i < newLayoutLines.length; i++) { + this.lines.set(i, newLayoutLines[i]); } this.lines.length = newLayout.length; From db488ebcc9699ef97c21bd6695f0983006947dfd Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 10:18:31 -0800 Subject: [PATCH 37/61] Reflow combined chars --- src/Buffer.test.ts | 15 +++++++++++++++ src/BufferLine.ts | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index faed180833..4c348ec654 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -377,6 +377,21 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(i).translateToString(), ' '); } }); + it('should transfer combined char data over to reflowed lines', () => { + buffer.fillViewportRows(); + buffer.resize(4, 2); + const firstLine = buffer.lines.get(0); + firstLine.set(0, [ null, 'a', 1, 'a'.charCodeAt(0) ]); + firstLine.set(1, [ null, 'b', 1, 'b'.charCodeAt(0) ]); + firstLine.set(2, [ null, 'c', 1, 'c'.charCodeAt(0) ]); + firstLine.set(3, [ null, '😁', 1, '😁'.charCodeAt(0) ]); + assert.equal(buffer.lines.length, 2); + assert.equal(buffer.lines.get(0).translateToString(), 'abc😁'); + assert.equal(buffer.lines.get(1).translateToString(), ' '); + buffer.resize(2, 2); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'c😁'); + }); }); }); diff --git a/src/BufferLine.ts b/src/BufferLine.ts index fd28af846a..619fa9caea 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -319,6 +319,15 @@ export class BufferLine implements IBufferLine { } } } + + // Move any combined data over as needed + const srcCombinedKeys = Object.keys(src._combined); + for (let i = 0; i < srcCombinedKeys.length; i++) { + const key = parseInt(srcCombinedKeys[i], 10); + if (key >= srcCol) { + this._combined[key - srcCol + destCol] = src._combined[key]; + } + } } public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = this.length): string { From 40e8618cf5810e61b8d42e6d0a1d37aab31516f3 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 10:26:10 -0800 Subject: [PATCH 38/61] Discard cut off combined data when resizing BufferLines --- src/BufferLine.test.ts | 13 +++++++++++++ src/BufferLine.ts | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/BufferLine.test.ts b/src/BufferLine.test.ts index c652ff4e77..f5e95fc857 100644 --- a/src/BufferLine.test.ts +++ b/src/BufferLine.test.ts @@ -9,6 +9,10 @@ import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE, DEFAULT_ATTR } from '. class TestBufferLine extends BufferLine { + public get combined(): {[index: number]: string} { + return this._combined; + } + public toArray(): CharData[] { const result = []; for (let i = 0; i < this.length; ++i) { @@ -154,6 +158,15 @@ describe('BufferLine', function(): void { line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)]); chai.expect(line.toArray()).eql(Array(0).fill([1, 'a', 0, 'a'.charCodeAt(0)])); }); + it('should remove combining data', () => { + const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); + line.set(9, [ null, '😁', 1, '😁'.charCodeAt(0) ]); + chai.expect(line.translateToString()).eql('aaaaaaaaa😁'); + chai.expect(Object.keys(line.combined).length).eql(1); + line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)]); + chai.expect(line.translateToString()).eql('aaaaa'); + chai.expect(Object.keys(line.combined).length).eql(0); + }); }); describe('getTrimLength', function(): void { it('empty line', function(): void { diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 619fa9caea..1c39147184 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -250,8 +250,17 @@ export class BufferLine implements IBufferLine { const data = new Uint32Array(cols * CELL_SIZE); data.set(this._data.subarray(0, cols * CELL_SIZE)); this._data = data; + // Remove any cut off combined data + const keys = Object.keys(this._combined); + for (let i = 0; i < keys.length; i++) { + const key = parseInt(keys[i], 10); + if (key >= cols) { + delete this._combined[key]; + } + } } else { this._data = null; + this._combined = {}; } } this.length = cols; From 840970eac7e1d157f68dd3e1d98a073921b31a07 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 13:15:23 -0800 Subject: [PATCH 39/61] Update markers after a reflow --- src/Buffer.test.ts | 118 +++++++++++++++++++++++++++++++++++++ src/Buffer.ts | 54 +++++++++++++++-- src/common/CircularList.ts | 20 +++++-- src/common/EventEmitter.ts | 13 ++++ 4 files changed, 195 insertions(+), 10 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 4c348ec654..7abf6f726d 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -392,6 +392,124 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(0).translateToString(), 'ab'); assert.equal(buffer.lines.get(1).translateToString(), 'c😁'); }); + it('should adjust markers when reflowing', () => { + buffer.fillViewportRows(); + buffer.resize(10, 15); + for (let i = 0; i < 10; i++) { + const code = 'a'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + buffer.lines.get(0).set(i, [null, char, 1, code]); + } + for (let i = 0; i < 10; i++) { + const code = '0'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + buffer.lines.get(1).set(i, [null, char, 1, code]); + } + for (let i = 0; i < 10; i++) { + const code = 'k'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + buffer.lines.get(2).set(i, [null, char, 1, code]); + } + // Buffer: + // abcdefghij + // 0123456789 + // abcdefghij + const firstMarker = buffer.addMarker(0); + const secondMarker = buffer.addMarker(1); + const thirdMarker = buffer.addMarker(2); + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); + assert.equal(firstMarker.line, 0); + assert.equal(secondMarker.line, 1); + assert.equal(thirdMarker.line, 2); + buffer.resize(2, 15); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'cd'); + assert.equal(buffer.lines.get(2).translateToString(), 'ef'); + assert.equal(buffer.lines.get(3).translateToString(), 'gh'); + assert.equal(buffer.lines.get(4).translateToString(), 'ij'); + assert.equal(buffer.lines.get(5).translateToString(), '01'); + assert.equal(buffer.lines.get(6).translateToString(), '23'); + assert.equal(buffer.lines.get(7).translateToString(), '45'); + assert.equal(buffer.lines.get(8).translateToString(), '67'); + assert.equal(buffer.lines.get(9).translateToString(), '89'); + assert.equal(buffer.lines.get(10).translateToString(), 'kl'); + assert.equal(buffer.lines.get(11).translateToString(), 'mn'); + assert.equal(buffer.lines.get(12).translateToString(), 'op'); + assert.equal(buffer.lines.get(13).translateToString(), 'qr'); + assert.equal(buffer.lines.get(14).translateToString(), 'st'); + assert.equal(firstMarker.line, 0, 'first marker should remain unchanged'); + assert.equal(secondMarker.line, 5, 'second marker should be shifted since the first line wrapped'); + assert.equal(thirdMarker.line, 10, 'third marker should be shifted since the first and second lines wrapped'); + buffer.resize(10, 15); + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); + assert.equal(firstMarker.line, 0, 'first marker should remain unchanged'); + assert.equal(secondMarker.line, 1, 'second marker should be restored to it\'s original line'); + assert.equal(thirdMarker.line, 2, 'third marker should be restored to it\'s original line'); + assert.equal(firstMarker.isDisposed, false); + assert.equal(secondMarker.isDisposed, false); + assert.equal(thirdMarker.isDisposed, false); + }); + it('should dispose markers whose rows are trimmed during a reflow', () => { + buffer.fillViewportRows(); + terminal.options.scrollback = 1; + buffer.resize(10, 10); + for (let i = 0; i < 10; i++) { + const code = 'a'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + buffer.lines.get(0).set(i, [null, char, 1, code]); + } + for (let i = 0; i < 10; i++) { + const code = '0'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + buffer.lines.get(1).set(i, [null, char, 1, code]); + } + for (let i = 0; i < 10; i++) { + const code = 'k'.charCodeAt(0) + i; + const char = String.fromCharCode(code); + buffer.lines.get(2).set(i, [null, char, 1, code]); + } + // Buffer: + // abcdefghij + // 0123456789 + // abcdefghij + const firstMarker = buffer.addMarker(0); + const secondMarker = buffer.addMarker(1); + const thirdMarker = buffer.addMarker(2); + buffer.y = 2; + assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); + assert.equal(firstMarker.line, 0); + assert.equal(secondMarker.line, 1); + assert.equal(thirdMarker.line, 2); + buffer.resize(2, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'ij'); + assert.equal(buffer.lines.get(1).translateToString(), '01'); + assert.equal(buffer.lines.get(2).translateToString(), '23'); + assert.equal(buffer.lines.get(3).translateToString(), '45'); + assert.equal(buffer.lines.get(4).translateToString(), '67'); + assert.equal(buffer.lines.get(5).translateToString(), '89'); + assert.equal(buffer.lines.get(6).translateToString(), 'kl'); + assert.equal(buffer.lines.get(7).translateToString(), 'mn'); + assert.equal(buffer.lines.get(8).translateToString(), 'op'); + assert.equal(buffer.lines.get(9).translateToString(), 'qr'); + assert.equal(buffer.lines.get(10).translateToString(), 'st'); + assert.equal(secondMarker.line, 1, 'second marker should remain the same as it was shifted 4 and trimmed 4'); + assert.equal(thirdMarker.line, 6, 'third marker should be shifted since the first and second lines wrapped'); + assert.equal(firstMarker.isDisposed, true, 'first marker was trimmed'); + assert.equal(secondMarker.isDisposed, false); + assert.equal(thirdMarker.isDisposed, false); + buffer.resize(10, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'ij '); + assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); + assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); + assert.equal(secondMarker.line, 1, 'second marker should be restored'); + assert.equal(thirdMarker.line, 2, 'third marker should be restored'); + }); }); }); diff --git a/src/Buffer.ts b/src/Buffer.ts index 27662708df..5b9af7428d 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { CircularList } from './common/CircularList'; +import { CircularList, IInsertEvent, IDeleteEvent } from './common/CircularList'; import { CharData, ITerminal, IBuffer, IBufferLine, BufferIndex, IBufferStringIterator, IBufferStringIteratorResult, IBufferLineConstructor } from './Types'; import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; @@ -238,7 +238,7 @@ export class Buffer implements IBuffer { this.scrollBottom = newRows - 1; - if (this.hasScrollback && this._bufferLineConstructor === BufferLine) { + if (this._hasScrollback && this._bufferLineConstructor === BufferLine) { this._reflow(newCols); // Trim the end of the line off if cols shrunk @@ -338,6 +338,13 @@ export class Buffer implements IBuffer { for (let i = 0; i < this.lines.length; i++) { if (nextToRemoveStart === i) { const countToRemove = toRemove[++nextToRemoveIndex]; + + // Tell markers that there was a deletion + this.lines.emit('delete', { + index: i - countRemovedSoFar, + amount: countToRemove + } as IDeleteEvent); + i += countToRemove - 1; countRemovedSoFar += countToRemove; nextToRemoveStart = toRemove[++nextToRemoveIndex]; @@ -474,6 +481,10 @@ export class Buffer implements IBuffer { // than earlier so that it's a single O(n) pass through the buffer, instead of O(n^2) from many // costly calls to CircularList.splice. if (toInsert.length > 0) { + // Record buffer insert events and then play them back backwards so that the indexes are + // correct + const insertEvents: IInsertEvent[] = []; + // Record original lines so they don't get overridden when we rearrange the list const originalLines: BufferLine[] = []; for (let i = 0; i < this.lines.length; i++) { @@ -492,14 +503,32 @@ export class Buffer implements IBuffer { for (let nextI = nextToInsert.newLines.length - 1; nextI >= 0; nextI--) { this.lines.set(i--, nextToInsert.newLines[nextI]); } - i++; // Don't skip for the first row + i++; + + // Create insert events for later + insertEvents.push({ + index: originalLineIndex + 1, + amount: nextToInsert.newLines.length + } as IInsertEvent); + countInsertedSoFar += nextToInsert.newLines.length; nextToInsert = toInsert[++nextToInsertIndex]; } else { this.lines.set(i, originalLines[originalLineIndex--]); } } - // TODO: Throw trim event + + // Update markers + let insertCountEmitted = 0; + for (let i = insertEvents.length - 1; i >= 0; i--) { + insertEvents[i].index += insertCountEmitted; + this.lines.emit('insert', insertEvents[i]); + insertCountEmitted += insertEvents[i].amount; + } + const amountToTrim = Math.max(0, originalLinesLength + countToInsert - this.lines.maxLength); + if (amountToTrim > 0) { + this.lines.emitMayRemoveListeners('trim', amountToTrim); + } } } @@ -618,12 +647,27 @@ export class Buffer implements IBuffer { marker.dispose(); } })); + marker.register(this.lines.addDisposableListener('insert', (event: IInsertEvent) => { + if (marker.line >= event.index) { + marker.line += event.amount; + } + })); + marker.register(this.lines.addDisposableListener('delete', (event: IDeleteEvent) => { + // Delete the marker if it's within the range + if (marker.line >= event.index && marker.line < event.index + event.amount) { + marker.dispose(); + } + + // Shift the marker if it's after the deleted range + if (marker.line > event.index) { + marker.line -= event.amount; + } + })); marker.register(marker.addDisposableListener('dispose', () => this._removeMarker(marker))); return marker; } private _removeMarker(marker: Marker): void { - // TODO: This could probably be optimized by relying on sort order and trimming the array using .length this.markers.splice(this.markers.indexOf(marker), 1); } diff --git a/src/common/CircularList.ts b/src/common/CircularList.ts index 9faf534aef..af4d8af507 100644 --- a/src/common/CircularList.ts +++ b/src/common/CircularList.ts @@ -6,6 +6,16 @@ import { EventEmitter } from './EventEmitter'; import { ICircularList } from './Types'; +export interface IInsertEvent { + index: number; + amount: number; +} + +export interface IDeleteEvent { + index: number; + amount: number; +} + /** * Represents a circular list; a list with a maximum size that wraps around when push is called, * overriding values at the start of the list. @@ -91,7 +101,7 @@ export class CircularList extends EventEmitter implements ICircularList { this._array[this._getCyclicIndex(this._length)] = value; if (this._length === this._maxLength) { this._startIndex = ++this._startIndex % this._maxLength; - this.emit('trim', 1); + this.emitMayRemoveListeners('trim', 1); } else { this._length++; } @@ -107,7 +117,7 @@ export class CircularList extends EventEmitter implements ICircularList { throw new Error('Can only recycle when the buffer is full'); } this._startIndex = ++this._startIndex % this._maxLength; - this.emit('trim', 1); + this.emitMayRemoveListeners('trim', 1); return this._array[this._getCyclicIndex(this._length - 1)]!; } @@ -158,7 +168,7 @@ export class CircularList extends EventEmitter implements ICircularList { const countToTrim = (this._length + items.length) - this._maxLength; this._startIndex += countToTrim; this._length = this._maxLength; - this.emit('trim', countToTrim); + this.emitMayRemoveListeners('trim', countToTrim); } else { this._length += items.length; } @@ -175,7 +185,7 @@ export class CircularList extends EventEmitter implements ICircularList { } this._startIndex += count; this._length -= count; - this.emit('trim', count); + this.emitMayRemoveListeners('trim', count); } public shiftElements(start: number, count: number, offset: number): void { @@ -199,7 +209,7 @@ export class CircularList extends EventEmitter implements ICircularList { while (this._length > this._maxLength) { this._length--; this._startIndex++; - this.emit('trim', 1); + this.emitMayRemoveListeners('trim', 1); } } } else { diff --git a/src/common/EventEmitter.ts b/src/common/EventEmitter.ts index f9c0c0014e..c6b09901bb 100644 --- a/src/common/EventEmitter.ts +++ b/src/common/EventEmitter.ts @@ -75,6 +75,19 @@ export class EventEmitter extends Disposable implements IEventEmitter, IDisposab } } + public emitMayRemoveListeners(type: string, ...args: any[]): void { + if (!this._events[type]) { + return; + } + const obj = this._events[type]; + let length = obj.length; + for (let i = 0; i < obj.length; i++) { + obj[i].apply(this, args); + i -= length - obj.length; + length = obj.length; + } + } + public listeners(type: string): XtermListener[] { return this._events[type] || []; } From 6559931f34239fb0dd1c9e26b4b1cedc1c6c5294 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 14:28:52 -0800 Subject: [PATCH 40/61] Add lots of tests --- src/Buffer.test.ts | 393 +++++++++++++++++++++++++++++++++++++++++++++ src/Buffer.ts | 2 +- 2 files changed, 394 insertions(+), 1 deletion(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 7abf6f726d..81d4484ba2 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -510,6 +510,399 @@ describe('Buffer', () => { assert.equal(secondMarker.line, 1, 'second marker should be restored'); assert.equal(thirdMarker.line, 2, 'third marker should be restored'); }); + + describe('reflowLarger cases', () => { + beforeEach(() => { + // Setup buffer state: + // 'ab' + // 'cd' (wrapped) + // 'ef' + // 'gh' (wrapped) + // 'ij' + // 'kl' (wrapped) + // ' ' + // ' ' + // ' ' + // ' ' + buffer.fillViewportRows(); + buffer.resize(2, 10); + buffer.lines.get(0).set(0, [null, 'a', 1, 'a'.charCodeAt(0)]); + buffer.lines.get(0).set(1, [null, 'b', 1, 'b'.charCodeAt(0)]); + buffer.lines.get(1).set(0, [null, 'c', 1, 'c'.charCodeAt(0)]); + buffer.lines.get(1).set(1, [null, 'd', 1, 'd'.charCodeAt(0)]); + buffer.lines.get(1).isWrapped = true; + buffer.lines.get(2).set(0, [null, 'e', 1, 'e'.charCodeAt(0)]); + buffer.lines.get(2).set(1, [null, 'f', 1, 'f'.charCodeAt(0)]); + buffer.lines.get(3).set(0, [null, 'g', 1, 'g'.charCodeAt(0)]); + buffer.lines.get(3).set(1, [null, 'h', 1, 'h'.charCodeAt(0)]); + buffer.lines.get(3).isWrapped = true; + buffer.lines.get(4).set(0, [null, 'i', 1, 'i'.charCodeAt(0)]); + buffer.lines.get(4).set(1, [null, 'j', 1, 'j'.charCodeAt(0)]); + buffer.lines.get(5).set(0, [null, 'k', 1, 'k'.charCodeAt(0)]); + buffer.lines.get(5).set(1, [null, 'l', 1, 'l'.charCodeAt(0)]); + buffer.lines.get(5).isWrapped = true; + }); + describe('viewport not yet filled', () => { + it('should move the cursor up and add empty lines', () => { + buffer.y = 6; + buffer.resize(4, 10); + assert.equal(buffer.y, 3); + assert.equal(buffer.ydisp, 0); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcd'); + assert.equal(buffer.lines.get(1).translateToString(), 'efgh'); + assert.equal(buffer.lines.get(2).translateToString(), 'ijkl'); + for (let i = 3; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines: number[] = []; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('viewport filled, scrollback remaining', () => { + beforeEach(() => { + buffer.y = 9; + }); + describe('ybase === 0', () => { + it('should move the cursor up and add empty lines', () => { + buffer.resize(4, 10); + assert.equal(buffer.y, 6); + assert.equal(buffer.ydisp, 0); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'abcd'); + assert.equal(buffer.lines.get(1).translateToString(), 'efgh'); + assert.equal(buffer.lines.get(2).translateToString(), 'ijkl'); + for (let i = 3; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines: number[] = []; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('ybase !== 0', () => { + beforeEach(() => { + // Add 10 empty rows to start + for (let i = 0; i < 10; i++) { + buffer.lines.splice(0, 0, buffer.getBlankLine(DEFAULT_ATTR)); + } + buffer.ybase = 10; + }); + describe('&& ydisp === ybase', () => { + it('should adjust the viewport and keep ydisp = ybase', () => { + buffer.ydisp = 10; + buffer.resize(4, 10); + assert.equal(buffer.y, 9); + assert.equal(buffer.ydisp, 7); + assert.equal(buffer.ybase, 7); + assert.equal(buffer.lines.length, 17); + for (let i = 0; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(10).translateToString(), 'abcd'); + assert.equal(buffer.lines.get(11).translateToString(), 'efgh'); + assert.equal(buffer.lines.get(12).translateToString(), 'ijkl'); + for (let i = 13; i < 17; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines: number[] = []; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('&& ydisp !== ybase', () => { + it('should keep ydisp at the same value', () => { + buffer.ydisp = 5; + buffer.resize(4, 10); + assert.equal(buffer.y, 9); + assert.equal(buffer.ydisp, 5); + assert.equal(buffer.ybase, 7); + assert.equal(buffer.lines.length, 17); + for (let i = 0; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(10).translateToString(), 'abcd'); + assert.equal(buffer.lines.get(11).translateToString(), 'efgh'); + assert.equal(buffer.lines.get(12).translateToString(), 'ijkl'); + for (let i = 13; i < 17; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines: number[] = []; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + }); + }); + describe('viewport filled, no scrollback remaining', () => { + // ybase === 0 doesn't make sense here as scrollback=0 isn't really supported + describe('ybase !== 0', () => { + beforeEach(() => { + terminal.options.scrollback = 10; + // Add 10 empty rows to start + for (let i = 0; i < 10; i++) { + buffer.lines.splice(0, 0, buffer.getBlankLine(DEFAULT_ATTR)); + } + buffer.y = 9; + buffer.ybase = 10; + }); + describe('&& ydisp === ybase', () => { + it('should trim lines and keep ydisp = ybase', () => { + buffer.ydisp = 10; + buffer.resize(4, 10); + assert.equal(buffer.y, 9); + assert.equal(buffer.ydisp, 7); + assert.equal(buffer.ybase, 7); + assert.equal(buffer.lines.length, 17); + for (let i = 0; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(10).translateToString(), 'abcd'); + assert.equal(buffer.lines.get(11).translateToString(), 'efgh'); + assert.equal(buffer.lines.get(12).translateToString(), 'ijkl'); + for (let i = 13; i < 17; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines: number[] = []; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('&& ydisp !== ybase', () => { + it('should trim lines and not change ydisp', () => { + buffer.ydisp = 5; + buffer.resize(4, 10); + assert.equal(buffer.y, 9); + assert.equal(buffer.ydisp, 5); + assert.equal(buffer.ybase, 7); + assert.equal(buffer.lines.length, 17); + for (let i = 0; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(10).translateToString(), 'abcd'); + assert.equal(buffer.lines.get(11).translateToString(), 'efgh'); + assert.equal(buffer.lines.get(12).translateToString(), 'ijkl'); + for (let i = 13; i < 17; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines: number[] = []; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + }); + }); + }); + describe('reflowSmaller cases', () => { + beforeEach(() => { + // Setup buffer state: + // 'abcd' + // 'efgh' (wrapped) + // 'ijkl' + // ' ' + // ' ' + // ' ' + // ' ' + // ' ' + // ' ' + // ' ' + buffer.fillViewportRows(); + buffer.resize(4, 10); + buffer.lines.get(0).set(0, [null, 'a', 1, 'a'.charCodeAt(0)]); + buffer.lines.get(0).set(1, [null, 'b', 1, 'b'.charCodeAt(0)]); + buffer.lines.get(0).set(2, [null, 'c', 1, 'c'.charCodeAt(0)]); + buffer.lines.get(0).set(3, [null, 'd', 1, 'd'.charCodeAt(0)]); + buffer.lines.get(1).set(0, [null, 'e', 1, 'e'.charCodeAt(0)]); + buffer.lines.get(1).set(1, [null, 'f', 1, 'f'.charCodeAt(0)]); + buffer.lines.get(1).set(2, [null, 'g', 1, 'g'.charCodeAt(0)]); + buffer.lines.get(1).set(3, [null, 'h', 1, 'h'.charCodeAt(0)]); + buffer.lines.get(2).set(0, [null, 'i', 1, 'i'.charCodeAt(0)]); + buffer.lines.get(2).set(1, [null, 'j', 1, 'j'.charCodeAt(0)]); + buffer.lines.get(2).set(2, [null, 'k', 1, 'k'.charCodeAt(0)]); + buffer.lines.get(2).set(3, [null, 'l', 1, 'l'.charCodeAt(0)]); + }); + describe('viewport not yet filled', () => { + it('should move the cursor down', () => { + buffer.y = 3; + buffer.resize(2, 10); + assert.equal(buffer.y, 6); + assert.equal(buffer.ydisp, 0); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'cd'); + assert.equal(buffer.lines.get(2).translateToString(), 'ef'); + assert.equal(buffer.lines.get(3).translateToString(), 'gh'); + assert.equal(buffer.lines.get(4).translateToString(), 'ij'); + assert.equal(buffer.lines.get(5).translateToString(), 'kl'); + for (let i = 6; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines = [1, 3, 5]; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('viewport filled, scrollback remaining', () => { + beforeEach(() => { + buffer.y = 9; + }); + describe('ybase === 0', () => { + it('should trim the top', () => { + buffer.resize(2, 10); + assert.equal(buffer.y, 9); + assert.equal(buffer.ydisp, 3); + assert.equal(buffer.ybase, 3); + assert.equal(buffer.lines.length, 13); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'cd'); + assert.equal(buffer.lines.get(2).translateToString(), 'ef'); + assert.equal(buffer.lines.get(3).translateToString(), 'gh'); + assert.equal(buffer.lines.get(4).translateToString(), 'ij'); + assert.equal(buffer.lines.get(5).translateToString(), 'kl'); + for (let i = 6; i < 13; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines = [1, 3, 5]; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('ybase !== 0', () => { + beforeEach(() => { + // Add 10 empty rows to start + for (let i = 0; i < 10; i++) { + buffer.lines.splice(0, 0, buffer.getBlankLine(DEFAULT_ATTR)); + } + buffer.ybase = 10; + }); + describe('&& ydisp === ybase', () => { + it('should adjust the viewport and keep ydisp = ybase', () => { + buffer.ydisp = 10; + buffer.resize(2, 10); + assert.equal(buffer.ydisp, 13); + assert.equal(buffer.ybase, 13); + assert.equal(buffer.lines.length, 23); + for (let i = 0; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(10).translateToString(), 'ab'); + assert.equal(buffer.lines.get(11).translateToString(), 'cd'); + assert.equal(buffer.lines.get(12).translateToString(), 'ef'); + assert.equal(buffer.lines.get(13).translateToString(), 'gh'); + assert.equal(buffer.lines.get(14).translateToString(), 'ij'); + assert.equal(buffer.lines.get(15).translateToString(), 'kl'); + for (let i = 16; i < 23; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines = [11, 13, 15]; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('&& ydisp !== ybase', () => { + it('should keep ydisp at the same value', () => { + buffer.ydisp = 5; + buffer.resize(2, 10); + assert.equal(buffer.ydisp, 5); + assert.equal(buffer.ybase, 13); + assert.equal(buffer.lines.length, 23); + for (let i = 0; i < 10; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(10).translateToString(), 'ab'); + assert.equal(buffer.lines.get(11).translateToString(), 'cd'); + assert.equal(buffer.lines.get(12).translateToString(), 'ef'); + assert.equal(buffer.lines.get(13).translateToString(), 'gh'); + assert.equal(buffer.lines.get(14).translateToString(), 'ij'); + assert.equal(buffer.lines.get(15).translateToString(), 'kl'); + for (let i = 16; i < 23; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines = [11, 13, 15]; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + }); + }); + describe('viewport filled, no scrollback remaining', () => { + // ybase === 0 doesn't make sense here as scrollback=0 isn't really supported + describe('ybase !== 0', () => { + beforeEach(() => { + terminal.options.scrollback = 10; + // Add 10 empty rows to start + for (let i = 0; i < 10; i++) { + buffer.lines.splice(0, 0, buffer.getBlankLine(DEFAULT_ATTR)); + } + buffer.ybase = 10; + }); + describe('&& ydisp === ybase', () => { + it('should trim lines and keep ydisp = ybase', () => { + buffer.ydisp = 10; + buffer.resize(2, 10); + assert.equal(buffer.ydisp, 10); + assert.equal(buffer.ybase, 10); + assert.equal(buffer.lines.length, 20); + for (let i = 0; i < 7; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(7).translateToString(), 'ab'); + assert.equal(buffer.lines.get(8).translateToString(), 'cd'); + assert.equal(buffer.lines.get(9).translateToString(), 'ef'); + assert.equal(buffer.lines.get(10).translateToString(), 'gh'); + assert.equal(buffer.lines.get(11).translateToString(), 'ij'); + assert.equal(buffer.lines.get(12).translateToString(), 'kl'); + for (let i = 13; i < 20; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines = [8, 10, 12]; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + describe('&& ydisp !== ybase', () => { + it('should trim lines and not change ydisp', () => { + buffer.ydisp = 5; + buffer.resize(2, 10); + assert.equal(buffer.ydisp, 5); + assert.equal(buffer.ybase, 10); + assert.equal(buffer.lines.length, 20); + for (let i = 0; i < 7; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + assert.equal(buffer.lines.get(7).translateToString(), 'ab'); + assert.equal(buffer.lines.get(8).translateToString(), 'cd'); + assert.equal(buffer.lines.get(9).translateToString(), 'ef'); + assert.equal(buffer.lines.get(10).translateToString(), 'gh'); + assert.equal(buffer.lines.get(11).translateToString(), 'ij'); + assert.equal(buffer.lines.get(12).translateToString(), 'kl'); + for (let i = 13; i < 20; i++) { + assert.equal(buffer.lines.get(i).translateToString(), ' '); + } + const wrappedLines = [8, 10, 12]; + for (let i = 0; i < buffer.lines.length; i++) { + assert.equal(buffer.lines.get(i).isWrapped, wrappedLines.indexOf(i) !== -1, `line ${i} isWrapped must equal ${wrappedLines.indexOf(i) !== -1}`); + } + }); + }); + }); + }); + }); }); }); diff --git a/src/Buffer.ts b/src/Buffer.ts index 5b9af7428d..861471b5bd 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -470,9 +470,9 @@ export class Buffer implements IBuffer { } } else { if (this.ybase === this.ydisp) { - this.ybase++; this.ydisp++; } + this.ybase++; } } } From 2ce67b89bcb62ccf95d34ceb285d02e2f1ed4a6c Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 14:43:50 -0800 Subject: [PATCH 41/61] Remove unneeded MockTerminal member --- src/ui/TestUtils.test.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/ui/TestUtils.test.ts b/src/ui/TestUtils.test.ts index 3b59ee5d69..10033a3355 100644 --- a/src/ui/TestUtils.test.ts +++ b/src/ui/TestUtils.test.ts @@ -19,9 +19,6 @@ export class TestTerminal extends Terminal { } export class MockTerminal implements ITerminal { - eraseAttr(): number { - throw new Error('Method not implemented.'); - } markers: IMarker[]; addMarker(cursorYOffset: number): IMarker { throw new Error('Method not implemented.'); From 8fbeadd5f9f1b3c3eff1a82ce869d1e0c9e1a2f8 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Tue, 1 Jan 2019 09:45:23 -0800 Subject: [PATCH 42/61] Add missing deleteCount argument to Array.splice calls. --- src/EscapeSequenceParser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 70c1c6c8ef..6d3de0e0ed 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -322,7 +322,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP dispose: () => { const handlerIndex = handlerList.indexOf(callback); if (handlerIndex !== -1) { - handlerList.splice(handlerIndex); + handlerList.splice(handlerIndex, 1); } } }; @@ -357,7 +357,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP dispose: () => { const handlerIndex = handlerList.indexOf(callback); if (handlerIndex !== -1) { - handlerList.splice(handlerIndex); + handlerList.splice(handlerIndex, 1); } } }; From 740ca9ce6d172c9bd37ca9bca82195cc9c36afcf Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 2 Jan 2019 14:23:09 -0800 Subject: [PATCH 43/61] v3.10.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0d797f789e..5ed3a4b800 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "xterm", "description": "Full xterm terminal, in your browser", - "version": "3.9.0", + "version": "3.10.0", "main": "lib/public/Terminal.js", "types": "typings/xterm.d.ts", "repository": "https://github.com/xtermjs/xterm.js", From c668bb1243eb35008fe5f405537ef400caf69297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 3 Jan 2019 16:08:40 +0100 Subject: [PATCH 44/61] remove js array based buffer line --- demo/client.ts | 3 +- src/Buffer.ts | 36 ++----------- src/BufferLine.ts | 127 +-------------------------------------------- src/Terminal.ts | 35 ++++--------- src/Types.ts | 4 -- typings/xterm.d.ts | 11 ---- 6 files changed, 16 insertions(+), 200 deletions(-) diff --git a/demo/client.ts b/demo/client.ts index acc83cb86f..a3a912f630 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -205,8 +205,7 @@ function initOptions(term: TerminalType): void { fontFamily: null, fontWeight: ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'], fontWeightBold: ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'], - rendererType: ['dom', 'canvas'], - experimentalBufferLineImpl: ['JsArray', 'TypedArray'] + rendererType: ['dom', 'canvas'] }; const options = Object.keys((term)._core.options); const booleanOptions = []; diff --git a/src/Buffer.ts b/src/Buffer.ts index 74750a8b4e..625a2497e7 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -4,10 +4,10 @@ */ import { CircularList } from './common/CircularList'; -import { CharData, ITerminal, IBuffer, IBufferLine, BufferIndex, IBufferStringIterator, IBufferStringIteratorResult, IBufferLineConstructor } from './Types'; +import { CharData, ITerminal, IBuffer, IBufferLine, BufferIndex, IBufferStringIterator, IBufferStringIteratorResult } from './Types'; import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; -import { BufferLine, BufferLineJSArray } from './BufferLine'; +import { BufferLine } from './BufferLine'; import { DEFAULT_COLOR } from './renderer/atlas/Types'; export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); @@ -45,7 +45,6 @@ export class Buffer implements IBuffer { public savedX: number; public savedCurAttr: number; public markers: Marker[] = []; - private _bufferLineConstructor: IBufferLineConstructor; /** * Create a new Buffer. @@ -60,35 +59,9 @@ export class Buffer implements IBuffer { this.clear(); } - public setBufferLineFactory(type: string): void { - if (type === 'JsArray') { - if (this._bufferLineConstructor !== BufferLineJSArray) { - this._bufferLineConstructor = BufferLineJSArray; - this._recreateLines(); - } - } else { - if (this._bufferLineConstructor !== BufferLine) { - this._bufferLineConstructor = BufferLine; - this._recreateLines(); - } - } - } - - private _recreateLines(): void { - if (!this.lines) return; - for (let i = 0; i < this.lines.length; ++i) { - const oldLine = this.lines.get(i); - const newLine = new this._bufferLineConstructor(oldLine.length); - for (let j = 0; j < oldLine.length; ++j) { - newLine.set(j, oldLine.get(j)); - } - this.lines.set(i, newLine); - } - } - public getBlankLine(attr: number, isWrapped?: boolean): IBufferLine { const fillCharData: CharData = [attr, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - return new this._bufferLineConstructor(this._terminal.cols, fillCharData, isWrapped); + return new BufferLine(this._terminal.cols, fillCharData, isWrapped); } public get hasScrollback(): boolean { @@ -135,7 +108,6 @@ export class Buffer implements IBuffer { * Clears the buffer to it's initial state, discarding all previous data. */ public clear(): void { - this.setBufferLineFactory(this._terminal.options.experimentalBufferLineImpl); this.ydisp = 0; this.ybase = 0; this.y = 0; @@ -188,7 +160,7 @@ export class Buffer implements IBuffer { // Add a blank line if there is no buffer left at the top to scroll to, or if there // are blank lines after the cursor const fillCharData: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - this.lines.push(new this._bufferLineConstructor(newCols, fillCharData)); + this.lines.push(new BufferLine(newCols, fillCharData)); } } } diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 3f93af626a..226c4a87eb 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -3,133 +3,8 @@ * @license MIT */ import { CharData, IBufferLine } from './Types'; -import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, WHITESPACE_CELL_CHAR } from './Buffer'; +import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, WHITESPACE_CELL_CHAR } from './Buffer'; -/** - * Class representing a terminal line. - * - * @deprecated to be removed with one of the next releases - */ -export class BufferLineJSArray implements IBufferLine { - protected _data: CharData[]; - public isWrapped = false; - public length: number; - - constructor(cols: number, fillCharData?: CharData, isWrapped?: boolean) { - this._data = []; - if (!fillCharData) { - fillCharData = [0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - } - for (let i = 0; i < cols; i++) { - this._push(fillCharData); // Note: the ctor ch is not cloned (resembles old behavior) - } - if (isWrapped) { - this.isWrapped = true; - } - this.length = this._data.length; - } - - private _pop(): CharData | undefined { - const data = this._data.pop(); - this.length = this._data.length; - return data; - } - - private _push(data: CharData): void { - this._data.push(data); - this.length = this._data.length; - } - - private _splice(start: number, deleteCount: number, ...items: CharData[]): CharData[] { - const removed = this._data.splice(start, deleteCount, ...items); - this.length = this._data.length; - return removed; - } - - public get(index: number): CharData { - return this._data[index]; - } - - public set(index: number, data: CharData): void { - this._data[index] = data; - } - - /** insert n cells ch at pos, right cells are lost (stable length) */ - public insertCells(pos: number, n: number, ch: CharData): void { - while (n--) { - this._splice(pos, 0, ch); - this._pop(); - } - } - - /** delete n cells at pos, right side is filled with fill (stable length) */ - public deleteCells(pos: number, n: number, fillCharData: CharData): void { - while (n--) { - this._splice(pos, 1); - this._push(fillCharData); - } - } - - /** replace cells from pos to pos + n - 1 with fill */ - public replaceCells(start: number, end: number, fillCharData: CharData): void { - while (start < end && start < this.length) { - this.set(start++, fillCharData); // Note: fill is not cloned (resembles old behavior) - } - } - - /** resize line to cols filling new cells with fill */ - public resize(cols: number, fillCharData: CharData, shrink: boolean = false): void { - while (this._data.length < cols) { - this._data.push(fillCharData); - } - if (shrink) { - while (this._data.length > cols) { - this._data.pop(); - } - } - this.length = this._data.length; - } - - public fill(fillCharData: CharData): void { - for (let i = 0; i < this.length; ++i) { - this.set(i, fillCharData); - } - } - - public copyFrom(line: BufferLineJSArray): void { - this._data = line._data.slice(0); - this.length = line.length; - this.isWrapped = line.isWrapped; - } - - public clone(): IBufferLine { - const newLine = new BufferLineJSArray(0); - newLine.copyFrom(this); - return newLine; - } - - public getTrimmedLength(): number { - for (let i = this.length - 1; i >= 0; --i) { - const ch = this.get(i); - if (ch[CHAR_DATA_CHAR_INDEX] !== '') { - return i + ch[CHAR_DATA_WIDTH_INDEX]; - } - } - return 0; - } - - public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = this.length): string { - if (trimRight) { - endCol = Math.min(endCol, this.getTrimmedLength()); - } - let result = ''; - while (startCol < endCol) { - result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; - startCol += this.get(startCol)[CHAR_DATA_WIDTH_INDEX] || 1; - } - return result; - } -} /** typed array slots taken by one cell */ const CELL_SIZE = 3; diff --git a/src/Terminal.ts b/src/Terminal.ts index bed45e46ec..bc97de29ff 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -105,8 +105,7 @@ const DEFAULT_OPTIONS: ITerminalOptions = { tabStopWidth: 8, theme: null, rightClickSelectsWord: Browser.isMac, - rendererType: 'canvas', - experimentalBufferLineImpl: 'TypedArray' + rendererType: 'canvas' }; export class Terminal extends EventEmitter implements ITerminal, IDisposable, IInputHandlingTerminal { @@ -497,11 +496,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II } break; case 'tabStopWidth': this.buffers.setupTabStops(); break; - case 'experimentalBufferLineImpl': - this.buffers.normal.setBufferLineFactory(value); - this.buffers.alt.setBufferLineFactory(value); - this._blankLine = null; - break; } // Inform renderer of changes if (this.renderer) { @@ -1180,17 +1174,12 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II */ public scroll(isWrapped: boolean = false): void { let newLine: IBufferLine; - const useRecycling = this.options.experimentalBufferLineImpl !== 'JsArray'; - if (useRecycling) { - newLine = this._blankLine; - if (!newLine || newLine.length !== this.cols || newLine.get(0)[CHAR_DATA_ATTR_INDEX] !== this.eraseAttr()) { - newLine = this.buffer.getBlankLine(this.eraseAttr(), isWrapped); - this._blankLine = newLine; - } - newLine.isWrapped = isWrapped; - } else { + newLine = this._blankLine; + if (!newLine || newLine.length !== this.cols || newLine.get(0)[CHAR_DATA_ATTR_INDEX] !== this.eraseAttr()) { newLine = this.buffer.getBlankLine(this.eraseAttr(), isWrapped); + this._blankLine = newLine; } + newLine.isWrapped = isWrapped; const topRow = this.buffer.ybase + this.buffer.scrollTop; const bottomRow = this.buffer.ybase + this.buffer.scrollBottom; @@ -1201,17 +1190,13 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // Insert the line using the fastest method if (bottomRow === this.buffer.lines.length - 1) { - if (useRecycling) { - if (willBufferBeTrimmed) { - this.buffer.lines.recycle().copyFrom(newLine); - } else { - this.buffer.lines.push(newLine.clone()); - } + if (willBufferBeTrimmed) { + this.buffer.lines.recycle().copyFrom(newLine); } else { - this.buffer.lines.push(newLine); + this.buffer.lines.push(newLine.clone()); } } else { - this.buffer.lines.splice(bottomRow + 1, 0, (useRecycling) ? newLine.clone() : newLine); + this.buffer.lines.splice(bottomRow + 1, 0, newLine.clone()); } // Only adjust ybase and ydisp when the buffer is not trimmed @@ -1233,7 +1218,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // scrollback, instead we can just shift them in-place. const scrollRegionHeight = bottomRow - topRow + 1/*as it's zero-based*/; this.buffer.lines.shiftElements(topRow + 1, scrollRegionHeight - 1, -1); - this.buffer.lines.set(bottomRow, (useRecycling) ? newLine.clone() : newLine); + this.buffer.lines.set(bottomRow, newLine.clone()); } // Move the viewport to the bottom of the buffer unless the user is diff --git a/src/Types.ts b/src/Types.ts index b54b9d6610..c6395ea3ca 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -529,7 +529,3 @@ export interface IBufferLine { getTrimmedLength(): number; translateToString(trimRight?: boolean, startCol?: number, endCol?: number): string; } - -export interface IBufferLineConstructor { - new(cols: number, fillCharData?: CharData, isWrapped?: boolean): IBufferLine; -} diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 0ceab01dcc..c5d2b0204e 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -101,17 +101,6 @@ declare module 'xterm' { */ experimentalCharAtlas?: 'none' | 'static' | 'dynamic'; - /** - * (EXPERIMENTAL) Defines which implementation to use for buffer lines. - * - * - 'JsArray': The default/stable implementation. - * - 'TypedArray': The new experimental implementation based on TypedArrays that is expected to - * significantly boost performance and memory consumption. Use at your own risk. - * - * @deprecated This option will be removed in the future. - */ - experimentalBufferLineImpl?: 'JsArray' | 'TypedArray'; - /** * The font size used to render text. */ From 7a416aa18ffb395c716d622dcde275bacc157c59 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 3 Jan 2019 22:49:03 -0800 Subject: [PATCH 45/61] Don't print keys that print more than a single char Fixes #1880 --- src/core/input/Keyboard.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index 9d86b34942..4fea8adb76 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -349,9 +349,8 @@ export function evaluateKeyboardEvent( if (ev.keyCode === 65) { // cmd + a result.type = KeyboardResultType.SELECT_ALL; } - } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && - ev.keyCode >= 48 && ev.keyCode !== 144 && ev.keyCode !== 145) { - // Include only keys that that result in a character; don't include num lock and scroll lock + } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 48 && ev.key.length === 1) { + // Include only keys that that result in a _single_ character; don't include num lock, volume up, etc. result.key = ev.key; } break; From 6581eb7d88015ac5b499d576436133d37ca51ae3 Mon Sep 17 00:00:00 2001 From: jerch Date: Sat, 5 Jan 2019 01:47:44 +0100 Subject: [PATCH 46/61] fix leftover BufferLineConstructor --- src/Buffer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 74cec10769..987e03247a 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -210,7 +210,7 @@ export class Buffer implements IBuffer { this.scrollBottom = newRows - 1; - if (this._hasScrollback && this._bufferLineConstructor === BufferLine) { + if (this._hasScrollback) { this._reflow(newCols); // Trim the end of the line off if cols shrunk @@ -343,7 +343,7 @@ export class Buffer implements IBuffer { if (this.ybase === 0) { this.y--; // Add an extra row at the bottom of the viewport - this.lines.push(new this._bufferLineConstructor(newCols, FILL_CHAR_DATA)); + this.lines.push(new BufferLine(newCols, FILL_CHAR_DATA)); } else { if (this.ydisp === this.ybase) { this.ydisp--; From f090fa822d145bb1b4414819a39b50d9233433dd Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Sun, 6 Jan 2019 15:07:36 -0800 Subject: [PATCH 47/61] doc: fix typos --- src/Terminal.ts | 2 +- src/common/EventEmitter.ts | 2 +- src/ui/Lifecycle.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Terminal.ts b/src/Terminal.ts index bc97de29ff..4c0cd0f8a4 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -1391,7 +1391,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II * processed by the terminal and what keys should not. * @param customKeyEventHandler The custom KeyboardEvent handler to attach. * This is a function that takes a KeyboardEvent, allowing consumers to stop - * propogation and/or prevent the default action. The function returns whether + * propagation and/or prevent the default action. The function returns whether * the event should be processed by xterm.js. */ public attachCustomKeyEventHandler(customKeyEventHandler: CustomKeyEventHandler): void { diff --git a/src/common/EventEmitter.ts b/src/common/EventEmitter.ts index f9c0c0014e..fb95ae9214 100644 --- a/src/common/EventEmitter.ts +++ b/src/common/EventEmitter.ts @@ -23,7 +23,7 @@ export class EventEmitter extends Disposable implements IEventEmitter, IDisposab } /** - * Adds a disposabe listener to the EventEmitter, returning the disposable. + * Adds a disposable listener to the EventEmitter, returning the disposable. * @param type The event type. * @param handler The handler for the listener. */ diff --git a/src/ui/Lifecycle.ts b/src/ui/Lifecycle.ts index d836711386..9f058106a6 100644 --- a/src/ui/Lifecycle.ts +++ b/src/ui/Lifecycle.ts @@ -6,7 +6,7 @@ import { IDisposable } from 'xterm'; /** - * Adds a disposabe listener to a node in the DOM, returning the disposable. + * Adds a disposable listener to a node in the DOM, returning the disposable. * @param type The event type. * @param handler The handler for the listener. */ From f04f2efc52b8dad12ca9fab46d47981b8ee9a9f7 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Sun, 6 Jan 2019 15:09:57 -0800 Subject: [PATCH 48/61] doc: fix more typos --- src/CompositionHelper.ts | 12 ++++++------ src/SelectionManager.ts | 2 +- src/core/input/Keyboard.ts | 2 +- typings/xterm.d.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/CompositionHelper.ts b/src/CompositionHelper.ts index b1745d41ed..31ad866b48 100644 --- a/src/CompositionHelper.ts +++ b/src/CompositionHelper.ts @@ -111,17 +111,17 @@ export class CompositionHelper { /** * Finalizes the composition, resuming regular input actions. This is called when a composition * is ending. - * @param waitForPropogation Whether to wait for events to propogate before sending + * @param waitForPropagation Whether to wait for events to propagate before sending * the input. This should be false if a non-composition keystroke is entered before the - * compositionend event is triggered, such as enter, so that the composition is send before + * compositionend event is triggered, such as enter, so that the composition is sent before * the command is executed. */ - private _finalizeComposition(waitForPropogation: boolean): void { + private _finalizeComposition(waitForPropagation: boolean): void { this._compositionView.classList.remove('active'); this._isComposing = false; this._clearTextareaPosition(); - if (!waitForPropogation) { + if (!waitForPropagation) { // Cancel any delayed composition send requests and send the input immediately. this._isSendingComposition = false; const input = this._textarea.value.substring(this._compositionPosition.start, this._compositionPosition.end); @@ -136,8 +136,8 @@ export class CompositionHelper { // Since composition* events happen before the changes take place in the textarea on most // browsers, use a setTimeout with 0ms time to allow the native compositionend event to - // complete. This ensures the correct character is retrieved, this solution was used - // because: + // complete. This ensures the correct character is retrieved. + // This solution was used because: // - The compositionend event's data property is unreliable, at least on Chromium // - The last compositionupdate event's data property does not always accurately describe // the character, a counter example being Korean where an ending consonsant can move to diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 1aea1cb53a..f93328fe6f 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -552,7 +552,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager */ private _onMouseMove(event: MouseEvent): void { // If the mousemove listener is active it means that a selection is - // currently being made, we should stop propogation to prevent mouse events + // currently being made, we should stop propagation to prevent mouse events // to be sent to the pty. event.stopImmediatePropagation(); diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index 9d86b34942..b5df46c46b 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -44,7 +44,7 @@ export function evaluateKeyboardEvent( ): IKeyboardResult { const result: IKeyboardResult = { type: KeyboardResultType.SEND_KEY, - // Whether to cancel event propogation (NOTE: this may not be needed since the event is + // Whether to cancel event propagation (NOTE: this may not be needed since the event is // canceled at the end of keyDown cancel: false, // The new key even to emit diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index c5d2b0204e..11fab9097e 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -465,7 +465,7 @@ declare module 'xterm' { * should be processed by the terminal and what keys should not. * @param customKeyEventHandler The custom KeyboardEvent handler to attach. * This is a function that takes a KeyboardEvent, allowing consumers to stop - * propogation and/or prevent the default action. The function returns + * propagation and/or prevent the default action. The function returns * whether the event should be processed by xterm.js. */ attachCustomKeyEventHandler(customKeyEventHandler: (event: KeyboardEvent) => boolean): void; From 7fe3f0a4e3e094ed82fec0b17219cc0187a3ed69 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 11 Jan 2019 12:58:41 -0800 Subject: [PATCH 49/61] Improve BufferLine test Ensure combined is cleared when shrinking and enlarging --- src/BufferLine.test.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/BufferLine.test.ts b/src/BufferLine.test.ts index f5e95fc857..0ef29505e3 100644 --- a/src/BufferLine.test.ts +++ b/src/BufferLine.test.ts @@ -158,14 +158,17 @@ describe('BufferLine', function(): void { line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)]); chai.expect(line.toArray()).eql(Array(0).fill([1, 'a', 0, 'a'.charCodeAt(0)])); }); - it('should remove combining data', () => { + it('should remove combining data on replaced cells after shrinking then enlarging', () => { const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false); + line.set(2, [ null, '😁', 1, '😁'.charCodeAt(0) ]); line.set(9, [ null, '😁', 1, '😁'.charCodeAt(0) ]); - chai.expect(line.translateToString()).eql('aaaaaaaaa😁'); - chai.expect(Object.keys(line.combined).length).eql(1); + chai.expect(line.translateToString()).eql('aa😁aaaaaa😁'); + chai.expect(Object.keys(line.combined).length).eql(2); line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)]); - chai.expect(line.translateToString()).eql('aaaaa'); - chai.expect(Object.keys(line.combined).length).eql(0); + chai.expect(line.translateToString()).eql('aa😁aa'); + line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)]); + chai.expect(line.translateToString()).eql('aa😁aaaaaaa'); + chai.expect(Object.keys(line.combined).length).eql(1); }); }); describe('getTrimLength', function(): void { From e02a9ff09e8026399f84a0eb41b427f7a4c13234 Mon Sep 17 00:00:00 2001 From: Juan Campa Date: Tue, 15 Jan 2019 11:00:34 -0500 Subject: [PATCH 50/61] Prevent charsizechanged from firing unnecessarily --- src/ui/CharMeasure.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ui/CharMeasure.ts b/src/ui/CharMeasure.ts index 2dfc4eb561..0ac755eab8 100644 --- a/src/ui/CharMeasure.ts +++ b/src/ui/CharMeasure.ts @@ -46,9 +46,10 @@ export class CharMeasure extends EventEmitter implements ICharMeasure { if (geometry.width === 0 || geometry.height === 0) { return; } - if (this._width !== geometry.width || this._height !== geometry.height) { + const adjustedHeight = Math.ceil(geometry.height); + if (this._width !== geometry.width || this._height !== adjustedHeight) { this._width = geometry.width; - this._height = Math.ceil(geometry.height); + this._height = adjustedHeight; this.emit('charsizechanged'); } } From 50abb43e9c0c4704898c8f404a954f54b82accb7 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 23 Oct 2018 15:45:52 -0700 Subject: [PATCH 51/61] Remove request term info handler We're not supporting it anyway, no point having it. --- src/InputHandler.ts | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index eb1ca1050b..8846b4be10 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -25,26 +25,6 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1, * DCS subparser implementations */ - /** - * DCS + q Pt ST (xterm) - * Request Terminfo String - * not supported - */ -class RequestTerminfo implements IDcsHandler { - private _data: string; - constructor(private _terminal: any) { } - hook(collect: string, params: number[], flag: number): void { - this._data = ''; - } - put(data: string, start: number, end: number): void { - this._data += data.substring(start, end); - } - unhook(): void { - // invalid: DCS 0 + r Pt ST - this._terminal.handler(`${C0.ESC}P0+r${this._data}${C0.ESC}\\`); - } -} - /** * DCS $ q Pt ST * DECRQSS (https://vt100.net/docs/vt510-rm/DECRQSS.html) @@ -87,7 +67,7 @@ class DECRQSS implements IDcsHandler { default: // invalid: DCS 0 $ r Pt ST (xterm) this._terminal.error('Unknown DCS $q %s', this._data); - this._terminal.handler(`${C0.ESC}P0$r${this._data}${C0.ESC}\\`); + this._terminal.handler(`${C0.ESC}P0$r${C0.ESC}\\`); } } } @@ -288,7 +268,6 @@ export class InputHandler extends Disposable implements IInputHandler { * DCS handler */ this._parser.setDcsHandler('$q', new DECRQSS(this._terminal)); - this._parser.setDcsHandler('+q', new RequestTerminfo(this._terminal)); } public dispose(): void { From 8bdb606fd1f16c95116d251ab6bbb0808870a1d2 Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Mon, 21 Jan 2019 11:09:00 +0100 Subject: [PATCH 52/61] fix mouse event listener change before term attached --- src/InputHandler.ts | 8 ++++++-- src/Terminal.ts | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 8846b4be10..1b3907fd23 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -1291,7 +1291,9 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.vt200Mouse = params[0] === 1000; this._terminal.normalMouse = params[0] > 1000; this._terminal.mouseEvents = true; - this._terminal.element.classList.add('enable-mouse-events'); + if (this._terminal.element) { + this._terminal.element.classList.add('enable-mouse-events'); + } this._terminal.selectionManager.disable(); this._terminal.log('Binding to mouse events.'); break; @@ -1479,7 +1481,9 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.vt200Mouse = false; this._terminal.normalMouse = false; this._terminal.mouseEvents = false; - this._terminal.element.classList.remove('enable-mouse-events'); + if (this._terminal.element) { + this._terminal.element.classList.remove('enable-mouse-events'); + } this._terminal.selectionManager.enable(); break; case 1004: // send focusin/focusout events diff --git a/src/Terminal.ts b/src/Terminal.ts index 4c0cd0f8a4..cf1a3ba589 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -733,6 +733,12 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.register(addDisposableDomListener(this._viewportElement, 'scroll', () => this.selectionManager.refresh())); this.mouseHelper = new MouseHelper(this.renderer); + // apply mouse event classes set by escape codes before terminal was attached + if (this.mouseEvents) { + this.element.classList.add('enable-mouse-events'); + } else { + this.element.classList.remove('enable-mouse-events'); + } if (this.options.screenReaderMode) { // Note that this must be done *after* the renderer is created in order to From 4843ca5bb879ed5e6116cba89d27e6c9c813ba40 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 21 Jan 2019 09:08:18 -0800 Subject: [PATCH 53/61] Fix reflow larger with wide chars --- src/Buffer.test.ts | 37 +++++++++++++++++++++++++++++++++++++ src/Buffer.ts | 18 +++++++++++++++--- src/BufferLine.ts | 4 ++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 81d4484ba2..040eb3ca79 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -510,6 +510,43 @@ describe('Buffer', () => { assert.equal(secondMarker.line, 1, 'second marker should be restored'); assert.equal(thirdMarker.line, 2, 'third marker should be restored'); }); + it('should wrap wide characters correctly when reflowing larger', () => { + buffer.fillViewportRows(); + buffer.resize(12, 10); + for (let i = 0; i < 12; i += 4) { + buffer.lines.get(0).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); + buffer.lines.get(1).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); + } + for (let i = 2; i < 12; i += 4) { + buffer.lines.get(0).set(i, [null, '语', 2, '语'.charCodeAt(0)]); + buffer.lines.get(1).set(i, [null, '语', 2, '语'.charCodeAt(0)]); + } + for (let i = 1; i < 12; i += 2) { + buffer.lines.get(0).set(i, [null, '', 0, undefined]); + buffer.lines.get(1).set(i, [null, '', 0, undefined]); + } + buffer.lines.get(1).isWrapped = true; + // Buffer: + // 汉语汉语汉语 (wrapped) + // 汉语汉语汉语 + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语'); + assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语汉语'); + buffer.resize(13, 10); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语'); + assert.equal(buffer.lines.get(0).translateToString(false), '汉语汉语汉语 '); + assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语汉语'); + assert.equal(buffer.lines.get(1).translateToString(false), '汉语汉语汉语 '); + buffer.resize(14, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语汉'); + assert.equal(buffer.lines.get(0).translateToString(false), '汉语汉语汉语汉'); + assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语'); + assert.equal(buffer.lines.get(1).translateToString(false), '语汉语汉语 '); + }); + it('should wrap wide characters correctly when reflowing smaller', () => { + // TODO: .. + }); describe('reflowLarger cases', () => { beforeEach(() => { diff --git a/src/Buffer.ts b/src/Buffer.ts index 987e03247a..72e43aa0d3 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -259,24 +259,36 @@ export class Buffer implements IBuffer { // Copy buffer data to new locations let destLineIndex = 0; - let destCol = this._cols; + let destCol = wrappedLines[destLineIndex].getTrimmedLength(); let srcLineIndex = 1; let srcCol = 0; while (srcLineIndex < wrappedLines.length) { - const srcRemainingCells = this._cols - srcCol; + const srcTrimmedTineLength = wrappedLines[srcLineIndex].getTrimmedLength(); + const srcRemainingCells = srcTrimmedTineLength - srcCol; const destRemainingCells = newCols - destCol; const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); + destCol += cellsToCopy; if (destCol === newCols) { destLineIndex++; destCol = 0; } srcCol += cellsToCopy; - if (srcCol === this._cols) { + if (srcCol === srcTrimmedTineLength) { srcLineIndex++; srcCol = 0; } + + // Make sure the last cell isn't wide, if it is copy it to the current dest + if (destCol === 0) { + if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) { + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false); + // Null out the end of the last row + wrappedLines[destLineIndex - 1].set(newCols - 1, FILL_CHAR_DATA); + } + } } // Clear out remaining cells or fragments could remain; diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 2fa4ef139d..6bc305860c 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -54,6 +54,10 @@ export class BufferLine implements IBufferLine { ]; } + public getWidth(index: number): number { + return this._data[index * CELL_SIZE + Cell.WIDTH]; + } + public set(index: number, value: CharData): void { this._data[index * CELL_SIZE + Cell.FLAGS] = value[0]; if (value[1].length > 1) { From ce69dceee7fec72cc80e2f61aafb382abbd5bfaf Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 23 Jan 2019 21:46:35 -0800 Subject: [PATCH 54/61] Progress on reflow smaller with wide chars --- src/Buffer.test.ts | 35 ++++++++++++- src/Buffer.ts | 24 ++++++--- src/BufferReflow.test.ts | 80 +++++++++++++++++++++++++++++ src/BufferReflow.ts | 107 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 238 insertions(+), 8 deletions(-) create mode 100644 src/BufferReflow.test.ts create mode 100644 src/BufferReflow.ts diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 040eb3ca79..8e1a2f601f 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -545,7 +545,40 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(1).translateToString(false), '语汉语汉语 '); }); it('should wrap wide characters correctly when reflowing smaller', () => { - // TODO: .. + buffer.fillViewportRows(); + buffer.resize(12, 10); + for (let i = 0; i < 12; i += 4) { + buffer.lines.get(0).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); + buffer.lines.get(1).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); + } + for (let i = 2; i < 12; i += 4) { + buffer.lines.get(0).set(i, [null, '语', 2, '语'.charCodeAt(0)]); + buffer.lines.get(1).set(i, [null, '语', 2, '语'.charCodeAt(0)]); + } + for (let i = 1; i < 12; i += 2) { + buffer.lines.get(0).set(i, [null, '', 0, undefined]); + buffer.lines.get(1).set(i, [null, '', 0, undefined]); + } + buffer.lines.get(1).isWrapped = true; + // Buffer: + // 汉语汉语汉语 (wrapped) + // 汉语汉语汉语 + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语'); + assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语汉语'); + buffer.resize(11, 10); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉', '1'); + assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语', '2'); + assert.equal(buffer.lines.get(2).translateToString(true), '汉语'); + buffer.resize(10, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉'); + assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语'); + assert.equal(buffer.lines.get(2).translateToString(true), '汉语'); + buffer.resize(9, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语'); + assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语'); + assert.equal(buffer.lines.get(2).translateToString(true), '汉语汉语'); }); describe('reflowLarger cases', () => { diff --git a/src/Buffer.ts b/src/Buffer.ts index 72e43aa0d3..3094132415 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -9,6 +9,7 @@ import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; import { BufferLine } from './BufferLine'; import { DEFAULT_COLOR } from './renderer/atlas/Types'; +import { reflowSmallerGetLinesNeeded, reflowSmallerGetNewLineLengths } from './BufferReflow'; export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); export const CHAR_DATA_ATTR_INDEX = 0; @@ -386,11 +387,17 @@ export class Buffer implements IBuffer { wrappedLines.unshift(nextLine); } - // Determine how many lines need to be inserted at the end, based on the trimmed length of - // the last wrapped line + + const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; - const linesNeeded = Math.ceil(cellsNeeded / newCols); + // const linesNeeded = reflowSmallerGetLinesNeeded(wrappedLines, this._cols, newCols); + const destLineLengths = reflowSmallerGetNewLineLengths(wrappedLines, this._cols, newCols); + console.log(destLineLengths); + const linesNeeded = destLineLengths.length; + + + const linesToAdd = linesNeeded - wrappedLines.length; let trimmedLines: number; if (this.ybase === 0 && this.y !== this.lines.length - 1) { @@ -418,8 +425,8 @@ export class Buffer implements IBuffer { wrappedLines.push(...newLines); // Copy buffer data to new locations, this needs to happen backwards to do in-place - let destLineIndex = Math.floor(cellsNeeded / newCols); - let destCol = cellsNeeded % newCols; + let destLineIndex = destLineLengths.length - 1; // Math.floor(cellsNeeded / newCols); + let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols; if (destCol === 0) { destLineIndex--; destCol = newCols; @@ -432,12 +439,13 @@ export class Buffer implements IBuffer { destCol -= cellsToCopy; if (destCol === 0) { destLineIndex--; - destCol = newCols; + destCol = destLineLengths[destLineIndex]; } srcCol -= cellsToCopy; if (srcCol === 0) { srcLineIndex--; - srcCol = this._cols; + // TODO: srcCol shoudl take trimmed length into account + srcCol = wrappedLines[Math.max(srcLineIndex, 0)].getTrimmedLength(); // this._cols; } } @@ -516,6 +524,8 @@ export class Buffer implements IBuffer { } } + // private _reflowSmallerGetLinesNeeded() + /** * Translates a string index back to a BufferIndex. * To get the correct buffer position the string must start at `startCol` 0 diff --git a/src/BufferReflow.test.ts b/src/BufferReflow.test.ts new file mode 100644 index 0000000000..a788fbc800 --- /dev/null +++ b/src/BufferReflow.test.ts @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2019 The xterm.js authors. All rights reserved. + * @license MIT + */ +import { assert } from 'chai'; +import { BufferLine } from './BufferLine'; +import { reflowSmallerGetNewLineLengths } from './BufferReflow'; + +describe('BufferReflow', () => { + describe('reflowSmallerGetNewLineLengths', () => { + it('should return correct line lengths for a small line with wide characters', () => { + const line = new BufferLine(4); + line.set(0, [null, '汉', 2, '汉'.charCodeAt(0)]); + line.set(1, [null, '', 0, undefined]); + line.set(2, [null, '语', 2, '语'.charCodeAt(0)]); + line.set(3, [null, '', 0, undefined]); + assert.equal(line.translateToString(true), '汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 3), [2, 2], 'line: 汉, 语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 2), [2, 2], 'line: 汉, 语'); + }); + it('should return correct line lengths for a large line with wide characters', () => { + const line = new BufferLine(12); + for (let i = 0; i < 12; i += 4) { + line.set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); + line.set(i + 2, [null, '语', 2, '语'.charCodeAt(0)]); + } + for (let i = 1; i < 12; i += 2) { + line.set(i, [null, '', 0, undefined]); + line.set(i, [null, '', 0, undefined]); + } + assert.equal(line.translateToString(), '汉语汉语汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 11), [10, 2], 'line: 汉语汉语汉, 语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 10), [10, 2], 'line: 汉语汉语汉, 语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 9), [8, 4], 'line: 汉语汉语, 汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 8), [8, 4], 'line: 汉语汉语, 汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 7), [6, 6], 'line: 汉语汉, 语汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 6), [6, 6], 'line: 汉语汉, 语汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 5), [4, 4, 4], 'line: 汉语, 汉语, 汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 4), [4, 4, 4], 'line: 汉语, 汉语, 汉语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 3), [2, 2, 2, 2, 2, 2], 'line: 汉, 语, 汉, 语, 汉, 语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 12, 2), [2, 2, 2, 2, 2, 2], 'line: 汉, 语, 汉, 语, 汉, 语'); + }); + it('should return correct line lengths for a string with wide and single characters', () => { + const line = new BufferLine(6); + line.set(0, [null, 'a', 1, 'a'.charCodeAt(0)]); + line.set(1, [null, '汉', 2, '汉'.charCodeAt(0)]); + line.set(2, [null, '', 0, undefined]); + line.set(3, [null, '语', 2, '语'.charCodeAt(0)]); + line.set(4, [null, '', 0, undefined]); + line.set(5, [null, 'b', 1, 'b'.charCodeAt(0)]); + assert.equal(line.translateToString(), 'a汉语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 6, 5), [5, 1], 'line: a汉语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 6, 4), [3, 3], 'line: a汉, 语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 6, 3), [3, 3], 'line: a汉, 语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 6, 2), [1, 2, 2, 1], 'line: a, 汉, 语, b'); + }); + it('should return correct line lengths for a wrapped line with wide and single characters', () => { + const line1 = new BufferLine(6); + line1.set(0, [null, 'a', 1, 'a'.charCodeAt(0)]); + line1.set(1, [null, '汉', 2, '汉'.charCodeAt(0)]); + line1.set(2, [null, '', 0, undefined]); + line1.set(3, [null, '语', 2, '语'.charCodeAt(0)]); + line1.set(4, [null, '', 0, undefined]); + line1.set(5, [null, 'b', 1, 'b'.charCodeAt(0)]); + const line2 = new BufferLine(6, undefined, true); + line2.set(0, [null, 'a', 1, 'a'.charCodeAt(0)]); + line2.set(1, [null, '汉', 2, '汉'.charCodeAt(0)]); + line2.set(2, [null, '', 0, undefined]); + line2.set(3, [null, '语', 2, '语'.charCodeAt(0)]); + line2.set(4, [null, '', 0, undefined]); + line2.set(5, [null, 'b', 1, 'b'.charCodeAt(0)]); + assert.equal(line1.translateToString(), 'a汉语b'); + assert.equal(line2.translateToString(), 'a汉语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 5), [5, 4, 3], 'lines: a汉语, ba汉, 语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 4), [3, 4, 4, 1], 'lines: a汉, 语ba, 汉语, b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 3), [3, 3, 3, 3], 'lines: a汉, 语b, a汉, 语b'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 2), [1, 2, 2, 2, 2, 2, 1], 'lines: a, 汉, 语, ba, 汉, 语, b'); + }); + }); +}); diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts new file mode 100644 index 0000000000..f56d48c571 --- /dev/null +++ b/src/BufferReflow.ts @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2019 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { BufferLine } from './BufferLine'; + +/** + * Determine how many lines need to be inserted at the end. This is done by finding what each + * wrapping point will be and counting the lines needed This would be a lot simpler but in the case + * of a line ending with a wide character, the wide character needs to be put on the following line + * or it would be cut in half. + * @param wrappedLines The original wrapped lines. + * @param newCols The new column count. + */ +export function reflowSmallerGetLinesNeeded(wrappedLines: BufferLine[], oldCols: number, newCols: number): number { + const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); + // const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; + + // TODO: Make faster + const cellsNeeded = wrappedLines.map(l => l.getTrimmedLength()).reduce((p, c) => p + c); + + // Lines needed needs to take into account what the ending character of each new line is + let linesNeeded = 0; + let cellsAvailable = 0; + // let currentCol = 0; + + // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and + // linesNeeded + let srcCol = -1; + let srcLine = 0; + while (cellsAvailable < cellsNeeded) { + // if (srcLine === wrappedLines.length - 1) { + // cellsAvailable += newCols; + // linesNeeded++; + // break; + // } + + srcCol += newCols; + if (srcCol >= oldCols) { + srcCol -= oldCols; + srcLine++; + } + if (srcLine >= wrappedLines.length) { + linesNeeded++; + break; + } + const endsWithWide = wrappedLines[srcLine].getWidth(srcCol) === 2; + if (endsWithWide) { + srcCol--; + } + cellsAvailable += endsWithWide ? newCols - 1 : newCols; + linesNeeded++; + } + + return linesNeeded; + // return Math.ceil(cellsNeeded / newCols); +} + +/** + * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre- + * compute the wrapping points since wide characters may need to be wrapped onto the following line. + * This function will return an array of numbers of where each line wraps to, the resulting array + * will only contain the values `newCols` (when the line does not end with a wide character) and + * `newCols - 1` (when the line does end with a wide character), except for the last value which + * will contain the remaining items to fill the line. + * + * Calling this with a `newCols` value of `1` will lock up. + * + * @param wrappedLines The wrapped lines to evaluate. + * @param oldCols The columns before resize. + * @param newCols The columns after resize. + */ +export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCols: number, newCols: number): number[] { + const newLineLengths: number[] = []; + + // TODO: Force cols = 2 to be minimum possible value, this will lock up + + const cellsNeeded = wrappedLines.map(l => l.getTrimmedLength()).reduce((p, c) => p + c); + + // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and + // linesNeeded + let srcCol = -1; + let srcLine = 0; + let cellsAvailable = 0; + while (cellsAvailable < cellsNeeded) { + srcCol += newCols; + if (srcCol >= oldCols) { + srcCol -= oldCols; + srcLine++; + } + if (srcLine >= wrappedLines.length) { + // Add the final line and exit the loop + newLineLengths.push(cellsNeeded - cellsAvailable); + break; + } + const endsWithWide = wrappedLines[srcLine].getWidth(srcCol) === 2; + if (endsWithWide) { + srcCol--; + } + const lineLength = endsWithWide ? newCols - 1 : newCols; + newLineLengths.push(lineLength); + cellsAvailable += lineLength; + } + + return newLineLengths; +} From df7cd9c319f42aaff3b3e79ef45a19ebda0cf719 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 23 Jan 2019 22:40:40 -0800 Subject: [PATCH 55/61] Get reflow smaller working for wide chars --- src/Buffer.test.ts | 4 ++-- src/Buffer.ts | 9 ++++++++- src/BufferReflow.test.ts | 13 +++++++++++++ src/BufferReflow.ts | 17 +++++++++-------- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 8e1a2f601f..82cdff6b36 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -568,8 +568,8 @@ describe('Buffer', () => { buffer.resize(11, 10); assert.equal(buffer.ybase, 0); assert.equal(buffer.lines.length, 10); - assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉', '1'); - assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语', '2'); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉'); + assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语'); assert.equal(buffer.lines.get(2).translateToString(true), '汉语'); buffer.resize(10, 10); assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉'); diff --git a/src/Buffer.ts b/src/Buffer.ts index 3094132415..5d3c1a521f 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -429,7 +429,7 @@ export class Buffer implements IBuffer { let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols; if (destCol === 0) { destLineIndex--; - destCol = newCols; + destCol = destLineLengths[destLineIndex]; } let srcLineIndex = wrappedLines.length - linesToAdd - 1; let srcCol = lastLineLength; @@ -449,6 +449,13 @@ export class Buffer implements IBuffer { } } + // Null out the end of the line ends if a wide character wrapped to the following line + for (let i = 0; i < wrappedLines.length; i++) { + if (destLineLengths[i] < newCols) { + wrappedLines[i].set(destLineLengths[i], FILL_CHAR_DATA); + } + } + // Adjust viewport as needed let viewportAdjustments = linesToAdd - trimmedLines; while (viewportAdjustments-- > 0) { diff --git a/src/BufferReflow.test.ts b/src/BufferReflow.test.ts index a788fbc800..9c978dc0ab 100644 --- a/src/BufferReflow.test.ts +++ b/src/BufferReflow.test.ts @@ -5,6 +5,7 @@ import { assert } from 'chai'; import { BufferLine } from './BufferLine'; import { reflowSmallerGetNewLineLengths } from './BufferReflow'; +import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer'; describe('BufferReflow', () => { describe('reflowSmallerGetNewLineLengths', () => { @@ -76,5 +77,17 @@ describe('BufferReflow', () => { assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 3), [3, 3, 3, 3], 'lines: a汉, 语b, a汉, 语b'); assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 2), [1, 2, 2, 2, 2, 2, 1], 'lines: a, 汉, 语, ba, 汉, 语, b'); }); + it('should work on lines ending in null space', () => { + const line = new BufferLine(5); + line.set(0, [null, '汉', 2, '汉'.charCodeAt(0)]); + line.set(1, [null, '', 0, undefined]); + line.set(2, [null, '语', 2, '语'.charCodeAt(0)]); + line.set(3, [null, '', 0, undefined]); + line.set(4, [null, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); + assert.equal(line.translateToString(true), '汉语'); + assert.equal(line.translateToString(false), '汉语 '); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 3), [2, 2], 'line: 汉, 语'); + assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 2), [2, 2], 'line: 汉, 语'); + }); }); }); diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index f56d48c571..f99acd1ca7 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -80,21 +80,22 @@ export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCo // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and // linesNeeded - let srcCol = -1; + let srcCol = 0; let srcLine = 0; let cellsAvailable = 0; while (cellsAvailable < cellsNeeded) { - srcCol += newCols; - if (srcCol >= oldCols) { - srcCol -= oldCols; - srcLine++; - } - if (srcLine >= wrappedLines.length) { + if (cellsNeeded - cellsAvailable < newCols) { // Add the final line and exit the loop newLineLengths.push(cellsNeeded - cellsAvailable); break; } - const endsWithWide = wrappedLines[srcLine].getWidth(srcCol) === 2; + srcCol += newCols; + const oldTrimmedLength = wrappedLines[srcLine].getTrimmedLength(); + if (srcCol > oldTrimmedLength) { + srcCol -= oldTrimmedLength; + srcLine++; + } + const endsWithWide = wrappedLines[srcLine].getWidth(srcCol - 1) === 2; if (endsWithWide) { srcCol--; } From 178c513407a86c4d1f64c1cf726091a044a7e16b Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 23 Jan 2019 22:48:32 -0800 Subject: [PATCH 56/61] Clean up --- src/Buffer.test.ts | 14 ++++++++++++ src/Buffer.ts | 13 ++--------- src/BufferReflow.ts | 54 --------------------------------------------- src/Terminal.ts | 11 +++++---- 4 files changed, 23 insertions(+), 69 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 82cdff6b36..1ef2e92c66 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -579,6 +579,20 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语'); assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语'); assert.equal(buffer.lines.get(2).translateToString(true), '汉语汉语'); + buffer.resize(8, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语'); + assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语'); + assert.equal(buffer.lines.get(2).translateToString(true), '汉语汉语'); + buffer.resize(7, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉'); + assert.equal(buffer.lines.get(1).translateToString(true), '语汉语'); + assert.equal(buffer.lines.get(2).translateToString(true), '汉语汉'); + assert.equal(buffer.lines.get(3).translateToString(true), '语汉语'); + buffer.resize(6, 10); + assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉'); + assert.equal(buffer.lines.get(1).translateToString(true), '语汉语'); + assert.equal(buffer.lines.get(2).translateToString(true), '汉语汉'); + assert.equal(buffer.lines.get(3).translateToString(true), '语汉语'); }); describe('reflowLarger cases', () => { diff --git a/src/Buffer.ts b/src/Buffer.ts index 5d3c1a521f..f534087d57 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -9,7 +9,7 @@ import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; import { BufferLine } from './BufferLine'; import { DEFAULT_COLOR } from './renderer/atlas/Types'; -import { reflowSmallerGetLinesNeeded, reflowSmallerGetNewLineLengths } from './BufferReflow'; +import { reflowSmallerGetNewLineLengths } from './BufferReflow'; export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); export const CHAR_DATA_ATTR_INDEX = 0; @@ -387,18 +387,9 @@ export class Buffer implements IBuffer { wrappedLines.unshift(nextLine); } - - const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); - const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; - // const linesNeeded = reflowSmallerGetLinesNeeded(wrappedLines, this._cols, newCols); const destLineLengths = reflowSmallerGetNewLineLengths(wrappedLines, this._cols, newCols); - console.log(destLineLengths); - const linesNeeded = destLineLengths.length; - - - - const linesToAdd = linesNeeded - wrappedLines.length; + const linesToAdd = destLineLengths.length - wrappedLines.length; let trimmedLines: number; if (this.ybase === 0 && this.y !== this.lines.length - 1) { // If the top section of the buffer is not yet filled diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index f99acd1ca7..5e815c7aab 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -5,58 +5,6 @@ import { BufferLine } from './BufferLine'; -/** - * Determine how many lines need to be inserted at the end. This is done by finding what each - * wrapping point will be and counting the lines needed This would be a lot simpler but in the case - * of a line ending with a wide character, the wide character needs to be put on the following line - * or it would be cut in half. - * @param wrappedLines The original wrapped lines. - * @param newCols The new column count. - */ -export function reflowSmallerGetLinesNeeded(wrappedLines: BufferLine[], oldCols: number, newCols: number): number { - const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); - // const cellsNeeded = (wrappedLines.length - 1) * this._cols + lastLineLength; - - // TODO: Make faster - const cellsNeeded = wrappedLines.map(l => l.getTrimmedLength()).reduce((p, c) => p + c); - - // Lines needed needs to take into account what the ending character of each new line is - let linesNeeded = 0; - let cellsAvailable = 0; - // let currentCol = 0; - - // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and - // linesNeeded - let srcCol = -1; - let srcLine = 0; - while (cellsAvailable < cellsNeeded) { - // if (srcLine === wrappedLines.length - 1) { - // cellsAvailable += newCols; - // linesNeeded++; - // break; - // } - - srcCol += newCols; - if (srcCol >= oldCols) { - srcCol -= oldCols; - srcLine++; - } - if (srcLine >= wrappedLines.length) { - linesNeeded++; - break; - } - const endsWithWide = wrappedLines[srcLine].getWidth(srcCol) === 2; - if (endsWithWide) { - srcCol--; - } - cellsAvailable += endsWithWide ? newCols - 1 : newCols; - linesNeeded++; - } - - return linesNeeded; - // return Math.ceil(cellsNeeded / newCols); -} - /** * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre- * compute the wrapping points since wide characters may need to be wrapped onto the following line. @@ -74,8 +22,6 @@ export function reflowSmallerGetLinesNeeded(wrappedLines: BufferLine[], oldCols: export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCols: number, newCols: number): number[] { const newLineLengths: number[] = []; - // TODO: Force cols = 2 to be minimum possible value, this will lock up - const cellsNeeded = wrappedLines.map(l => l.getTrimmedLength()).reduce((p, c) => p + c); // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and diff --git a/src/Terminal.ts b/src/Terminal.ts index 4c0cd0f8a4..8a5d96ba27 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -69,6 +69,9 @@ const WRITE_BUFFER_PAUSE_THRESHOLD = 5; */ const WRITE_BATCH_SIZE = 300; +const MINIMUM_COLS = 2; // Less than 2 can mess with wide chars +const MINIMUM_ROWS = 1; + /** * The set of options that only have an effect when set in the Terminal constructor. */ @@ -262,8 +265,8 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // TODO: WHy not document.body? this._parent = document ? document.body : null; - this.cols = this.options.cols; - this.rows = this.options.rows; + this.cols = Math.max(this.options.cols, MINIMUM_COLS); + this.rows = Math.max(this.options.rows, MINIMUM_ROWS); if (this.options.handler) { this.on('data', this.options.handler); @@ -1691,8 +1694,8 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II return; } - if (x < 1) x = 1; - if (y < 1) y = 1; + if (x < MINIMUM_COLS) x = MINIMUM_COLS; + if (y < MINIMUM_ROWS) y = MINIMUM_ROWS; this.buffers.resize(x, y); From dfff04cf3db7e29ba1c0e3097bcf16ac8f91af5e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 23 Jan 2019 23:51:28 -0800 Subject: [PATCH 57/61] Pull toRemove step into BufferReflow --- src/Buffer.ts | 77 +++----------------------------------------- src/BufferReflow.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 72 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index f534087d57..2f1c2e491c 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -9,7 +9,7 @@ import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; import { BufferLine } from './BufferLine'; import { DEFAULT_COLOR } from './renderer/atlas/Types'; -import { reflowSmallerGetNewLineLengths } from './BufferReflow'; +import { reflowSmallerGetNewLineLengths, reflowLargerGetLinesToRemove } from './BufferReflow'; export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); export const CHAR_DATA_ATTR_INDEX = 0; @@ -26,7 +26,7 @@ export const WHITESPACE_CELL_CHAR = ' '; export const WHITESPACE_CELL_WIDTH = 1; export const WHITESPACE_CELL_CODE = 32; -const FILL_CHAR_DATA: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; +export const FILL_CHAR_DATA: CharData = [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; /** * This class represents a terminal buffer (an internal state of the terminal), where the @@ -240,78 +240,11 @@ export class Buffer implements IBuffer { } private _reflowLarger(newCols: number): void { + // TODO: Can toRemove be pulled out into BufferReflow? + // Gather all BufferLines that need to be removed from the Buffer here so that they can be // batched up and only committed once - const toRemove: number[] = []; - for (let y = 0; y < this.lines.length - 1; y++) { - // Check if this row is wrapped - let i = y; - let nextLine = this.lines.get(++i) as BufferLine; - if (!nextLine.isWrapped) { - continue; - } - - // Check how many lines it's wrapped for - const wrappedLines: BufferLine[] = [this.lines.get(y) as BufferLine]; - while (i < this.lines.length && nextLine.isWrapped) { - wrappedLines.push(nextLine); - nextLine = this.lines.get(++i) as BufferLine; - } - - // Copy buffer data to new locations - let destLineIndex = 0; - let destCol = wrappedLines[destLineIndex].getTrimmedLength(); - let srcLineIndex = 1; - let srcCol = 0; - while (srcLineIndex < wrappedLines.length) { - const srcTrimmedTineLength = wrappedLines[srcLineIndex].getTrimmedLength(); - const srcRemainingCells = srcTrimmedTineLength - srcCol; - const destRemainingCells = newCols - destCol; - const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); - - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); - - destCol += cellsToCopy; - if (destCol === newCols) { - destLineIndex++; - destCol = 0; - } - srcCol += cellsToCopy; - if (srcCol === srcTrimmedTineLength) { - srcLineIndex++; - srcCol = 0; - } - - // Make sure the last cell isn't wide, if it is copy it to the current dest - if (destCol === 0) { - if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) { - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false); - // Null out the end of the last row - wrappedLines[destLineIndex - 1].set(newCols - 1, FILL_CHAR_DATA); - } - } - } - - // Clear out remaining cells or fragments could remain; - wrappedLines[destLineIndex].replaceCells(destCol, newCols, FILL_CHAR_DATA); - - // Work backwards and remove any rows at the end that only contain null cells - let countToRemove = 0; - for (let i = wrappedLines.length - 1; i > 0; i--) { - if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) { - countToRemove++; - } else { - break; - } - } - - if (countToRemove > 0) { - toRemove.push(y + wrappedLines.length - countToRemove); // index - toRemove.push(countToRemove); - } - - y += wrappedLines.length - 1; - } + const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, newCols); if (toRemove.length > 0) { // First iterate through the list and get the actual indexes to use for rows diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index 5e815c7aab..310443b794 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -4,6 +4,84 @@ */ import { BufferLine } from './BufferLine'; +import { CircularList } from './common/CircularList'; +import { IBufferLine } from './Types'; +import { FILL_CHAR_DATA } from './Buffer'; + +export function reflowLargerGetLinesToRemove(lines: CircularList, newCols: number): number[] { + const toRemove: number[] = []; + + for (let y = 0; y < lines.length - 1; y++) { + // Check if this row is wrapped + let i = y; + let nextLine = lines.get(++i) as BufferLine; + if (!nextLine.isWrapped) { + continue; + } + + // Check how many lines it's wrapped for + const wrappedLines: BufferLine[] = [lines.get(y) as BufferLine]; + while (i < lines.length && nextLine.isWrapped) { + wrappedLines.push(nextLine); + nextLine = lines.get(++i) as BufferLine; + } + + // Copy buffer data to new locations + let destLineIndex = 0; + let destCol = wrappedLines[destLineIndex].getTrimmedLength(); + let srcLineIndex = 1; + let srcCol = 0; + while (srcLineIndex < wrappedLines.length) { + const srcTrimmedTineLength = wrappedLines[srcLineIndex].getTrimmedLength(); + const srcRemainingCells = srcTrimmedTineLength - srcCol; + const destRemainingCells = newCols - destCol; + const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); + + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); + + destCol += cellsToCopy; + if (destCol === newCols) { + destLineIndex++; + destCol = 0; + } + srcCol += cellsToCopy; + if (srcCol === srcTrimmedTineLength) { + srcLineIndex++; + srcCol = 0; + } + + // Make sure the last cell isn't wide, if it is copy it to the current dest + if (destCol === 0) { + if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) { + wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false); + // Null out the end of the last row + wrappedLines[destLineIndex - 1].set(newCols - 1, FILL_CHAR_DATA); + } + } + } + + // Clear out remaining cells or fragments could remain; + wrappedLines[destLineIndex].replaceCells(destCol, newCols, FILL_CHAR_DATA); + + // Work backwards and remove any rows at the end that only contain null cells + let countToRemove = 0; + for (let i = wrappedLines.length - 1; i > 0; i--) { + if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) { + countToRemove++; + } else { + break; + } + } + + if (countToRemove > 0) { + toRemove.push(y + wrappedLines.length - countToRemove); // index + toRemove.push(countToRemove); + } + + y += wrappedLines.length - 1; + } + return toRemove; +} /** * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre- From d4bd8ae2be96ff976568811c70e6f3907a700149 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 23 Jan 2019 23:58:38 -0800 Subject: [PATCH 58/61] Pull more parts out of reflow larger --- src/Buffer.ts | 70 ++++++++++++--------------------------------- src/BufferReflow.ts | 43 +++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 2f1c2e491c..286d1311ec 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -9,7 +9,7 @@ import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; import { BufferLine } from './BufferLine'; import { DEFAULT_COLOR } from './renderer/atlas/Types'; -import { reflowSmallerGetNewLineLengths, reflowLargerGetLinesToRemove } from './BufferReflow'; +import { reflowSmallerGetNewLineLengths, reflowLargerGetLinesToRemove, reflowLargerCreateNewLayout, reflowLargerApplyNewLayout } from './BufferReflow'; export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); export const CHAR_DATA_ATTR_INDEX = 0; @@ -240,62 +240,28 @@ export class Buffer implements IBuffer { } private _reflowLarger(newCols: number): void { - // TODO: Can toRemove be pulled out into BufferReflow? - - // Gather all BufferLines that need to be removed from the Buffer here so that they can be - // batched up and only committed once const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, newCols); - if (toRemove.length > 0) { - // First iterate through the list and get the actual indexes to use for rows const newLayout: number[] = []; + const countRemoved = reflowLargerCreateNewLayout(this.lines, toRemove, newLayout); + reflowLargerApplyNewLayout(this.lines, newLayout); + this._reflowLargerAdjustViewport(newCols, countRemoved); + } + } - let nextToRemoveIndex = 0; - let nextToRemoveStart = toRemove[nextToRemoveIndex]; - let countRemovedSoFar = 0; - for (let i = 0; i < this.lines.length; i++) { - if (nextToRemoveStart === i) { - const countToRemove = toRemove[++nextToRemoveIndex]; - - // Tell markers that there was a deletion - this.lines.emit('delete', { - index: i - countRemovedSoFar, - amount: countToRemove - } as IDeleteEvent); - - i += countToRemove - 1; - countRemovedSoFar += countToRemove; - nextToRemoveStart = toRemove[++nextToRemoveIndex]; - } else { - newLayout.push(i); - } - } - - // Record original lines so they don't get overridden when we rearrange the list - const newLayoutLines: BufferLine[] = []; - for (let i = 0; i < newLayout.length; i++) { - newLayoutLines.push(this.lines.get(newLayout[i]) as BufferLine); - } - - // Rearrange the list - for (let i = 0; i < newLayoutLines.length; i++) { - this.lines.set(i, newLayoutLines[i]); - } - this.lines.length = newLayout.length; - - // Adjust viewport based on number of items removed - let viewportAdjustments = countRemovedSoFar; - while (viewportAdjustments-- > 0) { - if (this.ybase === 0) { - this.y--; - // Add an extra row at the bottom of the viewport - this.lines.push(new BufferLine(newCols, FILL_CHAR_DATA)); - } else { - if (this.ydisp === this.ybase) { - this.ydisp--; - } - this.ybase--; + private _reflowLargerAdjustViewport(newCols: number, countRemoved: number): void { + // Adjust viewport based on number of items removed + let viewportAdjustments = countRemoved; + while (viewportAdjustments-- > 0) { + if (this.ybase === 0) { + this.y--; + // Add an extra row at the bottom of the viewport + this.lines.push(new BufferLine(newCols, FILL_CHAR_DATA)); + } else { + if (this.ydisp === this.ybase) { + this.ydisp--; } + this.ybase--; } } } diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index 310443b794..51f83c1865 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -4,11 +4,13 @@ */ import { BufferLine } from './BufferLine'; -import { CircularList } from './common/CircularList'; +import { CircularList, IDeleteEvent } from './common/CircularList'; import { IBufferLine } from './Types'; import { FILL_CHAR_DATA } from './Buffer'; export function reflowLargerGetLinesToRemove(lines: CircularList, newCols: number): number[] { + // Gather all BufferLines that need to be removed from the Buffer here so that they can be + // batched up and only committed once const toRemove: number[] = []; for (let y = 0; y < lines.length - 1; y++) { @@ -83,6 +85,45 @@ export function reflowLargerGetLinesToRemove(lines: CircularList, n return toRemove; } +export function reflowLargerCreateNewLayout(lines: CircularList, toRemove: number[], newLayout: number[]): number { + // First iterate through the list and get the actual indexes to use for rows + let nextToRemoveIndex = 0; + let nextToRemoveStart = toRemove[nextToRemoveIndex]; + let countRemovedSoFar = 0; + for (let i = 0; i < lines.length; i++) { + if (nextToRemoveStart === i) { + const countToRemove = toRemove[++nextToRemoveIndex]; + + // Tell markers that there was a deletion + lines.emit('delete', { + index: i - countRemovedSoFar, + amount: countToRemove + } as IDeleteEvent); + + i += countToRemove - 1; + countRemovedSoFar += countToRemove; + nextToRemoveStart = toRemove[++nextToRemoveIndex]; + } else { + newLayout.push(i); + } + } + return countRemovedSoFar; +} + +export function reflowLargerApplyNewLayout(lines: CircularList, newLayout: number[]): void { + // Record original lines so they don't get overridden when we rearrange the list + const newLayoutLines: BufferLine[] = []; + for (let i = 0; i < newLayout.length; i++) { + newLayoutLines.push(lines.get(newLayout[i]) as BufferLine); + } + + // Rearrange the list + for (let i = 0; i < newLayoutLines.length; i++) { + lines.set(i, newLayoutLines[i]); + } + lines.length = newLayout.length; +} + /** * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre- * compute the wrapping points since wide characters may need to be wrapped onto the following line. From 99ac78031c8ebb1bef723b9aa1e45e11e0c4079a Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 24 Jan 2019 09:11:16 -0800 Subject: [PATCH 59/61] Remove out param from reflow large method --- src/Buffer.ts | 7 +++---- src/BufferReflow.ts | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 286d1311ec..52d9572d5c 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -242,10 +242,9 @@ export class Buffer implements IBuffer { private _reflowLarger(newCols: number): void { const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, newCols); if (toRemove.length > 0) { - const newLayout: number[] = []; - const countRemoved = reflowLargerCreateNewLayout(this.lines, toRemove, newLayout); - reflowLargerApplyNewLayout(this.lines, newLayout); - this._reflowLargerAdjustViewport(newCols, countRemoved); + const newLayoutResult = reflowLargerCreateNewLayout(this.lines, toRemove); + reflowLargerApplyNewLayout(this.lines, newLayoutResult.layout); + this._reflowLargerAdjustViewport(newCols, newLayoutResult.countRemoved); } } diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index 51f83c1865..003a3c7804 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -8,6 +8,11 @@ import { CircularList, IDeleteEvent } from './common/CircularList'; import { IBufferLine } from './Types'; import { FILL_CHAR_DATA } from './Buffer'; +export interface INewLayoutResult { + layout: number[]; + countRemoved: number; +} + export function reflowLargerGetLinesToRemove(lines: CircularList, newCols: number): number[] { // Gather all BufferLines that need to be removed from the Buffer here so that they can be // batched up and only committed once @@ -85,7 +90,8 @@ export function reflowLargerGetLinesToRemove(lines: CircularList, n return toRemove; } -export function reflowLargerCreateNewLayout(lines: CircularList, toRemove: number[], newLayout: number[]): number { +export function reflowLargerCreateNewLayout(lines: CircularList, toRemove: number[]): INewLayoutResult { + const layout: number[] = []; // First iterate through the list and get the actual indexes to use for rows let nextToRemoveIndex = 0; let nextToRemoveStart = toRemove[nextToRemoveIndex]; @@ -104,10 +110,13 @@ export function reflowLargerCreateNewLayout(lines: CircularList, to countRemovedSoFar += countToRemove; nextToRemoveStart = toRemove[++nextToRemoveIndex]; } else { - newLayout.push(i); + layout.push(i); } } - return countRemovedSoFar; + return { + layout, + countRemoved: countRemovedSoFar + }; } export function reflowLargerApplyNewLayout(lines: CircularList, newLayout: number[]): void { From 109e3a50e8e91a1b328cac7cc81391222c68dec8 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 24 Jan 2019 10:20:18 -0800 Subject: [PATCH 60/61] jsdoc --- src/BufferReflow.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index 003a3c7804..59934e46cb 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -13,6 +13,12 @@ export interface INewLayoutResult { countRemoved: number; } +/** + * Evaluates and returns indexes to be removed after a reflow larger occurs. Lines will be removed + * when a wrapped line unwraps. + * @param lines The buffer lines. + * @param newCols The columns after resize. + */ export function reflowLargerGetLinesToRemove(lines: CircularList, newCols: number): number[] { // Gather all BufferLines that need to be removed from the Buffer here so that they can be // batched up and only committed once @@ -90,6 +96,11 @@ export function reflowLargerGetLinesToRemove(lines: CircularList, n return toRemove; } +/** + * Creates and return the new layout for lines given an array of indexes to be removed. + * @param lines The buffer lines. + * @param toRemove The indexes to remove. + */ export function reflowLargerCreateNewLayout(lines: CircularList, toRemove: number[]): INewLayoutResult { const layout: number[] = []; // First iterate through the list and get the actual indexes to use for rows @@ -119,6 +130,12 @@ export function reflowLargerCreateNewLayout(lines: CircularList, to }; } +/** + * Applies a new layout to the buffer. This essentially does the same as many splice calls but it's + * done all at once in a single iteration through the list since splice is very expensive. + * @param lines The buffer lines. + * @param newLayout The new layout to apply. + */ export function reflowLargerApplyNewLayout(lines: CircularList, newLayout: number[]): void { // Record original lines so they don't get overridden when we rearrange the list const newLayoutLines: BufferLine[] = []; From e1363e9fe7746c4e11d7555df6428b04c5131d4e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 24 Jan 2019 11:32:09 -0800 Subject: [PATCH 61/61] Replace if/add/remove with a toggle --- src/Terminal.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Terminal.ts b/src/Terminal.ts index cf1a3ba589..b7d5bf7f9a 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -734,11 +734,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.mouseHelper = new MouseHelper(this.renderer); // apply mouse event classes set by escape codes before terminal was attached - if (this.mouseEvents) { - this.element.classList.add('enable-mouse-events'); - } else { - this.element.classList.remove('enable-mouse-events'); - } + this.element.classList.toggle('enable-mouse-events', this.mouseEvents); if (this.options.screenReaderMode) { // Note that this must be done *after* the renderer is created in order to