feat: Python Plugins

This commit is contained in:
Ian Chua
2026-07-02 17:49:36 +08:00
parent 395e070a0e
commit ecddf3d18f
183 changed files with 49955 additions and 2120 deletions

View File

@@ -0,0 +1,29 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Terminal</title>
<link rel="stylesheet" href="../../include/xterm/xterm.css" />
<link rel="stylesheet" type="text/css" href="../../include/global.css" />
<link rel="stylesheet" type="text/css" href="../css/common.css" />
<link rel="stylesheet" type="text/css" href="../css/dark.css" />
<link rel="stylesheet" href="./styles.css" />
<script type="text/javascript" src="../js/jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="../js/json2.js"></script>
<script type="text/javascript" src="../../data/text.js"></script>
<script type="text/javascript" src="../js/globalapi.js"></script>
<script type="text/javascript" src="../../include/xterm/xterm.js"></script>
<script type="text/javascript" src="../../include/xterm/xterm-addon-fit.js"></script>
<script src="./index.js"></script>
</head>
<body onLoad="OnInit()">
<div class="app">
<div id="terminal-container"></div>
<div class="input-row">
<input type="text" id="cmd-input" placeholder="python ... or uv ..." />
<button id="run-btn">Run</button>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,125 @@
let term = null;
let fitAddon = null;
function OnInit() {
if (typeof TranslatePage === "function")
TranslatePage();
term = new Terminal({
cursorBlink: true,
fontSize: 13,
fontFamily: '"Cascadia Code", "Fira Code", "JetBrains Mono", monospace',
theme: {
background: "#1e1e1e",
foreground: "#d4d4d4",
cursor: "#ffffff",
black: "#000000",
red: "#cd3131",
green: "#0dbc79",
yellow: "#e5e510",
blue: "#2472c8",
magenta: "#bc3fbc",
cyan: "#11a8cd",
white: "#e5e5e5",
brightBlack: "#666666",
brightRed: "#f14c4c",
brightGreen: "#23d18b",
brightYellow: "#f5f543",
brightBlue: "#3b8eea",
brightMagenta: "#d670d6",
brightCyan: "#29b8db",
brightWhite: "#ffffff"
},
allowProposedApi: true
});
fitAddon = new (FitAddon.FitAddon || FitAddon)();
term.loadAddon(fitAddon);
term.open(document.getElementById("terminal-container"));
fitAddon.fit();
// Focus the terminal so it captures keyboard input
term.focus();
// Re-focus terminal when user clicks on it
term.element.addEventListener("click", () => term.focus());
window.addEventListener("resize", () => fitAddon.fit());
// Send keystrokes to C++
term.onData((data) => {
SendWXMessage(JSON.stringify({
command: "write_stdin",
data: data
}));
});
document.getElementById("run-btn").addEventListener("click", onRun);
document.getElementById("cmd-input").addEventListener("keydown", (e) => {
if (e.key === "Enter")
onRun();
});
term.writeln("Plugin terminal ready.");
}
function onRun() {
const input = document.getElementById("cmd-input");
const cmd = input.value.trim();
if (!cmd)
return;
input.value = "";
term.writeln("$ " + cmd);
setRunning(true);
SendWXMessage(JSON.stringify({
command: "run_command",
cmd: cmd
}));
}
function setRunning(running) {
document.getElementById("run-btn").disabled = running;
document.getElementById("cmd-input").disabled = running;
}
function HandleStudio(value) {
const payload = (typeof value === "string") ? SafeJsonParse(value) : value;
if (!payload || typeof payload !== "object")
return;
switch (payload.command) {
case "output":
if (payload.lines && Array.isArray(payload.lines)) {
payload.lines.forEach((line) => {
if (line.is_stderr)
term.writeln("\x1b[91m" + line.text + "\x1b[0m");
else
term.writeln(line.text);
});
}
break;
case "process_done":
setRunning(false);
term.writeln("\x1b[90mProcess exited with code: " + payload.exit_code + "\x1b[0m");
break;
case "process_error":
setRunning(false);
term.writeln("\x1b[91m" + payload.message + "\x1b[0m");
break;
}
}
function SafeJsonParse(value) {
try {
return JSON.parse(value);
} catch (e) {
return null;
}
}

View File

@@ -0,0 +1,74 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
height: 100%;
overflow: hidden;
background: #1e1e1e;
color: #d4d4d4;
}
.app {
display: flex;
flex-direction: column;
height: 100%;
padding: 8px;
}
#terminal-container {
flex: 1;
border-radius: 4px;
overflow: hidden;
}
#terminal-container,
#terminal-container .xterm,
#terminal-container .xterm * {
font-family: "Cascadia Code", "Fira Code", "JetBrains Mono", monospace;
letter-spacing: 0;
word-spacing: 0;
}
.input-row {
display: flex;
gap: 8px;
margin-top: 8px;
}
#cmd-input {
flex: 1;
padding: 6px 10px;
border: 1px solid #444;
border-radius: 4px;
background: #2d2d2d;
color: #d4d4d4;
font-family: inherit;
font-size: 13px;
outline: none;
}
#cmd-input:focus {
border-color: #666;
}
#run-btn {
padding: 6px 20px;
border: none;
border-radius: 4px;
background: #0e639c;
color: #fff;
font-size: 13px;
cursor: pointer;
}
#run-btn:hover {
background: #1177bb;
}
#run-btn:disabled {
background: #555;
cursor: not-allowed;
}