UNPKG

@code-yeongyu/senpi

Version:

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

143 lines • 5.68 kB
import * as Diff from "diff"; import { getLanguageFromPath, highlightCode } from "../../modes/interactive/theme/theme.js"; function parseRenderableDiffLine(line) { const match = line.match(/^([+\- ])(\s*\d*)\s(.*)$/); if (!match) return { kind: "meta", text: line }; const sign = match[1]; const lineNumber = match[2]; if ((sign !== "+" && sign !== "-" && sign !== " ") || lineNumber === undefined) return { kind: "meta", text: line }; const content = match[3] ?? ""; if (sign === "+") return { content, kind: "added", lineNumber, sign }; if (sign === "-") return { content, kind: "removed", lineNumber, sign }; return { content, kind: "context", lineNumber, sign }; } function replaceTabs(text) { return text.replace(/\t/g, " "); } function highlightDiffContent(content, filePath) { const plainContent = replaceTabs(content); if (!filePath) return plainContent; const language = getLanguageFromPath(filePath); try { return highlightCode(plainContent, language)[0] ?? plainContent; } catch { return plainContent; } } function renderInlineDiff(oldContent, newContent, theme) { const parts = Diff.diffWords(replaceTabs(oldContent), replaceTabs(newContent)); let added = ""; let removed = ""; let firstAdded = true; let firstRemoved = true; for (const part of parts) { if (part.added) { let value = part.value; if (firstAdded) { const leadingWhitespace = value.match(/^(\s*)/)?.[1] ?? ""; added += leadingWhitespace; value = value.slice(leadingWhitespace.length); firstAdded = false; } if (value) added += theme.inverse(value); continue; } if (part.removed) { let value = part.value; if (firstRemoved) { const leadingWhitespace = value.match(/^(\s*)/)?.[1] ?? ""; removed += leadingWhitespace; value = value.slice(leadingWhitespace.length); firstRemoved = false; } if (value) removed += theme.inverse(value); continue; } added += part.value; removed += part.value; } return { added, removed }; } function renderToolDiffLine(line, filePath, theme, contentOverride) { const lineNumber = theme.fg("muted", line.lineNumber); if (line.kind === "context") { return `${theme.fg("toolDiffContext", line.sign)}${lineNumber} ${highlightDiffContent(line.content, filePath)}`; } const diffColor = line.kind === "added" ? "toolDiffAdded" : "toolDiffRemoved"; const background = line.kind === "added" ? "toolSuccessBg" : "toolErrorBg"; const content = contentOverride === undefined ? highlightDiffContent(line.content, filePath) : theme.fg(diffColor, replaceTabs(contentOverride)); const rendered = `${theme.fg(diffColor, line.sign)}${lineNumber} ${content}`; return theme.bg(background, rendered); } export function countDiffChanges(diffText) { let added = 0; let removed = 0; for (const line of diffText.split("\n")) { if (line.startsWith("+++") || line.startsWith("---")) continue; if (line.startsWith("+")) added++; else if (line.startsWith("-")) removed++; } return { added, removed }; } export function renderToolDiff(diffText, options) { const parsedLines = diffText.split("\n").map(parseRenderableDiffLine); const rendered = []; let index = 0; while (index < parsedLines.length) { const line = parsedLines[index]; if (!line) { index++; continue; } if (line.kind !== "removed") { rendered.push(line.kind === "meta" ? options.theme.fg("toolDiffContext", line.text) : renderToolDiffLine(line, options.filePath, options.theme)); index++; continue; } const removedLines = []; while (parsedLines[index]?.kind === "removed") { const removedLine = parsedLines[index]; if (removedLine?.kind === "removed") removedLines.push(removedLine); index++; } const addedLines = []; while (parsedLines[index]?.kind === "added") { const addedLine = parsedLines[index]; if (addedLine?.kind === "added") addedLines.push(addedLine); index++; } const pairedCount = Math.min(removedLines.length, addedLines.length); for (let pairIndex = 0; pairIndex < pairedCount; pairIndex++) { const removedLine = removedLines[pairIndex]; const addedLine = addedLines[pairIndex]; if (!removedLine || !addedLine) continue; const inline = renderInlineDiff(removedLine.content, addedLine.content, options.theme); rendered.push(renderToolDiffLine(removedLine, options.filePath, options.theme, inline.removed)); rendered.push(renderToolDiffLine(addedLine, options.filePath, options.theme, inline.added)); } for (const removedLine of removedLines.slice(pairedCount)) rendered.push(renderToolDiffLine(removedLine, options.filePath, options.theme)); for (const addedLine of addedLines.slice(pairedCount)) rendered.push(renderToolDiffLine(addedLine, options.filePath, options.theme)); } return rendered.join("\n"); } //# sourceMappingURL=diff-render.js.map