|
1 | 1 | <html lang="en"> |
2 | | -<head> |
3 | | - <meta charset="utf-8"> |
4 | | - <title>pyxterm.js</title> |
5 | | - <style> |
6 | | - html { |
7 | | - font-family: arial; |
8 | | - } |
9 | | - </style> |
10 | | - <link rel=" stylesheet" href=" https://unpkg.com/[email protected]/css/xterm.css" /> |
11 | | -</head> |
12 | | -<body> |
| 2 | + <head> |
| 3 | + <meta charset="utf-8" /> |
| 4 | + <title>pyxterm.js</title> |
| 5 | + <style> |
| 6 | + html { |
| 7 | + font-family: arial; |
| 8 | + } |
| 9 | + </style> |
| 10 | + <link |
| 11 | + rel="stylesheet" |
| 12 | + href=" https://unpkg.com/[email protected]/css/xterm.css" |
| 13 | + /> |
| 14 | + </head> |
| 15 | + <body> |
| 16 | + <span style="font-size: 1.4em">pyxterm.js</span> |
| 17 | + <span style="font-size: small" |
| 18 | + >status: |
| 19 | + <span style="font-size: small" id="status">connecting...</span></span |
| 20 | + > |
13 | 21 |
|
14 | | -<span style="font-size: 1.4em;">pyxterm.js</span> |
15 | | -<span style="font-size: small;">status: <span style="font-size: small;" id="status">connecting...</span></span> |
| 22 | + <div style="width: 100%; height: calc(100% - 50px)" id="terminal"></div> |
16 | 23 |
|
17 | | -<div style="width: 100%; height: calc(100% - 50px);" id="terminal"></div> |
18 | | - |
19 | | -<p style="text-align: right; font-size: small;"> |
20 | | - built by <a href="https://grassfedcode.com">Chad Smith</a> <a href="https://github.com/cs01">GitHub</a> |
21 | | -</p> |
22 | | -<!-- xterm --> |
23 | | -<script src=" https://unpkg.com/[email protected]/lib/xterm.js" ></script> |
24 | | -<script src=" https://unpkg.com/[email protected]/lib/xterm-addon-fit.js" ></script> |
25 | | -<script src=" https://unpkg.com/[email protected]/lib/xterm-addon-web-links.js" ></script> |
26 | | -<script src=" https://unpkg.com/[email protected]/lib/xterm-addon-sear |
| 24 | + <p style="text-align: right; font-size: small"> |
| 25 | + built by <a href="https://chadsmith.dev">Chad Smith</a> |
| 26 | + <a href="https://github.com/cs01">GitHub</a> |
| 27 | + </p> |
| 28 | + <!-- xterm --> |
| 29 | + <script src=" https://unpkg.com/[email protected]/lib/xterm.js" ></script> |
| 30 | + <script src=" https://unpkg.com/[email protected]/lib/xterm-addon-fit.js" ></script> |
| 31 | + <script src=" https://unpkg.com/[email protected]/lib/xterm-addon-web-links.js" ></script> |
| 32 | + <script src=" https://unpkg.com/[email protected]/lib/xterm-addon-sear |
27 | 33 | ch.js"></script> |
28 | | -<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script> |
| 34 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script> |
29 | 35 |
|
30 | | -<script> |
31 | | - const term = new Terminal({ |
| 36 | + <script> |
| 37 | + const term = new Terminal({ |
32 | 38 | cursorBlink: true, |
33 | 39 | macOptionIsMeta: true, |
34 | 40 | scrollback: true, |
35 | | - }); |
36 | | - // https://github.com/xtermjs/xterm.js/issues/2941 |
37 | | - const fit = new FitAddon.FitAddon(); |
38 | | - term.loadAddon(fit); |
39 | | - term.loadAddon(new WebLinksAddon.WebLinksAddon()); |
40 | | - term.loadAddon(new SearchAddon.SearchAddon()); |
41 | | - |
42 | | - term.open(document.getElementById('terminal')); |
43 | | - fit.fit() |
44 | | - term.resize(15, 50) |
45 | | - console.log(`size: ${term.cols} columns, ${term.rows} rows`) |
46 | | - fit.fit() |
47 | | - term.write("Welcome to pyxterm.js!\nhttps://github.com/cs01/pyxterm.js\n") |
48 | | - term.onKey((key, ev) => { |
49 | | - console.log("pressed key", key) |
50 | | - console.log("event", ev) |
51 | | - socket.emit("pty-input", {"input": key}) |
52 | | - }); |
53 | | - |
54 | | - const socket = io.connect('/pty'); |
55 | | - const status = document.getElementById("status") |
56 | | - |
57 | | - socket.on("pty-output", function(data){ |
58 | | - console.log("new output", data) |
59 | | - term.write(data.output) |
60 | | - }) |
| 41 | + }); |
| 42 | + // https://github.com/xtermjs/xterm.js/issues/2941 |
| 43 | + const fit = new FitAddon.FitAddon(); |
| 44 | + term.loadAddon(fit); |
| 45 | + term.loadAddon(new WebLinksAddon.WebLinksAddon()); |
| 46 | + term.loadAddon(new SearchAddon.SearchAddon()); |
61 | 47 |
|
62 | | - socket.on("connect", () => { |
63 | | - fitToscreen() |
64 | | - status.innerHTML = '<span style="background-color: lightgreen;">connected</span>' |
65 | | - } |
66 | | - ) |
| 48 | + term.open(document.getElementById("terminal")); |
| 49 | + fit.fit(); |
| 50 | + term.resize(15, 50); |
| 51 | + console.log(`size: ${term.cols} columns, ${term.rows} rows`); |
| 52 | + fit.fit(); |
| 53 | + term.writeln("Welcome to pyxterm.js!"); |
| 54 | + term.writeln("https://github.com/cs01/pyxterm.js"); |
| 55 | + term.onData((data) => { |
| 56 | + console.log("key pressed in browser:", data); |
| 57 | + socket.emit("pty-input", { input: data }); |
| 58 | + }); |
67 | 59 |
|
68 | | - socket.on("disconnect", () => { |
69 | | - status.innerHTML = '<span style="background-color: #ff8383;">disconnected</span>' |
70 | | - }) |
| 60 | + const socket = io.connect("/pty"); |
| 61 | + const status = document.getElementById("status"); |
71 | 62 |
|
72 | | - function fitToscreen(){ |
73 | | - fit.fit() |
74 | | - socket.emit("resize", {"cols": term.cols, "rows": term.rows}) |
75 | | - } |
| 63 | + socket.on("pty-output", function (data) { |
| 64 | + console.log("new output received from server:", data.output); |
| 65 | + term.write(data.output); |
| 66 | + }); |
76 | 67 |
|
77 | | - function debounce(func, wait_ms) { |
78 | | - let timeout |
79 | | - return function(...args) { |
80 | | - const context = this |
81 | | - clearTimeout(timeout) |
82 | | - timeout = setTimeout(() => func.apply(context, args), wait_ms) |
83 | | - } |
84 | | - } |
| 68 | + socket.on("connect", () => { |
| 69 | + fitToscreen(); |
| 70 | + status.innerHTML = |
| 71 | + '<span style="background-color: lightgreen;">connected</span>'; |
| 72 | + }); |
85 | 73 |
|
86 | | - const wait_ms = 50; |
87 | | - window.onresize = debounce(fitToscreen, wait_ms) |
| 74 | + socket.on("disconnect", () => { |
| 75 | + status.innerHTML = |
| 76 | + '<span style="background-color: #ff8383;">disconnected</span>'; |
| 77 | + }); |
88 | 78 |
|
| 79 | + function fitToscreen() { |
| 80 | + fit.fit(); |
| 81 | + const dims = { cols: term.cols, rows: term.rows }; |
| 82 | + console.log("sending new dimensions to server's pty", dims); |
| 83 | + socket.emit("resize", dims); |
| 84 | + } |
89 | 85 |
|
90 | | -</script> |
| 86 | + function debounce(func, wait_ms) { |
| 87 | + let timeout; |
| 88 | + return function (...args) { |
| 89 | + const context = this; |
| 90 | + clearTimeout(timeout); |
| 91 | + timeout = setTimeout(() => func.apply(context, args), wait_ms); |
| 92 | + }; |
| 93 | + } |
91 | 94 |
|
92 | | -</body> |
| 95 | + const wait_ms = 50; |
| 96 | + window.onresize = debounce(fitToscreen, wait_ms); |
| 97 | + </script> |
| 98 | + </body> |
93 | 99 | </html> |
0 commit comments