summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/components/SuggestionsDisplay.tsx
diff options
context:
space:
mode:
authorAllen Hutchison <[email protected]>2025-04-29 08:29:09 -0700
committerGitHub <[email protected]>2025-04-29 08:29:09 -0700
commite0de69f3846ecf9de40bb2e02546235d9db53393 (patch)
treec3c190b6ae0e555ebd01c27cf1d9a860fe2b61dc /packages/cli/src/ui/components/SuggestionsDisplay.tsx
parentdf44ffbcffaebbb508147f038d8f21f78ee88e33 (diff)
First four independent files for @ commands. (#205)
Diffstat (limited to 'packages/cli/src/ui/components/SuggestionsDisplay.tsx')
-rw-r--r--packages/cli/src/ui/components/SuggestionsDisplay.tsx77
1 files changed, 77 insertions, 0 deletions
diff --git a/packages/cli/src/ui/components/SuggestionsDisplay.tsx b/packages/cli/src/ui/components/SuggestionsDisplay.tsx
new file mode 100644
index 00000000..a9d24003
--- /dev/null
+++ b/packages/cli/src/ui/components/SuggestionsDisplay.tsx
@@ -0,0 +1,77 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import React from 'react';
+import { Box, Text } from 'ink';
+
+interface SuggestionsDisplayProps {
+ suggestions: string[];
+ activeIndex: number;
+ isLoading: boolean;
+ width: number;
+ scrollOffset: number;
+}
+
+export const MAX_SUGGESTIONS_TO_SHOW = 8;
+
+export function SuggestionsDisplay({
+ suggestions,
+ activeIndex,
+ isLoading,
+ width,
+ scrollOffset,
+}: SuggestionsDisplayProps) {
+ if (isLoading) {
+ return (
+ <Box borderStyle="round" paddingX={1} width={width}>
+ <Text color="gray">Loading suggestions...</Text>
+ </Box>
+ );
+ }
+
+ if (suggestions.length === 0) {
+ return null; // Don't render anything if there are no suggestions
+ }
+
+ // Calculate the visible slice based on scrollOffset
+ const startIndex = scrollOffset;
+ const endIndex = Math.min(
+ scrollOffset + MAX_SUGGESTIONS_TO_SHOW,
+ suggestions.length,
+ );
+ const visibleSuggestions = suggestions.slice(startIndex, endIndex);
+
+ return (
+ <Box
+ borderStyle="round"
+ flexDirection="column"
+ paddingX={1}
+ width={width} // Use the passed width
+ >
+ {scrollOffset > 0 && <Text color="gray">▲</Text>}
+
+ {visibleSuggestions.map((suggestion, index) => {
+ const originalIndex = startIndex + index;
+ const isActive = originalIndex === activeIndex;
+ return (
+ <Text
+ key={`${suggestion}-${originalIndex}`}
+ color={isActive ? 'black' : 'white'}
+ backgroundColor={isActive ? 'blue' : undefined}
+ >
+ {suggestion}
+ </Text>
+ );
+ })}
+ {endIndex < suggestions.length && <Text color="gray">▼</Text>}
+ {suggestions.length > MAX_SUGGESTIONS_TO_SHOW && (
+ <Text color="gray">
+ ({activeIndex + 1}/{suggestions.length})
+ </Text>
+ )}
+ </Box>
+ );
+}