MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / formatResults

Function formatResults

src/services/ripgrep/index.ts:255–295  ·  view source on GitHub ↗
(fileResults: SearchFileResult[], cwd: string)

Source from the content-addressed store, hash-verified

253}
254
255function formatResults(fileResults: SearchFileResult[], cwd: string): string {
256 const groupedResults: { [key: string]: SearchResult[] } = {}
257
258 const totalResults = fileResults.reduce((sum, file) => sum + file.searchResults.length, 0)
259 let output = ""
260 if (totalResults >= MAX_RESULTS) {
261 output += `Showing first ${MAX_RESULTS} of ${MAX_RESULTS}+ results. Use a more specific search if necessary.\n\n`
262 } else {
263 output += `Found ${totalResults === 1 ? "1 result" : `${totalResults.toLocaleString()} results`}.\n\n`
264 }
265
266 // Group results by file name
267 fileResults.slice(0, MAX_RESULTS).forEach((file) => {
268 const relativeFilePath = path.relative(cwd, file.file)
269 if (!groupedResults[relativeFilePath]) {
270 groupedResults[relativeFilePath] = []
271
272 groupedResults[relativeFilePath].push(...file.searchResults)
273 }
274 })
275
276 for (const [filePath, fileResults] of Object.entries(groupedResults)) {
277 output += `# ${filePath.toPosix()}\n`
278
279 fileResults.forEach((result) => {
280 // Only show results with at least one line
281 if (result.lines.length > 0) {
282 // Show all lines in the result
283 result.lines.forEach((line) => {
284 const lineNumber = String(line.line).padStart(3, " ")
285 output += `${lineNumber} | ${line.text.trimEnd()}\n`
286 })
287 output += "----\n"
288 }
289 })
290
291 output += "\n"
292 }
293
294 return output.trim()
295}

Callers 1

regexSearchFilesFunction · 0.85

Calls 4

forEachMethod · 0.80
entriesMethod · 0.80
toPosixMethod · 0.65
StringInterface · 0.50

Tested by

no test coverage detected