-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexample.js
62 lines (55 loc) · 1.73 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* trzsz: https://github.com/trzsz/trzsz.js
* Copyright(c) 2022 Lonny Wong <lonnywong@qq.com>
* @license MIT
*/
/**
* Initialize electron example for trzsz
* @function
* @param {object} terminal - the div element for xterm
*/
InitElectronExapmle = (terminal) => {
const term = new Terminal();
const fit = new FitAddon.FitAddon();
term.loadAddon(fit);
term.open(terminal);
fit.fit();
const cmd = window.platform === "win32" ? "powershell.exe" : "bash";
const pty = window.spawnPTY(cmd, [], {
name: "xterm-color",
cols: term.cols,
rows: term.rows,
encoding: null,
});
const trzsz = window.newTrzsz(
// write the server output to the terminal
(output) => term.write(output),
// send the user input to the server
(input) => pty.write(input),
// the terminal columns
term.cols,
// there is a windows shell
window.platform === "win32"
);
// let trzsz process the server output
pty.on("data", (data) => trzsz.processServerOutput(data));
// let trzsz process the user input
term.onData((data) => trzsz.processTerminalInput(data));
term.onBinary((data) => trzsz.processBinaryInput(data));
term.onResize((size) => {
pty.resize(size.cols, size.rows);
// tell trzsz the terminal columns has been changed
trzsz.setTerminalColumns(term.cols);
});
window.addEventListener("resize", () => fit.fit());
term.focus();
// enable drag files or directories to upload
terminal.addEventListener("dragover", (event) => event.preventDefault());
terminal.addEventListener("drop", (event) => {
event.preventDefault();
trzsz
.uploadFiles(event.dataTransfer.items)
.then(() => console.log("upload success"))
.catch((err) => console.log(err));
});
};