UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

146 lines • 6.25 kB
/** * Presentation for the grep tool. * * Renderers live apart from the implementation so a process that only displays tool output does not * load the execution path or its typebox parameter schema. `grep.ts` spreads these into its * definition, so the tool's public shape is unchanged. */ import { Text } from "@earendil-works/pi-tui"; import { keyHint } from "../../../modes/interactive/components/keybinding-hints.js"; import { displayPath } from "../grep/format.js"; import { getTextOutput, invalidArgText, linkPath, renderToolPath, str } from "../render-utils.js"; const COLLAPSED_LINE_BUDGET = 15; function isV1Details(value) { if (!value || typeof value !== "object") return false; const details = value; return details.version === 1 && Array.isArray(details.matches); } function formatCallPaths(path, theme, cwd) { if (Array.isArray(path)) { if (path.length === 0 || path.some((entry) => typeof entry !== "string")) return invalidArgText(theme); return path.map((entry) => renderToolPath(entry, theme, cwd, { emptyFallback: "." })).join(", "); } return renderToolPath(str(path), theme, cwd, { emptyFallback: "." }); } function formatCallGlobs(glob) { if (glob === undefined) return undefined; const values = Array.isArray(glob) ? glob : [glob]; const joined = values.filter((entry) => typeof entry === "string").join(","); return joined || undefined; } function formatGrepCall(args, theme, cwd) { const pattern = str(args?.pattern); const invalidArg = invalidArgText(theme); let text = `${theme.fg("toolTitle", theme.bold("grep"))} ${pattern === null ? invalidArg : theme.fg("accent", `/${pattern || ""}/`)}${theme.fg("toolOutput", " in ")}${formatCallPaths(args?.path, theme, cwd)}`; const glob = formatCallGlobs(args?.glob); if (glob) text += theme.fg("toolOutput", ` ${glob}`); if (typeof args?.mode === "string" && args.mode) text += theme.fg("toolOutput", ` ${args.mode}`); if (typeof args?.skip === "number") text += theme.fg("toolOutput", ` skip ${args.skip}`); return text; } function formatFileHeader(path, theme, cwd) { return linkPath(theme.fg("accent", displayPath(path)), path, cwd); } function formatMatchRow(match, theme) { const row = `${match.line}${match.isContext ? "-" : ":"} ${match.text}`; return match.isContext ? theme.fg("dim", row) : theme.fg("accent", row); } function noMatchText(details) { if (details.status === "pageEnd") return `No more results (skip=${details.skip})`; if (details.status === "partial") return "No matches found in searched portion"; return "No matches found"; } function groupsFromDetails(details) { if (details.fileMatches.length > 0) { return details.fileMatches.map((file) => ({ path: file.path, count: file.count, rows: details.matches.filter((match) => match.path === file.path), })); } const groups = []; for (const match of details.matches) { const last = groups[groups.length - 1]; if (!last || last.path !== match.path) groups.push({ path: match.path, count: null, rows: [match] }); else last.rows.push(match); } return groups; } function overflowHint(remaining, unit, theme) { return `${theme.fg("muted", `... (${remaining} more ${unit},`)} ${keyHint("app.tools.expand", "to expand")}${theme.fg("muted", ")")}`; } function formatGroupedResult(details, options, theme, cwd) { const lines = []; const groups = groupsFromDetails(details); if (groups.length === 0) { lines.push(theme.fg("toolOutput", noMatchText(details))); } else { let hiddenGroups = 0; for (let index = 0; index < groups.length; index++) { const group = groups[index]; const rows = options.expanded ? group.rows : group.rows.filter((row) => !row.isContext); const block = []; if (lines.length > 0) block.push(""); if (rows.length === 0 && group.count !== null) { block.push(`${formatFileHeader(group.path, theme, cwd)}${theme.fg("toolOutput", `: ${group.count}`)}`); } else { block.push(formatFileHeader(group.path, theme, cwd)); for (const row of rows) block.push(formatMatchRow(row, theme)); } if (!options.expanded && lines.length > 0 && lines.length + block.length > COLLAPSED_LINE_BUDGET) { hiddenGroups = groups.length - index; break; } lines.push(...block); } if (hiddenGroups > 0) lines.push(overflowHint(hiddenGroups, hiddenGroups === 1 ? "file" : "files", theme)); } return `\n${lines.join("\n")}`; } function formatTextFallback(result, options, theme, showImages) { const output = getTextOutput(result, showImages).trim(); if (!output) return ""; const lines = output.split("\n"); const maxLines = options.expanded ? lines.length : COLLAPSED_LINE_BUDGET; const displayLines = lines.slice(0, maxLines); const remaining = lines.length - maxLines; let text = `\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`; if (remaining > 0) { text += `\n${overflowHint(remaining, remaining === 1 ? "line" : "lines", theme)}`; } return text; } function formatGrepResult(result, options, theme, showImages, cwd) { if (isV1Details(result.details)) return formatGroupedResult(result.details, options, theme, cwd); return formatTextFallback(result, options, theme, showImages); } export const grepRenderers = { renderCall(args, theme, context) { const text = context.lastComponent ?? new Text("", 0, 0); text.setText(formatGrepCall(args, theme, context.cwd)); return text; }, renderResult(result, options, theme, context) { const text = context.lastComponent ?? new Text("", 0, 0); text.setText(formatGrepResult(result, options, theme, context.showImages, context.cwd)); return text; }, }; //# sourceMappingURL=grep.js.map