first kfc version and getTerminalSize

This commit is contained in:
MeexReay 2025-03-21 17:27:36 +03:00
parent 373e7307c4
commit 4842b99002
4 changed files with 47 additions and 0 deletions

31
app/kfc/kfc.js Normal file
View File

@ -0,0 +1,31 @@
async function main(args) {
if (args.length != 2) {
writeStdout(`Usage: kfc <file>\n`)
return 1
}
let content = readFile(args[1])
let pos = getCursor()
setStdinFlag(SILENT_STDIN)
while (true) {
let event = await pollStdinEvent()
if (event.type == "key") {
if (event.key == "Escape") {
break
}
writeStdout(event.key + "\n")
} else if (event.type == "char") {
writeStdout(event.char + "\n")
}
}
setStdinFlag(RENDER_STDIN)
return 0
}

8
app/kfc/package.json Normal file
View File

@ -0,0 +1,8 @@
{
"name": "kfc",
"version": "0.0.1-test",
"description": "Keep Files Customized",
"author": "MeexReay",
"apps": [ "kfc.js" ],
"configs": []
}

View File

@ -158,6 +158,7 @@ async function main(args) {
updateCursor() - обновить курсор updateCursor() - обновить курсор
getCursorIndex() -> index - получить индекс курсора в тексте терминала getCursorIndex() -> index - получить индекс курсора в тексте терминала
getCursor() -> [x, y] - получить курсор getCursor() -> [x, y] - получить курсор
getTerminalSize() -> [width, height] - получить ширину высоту терминала в символах
`) `)
return 0 return 0

View File

@ -5,6 +5,13 @@ var terminal = document.getElementById("terminal")
var terminal_text = "" var terminal_text = ""
var last_stdin_length = 0 var last_stdin_length = 0
function getTerminalSize() {
return [
Math.floor(window.innerWidth - CURSOR_OFFSET[0] * 2 / CHAR_SIZE[0]),
Math.floor(window.innerHeight - CURSOR_OFFSET[1] * 2 / CHAR_SIZE[1])
]
}
function writeTerminalAtCursor(content) { function writeTerminalAtCursor(content) {
let cursor_index = getCursorIndex() let cursor_index = getCursorIndex()
terminal_text = terminal_text.slice(0, cursor_index) + content + terminal_text.slice(cursor_index) terminal_text = terminal_text.slice(0, cursor_index) + content + terminal_text.slice(cursor_index)