Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/web/cowasm.sh/src/terminal.ts
1067 views
1
import "xterm/css/xterm.css";
2
import { Terminal } from "xterm";
3
import { WebLinksAddon } from "xterm-addon-web-links";
4
import setTheme from "./theme";
5
import dashWasm from "dash-wasm";
6
7
8
// https://patorjk.com/software/taag/#p=display&f=Ogre&t=CoCalc%0ACoWasm
9
10
export default async function terminal(element: HTMLDivElement) {
11
console.log("creating dashWasm");
12
const dash = await dashWasm();
13
(window as any).dash = dash;
14
const t = new Date();
15
console.log("dash.init done; time = ", new Date().valueOf() - t.valueOf());
16
const term = new Terminal({ convertEol: true });
17
term.open(element);
18
// @ts-ignore
19
element.children[0].style.padding = "15px";
20
term.resize(128, 40);
21
setTheme(term, "solarized-light");
22
23
term.options.allowProposedApi = true;
24
term.loadAddon(new WebLinksAddon());
25
26
term.write(
27
"Type 'ls /usr/bin' for a list of commands, including python (with numpy), lua, sqlite3, date, and du.\n"
28
);
29
term.write(
30
"This is new and *many* things are not implemented. Output redirection and capture is not implemented.\n"
31
);
32
term.write("Visit https://github.com/sagemathinc/cowasm and contribute.\n\n");
33
term.write(` ___ __ __
34
/ __\\___/ / /\\ \\ \\__ _ ___ _ __ ___
35
/ / / _ \\ \\/ \\/ / _\` / __| '_ \` _ \\
36
/ /__| (_) \\ /\\ / (_| \\__ \\ | | | | |
37
\\____/\\___/ \\/ \\/ \\__,_|___/_| |_| |_|
38
39
`);
40
term.onData((data) => {
41
dash.kernel.writeToStdin(data);
42
});
43
dash.kernel.on("stdout", (data) => {
44
term.write(data);
45
});
46
dash.kernel.on("stderr", (data) => {
47
term.write(data);
48
});
49
console.log("starting terminal");
50
const r = await dash.terminal();
51
console.log("terminal terminated", r);
52
dash.kernel.terminate();
53
}
54
55