Files
PyCanvas/xtermjs.html
2025-11-29 20:11:05 +00:00

66 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Xterm.js Console</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm/css/xterm.css" />
<style>
body { margin: 0; height: 100vh; display: flex; flex-direction: column; }
#terminal { flex: 1; }
</style>
</head>
<body>
<div id="terminal"></div>
<script src="https://cdn.jsdelivr.net/npm/xterm/lib/xterm.js"></script>
<script>
const { Terminal } = window;
const term = new Terminal({ cursorBlink: true });
term.open(document.getElementById('terminal'));
let inputResolve;
let inputBuffer = '';
let acceptingInput = false;
function printTermLn(text) {
term.write(text.replace(/\n/g, '\r\n'));
}
function readTermLn(promptText = '') {
return new Promise((resolve) => {
inputBuffer = '';
inputResolve = resolve;
acceptingInput = true;
term.write(promptText);
});
}
term.onKey(e => {
if (!acceptingInput) return;
const key = e.key;
const ev = e.domEvent;
if (ev.key === 'Enter') {
term.write('\r\n');
if (inputResolve) {
inputResolve(inputBuffer);
inputResolve = null;
acceptingInput = false;
}
} else if (ev.key === 'Backspace') {
if (inputBuffer.length > 0) {
inputBuffer = inputBuffer.slice(0, -1);
term.write('\b \b');
}
} else if (key.length === 1) {
inputBuffer += key;
term.write(key);
}
});
</script>
</body>
</html>