Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 35x 35x 35x 35x 35x 2x 2x 10x 3x 7x 2x 35x 1x 35x 1x 35x 2x 35x 54x 13x 13x 13x 26x 26x 13x 11x 11x 11x 54x 13x 13x 13x 13x 13x 41x 4x 37x 21x 18x 18x 18x 18x 18x 17x 18x 18x 18x 18x 21x 21x 21x 2x 2x 21x 1x 1x 1x 21x 21x 21x 21x 1x 2x 1x 1x 1x 1x 1x 1x 20x 20x 19x 18x 18x 17x 17x 3x 3x 3x 3x 14x 17x 17x 17x 17x 17x 16x 16x 18x 18x | import { useRef, useEffect } from "react";
import { ScrollArea } from "@/components/ui/scroll-area";
import type { OutputLine } from "@shared/schema";
interface SerialMonitorProps {
output: OutputLine[];
isConnected: boolean;
isSimulationRunning: boolean;
onSendMessage: (message: string) => void;
onClear: () => void;
showMonitor?: boolean;
autoScrollEnabled?: boolean;
}
// Simple ANSI escape code processor
// NOTE: Backspace (\b) is handled separately in applyBackspaceAcrossLines for cross-line support
function processAnsiCodes(text: string): string {
let processed = text.replace(/\x1b\[2J/g, "").replace(/\u001b\[2J/g, "");
processed = processed.replace(/\x1b\[H/g, "").replace(/\u001b\[H/g, "");
// Remove common ANSI color sequences
processed = processed
.replace(/\x1b\[[0-9;]*m/g, "")
.replace(/\u001b\[[0-9;]*m/g, "");
// Clear line CSI (ESC[K) - remove it
processed = processed.replace(/\x1b\[K/g, "").replace(/\u001b\[K/g, "");
// Backspace within the SAME chunk: apply locally
// (Cross-chunk backspaces are handled in applyBackspaceAcrossLines)
if (processed.includes("\b")) {
let out = "";
for (const ch of processed) {
if (ch === "\b") {
out = out.slice(0, -1);
} else {
out += ch;
}
}
processed = out;
}
// Expand tabs to 4 spaces
if (processed.includes("\t")) {
processed = processed.replace(/\t/g, " ");
}
// Bell character: replace with visible marker (so it's not silently dropped)
if (processed.includes("\x07")) {
processed = processed.replace(/\x07/g, "␇");
}
// Form feed and vertical tab => normalize to newline
if (processed.includes("\f") || processed.includes("\v")) {
processed = processed.replace(/\f/g, "\n").replace(/\v/g, "\n");
}
return processed;
}
// Exported for unit testing and reuse inside the hook
export function applyBackspaceAcrossLines(
lines: Array<{ text: string; incomplete: boolean }>,
text: string,
isComplete: boolean,
): string | null {
// Handle backspaces at the start of text
if (text.includes("\b")) {
// Count leading backspaces to remove from previous line
let backspaceCount = 0;
let idx = 0;
while (idx < text.length && text[idx] === "\b") {
backspaceCount++;
idx++;
}
if (
backspaceCount > 0 &&
lines.length > 0 &&
lines[lines.length - 1].incomplete
) {
const lastLine = lines[lines.length - 1];
lastLine.text = lastLine.text.slice(
0,
Math.max(0, lastLine.text.length - backspaceCount),
);
text = text.slice(backspaceCount);
}
}
// If there's still text to process and we have an incomplete line, append to it
if (text && lines.length > 0 && lines[lines.length - 1].incomplete) {
const cleanText = processAnsiCodes(text);
Eif (cleanText) {
lines[lines.length - 1].text += cleanText;
lines[lines.length - 1].incomplete = !isComplete;
}
return null; // already handled
}
// No text left after backspace processing, or no incomplete line to append to
if (!text) {
return null;
}
// Text remains: caller should handle it (new line or other processing)
return text;
}
function hasControlChars(text: string) {
return {
hasClearScreen: text.includes("\x1b[2J") || text.includes("\u001b[2J"),
hasCursorHome: text.includes("\x1b[H") || text.includes("\u001b[H"),
hasCarriageReturn: text.includes("\r"),
};
}
export function SerialMonitor({
output,
isConnected,
isSimulationRunning: _isSimulationRunning = false,
onSendMessage: _onSendMessage,
onClear: _onClear,
showMonitor = true,
autoScrollEnabled = true,
}: SerialMonitorProps) {
void isConnected;
const outputRef = useRef<HTMLDivElement | null>(null);
const shouldAutoScrollRef = useRef(true);
const lastScrollTopRef = useRef(0);
useEffect(() => {
// enable/disable autoscroll according to parent prop
shouldAutoScrollRef.current = !!autoScrollEnabled;
}, [autoScrollEnabled]);
useEffect(() => {
const lines: Array<{ text: string; incomplete: boolean }> = [];
let shouldClear = false;
output.forEach((line) => {
let text = line.text;
const controls = hasControlChars(text);
if (controls.hasClearScreen) {
shouldClear = true;
lines.length = 0;
}
if (controls.hasCursorHome) {
Eif (shouldClear) {
lines.length = 0;
shouldClear = false;
}
}
// Handle backspace across line boundaries: apply to last incomplete line
const backspaceResult = applyBackspaceAcrossLines(
lines,
text,
line.complete ?? true,
);
Iif (backspaceResult === null) {
return; // handled fully
}
text = backspaceResult;
if (controls.hasCarriageReturn) {
const parts = text.split("\r");
const cleanParts = parts.map((p) => processAnsiCodes(p));
Eif (cleanParts.length > 1) {
const finalText = cleanParts[cleanParts.length - 1];
Iif (lines.length > 0 && !lines[lines.length - 1].incomplete) {
lines.push({ text: finalText, incomplete: !line.complete });
} else {
Iif (lines.length > 0) {
lines[lines.length - 1] = {
text: finalText,
incomplete: !line.complete,
};
} else {
lines.push({ text: finalText, incomplete: !line.complete });
}
}
return;
}
}
const cleanText = processAnsiCodes(text);
if (cleanText) {
lines.push({ text: cleanText, incomplete: !line.complete });
}
});
const el = outputRef.current;
if (!el) return;
el.innerHTML = "";
if (lines.length === 0) {
const placeholder = document.createElement("div");
placeholder.className = "text-muted-foreground italic";
placeholder.textContent = "Serial output will appear here...";
el.appendChild(placeholder);
} else {
lines.forEach((ln) => {
const div = document.createElement("div");
div.className = "text-foreground whitespace-pre-wrap break-words";
div.textContent = ln.text;
el.appendChild(div);
});
}
if (shouldAutoScrollRef.current && el) {
el.scrollTop = el.scrollHeight;
lastScrollTopRef.current = el.scrollTop;
}
}, [output]);
const handleScroll = () => {
const el = outputRef.current;
if (!el) return;
const currentScrollTop = el.scrollTop;
const maxScrollTop = el.scrollHeight - el.clientHeight;
if (currentScrollTop < lastScrollTopRef.current - 5) {
shouldAutoScrollRef.current = false;
}
if (maxScrollTop - currentScrollTop < 20) {
shouldAutoScrollRef.current = true;
}
lastScrollTopRef.current = currentScrollTop;
};
return (
<div className="h-full flex flex-col" data-testid="serial-monitor">
<div className="flex-1 min-h-0">
{showMonitor ? (
<ScrollArea
className="h-full"
viewportRef={outputRef}
viewportTestId="serial-output"
viewportProps={{ onScroll: handleScroll }}
viewportClassName="p-3 text-ui-xs font-mono"
thumbClassName="bg-status-success"
/>
) : (
<div className="h-full" />
)}
</div>
</div>
);
}
|