multi-line cursor support

This commit is contained in:
Chris Wanstrath 2025-09-21 14:07:18 -07:00
parent 3ed6a8ea13
commit da7c3ceae1
3 changed files with 14 additions and 4 deletions

View File

@ -24,6 +24,14 @@ function showCursor(e: any) {
} }
requestAnimationFrame(() => requestAnimationFrame(() =>
cmdCursor.value = " ".repeat(cmdInput.selectionEnd) + cursor cmdCursor.value = buildBlankCursorLine() + cursor
) )
}
function buildBlankCursorLine(): string {
let line = ""
for (const char of cmdInput.value.slice(0, cmdInput.selectionEnd)) {
line += char === "\n" ? char : " "
}
return line
} }

View File

@ -1,7 +1,7 @@
//// ////
// Command input history storage and navigation. // Command input history storage and navigation.
import { cmdInput } from "./dom.js" import { cmdInput, cmdLine } from "./dom.js"
const history: string[] = ["one", "two", "three"] const history: string[] = ["one", "two", "three"]
let idx = -1 let idx = -1
@ -25,6 +25,8 @@ export function resetHistory() {
} }
function navigateHistory(e: KeyboardEvent) { function navigateHistory(e: KeyboardEvent) {
if (cmdLine.dataset.extended) return
if (e.key === "ArrowUp" || (e.ctrlKey && e.key === "p")) { if (e.key === "ArrowUp" || (e.ctrlKey && e.key === "p")) {
e.preventDefault() e.preventDefault()
if (idx >= history.length - 1) return if (idx >= history.length - 1) return

View File

@ -14,7 +14,7 @@ function inputHandler(event: KeyboardEvent) {
if (target?.id !== cmdInput.id) return if (target?.id !== cmdInput.id) return
if (event.key === "Escape" || (event.ctrlKey && event.key === "c")) { if (event.key === "Escape" || (event.ctrlKey && event.key === "c")) {
cmdInput.value = "" clearInput()
resetHistory() resetHistory()
} else if (event.key === "Tab") { } else if (event.key === "Tab") {
event.preventDefault() event.preventDefault()
@ -31,5 +31,5 @@ function inputHandler(event: KeyboardEvent) {
function clearInput() { function clearInput() {
cmdInput.value = "" cmdInput.value = ""
cmdInput.rows = 1 cmdInput.rows = 1
cmdLine.dataset.extended = "false" delete cmdLine.dataset.extended
} }