feat(SerialConsolePage): enhance input handling with line buffering and control character support

This commit is contained in:
Jaime Idolpx 2026-06-11 00:13:59 -04:00
parent 521fb8f613
commit 0fce95e9b8

View File

@ -11,6 +11,7 @@ export default function SerialConsolePage({ onBack }: Props) {
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const termRef = useRef<Terminal | null>(null); const termRef = useRef<Terminal | null>(null);
const fitRef = useRef<FitAddon | null>(null); const fitRef = useRef<FitAddon | null>(null);
const lineBuffer = useRef('');
const { status, send, subscribe } = useWs(); const { status, send, subscribe } = useWs();
// Keep a stable ref to `send` so the xterm onData closure never goes stale // Keep a stable ref to `send` so the xterm onData closure never goes stale
@ -46,7 +47,26 @@ export default function SerialConsolePage({ onBack }: Props) {
term.writeln('\x1b[2m── Meatloaf Serial Console ──\x1b[0m'); term.writeln('\x1b[2m── Meatloaf Serial Console ──\x1b[0m');
term.writeln(''); term.writeln('');
term.onData((data) => sendRef.current(data)); term.onData((data) => {
for (const char of data) {
if (char === '\r' || char === '\n') {
term.write('\r\n');
sendRef.current(lineBuffer.current + '\r');
lineBuffer.current = '';
} else if (char === '\x7f' || char === '\b') {
if (lineBuffer.current.length > 0) {
lineBuffer.current = lineBuffer.current.slice(0, -1);
term.write('\b \b');
}
} else if (char === '\x03') {
lineBuffer.current = '';
term.write('^C\r\n');
} else if (char >= ' ') {
lineBuffer.current += char;
term.write(char);
}
}
});
const onResize = () => fitRef.current?.fit(); const onResize = () => fitRef.current?.fit();
window.addEventListener('resize', onResize); window.addEventListener('resize', onResize);