make zterm working

This commit is contained in:
MeexReay 2025-05-24 00:45:33 +03:00
parent 7560e4d7af
commit 8448d5d20a
4 changed files with 124 additions and 21 deletions

View File

@ -20,8 +20,8 @@ async function drawWindowDecorations(ctx, x, y, width, height, title) {
const outerX = x - borderWidth - 1; const outerX = x - borderWidth - 1;
const outerY = y - headerHeight - borderWidth - 2; const outerY = y - headerHeight - borderWidth - 2;
const outerWidth = width + borderWidth * 2 + 1; const outerWidth = width + borderWidth * 2 + 2;
const outerHeight = height + headerHeight + borderWidth * 2 + 2; const outerHeight = height + headerHeight + borderWidth * 2 + 3;
ctx.fillStyle = "#f4f4f4"; ctx.fillStyle = "#f4f4f4";
ctx.fillRect(outerX, outerY, outerWidth, outerHeight) ctx.fillRect(outerX, outerY, outerWidth, outerHeight)

View File

@ -1,20 +1,87 @@
eval(readFile("/app/zcom.js")) eval(readFile("/app/zcom.js"))
var text = "" let text = ""
let stdin_text = ""
let stdin_visible = true
let stdin_disable = true
async function draw(ctx) { async function draw(ctx) {
ctx.fillStyle = "cyan" ctx.fillStyle = "black"
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height) ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
ctx.fillStyle = "#222"; let y = 500 - 12
ctx.font = "bold 14px sans-serif"; for (let line of text.split("\n").reverse()) {
ctx.textBaseline = "middle"; ctx.fillStyle = "white";
ctx.textAlign = "left"; ctx.font = "bold 14px terminus";
ctx.fillText(text, 10, 10); ctx.textBaseline = "middle";
ctx.textAlign = "left";
ctx.fillText(line, 5, y);
y -= 18
}
}
function setStdinFlag(flag) {
if (flag == SILENT_STDIN) {
stdin_visible = false
} else if (flag == RENDER_STDIN) {
stdin_visible = true
} else if (flag == DISABLE_STDIN) {
stdin_disable = true
} else if (flag == ENABLE_STDIN) {
stdin_disable = false
}
} }
async function readStdin() {
while (stdin_text.length == 0) {
await new Promise(resolve => setTimeout(resolve, 10))
}
let was_stdin = stdin_text.charAt(0)
stdin_text = stdin_text.slice(1)
return was_stdin
}
async function writeStdout(wh) {
text += wh
}
let altKey = false
let ctrlKey = false
let shiftKey = false
async function onKeyDown(key) { async function onKeyDown(key) {
text += key console.log(key)
if (!stdin_disable) {
if (key == "Enter") {
stdin_text += "\n"
if (stdin_visible) {
text += "\n"
}
} else if (key.length == 1) {
if (key == "\0") return
if (stdin_visible) {
text += key
}
stdin_text += key
} else if (key == "Alt") {
altKey = true
} else if (key == "Shift") {
shiftKey = true
} else if (key == "Control") {
ctrlKey = true
} else {
stdin_text += "\r"+(ctrlKey ? "1" : "0")+(altKey ? "1" : "0")+(shiftKey ? "1" : "0")+key+"\r"
}
}
}
async function onKeyUp(key) {
if (key == "Alt") {
altKey = false
} else if (key == "Shift") {
shiftKey = false
} else if (key == "Control") {
ctrlKey = false
}
} }
async function main(args) { async function main(args) {
@ -24,7 +91,12 @@ async function main(args) {
"height": 500, "height": 500,
"x": 50, "x": 50,
"y": 50, "y": 50,
"onkeydown": onKeyDown "onkeydown": onKeyDown,
"onkeyup": onKeyUp
})
setTimeout(() => {
executeCommand(["/app/posh.js"], readStdin, writeStdout, setStdinFlag)
}) })
while (graphics_canvas != null) { while (graphics_canvas != null) {

View File

@ -25,7 +25,7 @@ async function processCommand(command, args) {
await writeStdout("\nСтатус код: "+code+"\n") await writeStdout("\nСтатус код: "+code+"\n")
} }
} catch (e) { } catch (e) {
console.log(e) console.log(e.toString())
await writeStdout("Не запустилася\n") await writeStdout("Не запустилася\n")
} }
} else { } else {

View File

@ -13,7 +13,7 @@ const SILENT_STDIN = 2
const DISABLE_STDIN = 3 const DISABLE_STDIN = 3
const ENABLE_STDIN = 4 const ENABLE_STDIN = 4
async function readLine(on_key=(key, ctrl, alt, shift, content, pos) => [content, pos]) { const READ_FUNCTIONS_CODE = `async function readLine(on_key=(key, ctrl, alt, shift, content, pos) => [content, pos]) {
setStdinFlag(ENABLE_STDIN) setStdinFlag(ENABLE_STDIN)
let start_terminal = getTerminal() let start_terminal = getTerminal()
@ -63,8 +63,8 @@ async function readLine(on_key=(key, ctrl, alt, shift, content, pos) => [content
continue continue
} else if (event.type == "char") { } else if (event.type == "char") {
if (event.char == "\n") break if (event.char == "\\n") break
if (event.char == "\0") continue if (event.char == "\\0") continue
content = content.slice(0, pos) + event.char + content.slice(pos) content = content.slice(0, pos) + event.char + content.slice(pos)
pos += 1 pos += 1
@ -79,10 +79,10 @@ async function readLine(on_key=(key, ctrl, alt, shift, content, pos) => [content
async function pollStdinEvent() { async function pollStdinEvent() {
let char = await readStdin() let char = await readStdin()
if (char == "\r") { if (char == "\\r") {
let key = "" let key = ""
char = await readStdin() char = await readStdin()
while (char != "\r") { while (char != "\\r") {
key += char key += char
char = await readStdin() char = await readStdin()
} }
@ -107,6 +107,34 @@ async function pollStdinEvent() {
} }
} }
function executeCommand(args, read=readStdin, write=writeStdout, set_flag=setStdinFlag, inject="") {
let id = new Date().getMilliseconds().toString()+(Math.random()*100)
let func_content = readFile(args[0])
if (func_content == null || !func_content.includes("function main")) return
let func = new Function(
"args", "readStdin", "writeStdout", "setStdinFlag",
READ_FUNCTIONS_CODE+"\\n\\n\\n"+func_content+"\\n"+inject+"\\nreturn main(args)"
)
let process = {
"id": id,
"name": args.join(" "),
"promise": new Promise((resolve, reject) => {
setTimeout(() => {
try {
resolve(func(args, read, write, set_flag))
} catch (e) {
reject(e)
}
}, 0)
}).then(o => {
processes = processes.filter(x => x.id != id)
return o
})
}
processes.push(process)
return process
}`
async function readStdin() { async function readStdin() {
while (stdin.length == 0) { while (stdin.length == 0) {
await new Promise(resolve => setTimeout(resolve, 10)) await new Promise(resolve => setTimeout(resolve, 10))
@ -133,18 +161,21 @@ function setStdinFlag(flag) {
} }
} }
function executeCommand(args, read=readStdin, write=writeStdout) { function executeCommand(args, read=readStdin, write=writeStdout, set_flag=setStdinFlag, inject="") {
let id = new Date().getMilliseconds().toString()+(Math.random()*100) let id = new Date().getMilliseconds().toString()+(Math.random()*100)
let func_content = readFile(args[0]) let func_content = readFile(args[0])
if (func_content == null || !func_content.includes("function main")) return if (func_content == null || !func_content.includes("function main")) return
let func = new Function("args", "readStdin", "writeStdout", func_content+"\n\nreturn main(args)") let func = new Function(
"args", "readStdin", "writeStdout", "setStdinFlag",
READ_FUNCTIONS_CODE+"\n\n\n"+func_content+"\n"+inject+"\nreturn main(args)"
)
let process = { let process = {
"id": id, "id": id,
"name": args.join(" "), "name": args.join(" "),
"promise": new Promise((resolve, reject) => { "promise": new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
try { try {
resolve(func(args, read, write)) resolve(func(args, read, write, set_flag))
} catch (e) { } catch (e) {
reject(e) reject(e)
} }