chore: fmt

This commit is contained in:
Brooklyn Nicholson
2026-04-09 14:17:45 -05:00
parent 00e1d42b9e
commit 17f13013eb
2 changed files with 206 additions and 74 deletions

View File

@@ -4,9 +4,13 @@ import { useEffect, useRef, useState } from 'react'
function wordLeft(s: string, p: number) {
let i = p - 1
while (i > 0 && /\s/.test(s[i]!)) {i--}
while (i > 0 && /\s/.test(s[i]!)) {
i--
}
while (i > 0 && !/\s/.test(s[i - 1]!)) {i--}
while (i > 0 && !/\s/.test(s[i - 1]!)) {
i--
}
return Math.max(0, i)
}
@@ -14,9 +18,13 @@ function wordLeft(s: string, p: number) {
function wordRight(s: string, p: number) {
let i = p
while (i < s.length && !/\s/.test(s[i]!)) {i++}
while (i < s.length && !/\s/.test(s[i]!)) {
i++
}
while (i < s.length && /\s/.test(s[i]!)) {i++}
while (i < s.length && /\s/.test(s[i]!)) {
i++
}
return i
}
@@ -77,7 +85,14 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
}
}, [value])
useEffect(() => () => { if (pasteTimer.current) {clearTimeout(pasteTimer.current)} }, [])
useEffect(
() => () => {
if (pasteTimer.current) {
clearTimeout(pasteTimer.current)
}
},
[]
)
// ── Buffer ops (synchronous, ref-based) ─────────────────────────
@@ -88,7 +103,10 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
if (track && next !== prev) {
undoStack.current.push({ cursor: curRef.current, value: prev })
if (undoStack.current.length > 200) {undoStack.current.shift()}
if (undoStack.current.length > 200) {
undoStack.current.shift()
}
redoStack.current = []
}
@@ -105,7 +123,10 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
const swap = (from: typeof undoStack, to: typeof redoStack) => {
const entry = from.current.pop()
if (!entry) {return}
if (!entry) {
return
}
to.current.push({ cursor: curRef.current, value: vRef.current })
commit(entry.value, entry.cursor, false)
}
@@ -113,7 +134,9 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
const emitPaste = (e: PasteEvent) => {
const handled = onPasteRef.current?.(e)
if (handled) {commit(handled.value, handled.cursor)}
if (handled) {
commit(handled.value, handled.cursor)
}
return !!handled
}
@@ -124,7 +147,9 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
pasteBuf.current = ''
pasteTimer.current = null
if (!text) {return}
if (!text) {
return
}
if (!emitPaste({ cursor: at, text, value: vRef.current }) && PRINTABLE.test(text)) {
commit(vRef.current.slice(0, at) + text + vRef.current.slice(at), at + text.length)
@@ -146,15 +171,20 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
// Keys handled by App.useInput
if (
k.upArrow || k.downArrow ||
k.upArrow ||
k.downArrow ||
(k.ctrl && inp === 'c') ||
k.tab || (k.shift && k.tab) ||
k.pageUp || k.pageDown ||
k.tab ||
(k.shift && k.tab) ||
k.pageUp ||
k.pageDown ||
k.escape
) {return}
) {
return
}
if (k.return) {
;(k.shift || k.meta)
k.shift || k.meta
? commit(insert(vRef.current, curRef.current, '\n'), curRef.current + 1)
: onSubmitRef.current?.(vRef.current)
@@ -165,46 +195,85 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
let v = vRef.current
const mod = k.ctrl || k.meta
if (k.ctrl && inp === 'z') {return swap(undoStack, redoStack)}
if ((k.ctrl && inp === 'y') || (k.meta && k.shift && inp === 'z')) {return swap(redoStack, undoStack)}
if (k.home || (k.ctrl && inp === 'a')) {c = 0}
else if (k.end || (k.ctrl && inp === 'e')) {c = v.length}
else if (k.leftArrow) {c = mod ? wordLeft(v, c) : Math.max(0, c - 1)}
else if (k.rightArrow) {c = mod ? wordRight(v, c) : Math.min(v.length, c + 1)}
else if ((k.backspace || k.delete) && c > 0) {
if (mod) { const t = wordLeft(v, c); v = v.slice(0, t) + v.slice(c); c = t }
else { v = v.slice(0, c - 1) + v.slice(c); c-- }
if (k.ctrl && inp === 'z') {
return swap(undoStack, redoStack)
}
else if (k.ctrl && inp === 'w' && c > 0) { const t = wordLeft(v, c); v = v.slice(0, t) + v.slice(c); c = t }
else if (k.ctrl && inp === 'u') { v = v.slice(c); c = 0 }
else if (k.ctrl && inp === 'k') {v = v.slice(0, c)}
else if (k.meta && inp === 'b') {c = wordLeft(v, c)}
else if (k.meta && inp === 'f') {c = wordRight(v, c)}
else if (inp.length > 0) {
if ((k.ctrl && inp === 'y') || (k.meta && k.shift && inp === 'z')) {
return swap(redoStack, undoStack)
}
if (k.home || (k.ctrl && inp === 'a')) {
c = 0
} else if (k.end || (k.ctrl && inp === 'e')) {
c = v.length
} else if (k.leftArrow) {
c = mod ? wordLeft(v, c) : Math.max(0, c - 1)
} else if (k.rightArrow) {
c = mod ? wordRight(v, c) : Math.min(v.length, c + 1)
} else if ((k.backspace || k.delete) && c > 0) {
if (mod) {
const t = wordLeft(v, c)
v = v.slice(0, t) + v.slice(c)
c = t
} else {
v = v.slice(0, c - 1) + v.slice(c)
c--
}
} else if (k.ctrl && inp === 'w' && c > 0) {
const t = wordLeft(v, c)
v = v.slice(0, t) + v.slice(c)
c = t
} else if (k.ctrl && inp === 'u') {
v = v.slice(c)
c = 0
} else if (k.ctrl && inp === 'k') {
v = v.slice(0, c)
} else if (k.meta && inp === 'b') {
c = wordLeft(v, c)
} else if (k.meta && inp === 'f') {
c = wordRight(v, c)
} else if (inp.length > 0) {
const bracketed = inp.includes('[200~')
const raw = inp.replace(BRACKET_PASTE, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')
if (bracketed && emitPaste({ bracketed: true, cursor: c, text: raw, value: v })) {return}
if (bracketed && emitPaste({ bracketed: true, cursor: c, text: raw, value: v })) {
return
}
if (!raw) {return}
if (!raw) {
return
}
if (raw === '\n') {return commit(insert(v, c, '\n'), c + 1)}
if (raw === '\n') {
return commit(insert(v, c, '\n'), c + 1)
}
if (raw.length > 1 || raw.includes('\n')) {
if (!pasteBuf.current) {pastePos.current = c}
if (!pasteBuf.current) {
pastePos.current = c
}
pasteBuf.current += raw
if (pasteTimer.current) {clearTimeout(pasteTimer.current)}
if (pasteTimer.current) {
clearTimeout(pasteTimer.current)
}
pasteTimer.current = setTimeout(flushPaste, 50)
return
}
if (PRINTABLE.test(raw)) { v = v.slice(0, c) + raw + v.slice(c); c += raw.length }
else {return}
} else {return}
if (PRINTABLE.test(raw)) {
v = v.slice(0, c) + raw + v.slice(c)
c += raw.length
} else {
return
}
} else {
return
}
commit(v, c)
},
@@ -213,7 +282,9 @@ export function TextInput({ value, onChange, onPaste, onSubmit, placeholder = ''
// ── Render ──────────────────────────────────────────────────────
if (!focus) {return <Text>{value || (placeholder ? DIM + placeholder + DIM_OFF : '')}</Text>}
if (!focus) {
return <Text>{value || (placeholder ? DIM + placeholder + DIM_OFF : '')}</Text>
}
if (!value && placeholder) {
return <Text>{INV + (placeholder[0] ?? ' ') + INV_OFF + DIM + placeholder.slice(1) + DIM_OFF}</Text>