Skip to content

Commit

Permalink
Merge branch 'master' into xterm-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar authored May 14, 2019
2 parents f331269 + 9cb6706 commit 955c459
Show file tree
Hide file tree
Showing 22 changed files with 1,043 additions and 151 deletions.
38 changes: 23 additions & 15 deletions demo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
/// <reference path="../typings/xterm.d.ts"/>

import { Terminal } from '../lib/public/Terminal';
import * as attach from '../lib/addons/attach/attach';
import { AttachAddon } from 'xterm-addon-attach';
import { SearchAddon, ISearchOptions } from 'xterm-addon-search';
import { WebLinksAddon } from 'xterm-addon-web-links';

import * as fit from '../lib/addons/fit/fit';
import * as fullscreen from '../lib/addons/fullscreen/fullscreen';
import * as search from '../lib/addons/search/search';
import * as webLinks from '../lib/addons/webLinks/webLinks';
import { ISearchOptions } from '../lib/addons/search/Interfaces';

// Pulling in the module's types relies on the <reference> above, it's looks a
// little weird here as we're importing "this" module
Expand All @@ -25,14 +24,10 @@ export interface IWindowWithTerminal extends Window {
}
declare let window: IWindowWithTerminal;

Terminal.applyAddon(attach);
Terminal.applyAddon(fit);
Terminal.applyAddon(fullscreen);
Terminal.applyAddon(search);
Terminal.applyAddon(webLinks);


