feat: better control of arrow keys

This commit is contained in:
koehr 2023-12-11 23:54:14 +01:00 committed by Norman Köhring
parent c658d5492c
commit f2220ba1b0

View file

@ -32,6 +32,11 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
inputEl.setSelectionRange(pos, pos) inputEl.setSelectionRange(pos, pos)
} }
function cursorTouchesPrompt() {
const promptEnd = inputEl.value.lastIndexOf(prompt) + prompt.length
return inputEl.selectionStart === promptEnd
}
function setFocus() { function setFocus() {
inputEl.focus() inputEl.focus()
} }
@ -189,6 +194,15 @@ export default function useTerminal(inputEl: HTMLTextAreaElement, commands: Simp
case 'Backspace': case 'Backspace':
if (cursorAtPrompt()) ev.preventDefault() if (cursorAtPrompt()) ev.preventDefault()
break break
case 'ArrowUp':
// TODO: commandHistory(-1)
ev.preventDefault()
break
case 'ArrowLeft':
if (cursorTouchesPrompt()) ev.preventDefault()
break
default:
console.debug('key pressed:', ev.key, ev.keyCode)
} }
} }