forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathInput.tsx
More file actions
341 lines (305 loc) · 10.2 KB
/
Copy pathPathInput.tsx
File metadata and controls
341 lines (305 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import type { PathType } from '../../../schema';
import { Cursor } from './Cursor';
import { Box, Text, useInput } from 'ink';
import { existsSync, readdirSync, statSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { useState } from 'react';
interface PathInputProps {
onSubmit: (value: string) => void;
onCancel: () => void;
placeholder?: string;
initialValue?: string;
basePath?: string;
/** Path type: 'file' shows all entries, 'directory' shows only directories */
pathType?: PathType;
/** Maximum number of dropdown items visible before scrolling (default: 8) */
maxVisibleItems?: number;
/** Allow the final path segment to not exist (for create workflows). Parent directory must still exist. */
allowCreate?: boolean;
/** Show hidden files (dotfiles) in completions (default: false) */
showHidden?: boolean;
/** Allow empty input (user presses Enter without selecting a file) */
allowEmpty?: boolean;
/** Message shown when user submits empty input (only if allowEmpty is true) */
emptyHelpText?: string;
}
interface CompletionItem {
value: string;
name: string;
isDirectory: boolean;
}
function parsePath(input: string, basePath: string): { dir: string; prefix: string } {
if (!input || input === '') {
return { dir: resolve(basePath), prefix: '' };
}
if (input.endsWith('/')) {
return { dir: resolve(basePath, input), prefix: '' };
}
const dir = dirname(resolve(basePath, input));
const prefix = input.split('/').pop() ?? '';
return { dir, prefix };
}
function getCompletions(input: string, basePath: string, pathType: PathType, showHidden = false): CompletionItem[] {
try {
const { dir, prefix } = parsePath(input, basePath);
const entries = readdirSync(dir, { withFileTypes: true });
const items = entries
.filter(entry => {
if (!entry.name.toLowerCase().startsWith(prefix.toLowerCase())) return false;
if (!showHidden && entry.name.startsWith('.')) return false;
if (pathType === 'directory' && !entry.isDirectory()) return false;
return true;
})
.map(entry => {
const inputDir = input.endsWith('/') ? input : input.substring(0, input.lastIndexOf('/') + 1);
const suffix = entry.isDirectory() ? '/' : '';
return {
value: inputDir + entry.name + suffix,
name: entry.name + suffix,
isDirectory: entry.isDirectory(),
};
});
// Sort: directories first, then files, alphabetically within each group
return items.sort((a, b) => {
if (a.isDirectory && !b.isDirectory) return -1;
if (!a.isDirectory && b.isDirectory) return 1;
return a.name.localeCompare(b.name);
});
} catch {
return [];
}
}
/** Validate that a path exists and matches the expected type */
function validatePath(path: string, basePath: string, pathType: PathType): string | null {
const resolved = resolve(basePath, path);
if (!existsSync(resolved)) {
return `"${path}" is not a valid path`;
}
if (pathType === 'directory') {
try {
if (!statSync(resolved).isDirectory()) {
return `"${path}" is not a directory`;
}
} catch {
return `"${path}" is not a valid path`;
}
}
return null; // Valid
}
/** Validate path for create mode: parent directory must exist, final segment can be new */
function validatePathForCreate(path: string, basePath: string): string | null {
const resolved = resolve(basePath, path);
const parent = dirname(resolved);
// Check if path already exists - that's fine for create mode
if (existsSync(resolved)) {
try {
if (!statSync(resolved).isDirectory()) {
return `"${path}" exists but is not a directory`;
}
} catch {
return `"${path}" is not a valid path`;
}
return null; // Existing directory is valid
}
// Path doesn't exist - check that parent directory exists
if (!existsSync(parent)) {
return `Parent directory "${dirname(path)}" does not exist`;
}
try {
if (!statSync(parent).isDirectory()) {
return `"${dirname(path)}" is not a directory`;
}
} catch {
return `Parent directory "${dirname(path)}" is not valid`;
}
return null; // Valid - parent exists, final segment will be created
}
export function PathInput({
onSubmit,
onCancel,
placeholder,
initialValue = '',
basePath = process.cwd(),
pathType = 'file',
maxVisibleItems = 8,
allowCreate = false,
showHidden = false,
allowEmpty = false,
emptyHelpText,
}: PathInputProps) {
const [value, setValue] = useState(initialValue);
const [cursor, setCursor] = useState(initialValue.length);
const [selectedIndex, setSelectedIndex] = useState(0);
const [error, setError] = useState<string | null>(null);
// Get live completions based on current value
const matches = getCompletions(value, basePath, pathType, showHidden);
// Calculate viewport for scrolling
const totalItems = matches.length;
const clampedIndex = Math.min(selectedIndex, Math.max(0, totalItems - 1));
// Adjust viewport to keep selected item visible
const halfVisible = Math.floor(maxVisibleItems / 2);
let viewportStart = Math.max(0, clampedIndex - halfVisible);
const viewportEnd = Math.min(totalItems, viewportStart + maxVisibleItems);
// Adjust start if we're near the end
if (viewportEnd - viewportStart < maxVisibleItems) {
viewportStart = Math.max(0, viewportEnd - maxVisibleItems);
}
const visibleItems = matches.slice(viewportStart, viewportEnd);
// Helper to select the current highlighted item
const selectHighlightedItem = () => {
if (matches.length > 0 && matches[clampedIndex]) {
const selected = matches[clampedIndex];
setValue(selected.value);
setCursor(selected.value.length);
setSelectedIndex(0);
}
};
// Go back one directory level
const goBack = () => {
if (value.endsWith('/')) {
// Remove trailing slash and go up one level
const withoutSlash = value.slice(0, -1);
const lastSlash = withoutSlash.lastIndexOf('/');
if (lastSlash >= 0) {
setValue(withoutSlash.slice(0, lastSlash + 1));
setCursor(lastSlash + 1);
} else {
setValue('');
setCursor(0);
}
} else {
// Go to parent directory
const lastSlash = value.lastIndexOf('/');
if (lastSlash >= 0) {
setValue(value.slice(0, lastSlash + 1));
setCursor(lastSlash + 1);
} else {
setValue('');
setCursor(0);
}
}
setSelectedIndex(0);
};
useInput((input, key) => {
// Clear error on any input
if (error) setError(null);
// Esc: Cancel
if (key.escape) {
onCancel();
return;
}
// Enter: Validate and submit the current path
if (key.return) {
const trimmed = value.trim();
if (!trimmed) {
if (allowEmpty) {
onSubmit('');
return;
}
setError('Please enter a path');
return;
}
const validationError = allowCreate
? validatePathForCreate(trimmed, basePath)
: validatePath(trimmed, basePath, pathType);
if (validationError) {
setError(validationError);
return;
}
onSubmit(trimmed);
return;
}
// ↑↓: Move through dropdown
if (key.upArrow) {
if (matches.length > 0) {
setSelectedIndex(i => (i - 1 + matches.length) % matches.length);
}
return;
}
if (key.downArrow) {
if (matches.length > 0) {
setSelectedIndex(i => (i + 1) % matches.length);
}
return;
}
// →: Open (select highlighted item / drill into directory)
if (key.rightArrow) {
selectHighlightedItem();
return;
}
// ←: Back (go up one directory level)
if (key.leftArrow) {
goBack();
return;
}
// Backspace: Delete character or go back if at start
if (key.backspace || key.delete) {
if (cursor > 0) {
setValue(value.slice(0, cursor - 1) + value.slice(cursor));
setCursor(cursor - 1);
setSelectedIndex(0);
}
return;
}
// Tab: Same as right arrow (open/drill)
if (key.tab || input === '\t') {
selectHighlightedItem();
return;
}
// Regular character input at cursor position
if (input && !key.ctrl && !key.meta) {
setValue(value.slice(0, cursor) + input + value.slice(cursor));
setCursor(cursor + input.length);
setSelectedIndex(0);
}
});
const pathTypeLabel = pathType === 'directory' ? 'directory' : 'file';
const hasMatches = matches.length > 0;
return (
<Box flexDirection="column">
<Box>
<Text dimColor>Select a {pathTypeLabel}:</Text>
</Box>
<Box>
<Text color="cyan">> </Text>
<Text>{value}</Text>
<Cursor />
{!value && <Text dimColor>{placeholder}</Text>}
</Box>
{/* Error message */}
{error && (
<Box marginTop={1}>
<Text color="red">{error}</Text>
</Box>
)}
{/* Dropdown menu */}
{hasMatches && (
<Box flexDirection="column" marginTop={1} marginLeft={2}>
{/* Scroll indicator at top */}
{viewportStart > 0 && <Text dimColor> ↑ {viewportStart} more</Text>}
{visibleItems.map((item, idx) => {
const actualIndex = viewportStart + idx;
const isSelected = actualIndex === clampedIndex;
const icon = item.isDirectory ? '📁' : '📄';
return (
<Box key={item.value}>
<Text color={isSelected ? 'cyan' : undefined}>{isSelected ? '❯ ' : ' '}</Text>
<Text>{icon} </Text>
<Text color={isSelected ? 'cyan' : item.isDirectory ? 'blue' : undefined} bold={isSelected}>
{item.name}
</Text>
</Box>
);
})}
{/* Scroll indicator at bottom */}
{viewportEnd < totalItems && <Text dimColor> ↓ {totalItems - viewportEnd} more</Text>}
</Box>
)}
{/* Help text */}
<Box marginTop={1} flexDirection="column">
<Text dimColor>↑↓ move → open ← back Enter submit Esc cancel</Text>
{allowEmpty && emptyHelpText && !value && <Text dimColor>{emptyHelpText}</Text>}
</Box>
</Box>
);
}