From f111aa27dc5a076402ee4d82153343c11c07b615 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 9 Apr 2019 19:35:41 -0700 Subject: [PATCH] Convert linefeed to EventEmitter2 --- src/AccessibilityManager.ts | 2 +- src/InputHandler.ts | 10 ++++------ src/Terminal.test.ts | 12 +++++++++++- src/Terminal.ts | 4 +++- src/WindowsMode.ts | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/AccessibilityManager.ts b/src/AccessibilityManager.ts index a8e0ba1b..866dc3c7 100644 --- a/src/AccessibilityManager.ts +++ b/src/AccessibilityManager.ts @@ -77,7 +77,7 @@ export class AccessibilityManager extends Disposable { this.register(this._terminal.addDisposableListener('scroll', data => this._refreshRows())); // Line feed is an issue as the prompt won't be read out after a command is run this.register(this._terminal.addDisposableListener('a11y.char', (char) => this._onChar(char))); - this.register(this._terminal.addDisposableListener('linefeed', () => this._onChar('\n'))); + this.register(this._terminal.onLineFeed(() => this._onChar('\n'))); this.register(this._terminal.addDisposableListener('a11y.tab', spaceCount => this._onTab(spaceCount))); this.register(this._terminal.onKey(e => this._onKey(e.key))); this.register(this._terminal.addDisposableListener('blur', () => this._clearLiveRegion())); diff --git a/src/InputHandler.ts b/src/InputHandler.ts index d0471286..da167e26 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -109,6 +109,8 @@ export class InputHandler extends Disposable implements IInputHandler { private _onCursorMove = new EventEmitter2(); public get onCursorMove(): IEvent { return this._onCursorMove.event; } + private _onLineFeed = new EventEmitter2(); + public get onLineFeed(): IEvent { return this._onLineFeed.event; } constructor( protected _terminal: IInputHandlingTerminal, @@ -458,12 +460,8 @@ export class InputHandler extends Disposable implements IInputHandler { if (buffer.x >= this._terminal.cols) { buffer.x--; } - /** - * This event is emitted whenever the terminal outputs a LF or NL. - * - * @event linefeed - */ - this._terminal.emit('linefeed'); + + this._onLineFeed.fire(); } /** diff --git a/src/Terminal.test.ts b/src/Terminal.test.ts index 2422c038..b44baa5b 100644 --- a/src/Terminal.test.ts +++ b/src/Terminal.test.ts @@ -17,7 +17,7 @@ class TestTerminal extends Terminal { public keyPress(ev: any): boolean { return this._keyPress(ev); } } -describe('term.js addons', () => { +describe('xterm.js', () => { let term: TestTerminal; const termOptions = { cols: INIT_COLS, @@ -95,6 +95,16 @@ describe('term.js addons', () => { }); }); + describe('linefeed', () => { + it('should emit a linefeed event', (done) => { + term.on('linefeed', () => { + done(); + }); + + term.write('\n'); + }); + }); + describe(`keypress (including 'key' event)`, () => { it('should receive a string and event object', (done) => { let steps = 0; diff --git a/src/Terminal.ts b/src/Terminal.ts index 64178c74..662ce7eb 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -261,7 +261,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this._setup(); // TODO: Replace EventEmitter with EventEmitter2 internally - this.on('linefeed', () => this._onLineFeed.fire()); this.on('selection', () => this._onSelectionChange.fire()); this.on('data', e => this._onInput.fire(e)); this.on('scroll', e => this._onScroll.fire(e)); @@ -271,6 +270,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // Fire old style events from new emitters this.onCursorMove(() => this.emit('cursormove')); this.onKey(e => this.emit('key', e.key, e.domEvent)); + this.onLineFeed(() => this.emit('linefeed')); this.onResize(e => this.emit('resize', e)); this.onTitleChange(e => this.emit('title', e)); } @@ -349,8 +349,10 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // this._writeStopped = false; this._userScrolling = false; + // Register input handler and refire/handle events this._inputHandler = new InputHandler(this); this._inputHandler.onCursorMove(() => this._onCursorMove.fire()); + this._inputHandler.onLineFeed(() => this._onLineFeed.fire()); this.register(this._inputHandler); // Reuse renderer if the Terminal is being recreated via a reset call. diff --git a/src/WindowsMode.ts b/src/WindowsMode.ts index 33a9bed5..ac1d193e 100644 --- a/src/WindowsMode.ts +++ b/src/WindowsMode.ts @@ -18,7 +18,7 @@ export function applyWindowsMode(terminal: ITerminal): IDisposable { // space. This is certainly not without its problems, but generally on // Windows when text reaches the end of the terminal it's likely going to be // wrapped. - return terminal.addDisposableListener('linefeed', () => { + return terminal.onLineFeed(() => { const line = terminal.buffer.lines.get(terminal.buffer.ybase + terminal.buffer.y - 1); const lastChar = line.get(terminal.cols - 1);