-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvtx.js
More file actions
317 lines (247 loc) · 9.66 KB
/
vtx.js
File metadata and controls
317 lines (247 loc) · 9.66 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"use strict";
var readline = { last_cx : -1 , index : 0, history : ["help()"] }
// two modes based on RAW_MODE: default readline emulation or python.rawstdin
readline.complete = function (line) {
if ( readline.history[ readline.history.length -1 ] != line )
readline.history.push(line);
readline.index = 0;
python.readline(line + "\n")
}
if (!window.Terminal) {
var xterm_cdn
if (window.Module.config && window.Module.config.cdn) {
xterm_cdn = window.Module.config.cdn+"../vt/"
console.log("Terminal+ImageAddon importing from CDN :", xterm_cdn)
} else {
xterm_cdn = xterm_cdn || "https://pygame-web.github.io/cdn/vt/"
console.warn("Terminal+ImageAddon importing from fallback ", xterm_cdn)
}
for (const css of ["xterm.css"]) { //,"style.css"]) {
const cssref = document.createElement('link')
cssref.setAttribute("rel", "stylesheet")
cssref.setAttribute("type", "text/css")
cssref.setAttribute("href", xterm_cdn + css)
document.getElementsByTagName("head")[0].appendChild(cssref)
}
await import(xterm_cdn + "xterm.js")
await import(xterm_cdn + "xterm-addon-image.js")
} else {
console.warn("Terminal+ImageAddon were inlined")
}
export class WasmTerminal {
constructor(hostid, cols, rows, fontsize, is_fbdev, addons_list) {
this.input = ''
this.resolveInput = null
this.activeInput = true
this.inputStartCursor = null
this.nodup = 1
var theme = {
background: '#1a1c1f'
}
var transparency = false
var sback = 1000
if (is_fbdev) {
theme = {
foreground: '#ffffff',
background: 'rgba(0, 0, 0, 0)'
}
sback = 0
transparency = true
}
this.xterm = new Terminal(
{
// rendererType : "dom",
rendererType : "webgl",
experimentalCharAtlas : "webgl",
theme: theme,
allowTransparency: transparency,
allowProposedApi : true , // xterm 0.5 + sixel
scrollback: sback,
fontFamily: 'Courier-new, courier, monospace',
fontSize: (fontsize || 12),
cols: (cols || 132),
rows: (rows || 32)
}
);
if (typeof(Worker) !== "undefined") {
for (const addon of (addons_list||[]) ) {
console.warn(hostid, cols, rows, addon)
const imageAddon = new ImageAddon.ImageAddon(addon.url , addon);
this.xterm.loadAddon(imageAddon);
this.sixel = function write(data) {
this.xterm.write(data)
}
}
} else {
console.warn("No worker support, not loading xterm addons")
this.sixel = function ni() {
console.warn("SIXEL N/I")
}
}
this.xterm.open(document.getElementById(hostid))
this.xterm.onKey((keyEvent) => {
// Fix for iOS Keyboard Jumping on space
if (keyEvent.key === " ") {
keyEvent.domEvent.preventDefault();
}
});
this.xterm.onData(this.handleTermData)
}
open(container) {
this.xterm.open(container);
}
ESC() {
for (var i=0; i < arguments.length; i++)
this.xterm.write("\x1b"+arguments[i])
}
handleTermData = (data) => {
// TODO: check mouse Y pos for raw mode in debug mode
if (window.RAW_MODE) {
python.rawstdin(data)
return
}
const ord = data.charCodeAt(0);
let ofs;
const cx = this.xterm.buffer.active.cursorX
// TODO: Handle ANSI escape sequences
if (ord === 0x1b) {
// Handle special characters
switch ( data.charCodeAt(1) ) {
case 0x5b:
const cursor = readline.history.length + readline.index
var histo = ">h> "
switch ( data.charCodeAt(2) ) {
// "?"
case 63:
const c4 = data.charCodeAt(4)
const c5 = data.charCodeAt(5)
if ((c4==54) && (c5==99)) {
// Primary Device Attribute of Sixel support : 4
// "?6c" https://github.com/odknt/st/issues/1
console.log("query")
}
case 65:
//console.log("VT UP")
// memo cursor pos before entering histo
if (!readline.index) {
if (readline.last_cx < 0 ) {
readline.last_cx = cx
readline.buffer = this.input
}
// TODO: get current line content from XTERM
}
if ( cursor >0 ) {
readline.index--
histo = ">h> " +readline.history[cursor-1]
//console.log(__FILE__," histo-up :", readline.index, cursor, histo)
this.ESC("[132D","[2K")
this.xterm.write(histo)
this.input = histo.substr(4)
}
break;
case 66:
//console.log("VT DOWN")
if ( readline.index < 0 ) {
readline.index++
histo = histo + readline.history[cursor]
this.ESC("[132D","[2K")
this.xterm.write(histo)
this.input = histo.substr(4)
} else {
// we are back
if (readline.last_cx >= 0) {
histo = histo + readline.buffer
readline.buffer = ""
this.ESC("[2K")
this.ESC("[132D")
this.xterm.write(histo)
this.input = histo.substr(4)
this.ESC("[132D")
this.ESC("["+readline.last_cx+"C")
//console.log(__FILE__," histo-back", readline.index, cursor, histo)
readline.last_cx = -1
}
}
break;
case 67:
//console.log("VT RIGHT")
break;
case 68:
//console.log("VT LEFT")
break;
case 60:
// python.rawstdin(data)
break;
default:
console.log(__FILE__,"VT unhandled ? "+data.charCodeAt(2))
}
break
default:
console.log(__FILE__,"VT ESC "+data.charCodeAt(1))
}
} else if (ord < 32 || ord === 0x7f) {
switch (data) {
case "\r": // ENTER
case "\x0a": // CTRL+J
case "\x0d": // CTRL+M
this.xterm.write('\r\n');
readline.complete(this.input)
this.input = '';
break;
case "\x7F": // BACKSPACE
case "\x08": // CTRL+H
case "\x04": // CTRL+D
this.handleCursorErase(true);
break;
case "\0x03": // CTRL+C
break
// ^L for clearing VT but keep X pos.
case "\x0c":
const cy = this.xterm.buffer.active.cursorY
if (cy < this.xterm.rows )
this.ESC("[B","[J","[A")
this.ESC("[A","[K","[1J")
for (var i=1;i<cy;i++) {
this.ESC("[A","[M")
}
this.ESC("[M")
if ( cx>0 )
this.ESC("["+cx+"C")
break;
default:
switch (ord) {
case 3:
readline.complete("raise KeyboardInterrupt")
break
default :
console.log("vt:" + ord )
}
}
} else {
this.input += data;
this.xterm.write(data)
}
}
handleCursorErase() {
// Don't delete past the start of input
if (this.xterm.buffer.active.cursorX <= this.inputStartCursor) {
return
}
this.input = this.input.slice(0, -1)
this.xterm.write('\x1B[D')
this.xterm.write('\x1B[P')
}
clear() {
this.xterm.clear()
}
// direct write
sixel(data) {
this.xterm.write(data)
}
print(message) {
const normInput = message.replace(/[\r\n]+/g, "\n").replace(/\n/g, "\r\n")
this.xterm.write(normInput)
}
}
window.WasmTerminal = WasmTerminal
window.readline = readline