diff --git a/src/InputHandler.ts b/src/InputHandler.ts new file mode 100644 index 0000000000..38b853671a --- /dev/null +++ b/src/InputHandler.ts @@ -0,0 +1,50 @@ +import { IInputHandler, ITerminal } from './Interfaces'; + +export class InputHandler implements IInputHandler { + // TODO: We want to type _terminal when it's pulled into TS + constructor(private _terminal: any) { } + + public bell(): void { + if (!this._terminal.visualBell) { + return; + } + this._terminal.element.style.borderColor = 'white'; + setTimeout(() => this._terminal.element.style.borderColor = '', 10); + if (this._terminal.popOnBell) { + this._terminal.focus(); + } + } + + public lineFeed(): void { + if (this._terminal.convertEol) { + this._terminal.x = 0; + } + this._terminal.y++; + if (this._terminal.y > this._terminal.scrollBottom) { + this._terminal.y--; + this._terminal.scroll(); + } + } + + public carriageReturn(): void { + this._terminal.x = 0; + } + + public backspace(): void { + if (this._terminal.x > 0) { + this._terminal.x--; + } + } + + public tab(): void { + this._terminal.x = this._terminal.nextStop(); + } + + public shiftOut(): void { + this._terminal.setgLevel(1); + } + + public shiftIn(): void { + this._terminal.setgLevel(0); + } +} diff --git a/src/Interfaces.ts b/src/Interfaces.ts index ff7bbef2cc..ae05c79f63 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -32,3 +32,16 @@ export interface ITerminal { scrollDisp(disp: number, suppressScrollEvent: boolean); cancel(ev: Event, force?: boolean); } + +/** + * Handles actions generated by the parser. + */ +export interface IInputHandler { + bell(): void; + lineFeed(): void; + carriageReturn(): void; + backspace(): void; + tab(): void; + shiftOut(): void; + shiftIn(): void; +} diff --git a/src/xterm.js b/src/xterm.js index 0d9e1ca8bf..f33f0a9ccd 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -16,6 +16,7 @@ import { Viewport } from './Viewport.js'; import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard.js'; import { CircularList } from './utils/CircularList.js'; import { C0 } from './EscapeSequences'; +import { InputHandler } from './InputHandler'; import * as Browser from './utils/Browser'; import * as Keyboard from './utils/Keyboard'; @@ -203,6 +204,8 @@ function Terminal(options) { this.prefix = ''; this.postfix = ''; + this.inputHandler = new InputHandler(this); + // leftover surrogate high from previous write invocation this.surrogate_high = ''; @@ -1378,47 +1381,38 @@ Terminal.prototype.write = function(data) { case normal: switch (ch) { case C0.BEL: - this.bell(); + this.inputHandler.bell(); break; case C0.LF: case C0.VT: case C0.FF: - if (this.convertEol) { - this.x = 0; - } - this.y++; - if (this.y > this.scrollBottom) { - this.y--; - this.scroll(); - } + this.inputHandler.lineFeed(); break; // '\r' case C0.CR: - this.x = 0; + this.inputHandler.carriageReturn(); break; // '\b' case C0.BS: - if (this.x > 0) { - this.x--; - } + this.inputHandler.backspace(); break; // '\t' case C0.HT: - this.x = this.nextStop(); + this.inputHandler.tab(); break; // shift out case C0.SO: - this.setgLevel(1); + this.inputHandler.shiftOut(); break; // shift in case C0.SI: - this.setgLevel(0); + this.inputHandler.shiftIn(); break; // '\e'