| 38 | } |
| 39 | |
| 40 | function parseGrepOutput(result: string): GrepGroup[] { |
| 41 | const lines = result.split("\n").filter(Boolean); |
| 42 | const groups: Map<string, GrepMatch[]> = new Map(); |
| 43 | |
| 44 | for (const line of lines) { |
| 45 | // Format: "file:lineNo:content" or "file:content" or just "file" |
| 46 | const colonMatch = line.match(/^([^:]+):(\d+):(.*)$/); |
| 47 | if (colonMatch) { |
| 48 | const [, file, lineNo, content] = colonMatch; |
| 49 | if (!groups.has(file)) groups.set(file, []); |
| 50 | groups.get(file)!.push({ file, lineNo: parseInt(lineNo, 10), content }); |
| 51 | } else if (line.match(/^[^:]+$/)) { |
| 52 | // Files-only mode |
| 53 | if (!groups.has(line)) groups.set(line, []); |
| 54 | } else { |
| 55 | // fallback: treat entire line as content with unknown file |
| 56 | if (!groups.has("")) groups.set("", []); |
| 57 | groups.get("")!.push({ file: "", content: line }); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return Array.from(groups.entries()).map(([file, matches]) => ({ |
| 62 | file, |
| 63 | matches, |
| 64 | })); |
| 65 | } |
| 66 | |
| 67 | function highlightPattern(text: string, pattern: string): React.ReactNode { |
| 68 | try { |