Skip to content

Commit 8d0b7f5

Browse files
authored
Merge pull request #19 from coolreader18/xtermv4
Update to work with xtermjs 4
2 parents 8bc74e0 + 5bbd5b8 commit 8d0b7f5

2 files changed

Lines changed: 44 additions & 10 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ The local echo controller tries to replicate most of the bash-like user experien
4343
const term = new Terminal();
4444
term.open(document.getElementById('terminal'));
4545
46-
// Create a local echo controller
46+
// Create a local echo controller (xterm.js v3)
4747
const localEcho = new LocalEchoController(term);
48+
// Create a local echo controller (xterm.js >=v4)
49+
const localEcho = new LocalEchoController();
50+
term.loadAddon(localEcho);
4851
4952
// Read a single line from the user
5053
localEcho.read("~$ ")
@@ -68,8 +71,11 @@ The local echo controller tries to replicate most of the bash-like user experien
6871
const term = new Terminal();
6972
term.open(document.getElementById('terminal'));
7073
71-
// Create a local echo controller
74+
// Create a local echo controller (xterm.js v3)
7275
const localEcho = new LocalEchoController(term);
76+
// Create a local echo controller (xterm.js >=v4)
77+
const localEcho = new LocalEchoController();
78+
term.loadAddon(localEcho);
7379
7480
// Read a single line from the user
7581
localEcho.read("~$ ")

lib/LocalEchoController.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
* - Auto-complete hooks
2424
*/
2525
export default class LocalEchoController {
26-
constructor(term, options = {}) {
26+
constructor(term = null, options = {}) {
2727
this.term = term;
2828
this._handleTermData = this.handleTermData.bind(this);
2929
this._handleTermResize = this.handleTermResize.bind(this)
@@ -38,11 +38,25 @@ export default class LocalEchoController {
3838
this._activePrompt = null;
3939
this._activeCharPrompt = null;
4040
this._termSize = {
41-
cols: this.term.cols,
42-
rows: this.term.rows
41+
cols: 0,
42+
rows: 0,
4343
};
44+
45+
this._disposables = [];
4446

45-
this.attach()
47+
if (term) {
48+
if (term.loadAddon) term.loadAddon(this);
49+
else this.attach();
50+
}
51+
}
52+
53+
// xterm.js new plugin API:
54+
activate(term) {
55+
this.term = term;
56+
this.attach();
57+
}
58+
dispose() {
59+
this.detach();
4660
}
4761

4862
/////////////////////////////////////////////////////////////////////////////
@@ -53,16 +67,30 @@ export default class LocalEchoController {
5367
* Detach the controller from the terminal
5468
*/
5569
detach() {
56-
this.term.off("data", this._handleTermData);
57-
this.term.off("resize", this._handleTermResize);
70+
if (this.term.off) {
71+
this.term.off("data", this._handleTermData);
72+
this.term.off("resize", this._handleTermResize);
73+
} else {
74+
this._disposables.forEach(d => d.dispose());
75+
this._disposables = [];
76+
}
5877
}
5978

6079
/**
6180
* Attach controller to the terminal, handling events
6281
*/
6382
attach() {
64-
this.term.on("data", this._handleTermData);
65-
this.term.on("resize", this._handleTermResize);
83+
if (this.term.on) {
84+
this.term.on("data", this._handleTermData);
85+
this.term.on("resize", this._handleTermResize);
86+
} else {
87+
this._disposables.push(this.term.onData(this._handleTermData));
88+
this._disposables.push(this.term.onResize(this._handleTermResize));
89+
}
90+
this._termSize = {
91+
cols: this.term.cols,
92+
rows: this.term.rows,
93+
};
6694
}
6795

6896
/**

0 commit comments

Comments
 (0)