Skip to content

Commit b36d76d

Browse files
committed
Enhance file search functionality: limit visible files and handle file reading errors
1 parent 24a9d9e commit b36d76d

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/ui/FileSearch.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ import { Box, Text } from "ink";
33

44
type FileSearchProps = {
55
query: string;
6-
files: string[];
6+
visibleFiles: string[];
7+
totalFiles: number;
78
selectedIndex: number;
89
};
910

10-
export function FileSearch({ query, files, selectedIndex }: FileSearchProps) {
11+
export function FileSearch({ query, visibleFiles, totalFiles, selectedIndex }: FileSearchProps) {
12+
const hiddenFiles = totalFiles - visibleFiles.length;
1113
return (
1214
<Box flexDirection="column" borderStyle="round" borderColor="yellow">
1315
<Text>Searching for: {query}</Text>
14-
{files.map((file, index) => (
16+
{visibleFiles.map((file, index) => (
1517
<Text key={file} color={selectedIndex === index ? "blue" : "white"}>
1618
{file}
1719
</Text>
1820
))}
21+
{hiddenFiles > 0 && <Text>...and {hiddenFiles} more</Text>}
1922
</Box>
2023
);
2124
}

src/ui/UserInput.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import { useShallow } from "zustand/react/shallow";
77
import { FileSearch } from "./FileSearch.js";
88
import { runTool } from "@/agent/tools/index.js";
99

10+
const MAX_VISIBLE_FILES = 5;
11+
1012
export function UserInput() {
1113
const [useStateInput, setInputValue] = useState("");
1214
const [searchActive, setSearchActive] = useState(false);
1315
const [searchQuery, setSearchQuery] = useState("");
1416
const [searchResults, setSearchResults] = useState<string[]>([]);
1517
const [selectedIndex, setSelectedIndex] = useState(0);
18+
const [visibleFileCount, setVisibleFileCount] = useState(MAX_VISIBLE_FILES);
1619

1720
const {
1821
startAgent,
@@ -60,6 +63,7 @@ export function UserInput() {
6063
setSearchActive(true);
6164
setSearchQuery(query);
6265
setSelectedIndex(0);
66+
setVisibleFileCount(MAX_VISIBLE_FILES);
6367
} else {
6468
setSearchActive(false);
6569
}
@@ -90,7 +94,13 @@ export function UserInput() {
9094
if (key.upArrow) {
9195
setSelectedIndex((prev) => (prev > 0 ? prev - 1 : prev));
9296
} else if (key.downArrow) {
93-
setSelectedIndex((prev) => (prev < searchResults.length - 1 ? prev + 1 : prev));
97+
setSelectedIndex((prev) => {
98+
const newIndex = prev + 1;
99+
if (newIndex >= visibleFileCount && newIndex < searchResults.length) {
100+
setVisibleFileCount((prevCount) => prevCount + 1);
101+
}
102+
return newIndex < searchResults.length ? newIndex : prev;
103+
});
94104
}
95105
} else if (!isThinking) {
96106
if (key.upArrow) {
@@ -107,7 +117,7 @@ export function UserInput() {
107117
}
108118
});
109119

110-
const handleSubmit = () => {
120+
const handleSubmit = async () => {
111121
const value = useStateInput.trim();
112122

113123
if (value === "/help") {
@@ -128,10 +138,21 @@ export function UserInput() {
128138
const lastAtIndex = useStateInput.lastIndexOf("@");
129139
const file = searchResults[selectedIndex];
130140
if (file) {
131-
const newValue = `${useStateInput.slice(0, lastAtIndex)}@${file} `;
132-
setInputValue(newValue);
133-
setSearchActive(false);
134-
setSearchResults([]);
141+
try {
142+
const content = await runTool(
143+
{ toolName: "readFile", args: { path: file } },
144+
config!,
145+
);
146+
const fileBlock = `File: ${file}\n\n${content}`;
147+
const newValue = `${useStateInput.slice(0, lastAtIndex)}${fileBlock}`;
148+
setInputValue(newValue);
149+
} catch (error) {
150+
const newValue = `${useStateInput.slice(0, lastAtIndex)}@${file} `;
151+
setInputValue(newValue);
152+
} finally {
153+
setSearchActive(false);
154+
setSearchResults([]);
155+
}
135156
}
136157
return;
137158
}
@@ -178,12 +199,15 @@ export function UserInput() {
178199
}
179200
};
180201

202+
const visibleFiles = searchResults.slice(0, visibleFileCount);
203+
181204
return (
182205
<Box flexDirection="column">
183206
{searchActive && searchResults.length > 0 && (
184207
<FileSearch
185208
query={searchQuery}
186-
files={searchResults}
209+
visibleFiles={visibleFiles}
210+
totalFiles={searchResults.length}
187211
selectedIndex={selectedIndex}
188212
/>
189213
)}

0 commit comments

Comments
 (0)