Skip to content

Commit

Permalink
Move handler into CoreService and adopt
Browse files Browse the repository at this point in the history
This also fine tunes some of the data events to not clear selection
and scroll to the bottom of the viewport anymore.

Part of xtermjs#1507
Fixes xtermjs#2112
  • Loading branch information
Tyriar committed Jun 23, 2019
1 parent 94a2ad9 commit bfa7a2d
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 82 deletions.
3 changes: 2 additions & 1 deletion src/CompositionHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assert } from 'chai';
import { CompositionHelper } from './CompositionHelper';
import { ITerminal } from './Types';
import { MockCharSizeService } from 'browser/TestUtils.test';
import { MockCoreService } from '../out/common/TestUtils.test';

describe('CompositionHelper', () => {
let terminal: ITerminal;
Expand Down Expand Up @@ -54,7 +55,7 @@ describe('CompositionHelper', () => {
}
} as any;
handledText = '';
compositionHelper = new CompositionHelper(textarea, compositionView, terminal, new MockCharSizeService(10, 10));
compositionHelper = new CompositionHelper(textarea, compositionView, terminal, new MockCharSizeService(10, 10), new MockCoreService());
});

describe('Input', () => {
Expand Down
16 changes: 9 additions & 7 deletions src/CompositionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { ITerminal } from './Types';
import { ICharSizeService } from 'browser/services/Services';
import { ICoreService } from 'common/services/Services';

interface IPosition {
start: number;
Expand Down Expand Up @@ -41,10 +42,11 @@ export class CompositionHelper {
* @param _terminal The Terminal to forward the finished composition to.
*/
constructor(
private _textarea: HTMLTextAreaElement,
private _compositionView: HTMLElement,
private _terminal: ITerminal,
private _charSizeService: ICharSizeService
private readonly _textarea: HTMLTextAreaElement,
private readonly _compositionView: HTMLElement,
private readonly _terminal: ITerminal,
private readonly _charSizeService: ICharSizeService,
private readonly _coreService: ICoreService
) {
this._isComposing = false;
this._isSendingComposition = false;
Expand Down Expand Up @@ -127,7 +129,7 @@ export class CompositionHelper {
// 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);
this._terminal.handler(input);
this._coreService.triggerDataEvent(input, true);
} else {
// Make a deep copy of the composition position here as a new compositionstart event may
// fire before the setTimeout executes.
Expand Down Expand Up @@ -159,7 +161,7 @@ export class CompositionHelper {
// (eg. 2) after a composition character.
input = this._textarea.value.substring(currentCompositionPosition.start);
}
this._terminal.handler(input);
this._coreService.triggerDataEvent(input, true);
}
}, 0);
}
Expand All @@ -179,7 +181,7 @@ export class CompositionHelper {
const newValue = this._textarea.value;
const diff = newValue.replace(oldValue, '');
if (diff.length > 0) {
this._terminal.handler(diff);
this._coreService.triggerDataEvent(diff, true);
}
}
}, 0);
Expand Down
19 changes: 10 additions & 9 deletions src/InputHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
import { CellData } from 'common/buffer/CellData';
import { Attributes } from 'common/buffer/Constants';
import { AttributeData } from 'common/buffer/AttributeData';
import { MockCoreService } from 'common/TestUtils.test';

