2024-03-04 09:01:54 +08:00
|
|
|
// Copyright 2024 The TCell Authors
|
2023-01-03 10:46:41 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the license at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
const wasmFilePath = "main.wasm";
|
|
|
|
const term = document.getElementById("terminal");
|
|
|
|
var width = 80;
|
|
|
|
var height = 24;
|
2023-01-03 09:29:40 +08:00
|
|
|
const beepAudio = new Audio("beep.wav");
|
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
var cx = -1;
|
|
|
|
var cy = -1;
|
|
|
|
var cursorClass = "cursor-blinking-block";
|
2024-03-07 23:28:12 +08:00
|
|
|
var cursorColor = "";
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
var content; // {data: row[height], dirty: bool}
|
2023-01-03 09:29:40 +08:00
|
|
|
// row = {data: element[width], previous: span}
|
2023-02-21 07:41:09 +08:00
|
|
|
// dirty/[previous being null] indicates if previous (or entire terminal) needs to be recalculated.
|
2023-01-03 09:29:40 +08:00
|
|
|
// dirty is true/null if terminal/previous need to be re-calculated/shown
|
|
|
|
|
|
|
|
function initialize() {
|
2024-03-04 09:01:54 +08:00
|
|
|
resize(width, height); // initialize content
|
|
|
|
show(); // then show the screen
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function resize(w, h) {
|
2024-03-04 09:01:54 +08:00
|
|
|
width = w;
|
|
|
|
height = h;
|
|
|
|
content = { data: new Array(height), dirty: true };
|
|
|
|
for (let i = 0; i < height; i++) {
|
|
|
|
content.data[i] = { data: new Array(width), previous: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
clearScreen();
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearScreen(fg, bg) {
|
2024-03-04 09:01:54 +08:00
|
|
|
if (fg) {
|
|
|
|
term.style.color = intToHex(fg);
|
|
|
|
}
|
|
|
|
if (bg) {
|
|
|
|
term.style.backgroundColor = intToHex(bg);
|
|
|
|
}
|
|
|
|
|
|
|
|
content.dirty = true;
|
|
|
|
for (let i = 0; i < height; i++) {
|
|
|
|
content.data[i].previous = null; // we set the row to be recalculated later
|
|
|
|
for (let j = 0; j < width; j++) {
|
|
|
|
content.data[i].data[j] = document.createTextNode(" "); // set the entire row to spaces.
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
2024-03-04 09:01:54 +08:00
|
|
|
}
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
2024-03-10 10:15:42 +08:00
|
|
|
function drawCell(x, y, s, fg, bg, attrs, us, uc) {
|
2024-03-04 09:01:54 +08:00
|
|
|
var span = document.createElement("span");
|
|
|
|
var use = false;
|
|
|
|
|
|
|
|
if ((attrs & (1 << 2)) != 0) {
|
|
|
|
// reverse video
|
|
|
|
var temp = bg;
|
|
|
|
bg = fg;
|
|
|
|
fg = temp;
|
|
|
|
use = true;
|
|
|
|
}
|
|
|
|
if (fg != -1) {
|
|
|
|
span.style.color = intToHex(fg);
|
|
|
|
use = true;
|
|
|
|
}
|
|
|
|
if (bg != -1) {
|
|
|
|
span.style.backgroundColor = intToHex(bg);
|
|
|
|
use = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: these has to be updated if Attrs.go changes
|
|
|
|
if (attrs != 0) {
|
|
|
|
use = true;
|
|
|
|
if ((attrs & 1) != 0) {
|
|
|
|
span.classList.add("bold");
|
|
|
|
}
|
|
|
|
if ((attrs & (1 << 4)) != 0) {
|
|
|
|
span.classList.add("dim");
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
2024-03-04 09:01:54 +08:00
|
|
|
if ((attrs & (1 << 5)) != 0) {
|
|
|
|
span.classList.add("italic");
|
|
|
|
}
|
|
|
|
if ((attrs & (1 << 6)) != 0) {
|
|
|
|
span.classList.add("strikethrough");
|
|
|
|
}
|
2024-03-05 18:40:30 +08:00
|
|
|
}
|
|
|
|
if (us != 0) {
|
|
|
|
use = true;
|
|
|
|
if (us == 1) {
|
|
|
|
span.classList.add("underline");
|
|
|
|
} else if (us == 2) {
|
2024-03-04 09:01:54 +08:00
|
|
|
span.classList.add("double_underline");
|
2024-03-05 18:40:30 +08:00
|
|
|
} else if (us == 3) {
|
2024-03-04 09:01:54 +08:00
|
|
|
span.classList.add("curly_underline");
|
2024-03-05 18:40:30 +08:00
|
|
|
} else if (us == 4) {
|
2024-03-04 09:01:54 +08:00
|
|
|
span.classList.add("dotted_underline");
|
2024-03-05 18:40:30 +08:00
|
|
|
} else if (us == 5) {
|
2024-03-04 09:01:54 +08:00
|
|
|
span.classList.add("dashed_underline");
|
|
|
|
}
|
2024-03-05 18:40:30 +08:00
|
|
|
if (uc != -1) {
|
|
|
|
span.style.textDecorationColor = intToHex(uc);
|
|
|
|
}
|
2024-03-04 09:01:54 +08:00
|
|
|
}
|
2024-03-05 18:40:30 +08:00
|
|
|
|
2024-03-04 11:13:22 +08:00
|
|
|
if ((attrs & (1 << 1)) != 0) {
|
|
|
|
var blink = document.createElement("span");
|
|
|
|
blink.classList.add("blink");
|
2024-03-10 10:15:42 +08:00
|
|
|
var textnode = document.createTextNode(s);
|
2024-03-04 11:13:22 +08:00
|
|
|
blink.appendChild(textnode);
|
|
|
|
span.appendChild(blink);
|
|
|
|
} else {
|
2024-03-10 10:15:42 +08:00
|
|
|
var textnode = document.createTextNode(s);
|
2024-03-04 11:13:22 +08:00
|
|
|
span.appendChild(textnode);
|
|
|
|
}
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
content.dirty = true; // invalidate terminal- new cell
|
|
|
|
content.data[y].previous = null; // invalidate row- new row
|
|
|
|
content.data[y].data[x] = use ? span : textnode;
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function show() {
|
2024-03-04 09:01:54 +08:00
|
|
|
if (!content.dirty) {
|
|
|
|
return; // no new draws; no need to update
|
|
|
|
}
|
|
|
|
|
|
|
|
displayCursor();
|
|
|
|
|
|
|
|
term.innerHTML = "";
|
|
|
|
content.data.forEach((row) => {
|
|
|
|
if (row.previous == null) {
|
|
|
|
row.previous = document.createElement("span");
|
|
|
|
row.data.forEach((c) => {
|
|
|
|
row.previous.appendChild(c);
|
|
|
|
});
|
|
|
|
row.previous.appendChild(document.createTextNode("\n"));
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
2024-03-04 09:01:54 +08:00
|
|
|
term.appendChild(row.previous);
|
|
|
|
});
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
content.dirty = false;
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function showCursor(x, y) {
|
2024-03-04 09:01:54 +08:00
|
|
|
content.dirty = true;
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
if (!(cx < 0 || cy < 0)) {
|
|
|
|
// if original position is a valid cursor position
|
|
|
|
content.data[cy].previous = null;
|
|
|
|
if (content.data[cy].data[cx].classList) {
|
|
|
|
content.data[cy].data[cx].classList.remove(cursorClass);
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
2024-03-04 09:01:54 +08:00
|
|
|
}
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
cx = x;
|
|
|
|
cy = y;
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function displayCursor() {
|
2024-03-04 09:01:54 +08:00
|
|
|
content.dirty = true;
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
if (!(cx < 0 || cy < 0)) {
|
|
|
|
// if new position is a valid cursor position
|
|
|
|
content.data[cy].previous = null;
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
if (!content.data[cy].data[cx].classList) {
|
|
|
|
var span = document.createElement("span");
|
|
|
|
span.appendChild(content.data[cy].data[cx]);
|
|
|
|
content.data[cy].data[cx] = span;
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
2024-03-04 09:01:54 +08:00
|
|
|
|
2024-03-07 23:28:12 +08:00
|
|
|
if (cursorColor != "") {
|
|
|
|
term.style.setProperty("--cursor-color", cursorColor);
|
|
|
|
} else {
|
|
|
|
term.style.setProperty("--cursor-color", "lightgrey");
|
|
|
|
}
|
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
content.data[cy].data[cx].classList.add(cursorClass);
|
|
|
|
}
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
2024-03-07 23:28:12 +08:00
|
|
|
function setCursorStyle(newClass, newColor) {
|
|
|
|
if (newClass == cursorClass && newColor == cursorColor) {
|
2024-03-04 09:01:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
if (!(cx < 0 || cy < 0)) {
|
|
|
|
// mark cursor row as dirty; new class has been applied to (cx, cy)
|
|
|
|
content.dirty = true;
|
|
|
|
content.data[cy].previous = null;
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
if (content.data[cy].data[cx].classList) {
|
|
|
|
content.data[cy].data[cx].classList.remove(cursorClass);
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
// adding the new class will be dealt with when displayCursor() is called
|
|
|
|
}
|
|
|
|
|
|
|
|
cursorClass = newClass;
|
2024-03-07 23:28:12 +08:00
|
|
|
cursorColor = newColor;
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function beep() {
|
2024-03-04 09:01:54 +08:00
|
|
|
beepAudio.currentTime = 0;
|
|
|
|
beepAudio.play();
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
2024-03-10 02:05:58 +08:00
|
|
|
function setTitle(title) {
|
|
|
|
document.title = title;
|
|
|
|
}
|
|
|
|
|
2023-01-03 09:29:40 +08:00
|
|
|
function intToHex(n) {
|
2024-03-04 09:01:54 +08:00
|
|
|
return "#" + n.toString(16).padStart(6, "0");
|
2023-01-03 09:29:40 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
initialize();
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
let fontwidth = term.clientWidth / width;
|
|
|
|
let fontheight = term.clientHeight / height;
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
document.addEventListener("keydown", (e) => {
|
|
|
|
onKeyEvent(e.key, e.shiftKey, e.altKey, e.ctrlKey, e.metaKey);
|
|
|
|
});
|
2023-01-03 09:29:40 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
term.addEventListener("click", (e) => {
|
|
|
|
onMouseClick(
|
|
|
|
Math.min((e.offsetX / fontwidth) | 0, width - 1),
|
|
|
|
Math.min((e.offsetY / fontheight) | 0, height - 1),
|
|
|
|
e.which,
|
|
|
|
e.shiftKey,
|
|
|
|
e.altKey,
|
|
|
|
e.ctrlKey
|
|
|
|
);
|
|
|
|
});
|
2023-08-15 09:42:16 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
term.addEventListener("mousemove", (e) => {
|
|
|
|
onMouseMove(
|
|
|
|
Math.min((e.offsetX / fontwidth) | 0, width - 1),
|
|
|
|
Math.min((e.offsetY / fontheight) | 0, height - 1),
|
|
|
|
e.which,
|
|
|
|
e.shiftKey,
|
|
|
|
e.altKey,
|
|
|
|
e.ctrlKey
|
|
|
|
);
|
|
|
|
});
|
2023-08-15 09:42:16 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
term.addEventListener("focus", (e) => {
|
|
|
|
onFocus(true);
|
|
|
|
});
|
2023-08-15 09:42:16 +08:00
|
|
|
|
2024-03-04 09:01:54 +08:00
|
|
|
term.addEventListener("blur", (e) => {
|
|
|
|
onFocus(false);
|
|
|
|
});
|
|
|
|
term.tabIndex = 0;
|
|
|
|
|
|
|
|
document.addEventListener("paste", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
var text = (e.originalEvent || e).clipboardData.getData("text/plain");
|
|
|
|
onPaste(true);
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
onKeyEvent(text.charAt(i), false, false, false, false);
|
|
|
|
}
|
|
|
|
onPaste(false);
|
2023-01-03 09:29:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const go = new Go();
|
2024-03-04 09:01:54 +08:00
|
|
|
WebAssembly.instantiateStreaming(fetch(wasmFilePath), go.importObject).then(
|
|
|
|
(result) => {
|
2023-01-03 09:29:40 +08:00
|
|
|
go.run(result.instance);
|
2024-03-04 09:01:54 +08:00
|
|
|
}
|
|
|
|
);
|