Skip to content

Commit

Permalink
Create InputHandler which performs C0 actions
Browse files Browse the repository at this point in the history
Part of xtermjs#459
  • Loading branch information
Tyriar committed Jan 9, 2017
1 parent 1685cf2 commit 04b1ebf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
50 changes: 50 additions & 0 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
26 changes: 10 additions & 16 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 = '';

Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 04b1ebf

Please sign in to comment.