describe('InputHandler', () => {
describe('save and restore cursor', () => {
Expand All @@ -20,7 +21,7 @@ describe('InputHandler', () => {
terminal.buffer.y = 2;
terminal.buffer.ybase = 0;
terminal.curAttrData.fg = 3;
const inputHandler = new InputHandler(terminal);
const inputHandler = new InputHandler(terminal, new MockCoreService());
// Save cursor position
inputHandler.saveCursor([]);
assert.equal(terminal.buffer.x, 1);
Expand All @@ -39,7 +40,7 @@ describe('InputHandler', () => {
describe('setCursorStyle', () => {
it('should call Terminal.setOption with correct params', () => {
const terminal = new MockInputHandlingTerminal();
const inputHandler = new InputHandler(terminal);
const inputHandler = new InputHandler(terminal, new MockCoreService());
const collect = ' ';

inputHandler.setCursorStyle([0], collect);
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('InputHandler', () => {
const terminal = new MockInputHandlingTerminal();
const collect = '?';
terminal.bracketedPasteMode = false;
const inputHandler = new InputHandler(terminal);
const inputHandler = new InputHandler(terminal, new MockCoreService());
// Set bracketed paste mode
inputHandler.setMode([2004], collect);
assert.equal(terminal.bracketedPasteMode, true);
Expand All @@ -100,7 +101,7 @@ describe('InputHandler', () => {

it('insertChars', function(): void {
const term = new Terminal();
const inputHandler = new InputHandler(term);
const inputHandler = new InputHandler(term, new MockCoreService());

// insert some data in first and second line
inputHandler.parse(Array(term.cols - 9).join('a'));
Expand Down Expand Up @@ -137,7 +138,7 @@ describe('InputHandler', () => {
});
it('deleteChars', function(): void {
const term = new Terminal();
const inputHandler = new InputHandler(term);
const inputHandler = new InputHandler(term, new MockCoreService());

// insert some data in first and second line
inputHandler.parse(Array(term.cols - 9).join('a'));
Expand Down Expand Up @@ -177,7 +178,7 @@ describe('InputHandler', () => {
});
it('eraseInLine', function(): void {
const term = new Terminal();
const inputHandler = new InputHandler(term);
const inputHandler = new InputHandler(term, new MockCoreService());

// fill 6 lines to test 3 different states
inputHandler.parse(Array(term.cols + 1).join('a'));
Expand Down Expand Up @@ -205,7 +206,7 @@ describe('InputHandler', () => {
});
it('eraseInDisplay', function(): void {
const term = new Terminal({cols: 80, rows: 7});
const inputHandler = new InputHandler(term);
const inputHandler = new InputHandler(term, new MockCoreService());

// fill display with a's
for (let i = 0; i < term.rows; ++i) inputHandler.parse(Array(term.cols + 1).join('a'));
Expand Down Expand Up @@ -340,7 +341,7 @@ describe('InputHandler', () => {
describe('print', () => {
it('should not cause an infinite loop (regression test)', () => {
const term = new Terminal();
const inputHandler = new InputHandler(term);
const inputHandler = new InputHandler(term, new MockCoreService());
const container = new Uint32Array(10);
container[0] = 0x200B;
inputHandler.print(container, 0, 1);
Expand All @@ -353,7 +354,7 @@ describe('InputHandler', () => {

beforeEach(() => {
term = new Terminal();
handler = new InputHandler(term);
handler = new InputHandler(term, new MockCoreService());
});
it('should handle DECSET/DECRST 47 (alt screen buffer)', () => {
handler.parse('\x1b[?47h\r\n\x1b[31mJUNK\x1b[?47lTEST');
Expand Down
22 changes: 11 additions & 11 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { IParsingState, IDcsHandler, IEscapeSequenceParser } from 'common/parser
import { NULL_CELL_CODE, NULL_CELL_WIDTH, Attributes, FgFlags, BgFlags } from 'common/buffer/Constants';
import { CellData } from 'common/buffer/CellData';
import { AttributeData } from 'common/buffer/AttributeData';
import { ICoreService } from 'common/services/Services';

/**
* Map collect to glevel. Used in `selectCharset`.
Expand Down Expand Up @@ -113,15 +114,14 @@ export class InputHandler extends Disposable implements IInputHandler {

private _onCursorMove = new EventEmitter<void>();
public get onCursorMove(): IEvent<void> { return this._onCursorMove.event; }
private _onData = new EventEmitter<string>();
public get onData(): IEvent<string> { return this._onData.event; }
private _onLineFeed = new EventEmitter<void>();
public get onLineFeed(): IEvent<void> { return this._onLineFeed.event; }
private _onScroll = new EventEmitter<number>();
public get onScroll(): IEvent<number> { return this._onScroll.event; }

constructor(
protected _terminal: IInputHandlingTerminal,
private _coreService: ICoreService,
private _parser: IEscapeSequenceParser = new EscapeSequenceParser())
{
super();
Expand Down Expand Up @@ -1098,24 +1098,24 @@ export class InputHandler extends Disposable implements IInputHandler {

if (!collect) {
if (this._terminal.is('xterm') || this._terminal.is('rxvt-unicode') || this._terminal.is('screen')) {
this._terminal.handler(C0.ESC + '[?1;2c');
this._coreService.triggerDataEvent(C0.ESC + '[?1;2c');
} else if (this._terminal.is('linux')) {
this._terminal.handler(C0.ESC + '[?6c');
this._coreService.triggerDataEvent(C0.ESC + '[?6c');
}
} else if (collect === '>') {
// xterm and urxvt
// seem to spit this
// out around ~370 times (?).
if (this._terminal.is('xterm')) {
this._terminal.handler(C0.ESC + '[>0;276;0c');
this._coreService.triggerDataEvent(C0.ESC + '[>0;276;0c');
} else if (this._terminal.is('rxvt-unicode')) {
this._terminal.handler(C0.ESC + '[>85;95;0c');
this._coreService.triggerDataEvent(C0.ESC + '[>85;95;0c');
} else if (this._terminal.is('linux')) {
// not supported by linux console.
// linux console echoes parameters.
this._terminal.handler(params[0] + 'c');
this._coreService.triggerDataEvent(params[0] + 'c');
} else if (this._terminal.is('screen')) {
this._terminal.handler(C0.ESC + '[>83;40003;0c');
this._coreService.triggerDataEvent(C0.ESC + '[>83;40003;0c');
}
}
}
Expand Down Expand Up @@ -1799,13 +1799,13 @@ export class InputHandler extends Disposable implements IInputHandler {
switch (params[0]) {
case 5:
// status report
this._onData.fire(`${C0.ESC}[0n`);
this._coreService.triggerDataEvent(`${C0.ESC}[0n`);
break;
case 6:
// cursor position
const y = this._terminal.buffer.y + 1;
const x = this._terminal.buffer.x + 1;
this._onData.fire(`${C0.ESC}[${y};${x}R`);
this._coreService.triggerDataEvent(`${C0.ESC}[${y};${x}R`);
break;
}
} else if (collect === '?') {
Expand All @@ -1816,7 +1816,7 @@ export class InputHandler extends Disposable implements IInputHandler {
// cursor position
const y = this._terminal.buffer.y + 1;
const x = this._terminal.buffer.x + 1;
this._onData.fire(`${C0.ESC}[?${y};${x}R`);
this._coreService.triggerDataEvent(`${C0.ESC}[?${y};${x}R`);
break;
case 15:
// no printer
Expand Down
4 changes: 2 additions & 2 deletions src/SelectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ITerminal } from './Types';
import { IBuffer } from 'common/buffer/Types';
import { IBufferLine } from 'common/Types';
import { MockTerminal } from './TestUtils.test';
import { MockBufferService, MockOptionsService } from 'common/TestUtils.test';
import { MockBufferService, MockOptionsService, MockCoreService } from 'common/TestUtils.test';
import { BufferLine } from 'common/buffer/BufferLine';
import { IBufferService, IOptionsService } from 'common/services/Services';
import { MockCharSizeService, MockMouseService } from 'browser/TestUtils.test';
Expand All @@ -26,7 +26,7 @@ class TestSelectionManager extends SelectionManager {
bufferService: IBufferService,
optionsService: IOptionsService
) {
super(terminal, null, new MockCharSizeService(10, 10), bufferService, new MockMouseService(), optionsService);
super(terminal, null, new MockCharSizeService(10, 10), bufferService, new MockCoreService(), new MockMouseService(), optionsService);
}

public get model(): SelectionModel { return this._model; }
Expand Down
11 changes: 8 additions & 3 deletions src/SelectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CellData } from 'common/buffer/CellData';
import { IDisposable } from 'xterm';
import { EventEmitter, IEvent } from 'common/EventEmitter';
import { ICharSizeService, IMouseService } from 'browser/services/Services';
import { IBufferService, IOptionsService } from 'common/services/Services';
import { IBufferService, IOptionsService, ICoreService } from 'common/services/Services';
import { getCoordsRelativeToElement } from 'browser/input/Mouse';
import { moveToCellSequence } from 'browser/input/MoveToCell';

Expand Down Expand Up @@ -117,6 +117,7 @@ export class SelectionManager implements ISelectionManager {
private readonly _screenElement: HTMLElement,
private readonly _charSizeService: ICharSizeService,
private readonly _bufferService: IBufferService,
private readonly _coreService: ICoreService,
private readonly _mouseService: IMouseService,
private readonly _optionsService: IOptionsService
) {
Expand All @@ -137,7 +138,11 @@ export class SelectionManager implements ISelectionManager {
private _initListeners(): void {
this._mouseMoveListener = event => this._onMouseMove(<MouseEvent>event);
this._mouseUpListener = event => this._onMouseUp(<MouseEvent>event);

this._coreService.onUserInput(() => {
if (this.hasSelection) {
this.clearSelection();
}
});
this.initBuffersListeners();
}

Expand Down Expand Up @@ -661,7 +666,7 @@ export class SelectionManager implements ISelectionManager {
);
if (coordinates && coordinates[0] !== undefined && coordinates[1] !== undefined) {
const sequence = moveToCellSequence(coordinates[0] - 1, coordinates[1] - 1, this._bufferService, this._terminal.applicationCursor);
this._terminal.handler(sequence);
this._coreService.triggerDataEvent(sequence, true);
}
}
} else if (this.hasSelection) {
Expand Down
11 changes: 5 additions & 6 deletions src/Terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ describe('Terminal', () => {
});

describe('events', () => {
it('should fire the onData evnet', (done) => {
term.onData(() => done());
term.handler('fake');
});
// TODO: Add an onData test back
// it('should fire the onData evnet', (done) => {
// term.onData(() => done());
// term.handler('fake');
// });
it('should fire the onCursorMove event', (done) => {
term.onCursorMove(() => done());
term.write('foo');
Expand Down Expand Up @@ -142,7 +143,6 @@ describe('Terminal', () => {
};

beforeEach(() => {
term.handler = () => { };
term.showCursor = () => { };
term.clearSelection = () => { };
});
Expand Down Expand Up @@ -520,7 +520,6 @@ describe('Terminal', () => {
let evKeyPress: any;

beforeEach(() => {
term.handler = () => { };
term.showCursor = () => { };
term.clearSelection = () => { };
// term.compositionHelper = {
Expand Down
Loading

0 comments on commit bfa7a2d

Please sign in to comment.