@casualjim/pi-pretty 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +48 -0
- package/package.json +71 -0
- package/src/autocomplete.ts +91 -0
- package/src/config.ts +468 -0
- package/src/fff-helpers.ts +86 -0
- package/src/fff.ts +340 -0
- package/src/find-glob.ts +21 -0
- package/src/helpers.ts +75 -0
- package/src/index.ts +556 -0
- package/src/notices.ts +3 -0
- package/src/prompt-editor.ts +104 -0
- package/src/render.ts +488 -0
- package/src/session-color.ts +172 -0
- package/src/tools/bash.d.ts +21 -0
- package/src/tools/bash.ts +163 -0
- package/src/tools/find.ts +231 -0
- package/src/tools/grep.ts +187 -0
- package/src/tools/labels.ts +3 -0
- package/src/tools/ls.ts +119 -0
- package/src/tools/metrics.ts +60 -0
- package/src/tools/read.ts +319 -0
- package/src/tui-text.ts +17 -0
- package/src/types.ts +202 -0
- package/src/working-indicator.ts +912 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FFF helper functions — extracted for testability.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions and classes used by the FFF integration in index.ts.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { GrepCursor, GrepMatch } from "@ff-labs/fff-node";
|
|
8
|
+
|
|
9
|
+
function sanitizeGrepRecordContent(text: string): string {
|
|
10
|
+
let content = text;
|
|
11
|
+
if (content.endsWith("\r\n")) content = content.slice(0, -2);
|
|
12
|
+
else if (content.endsWith("\r") || content.endsWith("\n")) content = content.slice(0, -1);
|
|
13
|
+
|
|
14
|
+
return content.replace(/\r\n/g, "\\n").replace(/\r/g, "\\r").replace(/\n/g, "\\n");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function truncateGrepRecordContent(text: string): string {
|
|
18
|
+
const content = sanitizeGrepRecordContent(text);
|
|
19
|
+
return content.length > 500 ? `${content.slice(0, 500)}...` : content;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Store for FFF grep pagination cursors.
|
|
24
|
+
* Evicts oldest entry when exceeding maxSize.
|
|
25
|
+
*/
|
|
26
|
+
export class CursorStore {
|
|
27
|
+
private cursors = new Map<string, GrepCursor>();
|
|
28
|
+
private counter = 0;
|
|
29
|
+
private maxSize: number;
|
|
30
|
+
|
|
31
|
+
constructor(maxSize = 200) {
|
|
32
|
+
this.maxSize = maxSize;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
store(cursor: GrepCursor): string {
|
|
36
|
+
const id = `fff_c${++this.counter}`;
|
|
37
|
+
this.cursors.set(id, cursor);
|
|
38
|
+
if (this.cursors.size > this.maxSize) {
|
|
39
|
+
const first = this.cursors.keys().next().value;
|
|
40
|
+
if (first) this.cursors.delete(first);
|
|
41
|
+
}
|
|
42
|
+
return id;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get(id: string): GrepCursor | undefined {
|
|
46
|
+
return this.cursors.get(id);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get size(): number {
|
|
50
|
+
return this.cursors.size;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Convert FFF GrepResult items to ripgrep-style "file:line:content" text.
|
|
56
|
+
* This ensures pi-pretty's renderGrepResults works unchanged.
|
|
57
|
+
*/
|
|
58
|
+
export function fffFormatGrepText(items: GrepMatch[], limit: number): string {
|
|
59
|
+
const capped = items.slice(0, limit);
|
|
60
|
+
if (!capped.length) return "No matches found";
|
|
61
|
+
|
|
62
|
+
const lines: string[] = [];
|
|
63
|
+
let currentFile = "";
|
|
64
|
+
|
|
65
|
+
for (const match of capped) {
|
|
66
|
+
if (match.relativePath !== currentFile) {
|
|
67
|
+
if (currentFile) lines.push("");
|
|
68
|
+
currentFile = match.relativePath;
|
|
69
|
+
}
|
|
70
|
+
if (match.contextBefore?.length) {
|
|
71
|
+
const startLine = match.lineNumber - match.contextBefore.length;
|
|
72
|
+
for (let i = 0; i < match.contextBefore.length; i++) {
|
|
73
|
+
lines.push(`${match.relativePath}-${startLine + i}-${truncateGrepRecordContent(match.contextBefore[i] ?? "")}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
lines.push(`${match.relativePath}:${match.lineNumber}:${truncateGrepRecordContent(match.lineContent)}`);
|
|
77
|
+
if (match.contextAfter?.length) {
|
|
78
|
+
const startLine = match.lineNumber + 1;
|
|
79
|
+
for (let i = 0; i < match.contextAfter.length; i++) {
|
|
80
|
+
lines.push(`${match.relativePath}-${startLine + i}-${truncateGrepRecordContent(match.contextAfter[i] ?? "")}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return lines.join("\n");
|
|
86
|
+
}
|
package/src/fff.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-pretty: FFF lifecycle management.
|
|
3
|
+
*
|
|
4
|
+
* Manages the FFF file finder lifecycle: create, wait for scan, destroy.
|
|
5
|
+
* Tools check `isAvailable` and use `finder` directly for search operations.
|
|
6
|
+
* Graceful fallback: if FFF is not installed or fails, tools degrade to SDK.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { mkdirSync, realpathSync } from "node:fs";
|
|
10
|
+
import { homedir } from "node:os";
|
|
11
|
+
import { join, parse, resolve } from "node:path";
|
|
12
|
+
|
|
13
|
+
import type { FileFinder as FffFileFinder, GrepCursor } from "@ff-labs/fff-node";
|
|
14
|
+
|
|
15
|
+
export interface FffInitOptions {
|
|
16
|
+
enableHomeDirScanning: boolean;
|
|
17
|
+
enableFsRootScanning: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type FffInitFailureKind = "restricted-base-path" | "native";
|
|
21
|
+
export type FffRestrictedScanScope = "home" | "root";
|
|
22
|
+
|
|
23
|
+
export class FffInitError extends Error {
|
|
24
|
+
readonly kind: FffInitFailureKind;
|
|
25
|
+
readonly restrictedScope: FffRestrictedScanScope | undefined;
|
|
26
|
+
|
|
27
|
+
constructor(kind: FffInitFailureKind, message: string, restrictedScope?: FffRestrictedScanScope) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "FffInitError";
|
|
30
|
+
this.kind = kind;
|
|
31
|
+
this.restrictedScope = restrictedScope;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isFffRestrictedBasePathError(error: unknown, options: FffInitOptions): boolean {
|
|
36
|
+
if (!(error instanceof FffInitError) || error.kind !== "restricted-base-path") return false;
|
|
37
|
+
if (error.restrictedScope === "home") return !options.enableHomeDirScanning;
|
|
38
|
+
if (error.restrictedScope === "root") return !options.enableFsRootScanning;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const DEFAULT_FFF_INIT_OPTIONS: FffInitOptions = {
|
|
43
|
+
enableHomeDirScanning: false,
|
|
44
|
+
enableFsRootScanning: false,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Cursor store — pagination cursors for grep
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
export class CursorStore {
|
|
52
|
+
private cursors = new Map<string, GrepCursor>();
|
|
53
|
+
private counter = 0;
|
|
54
|
+
private maxSize: number;
|
|
55
|
+
|
|
56
|
+
constructor(maxSize = 200) {
|
|
57
|
+
this.maxSize = maxSize;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
store(cursor: GrepCursor): string {
|
|
61
|
+
const id = `fff_c${++this.counter}`;
|
|
62
|
+
this.cursors.set(id, cursor);
|
|
63
|
+
if (this.cursors.size > this.maxSize) {
|
|
64
|
+
const first = this.cursors.keys().next().value;
|
|
65
|
+
if (first) this.cursors.delete(first);
|
|
66
|
+
}
|
|
67
|
+
return id;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get(id: string): GrepCursor | undefined {
|
|
71
|
+
return this.cursors.get(id);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get size(): number {
|
|
75
|
+
return this.cursors.size;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
// FffService — wraps a single FileFinder instance
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
|
|
83
|
+
const SHARED_FFF_SERVICE_KEY = Symbol.for("pi-pretty.fff-service");
|
|
84
|
+
|
|
85
|
+
type SharedFffState = {
|
|
86
|
+
service: FffService;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
type FinderConfig = {
|
|
90
|
+
basePath: string;
|
|
91
|
+
enableHomeDirScanning: boolean;
|
|
92
|
+
enableFsRootScanning: boolean;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
type GlobalWithFffService = typeof globalThis & {
|
|
96
|
+
[SHARED_FFF_SERVICE_KEY]?: SharedFffState;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export function getSharedFffService(fffModule?: typeof import("@ff-labs/fff-node"), agentDir?: string): FffService {
|
|
100
|
+
const sharedGlobal = globalThis as GlobalWithFffService;
|
|
101
|
+
const existing = sharedGlobal[SHARED_FFF_SERVICE_KEY];
|
|
102
|
+
if (existing) {
|
|
103
|
+
existing.service.configure(fffModule, agentDir);
|
|
104
|
+
return existing.service;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const service = new FffService(fffModule, agentDir);
|
|
108
|
+
sharedGlobal[SHARED_FFF_SERVICE_KEY] = { service };
|
|
109
|
+
return service;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function resetSharedFffServiceForTests(): void {
|
|
113
|
+
const sharedGlobal = globalThis as GlobalWithFffService;
|
|
114
|
+
sharedGlobal[SHARED_FFF_SERVICE_KEY]?.service.destroy();
|
|
115
|
+
delete sharedGlobal[SHARED_FFF_SERVICE_KEY];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export class FffService {
|
|
119
|
+
finder: FffFileFinder | null = null;
|
|
120
|
+
partialIndex = false;
|
|
121
|
+
cursorStore = new CursorStore();
|
|
122
|
+
|
|
123
|
+
private fffModule: typeof import("@ff-labs/fff-node") | null = null;
|
|
124
|
+
private dbDir: string | null = null;
|
|
125
|
+
private finderConfig: FinderConfig | null = null;
|
|
126
|
+
private finderPromise: Promise<void> | null = null;
|
|
127
|
+
private finderPromiseConfig: FinderConfig | null = null;
|
|
128
|
+
|
|
129
|
+
constructor(fffModule?: typeof import("@ff-labs/fff-node"), agentDir?: string) {
|
|
130
|
+
this.configure(fffModule, agentDir);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
configure(fffModule?: typeof import("@ff-labs/fff-node"), agentDir?: string): void {
|
|
134
|
+
if (fffModule) this.fffModule = fffModule;
|
|
135
|
+
if (agentDir) {
|
|
136
|
+
this.dbDir = join(agentDir, "pi-pretty", "fff");
|
|
137
|
+
try {
|
|
138
|
+
mkdirSync(this.dbDir, { recursive: true });
|
|
139
|
+
} catch {
|
|
140
|
+
// FFF can still run without persistent frecency/history paths.
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Returns true if the FFF native module is loaded (installed). */
|
|
146
|
+
isModuleLoaded(): boolean {
|
|
147
|
+
return this.fffModule !== null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
get isAvailable(): boolean {
|
|
151
|
+
return this.finder !== null && !this.finder.isDestroyed;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Attempt to load FFF module dynamically (called during session_start if not injected). */
|
|
155
|
+
async tryLoadModule(): Promise<boolean> {
|
|
156
|
+
if (this.fffModule) return true;
|
|
157
|
+
try {
|
|
158
|
+
const mod = await import("@ff-labs/fff-node");
|
|
159
|
+
this.fffModule = mod;
|
|
160
|
+
if (!this.dbDir && process.env.HOME) {
|
|
161
|
+
this.dbDir = join(process.env.HOME, ".pi/agent", "pi-pretty", "fff");
|
|
162
|
+
try {
|
|
163
|
+
mkdirSync(this.dbDir, { recursive: true });
|
|
164
|
+
} catch {
|
|
165
|
+
/* ignore */
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
} catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async ensureFinder(cwd: string, options: FffInitOptions = DEFAULT_FFF_INIT_OPTIONS): Promise<void> {
|
|
175
|
+
const requestedConfig = finderConfig(cwd, options);
|
|
176
|
+
|
|
177
|
+
for (;;) {
|
|
178
|
+
if (this.finder && !this.finder.isDestroyed && sameFinderConfig(this.finderConfig, requestedConfig)) return;
|
|
179
|
+
|
|
180
|
+
const pending = this.finderPromise;
|
|
181
|
+
if (pending) {
|
|
182
|
+
const pendingConfig = this.finderPromiseConfig;
|
|
183
|
+
try {
|
|
184
|
+
await pending;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
// A concurrent request for the same scope must observe the same
|
|
187
|
+
// failure. A different scope can retry after the pending attempt.
|
|
188
|
+
if (sameFinderConfig(pendingConfig, requestedConfig)) throw error;
|
|
189
|
+
}
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const promise = this._createFinder(cwd, options, requestedConfig);
|
|
194
|
+
this.finderPromise = promise;
|
|
195
|
+
this.finderPromiseConfig = requestedConfig;
|
|
196
|
+
try {
|
|
197
|
+
await promise;
|
|
198
|
+
return;
|
|
199
|
+
} finally {
|
|
200
|
+
if (this.finderPromise === promise) {
|
|
201
|
+
this.finderPromise = null;
|
|
202
|
+
this.finderPromiseConfig = null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private async _createFinder(cwd: string, options: FffInitOptions, config: FinderConfig): Promise<void> {
|
|
209
|
+
if (!this.fffModule) return;
|
|
210
|
+
|
|
211
|
+
const result = this.createFinder(cwd, this.dbDir, options);
|
|
212
|
+
if (!result.ok) {
|
|
213
|
+
const kind = isRestrictedBasePathMessage(result.error) ? "restricted-base-path" : "native";
|
|
214
|
+
throw new FffInitError(
|
|
215
|
+
kind,
|
|
216
|
+
result.error,
|
|
217
|
+
kind === "restricted-base-path" ? restrictedScanScope(cwd) : undefined,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const candidate = result.value;
|
|
222
|
+
let scan: Awaited<ReturnType<typeof candidate.waitForScan>>;
|
|
223
|
+
try {
|
|
224
|
+
scan = await candidate.waitForScan(15_000);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (!candidate.isDestroyed) {
|
|
227
|
+
try {
|
|
228
|
+
candidate.destroy();
|
|
229
|
+
} catch {
|
|
230
|
+
// Preserve the scan failure; the candidate is no longer selected.
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const previous = this.finder;
|
|
237
|
+
this.finder = candidate;
|
|
238
|
+
this.partialIndex = scan.ok && !scan.value;
|
|
239
|
+
this.finderConfig = config;
|
|
240
|
+
if (previous && previous !== candidate && !previous.isDestroyed) {
|
|
241
|
+
try {
|
|
242
|
+
previous.destroy();
|
|
243
|
+
} catch {
|
|
244
|
+
// Keep the replacement active; native cleanup is best effort.
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private createFinder(
|
|
250
|
+
cwd: string,
|
|
251
|
+
dbDir: string | null,
|
|
252
|
+
options: FffInitOptions,
|
|
253
|
+
): ReturnType<typeof import("@ff-labs/fff-node").FileFinder.create> {
|
|
254
|
+
if (!this.fffModule) throw new Error("FFF module is not loaded");
|
|
255
|
+
const commonOptions = {
|
|
256
|
+
basePath: cwd,
|
|
257
|
+
aiMode: true,
|
|
258
|
+
enableHomeDirScanning: options.enableHomeDirScanning,
|
|
259
|
+
enableFsRootScanning: options.enableFsRootScanning,
|
|
260
|
+
};
|
|
261
|
+
const result = this.fffModule.FileFinder.create({
|
|
262
|
+
...commonOptions,
|
|
263
|
+
frecencyDbPath: dbDir ? join(dbDir, "frecency.mdb") : "",
|
|
264
|
+
historyDbPath: dbDir ? join(dbDir, "history.mdb") : "",
|
|
265
|
+
});
|
|
266
|
+
if (result.ok || !isAlreadyOpenError(result.error) || !dbDir) return result;
|
|
267
|
+
|
|
268
|
+
const isolatedDir = join(dbDir, `runtime-${process.pid}-${Date.now()}`);
|
|
269
|
+
try {
|
|
270
|
+
mkdirSync(isolatedDir, { recursive: true });
|
|
271
|
+
} catch {
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
return this.fffModule.FileFinder.create({
|
|
275
|
+
...commonOptions,
|
|
276
|
+
frecencyDbPath: join(isolatedDir, "frecency.mdb"),
|
|
277
|
+
historyDbPath: join(isolatedDir, "history.mdb"),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
destroy(): void {
|
|
282
|
+
this.disposeFinder();
|
|
283
|
+
this.finderPromise = null;
|
|
284
|
+
this.finderPromiseConfig = null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private disposeFinder(): void {
|
|
288
|
+
if (this.finder && !this.finder.isDestroyed) this.finder.destroy();
|
|
289
|
+
this.finder = null;
|
|
290
|
+
this.finderConfig = null;
|
|
291
|
+
this.partialIndex = false;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
getFinder(): import("@ff-labs/fff-node").FileFinder | null {
|
|
295
|
+
return this.finder;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
getCursorStore(): CursorStore {
|
|
299
|
+
return this.cursorStore;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function finderConfig(cwd: string, options: FffInitOptions): FinderConfig {
|
|
304
|
+
return {
|
|
305
|
+
basePath: canonicalPath(cwd),
|
|
306
|
+
enableHomeDirScanning: options.enableHomeDirScanning,
|
|
307
|
+
enableFsRootScanning: options.enableFsRootScanning,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function sameFinderConfig(left: FinderConfig | null, right: FinderConfig): boolean {
|
|
312
|
+
return (
|
|
313
|
+
left?.basePath === right.basePath &&
|
|
314
|
+
left.enableHomeDirScanning === right.enableHomeDirScanning &&
|
|
315
|
+
left.enableFsRootScanning === right.enableFsRootScanning
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function isAlreadyOpenError(error: string): boolean {
|
|
320
|
+
return /environment already open/i.test(error);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function isRestrictedBasePathMessage(error: string): boolean {
|
|
324
|
+
return /certain FFF features in a file system root or home directories/i.test(error);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function restrictedScanScope(cwd: string): FffRestrictedScanScope | undefined {
|
|
328
|
+
const basePath = canonicalPath(cwd);
|
|
329
|
+
if (basePath === parse(basePath).root) return "root";
|
|
330
|
+
if (basePath === canonicalPath(homedir())) return "home";
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function canonicalPath(path: string): string {
|
|
335
|
+
try {
|
|
336
|
+
return realpathSync.native(path);
|
|
337
|
+
} catch {
|
|
338
|
+
return resolve(path);
|
|
339
|
+
}
|
|
340
|
+
}
|
package/src/find-glob.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Glob normalization for FFF glob() (repo-relative paths).
|
|
3
|
+
* Aligns with Pi SDK find: bare *.ts searches under cwd recursively.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Do not collapse double-star-slash-star to single star. */
|
|
7
|
+
export function normalizeFindGlobPattern(pattern: string): string {
|
|
8
|
+
const p = pattern.trim();
|
|
9
|
+
if (!p) return p;
|
|
10
|
+
if (p === "**/*") return "**/*";
|
|
11
|
+
if (p.includes("/") || p.startsWith("**/")) return p;
|
|
12
|
+
if (/[*?[\]]/.test(p)) return `**/${p}`;
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Patterns where an empty FFF result is suspicious — try SDK find (fd). */
|
|
17
|
+
export function isLikelyGlobPattern(pattern: string): boolean {
|
|
18
|
+
const p = pattern.trim();
|
|
19
|
+
if (!p) return false;
|
|
20
|
+
return /[*?[\]]/.test(p) || p === "**/*" || p.startsWith("**/");
|
|
21
|
+
}
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-pretty: utility helpers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { relative } from "node:path";
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// String / normalization
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
export function normalizeLineEndings(text: string): string {
|
|
12
|
+
return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function shortPath(cwd: string, home: string, p: string): string {
|
|
16
|
+
if (!p) return "";
|
|
17
|
+
const r = relative(cwd, p);
|
|
18
|
+
if (!r.startsWith("..") && !r.startsWith("/")) return r;
|
|
19
|
+
return p.replace(home, "~");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function stripBashExitStatusLine(text: string): string {
|
|
23
|
+
return normalizeLineEndings(text)
|
|
24
|
+
.split("\n")
|
|
25
|
+
.filter((line) => !/^Command exited with code \d+$/i.test(line.trim()))
|
|
26
|
+
.join("\n");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Tool metrics
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
export function formatElapsedMs(ms: number | undefined): string {
|
|
34
|
+
if (typeof ms !== "number" || !Number.isFinite(ms)) return "";
|
|
35
|
+
if (ms < 1000) return `${Math.round(ms)}ms`;
|
|
36
|
+
const s = ms / 1000;
|
|
37
|
+
return s < 10 ? `${s.toFixed(1)}s` : `${Math.round(s)}s`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function formatCharCount(chars: number | undefined): string {
|
|
41
|
+
if (typeof chars !== "number" || !Number.isFinite(chars) || chars <= 0) return "";
|
|
42
|
+
if (chars < 1000) return `${chars} chars`;
|
|
43
|
+
if (chars < 10_000) return `${(chars / 1000).toFixed(1)}k chars`;
|
|
44
|
+
return `${Math.round(chars / 1000)}k chars`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const ELAPSED_KEY = "__prettyElapsedMs";
|
|
48
|
+
export const CHARS_KEY = "__prettyOutputChars";
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Infer bash exit code
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
export function inferBashExitCode(text: string, fallback: number | null): number | null {
|
|
55
|
+
const exitMatch = text.match(/(?:exit code|exited with(?: code)?|exit status)[:\s]*(\d+)/i);
|
|
56
|
+
if (exitMatch) return Number(exitMatch[1]);
|
|
57
|
+
if (text.includes("command not found") || text.includes("No such file")) return 1;
|
|
58
|
+
return fallback;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// Compact error lines
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
export function compactErrorLines(error: string): string[] {
|
|
66
|
+
const compactedLines: string[] = [];
|
|
67
|
+
let previousBlank = false;
|
|
68
|
+
for (const line of normalizeLineEndings(error).trim().split("\n")) {
|
|
69
|
+
const isBlank = line.trim() === "";
|
|
70
|
+
if (isBlank && previousBlank) continue;
|
|
71
|
+
compactedLines.push(line);
|
|
72
|
+
previousBlank = isBlank;
|
|
73
|
+
}
|
|
74
|
+
return compactedLines;
|
|
75
|
+
}
|