kfc display improvement

This commit is contained in:
MeexReay 2025-03-21 20:01:52 +03:00
parent 1e0f45af0f
commit a91876b6a5

View File

@ -17,19 +17,32 @@ async function cropToScreen(text, x, y, width, height) {
return screen.join("\n")
}
async function printScreen(start_cursor, pos, content, mode, pos, x, y, width, height) {
trimTerminal(getTerminal().length - screen_length)
let screen = await cropToScreen(content, 0, 0, width, height - 1)
await writeStdout(screen)
let status_line = `\nmode: ${mode} | size: ${content.length} | lines: ${content.split("\n").length} | x: ${pos[0]} | y: ${pos[1]}`
await writeStdout(status_line)
setCursor(start_cursor[0] + pos[0], start_cursor[1] + pos[1])
return [screen.length + status_line.length, status_line.length - 1]
}
async function main(args) {
if (args.length != 2) {
writeStdout(`Usage: kfc <file>\n`)
return 1
}
let [terminal_width, terminal_height] = getTerminalSize()
let content = readFile(args[1])
let pos = getCursor()
let mode = "normal"
let pos = [0, 0]
let start_cursor = getCursor()
let [width, height] = getTerminalSize()
let [screen_length, status_length] = await printScreen(0, start_cursor, pos, content, mode, pos, 0, 0, width, height)
await writeStdout(await cropToScreen(content, 0, 0, terminal_width, terminal_height - 1))
setStdinFlag(ENABLE_STDIN)
setStdinFlag(SILENT_STDIN)
@ -40,14 +53,54 @@ async function main(args) {
console.log(event)
if (event.type == "key") {
if (event.key == "Escape") {
if (event.key == "Backspace") {
} else if (event.key == "Delete") {
} else if (event.key == "ArrowUp") {
pos[1] = Math.max(0, pos[1] - 1)
} else if (event.key == "ArrowDown") {
pos[1] = Math.min(height - 2, pos[1] + 1)
} else if (event.key == "ArrowLeft") {
pos[0] = Math.max(0, pos[0] - 1)
} else if (event.key == "ArrowRight") {
pos[0] = Math.min(height - 1, pos[0] + 1)
} else if (event.key == "Escape") {
mode = "normal"
} else if (event.key == "Insert") {
mode = "insert"
}
} else if (event.type == "char") {
if (mode == "normal") {
if (event.char == ":") {
trimTerminal(getTerminal().length - status_length)
writeStdout(":")
let command = readLine()
setStdinFlag(ENABLE_STDIN)
setStdinFlag(SILENT_STDIN)
if (command == "w") {
// save
} else if (command == "q") {
break
} else if (command == "x") {
// save
break
}
writeStdout(JSON.stringify(event) + "\n")
} else if (event.type == "char") {
writeStdout(JSON.stringify(event) + "\n")
} else if (event.char == "i") {
mode = "insert"
}
} else if (mode == "insert") {
}
}
let res = await printScreen(screen_length, start_cursor, pos, content, mode, pos, 0, 0, width, height)
screen_length = res[0]
status_length = res[1]
}
setStdinFlag(RENDER_STDIN)