let term;
let searchAddon: SearchAddon;
let protocol;
let socketURL;
let socket;
Expand Down Expand Up @@ -85,10 +80,18 @@ function createTerminal(): void {
while (terminalContainer.children.length) {
terminalContainer.removeChild(terminalContainer.children[0]);
}

const isWindows = ['Windows', 'Win16', 'Win32', 'WinCE'].indexOf(navigator.platform) >= 0;
term = new Terminal({
windowsMode: isWindows
} as ITerminalOptions);

// Load addons
const typedTerm = term as TerminalType;
typedTerm.loadAddon(new WebLinksAddon());
searchAddon = new SearchAddon();
typedTerm.loadAddon(searchAddon);

window.term = term; // Expose `term` to window for debugging purposes
term.onResize((size: { cols: number, rows: number }) => {
if (!pid) {
Expand All @@ -104,8 +107,6 @@ function createTerminal(): void {
socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/terminals/';

term.open(terminalContainer);

term.webLinksInit();
term.fit();
term.focus();

Expand All @@ -114,12 +115,12 @@ function createTerminal(): void {
addDomListener(actionElements.findNext, 'keyup', (e) => {
const searchOptions = getSearchOptions();
searchOptions.incremental = e.key !== `Enter`;
term.findNext(actionElements.findNext.value, searchOptions);
searchAddon.findNext(actionElements.findNext.value, searchOptions);
});

addDomListener(actionElements.findPrevious, 'keyup', (e) => {
if (e.key === `Enter`) {
term.findPrevious(actionElements.findPrevious.value, getSearchOptions());
searchAddon.findPrevious(actionElements.findPrevious.value, getSearchOptions());
}
});

Expand Down Expand Up @@ -148,7 +149,14 @@ function createTerminal(): void {
}

function runRealTerminal(): void {
term.attach(socket);
/**
* The demo defaults to string transport by default.
* To run it with UTF8 binary transport, swap comment on
* the lines below. (Must also be switched in server.js)
*/
term.loadAddon(new AttachAddon(socket));
// term.loadAddon(new AttachAddon(socket, {inputUtf8: true}));

term._initialized = true;
}

Expand Down
3 changes: 1 addition & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<title>xterm.js demo</title>
<link rel="stylesheet" href="/src/xterm.css" />
<link rel="stylesheet" href="/src/addons/fullscreen/fullscreen.css" />
<link rel="stylesheet" href="style.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/4.1.1/es6-promise.auto.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.min.js"></script>
Expand All @@ -16,7 +15,7 @@ <h3>Actions</h3>
<p>
<label>Find next <input id="find-next"/></label>
<label>Find previous <input id="find-previous"/></label>
<label>Use regex<input type="checkbox" id="regex"/></label>
<label>Use regex<input type="checkbox" id="regex"/></label>
<label>Case sensitive<input type="checkbox" id="case-sensitive"/></label>
<label>Whole word<input type="checkbox" id="whole-word"/></label>
</p>
Expand Down
31 changes: 29 additions & 2 deletions demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ var expressWs = require('express-ws');
var os = require('os');
var pty = require('node-pty');

/**
* Whether to use UTF8 binary transport.
* (Must also be switched in client.ts)
*/
const USE_BINARY_UTF8 = false;


function startServer() {
var app = express();
expressWs(app);
Expand Down Expand Up @@ -36,7 +43,8 @@ function startServer() {
cols: cols || 80,
rows: rows || 24,
cwd: process.env.PWD,
env: process.env
env: process.env,
encoding: USE_BINARY_UTF8 ? null : 'utf8'
});

console.log('Created terminal with PID: ' + term.pid);
Expand Down Expand Up @@ -65,6 +73,7 @@ function startServer() {
console.log('Connected to terminal ' + term.pid);
ws.send(logs[term.pid]);

// string message buffering
function buffer(socket, timeout) {
let s = '';
let sender = null;
Expand All @@ -79,7 +88,25 @@ function startServer() {
}
};
}
const send = buffer(ws, 5);
// binary message buffering
function bufferUtf8(socket, timeout) {
let buffer = [];
let sender = null;
let length = 0;
return (data) => {
buffer.push(data);
length += data.length;
if (!sender) {
sender = setTimeout(() => {
socket.send(Buffer.concat(buffer, length));
buffer = [];
sender = null;
length = 0;
}, timeout);
}
};
}
const send = USE_BINARY_UTF8 ? bufferUtf8(ws, 5) : buffer(ws, 5);

term.on('data', function(data) {
try {
Expand Down
4 changes: 3 additions & 1 deletion demo/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ const clientConfig = {
{
test: /\.js$/,
use: ["source-map-loader"],
enforce: "pre"
enforce: "pre",
exclude: /node_modules/
}
]
},
resolve: {
modules: [path.resolve(__dirname, '..'), 'node_modules'],
extensions: [ '.tsx', '.ts', '.js' ]
},
output: {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/mocha": "^2.2.33",
"@types/node": "6.0.108",
"@types/puppeteer": "^1.12.4",
"@types/utf8": "^2.1.6",
"@types/webpack": "^4.4.11",
"browserify": "^13.3.0",
"chai": "3.5.0",
Expand Down Expand Up @@ -40,10 +41,14 @@
"tslint": "^5.9.1",
"tslint-consistent-codestyle": "^1.13.0",
"typescript": "3.4",
"utf8": "^3.0.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0",
"xterm-addon-attach": "0.1.0-beta8",
"xterm-addon-search": "0.1.0-beta4",
"xterm-addon-web-links": "0.1.0-beta6",
"zmodem.js": "^0.1.5"
},
"scripts": {
Expand Down
29 changes: 28 additions & 1 deletion src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EscapeSequenceParser } from './EscapeSequenceParser';
import { IDisposable } from 'xterm';
import { Disposable } from './common/Lifecycle';
import { concat } from './common/TypedArrayUtils';
import { StringToUtf32, stringFromCodePoint, utf32ToString } from './core/input/TextDecoder';
import { StringToUtf32, stringFromCodePoint, utf32ToString, Utf8ToUtf32 } from './core/input/TextDecoder';
import { CellData, Attributes, FgFlags, BgFlags, AttributeData, NULL_CELL_WIDTH, NULL_CELL_CODE, DEFAULT_ATTR_DATA } from './core/buffer/BufferLine';
import { EventEmitter2, IEvent } from './common/EventEmitter2';

Expand Down Expand Up @@ -104,6 +104,7 @@ class DECRQSS implements IDcsHandler {
export class InputHandler extends Disposable implements IInputHandler {
private _parseBuffer: Uint32Array = new Uint32Array(4096);
private _stringDecoder: StringToUtf32 = new StringToUtf32();
private _utf8Decoder: Utf8ToUtf32 = new Utf8ToUtf32();
private _workCell: CellData = new CellData();

private _onCursorMove = new EventEmitter2<void>();
Expand Down Expand Up @@ -318,6 +319,32 @@ export class InputHandler extends Disposable implements IInputHandler {
}
}

public parseUtf8(data: Uint8Array): void {
// Ensure the terminal is not disposed
if (!this._terminal) {
return;
}

let buffer = this._terminal.buffer;
const cursorStartX = buffer.x;
const cursorStartY = buffer.y;

// TODO: Consolidate debug/logging #1560
if ((<any>this._terminal).debug) {
this._terminal.log('data: ' + data);
}

if (this._parseBuffer.length < data.length) {
this._parseBuffer = new Uint32Array(data.length);
}
this._parser.parse(this._parseBuffer, this._utf8Decoder.decode(data, this._parseBuffer));

buffer = this._terminal.buffer;
if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) {
this._terminal.emit('cursormove');
}
}

public print(data: Uint32Array, start: number, end: number): void {
let code: number;
let chWidth: number;
Expand Down
2 changes: 1 addition & 1 deletion src/Terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestTerminal extends Terminal {
public keyPress(ev: any): boolean { return this._keyPress(ev); }
}

describe('xterm.js', () => {
describe('Terminal', () => {
let term: TestTerminal;
const termOptions = {
cols: INIT_COLS,
Expand Down
Loading

0 comments on commit 955c459

Please sign in to comment.