Skip to content

Commit 65a2b5a

Browse files
lambdalisueclaude
andcommitted
feat: add smart grep renderer for formatting grep-like results
Implements a renderer that reformats grep-like results with proper alignment, optional grouping by directory, and colorization. Supports configurable text length, line numbers display, and column alignment for improved readability. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c4e2c32 commit 65a2b5a

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed

builtin/renderer/smart_grep.ts

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import { defineRenderer, type Renderer } from "../../renderer.ts";
2+
import { dirname } from "@std/path/dirname";
3+
import { basename } from "@std/path/basename";
4+
5+
type Detail = {
6+
/**
7+
* File path
8+
*/
9+
path: string;
10+
11+
/**
12+
* Line number
13+
*/
14+
line?: number;
15+
16+
/**
17+
* Column number
18+
*/
19+
column?: number;
20+
21+
/**
22+
* Matched text or line content
23+
*/
24+
text?: string;
25+
};
26+
27+
export type SmartGrepOptions = {
28+
/**
29+
* Maximum length for displayed text.
30+
* @default 80
31+
*/
32+
maxTextLength?: number;
33+
34+
/**
35+
* Whether to show line and column numbers.
36+
* @default true
37+
*/
38+
showLineNumbers?: boolean;
39+
40+
/**
41+
* Whether to group results by directory.
42+
* @default false
43+
*/
44+
groupByDirectory?: boolean;
45+
46+
/**
47+
* Whether to use relative paths.
48+
* @default true
49+
*/
50+
useRelativePaths?: boolean;
51+
52+
/**
53+
* Whether to colorize output (using ANSI codes).
54+
* @default false
55+
*/
56+
colorize?: boolean;
57+
58+
/**
59+
* Whether to align columns.
60+
* @default true
61+
*/
62+
alignColumns?: boolean;
63+
};
64+
65+
/**
66+
* Creates a Renderer that formats grep-like results in a smart way.
67+
*
68+
* This Renderer reformats items that contain file paths, line numbers,
69+
* and matched text into a more readable format with proper alignment
70+
* and optional grouping.
71+
*
72+
* @param options - Options to customize smart grep display.
73+
* @returns A Renderer that reformats grep-like results.
74+
*/
75+
export function smartGrep(
76+
options: Readonly<SmartGrepOptions> = {},
77+
): Renderer<Detail> {
78+
const maxTextLength = options.maxTextLength ?? 80;
79+
const showLineNumbers = options.showLineNumbers ?? true;
80+
const groupByDirectory = options.groupByDirectory ?? false;
81+
const useRelativePaths = options.useRelativePaths ?? true;
82+
const colorize = options.colorize ?? false;
83+
const alignColumns = options.alignColumns ?? true;
84+
85+
return defineRenderer(async (_denops, { items }) => {
86+
if (items.length === 0) {
87+
return;
88+
}
89+
90+
// Calculate maximum widths for alignment
91+
let maxPathWidth = 0;
92+
let maxLineWidth = 0;
93+
let maxColumnWidth = 0;
94+
95+
if (alignColumns) {
96+
for (const item of items) {
97+
const { path, line, column } = item.detail;
98+
const displayPath = useRelativePaths ? path : path;
99+
maxPathWidth = Math.max(maxPathWidth, displayPath.length);
100+
if (line !== undefined) {
101+
maxLineWidth = Math.max(maxLineWidth, line.toString().length);
102+
}
103+
if (column !== undefined) {
104+
maxColumnWidth = Math.max(maxColumnWidth, column.toString().length);
105+
}
106+
}
107+
}
108+
109+
// Group items by directory if requested
110+
if (groupByDirectory) {
111+
const groups = new Map<string, typeof items>();
112+
113+
for (const item of items) {
114+
const dir = dirname(item.detail.path);
115+
if (!groups.has(dir)) {
116+
groups.set(dir, []);
117+
}
118+
groups.get(dir)!.push(item);
119+
}
120+
121+
// Process each group
122+
let currentDir = "";
123+
for (const item of items) {
124+
const dir = dirname(item.detail.path);
125+
126+
// Add directory header if changed
127+
if (dir !== currentDir) {
128+
currentDir = dir;
129+
// Modify the first item in each directory group to include header
130+
const dirHeader = `=== ${dir} ===`;
131+
if (colorize) {
132+
item.label = `\x1b[36m${dirHeader}\x1b[0m\n${formatItem(item)}`;
133+
} else {
134+
item.label = `${dirHeader}\n${formatItem(item)}`;
135+
}
136+
} else {
137+
item.label = formatItem(item);
138+
}
139+
}
140+
} else {
141+
// Format each item individually
142+
for (const item of items) {
143+
item.label = formatItem(item);
144+
}
145+
}
146+
147+
function formatItem(item: typeof items[0]): string {
148+
const { path, line, column, text } = item.detail;
149+
const parts: string[] = [];
150+
151+
// Format path
152+
const displayPath = useRelativePaths ? path : path;
153+
const filename = basename(displayPath);
154+
const pathPart = alignColumns
155+
? displayPath.padEnd(maxPathWidth)
156+
: displayPath;
157+
158+
if (colorize) {
159+
parts.push(`\x1b[35m${pathPart}\x1b[0m`); // Magenta for path
160+
} else {
161+
parts.push(pathPart);
162+
}
163+
164+
// Format line and column numbers
165+
if (showLineNumbers && line !== undefined) {
166+
const lineStr = alignColumns
167+
? line.toString().padStart(maxLineWidth)
168+
: line.toString();
169+
170+
if (column !== undefined) {
171+
const colStr = alignColumns
172+
? column.toString().padStart(maxColumnWidth)
173+
: column.toString();
174+
175+
if (colorize) {
176+
parts.push(`\x1b[32m${lineStr}:${colStr}\x1b[0m`); // Green for numbers
177+
} else {
178+
parts.push(`${lineStr}:${colStr}`);
179+
}
180+
} else {
181+
if (colorize) {
182+
parts.push(`\x1b[32m${lineStr}\x1b[0m`); // Green for line number
183+
} else {
184+
parts.push(lineStr);
185+
}
186+
}
187+
}
188+
189+
// Format text
190+
if (text) {
191+
let displayText = text.trim();
192+
if (displayText.length > maxTextLength) {
193+
displayText = displayText.substring(0, maxTextLength - 3) + "...";
194+
}
195+
196+
if (colorize) {
197+
// Try to highlight the matched part (simple approach)
198+
parts.push(`\x1b[37m${displayText}\x1b[0m`); // White for text
199+
} else {
200+
parts.push(displayText);
201+
}
202+
}
203+
204+
return parts.join(" ");
205+
}
206+
});
207+
}

0 commit comments

Comments
 (0)