move terminal.ts and keys.ts into the emulator folder

This commit is contained in:
2025-02-26 16:48:51 -05:00
parent e09891482e
commit 596a21f49d
3 changed files with 5 additions and 5 deletions

34
src/rt/emulator/keys.ts Normal file
View File

@ -0,0 +1,34 @@
import run from "../shell/command/command"
import history from "../shell/history"
type InputClosure = (key_event: KeyboardEvent) => void
interface EnterArgs {
readonly term_win_safe: HTMLElement,
readonly ps1input: HTMLInputElement,
readonly closure: InputClosure
}
interface Keys {
enter: (input: EnterArgs) => void,
up_arrow: (ps1input: HTMLInputElement) => void,
down_arrow: (ps1input: HTMLInputElement) => void,
}
const keys = {} as Keys
keys.enter = function(input: EnterArgs) {
const unknown_command_msg = run(input.term_win_safe, input.ps1input.value)
if (unknown_command_msg) {
input.term_win_safe.appendChild(unknown_command_msg)
}
input.ps1input.removeEventListener("keydown", input.closure)
}
keys.up_arrow = function(ps1input: HTMLInputElement) {
history.index_up(ps1input)
}
keys.down_arrow = function(ps1input: HTMLInputElement) {
history.index_down(ps1input)
}
export default keys

View File

@ -0,0 +1,53 @@
import history from "../shell/history"
import prompt from "../elements/prompt"
import keys from "./keys"
const term_win_unsafe = document.querySelector("main")
const enum Key {
Enter = "Enter",
ArrowRight = "ArrowRight",
ArrowUp = "ArrowUp",
ArrowDown = "ArrowDown",
Tab = "Tab"
}
function spawnps1(term_win_safe: HTMLElement) {
const ps1prompt = prompt()
term_win_safe.appendChild(ps1prompt.body)
bind_processor(term_win_safe, ps1prompt.input)
history.file.cursor_reset()
ps1prompt.input.focus()
}
function bind_processor(term_win_safe: HTMLElement, ps1prompt_input: HTMLInputElement) {
const input_closure = (key_event: KeyboardEvent) => {
if (key_event.key === Key.Enter) {
key_event.preventDefault()
keys.enter({
term_win_safe: term_win_safe,
ps1input: ps1prompt_input,
closure: input_closure
})
spawnps1(term_win_safe)
} else if (key_event.key === Key.Tab) {
key_event.preventDefault()
} else if (key_event.key === Key.ArrowRight) {
key_event.preventDefault()
} else if (key_event.key === Key.ArrowUp) {
key_event.preventDefault()
keys.up_arrow(ps1prompt_input)
} else if (key_event.key === Key.ArrowDown) {
key_event.preventDefault()
keys.down_arrow(ps1prompt_input)
}
}
ps1prompt_input.addEventListener("keydown", input_closure)
}
if (term_win_unsafe) {
spawnps1(term_win_unsafe)
} else {
}