File size: 20,503 Bytes
52efc7b | 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useMemo, useState, useCallback } from 'react';
import { Box, Text } from 'ink';
import chalk from 'chalk';
import { theme } from '../../semantic-colors.js';
import type { LoadableSettingScope } from '../../../config/settings.js';
import type {
SettingsType,
SettingsValue,
} from '../../../config/settingsSchema.js';
import { getScopeItems } from '../../../utils/dialogScopeUtils.js';
import { RadioButtonSelect } from './RadioButtonSelect.js';
import { TextInput } from './TextInput.js';
import type { TextBuffer } from './text-buffer.js';
import { cpSlice, cpLen, cpIndexToOffset } from '../../utils/textUtils.js';
import { useKeypress, type Key } from '../../hooks/useKeypress.js';
import { Command, type KeyMatchers } from '../../key/keyMatchers.js';
import { useSettingsNavigation } from '../../hooks/useSettingsNavigation.js';
import { useInlineEditBuffer } from '../../hooks/useInlineEditBuffer.js';
import { formatCommand } from '../../key/keybindingUtils.js';
import { useKeyMatchers } from '../../hooks/useKeyMatchers.js';
/**
* Represents a single item in the settings dialog.
*/
export interface SettingsDialogItem {
/** Unique identifier for the item */
key: string;
/** Display label */
label: string;
/** Optional description below label */
description?: string;
/** Item type for determining interaction behavior */
type: SettingsType;
/** Pre-formatted display value (with * if modified) */
displayValue: string;
/** Grey out value (at default) */
isGreyedOut?: boolean;
/** Scope message e.g., "(Modified in Workspace)" */
scopeMessage?: string;
/** Raw value for edit mode initialization */
rawValue?: SettingsValue;
/** Optional pre-formatted edit buffer value for complex types */
editValue?: string;
}
/**
* Props for BaseSettingsDialog component.
*/
export interface BaseSettingsDialogProps {
// Header
/** Dialog title displayed at the top */
title: string;
/** Optional border color for the dialog */
borderColor?: string;
// Search (optional feature)
/** Whether to show the search input. Default: true */
searchEnabled?: boolean;
/** Placeholder text for search input. Default: "Search to filter" */
searchPlaceholder?: string;
/** Text buffer for search input */
searchBuffer?: TextBuffer;
// Items - parent provides the list
/** List of items to display */
items: SettingsDialogItem[];
// Scope selector
/** Whether to show the scope selector. Default: true */
showScopeSelector?: boolean;
/** Editable scope items to display. Defaults to all loadable scopes. */
scopeItems?: Array<{
label: string;
value: LoadableSettingScope;
}>;
/** Currently selected scope */
selectedScope: LoadableSettingScope;
/** Callback when scope changes */
onScopeChange?: (scope: LoadableSettingScope) => void;
// Layout
/** Maximum number of items to show at once */
maxItemsToShow: number;
/** Maximum label width for alignment */
maxLabelWidth?: number;
// Action callbacks
/** Called when a boolean/enum item is toggled */
onItemToggle: (key: string, item: SettingsDialogItem) => void;
/** Called when edit mode is committed with new value */
onEditCommit: (
key: string,
newValue: string,
item: SettingsDialogItem,
) => void;
/** Called when Ctrl+C is pressed to clear/reset an item */
onItemClear: (key: string, item: SettingsDialogItem) => void;
/** Called when dialog should close */
onClose: () => void;
/** Optional custom key handler for parent-specific keys. Return true if handled. */
onKeyPress?: (
key: Key,
currentItem: SettingsDialogItem | undefined,
) => boolean;
/** Optional override for key matchers used for navigation. */
keyMatchers?: KeyMatchers;
/** Available terminal height for dynamic windowing */
availableHeight?: number;
/** Optional footer configuration */
footer?: {
content: React.ReactNode;
height: number;
};
}
/**
* A base settings dialog component that handles rendering, layout, and keyboard navigation.
* Parent components handle business logic (saving, filtering, etc.) via callbacks.
*/
export function BaseSettingsDialog({
title,
borderColor,
searchEnabled = true,
searchPlaceholder = 'Search to filter',
searchBuffer,
items,
showScopeSelector = true,
scopeItems: providedScopeItems,
selectedScope,
onScopeChange,
maxItemsToShow,
maxLabelWidth,
onItemToggle,
onEditCommit,
onItemClear,
onClose,
onKeyPress,
keyMatchers: customKeyMatchers,
availableHeight,
footer,
}: BaseSettingsDialogProps): React.JSX.Element {
const globalKeyMatchers = useKeyMatchers();
const keyMatchers = customKeyMatchers ?? globalKeyMatchers;
const scopeItems = useMemo(
() =>
(providedScopeItems ?? getScopeItems()).map((item) => ({
...item,
key: item.value,
})),
[providedScopeItems],
);
const showAvailableScopes = showScopeSelector && scopeItems.length > 1;
// Calculate effective max items and scope visibility based on terminal height
const { effectiveMaxItemsToShow, finalShowScopeSelector } = useMemo(() => {
const initialShowScope = showAvailableScopes;
const initialMaxItems = maxItemsToShow;
if (!availableHeight) {
return {
effectiveMaxItemsToShow: initialMaxItems,
finalShowScopeSelector: initialShowScope,
};
}
// Layout constants based on BaseSettingsDialog structure:
const DIALOG_PADDING = 4;
const SETTINGS_TITLE_HEIGHT = 1;
// Account for the unconditional spacer below search/title section
const SEARCH_SECTION_HEIGHT = searchEnabled ? 5 : 1;
const SCROLL_ARROWS_HEIGHT = 2;
const ITEMS_SPACING_AFTER = 1;
const SCOPE_SECTION_HEIGHT = 5;
const HELP_TEXT_HEIGHT = 1;
const FOOTER_CONTENT_HEIGHT = footer?.height ?? 0;
const ITEM_HEIGHT = 3;
const currentAvailableHeight = availableHeight - DIALOG_PADDING;
const baseFixedHeight =
SETTINGS_TITLE_HEIGHT +
SEARCH_SECTION_HEIGHT +
SCROLL_ARROWS_HEIGHT +
ITEMS_SPACING_AFTER +
HELP_TEXT_HEIGHT +
FOOTER_CONTENT_HEIGHT;
// Calculate max items with scope selector
const heightWithScope = baseFixedHeight + SCOPE_SECTION_HEIGHT;
const availableForItemsWithScope = currentAvailableHeight - heightWithScope;
const maxItemsWithScope = Math.max(
1,
Math.floor(availableForItemsWithScope / ITEM_HEIGHT),
);
// Calculate max items without scope selector
const availableForItemsWithoutScope =
currentAvailableHeight - baseFixedHeight;
const maxItemsWithoutScope = Math.max(
1,
Math.floor(availableForItemsWithoutScope / ITEM_HEIGHT),
);
// In small terminals, hide scope selector if it would allow more items to show
let shouldShowScope = initialShowScope;
let maxItems = initialShowScope ? maxItemsWithScope : maxItemsWithoutScope;
if (initialShowScope && availableHeight < 25) {
// Hide scope selector if it gains us more than 1 extra item
if (maxItemsWithoutScope > maxItemsWithScope + 1) {
shouldShowScope = false;
maxItems = maxItemsWithoutScope;
}
}
return {
effectiveMaxItemsToShow: Math.min(maxItems, items.length),
finalShowScopeSelector: shouldShowScope,
};
}, [
availableHeight,
maxItemsToShow,
items.length,
searchEnabled,
showAvailableScopes,
footer,
]);
// Internal state
const { activeIndex, windowStart, moveUp, moveDown } = useSettingsNavigation({
items,
maxItemsToShow: effectiveMaxItemsToShow,
});
const { editState, editDispatch, startEditing, commitEdit, cursorVisible } =
useInlineEditBuffer({
onCommit: (key, value) => {
const itemToCommit = items.find((i) => i.key === key);
if (itemToCommit) {
onEditCommit(key, value, itemToCommit);
}
},
});
const {
editingKey,
buffer: editBuffer,
cursorPos: editCursorPos,
} = editState;
const [focusSection, setFocusSection] = useState<'settings' | 'scope'>(
'settings',
);
const effectiveFocusSection =
!finalShowScopeSelector && focusSection === 'scope'
? 'settings'
: focusSection;
// Calculate visible items based on scroll offset
const visibleItems = items.slice(
windowStart,
windowStart + effectiveMaxItemsToShow,
);
// Show scroll indicators if there are more items than can be displayed
const showScrollUp = items.length > effectiveMaxItemsToShow;
const showScrollDown = items.length > effectiveMaxItemsToShow;
// Get current item
const currentItem = items[activeIndex];
// Handle scope changes (for RadioButtonSelect)
const handleScopeChange = useCallback(
(scope: LoadableSettingScope) => {
onScopeChange?.(scope);
},
[onScopeChange],
);
// Keyboard handling
useKeypress(
(key: Key) => {
// Let parent handle custom keys first (only if not editing)
if (!editingKey && onKeyPress?.(key, currentItem)) {
return;
}
// Edit mode handling
if (editingKey) {
const item = items.find((i) => i.key === editingKey);
const type = item?.type ?? 'string';
// Navigation within edit buffer
if (keyMatchers[Command.MOVE_LEFT](key)) {
editDispatch({ type: 'MOVE_LEFT' });
return;
}
if (keyMatchers[Command.MOVE_RIGHT](key)) {
editDispatch({ type: 'MOVE_RIGHT' });
return;
}
if (keyMatchers[Command.HOME](key)) {
editDispatch({ type: 'HOME' });
return;
}
if (keyMatchers[Command.END](key)) {
editDispatch({ type: 'END' });
return;
}
// Backspace
if (keyMatchers[Command.DELETE_CHAR_LEFT](key)) {
editDispatch({ type: 'DELETE_LEFT' });
return;
}
// Delete
if (keyMatchers[Command.DELETE_CHAR_RIGHT](key)) {
editDispatch({ type: 'DELETE_RIGHT' });
return;
}
// Escape in edit mode - commit (consistent with SettingsDialog)
if (keyMatchers[Command.ESCAPE](key)) {
commitEdit();
return;
}
// Enter in edit mode - commit
if (keyMatchers[Command.RETURN](key)) {
commitEdit();
return;
}
// Up/Down in edit mode - commit and navigate.
// Only trigger on non-insertable keys (arrow keys) so that typing
// j/k characters into the edit buffer is not intercepted.
if (keyMatchers[Command.DIALOG_NAVIGATION_UP](key) && !key.insertable) {
commitEdit();
moveUp();
return;
}
if (
keyMatchers[Command.DIALOG_NAVIGATION_DOWN](key) &&
!key.insertable
) {
commitEdit();
moveDown();
return;
}
// Character input
if (key.sequence) {
editDispatch({
type: 'INSERT_CHAR',
char: key.sequence,
isNumberType: type === 'number',
});
}
return;
}
// Not in edit mode - handle navigation and actions
if (effectiveFocusSection === 'settings') {
// Up/Down navigation with wrap-around
if (keyMatchers[Command.DIALOG_NAVIGATION_UP](key)) {
moveUp();
return true;
}
if (keyMatchers[Command.DIALOG_NAVIGATION_DOWN](key)) {
moveDown();
return true;
}
// Enter - toggle or start edit
if (keyMatchers[Command.RETURN](key) && currentItem) {
if (currentItem.type === 'boolean' || currentItem.type === 'enum') {
onItemToggle(currentItem.key, currentItem);
} else {
// Start editing for string/number/array/object
const rawVal = currentItem.rawValue;
const initialValue =
currentItem.editValue ??
(rawVal !== undefined ? String(rawVal) : '');
startEditing(currentItem.key, initialValue);
}
return true;
}
// Ctrl+L - clear/reset to default (using only Ctrl+L to avoid Ctrl+C exit conflict)
if (keyMatchers[Command.CLEAR_SCREEN](key) && currentItem) {
onItemClear(currentItem.key, currentItem);
return true;
}
// Number keys for quick edit on number fields
if (currentItem?.type === 'number' && /^[0-9]$/.test(key.sequence)) {
startEditing(currentItem.key, key.sequence);
return true;
}
}
// Tab - switch focus section
if (key.name === 'tab' && finalShowScopeSelector) {
setFocusSection((s) => (s === 'settings' ? 'scope' : 'settings'));
return;
}
// Escape - close dialog
if (keyMatchers[Command.ESCAPE](key)) {
onClose();
return;
}
return;
},
{
isActive: true,
priority: effectiveFocusSection === 'settings',
},
);
return (
<Box
borderStyle="round"
borderColor={borderColor ?? theme.border.default}
flexDirection="row"
padding={1}
width="100%"
maxHeight={availableHeight}
>
<Box flexDirection="column" flexGrow={1}>
{/* Title */}
<Box marginX={1}>
<Text
bold={effectiveFocusSection === 'settings' && !editingKey}
wrap="truncate"
>
{effectiveFocusSection === 'settings' ? '> ' : ' '}
{title}{' '}
</Text>
</Box>
{/* Search input (if enabled) */}
{searchEnabled && searchBuffer && (
<Box
borderStyle="round"
borderColor={
editingKey
? theme.border.default
: effectiveFocusSection === 'settings'
? theme.ui.focus
: theme.border.default
}
paddingX={1}
height={3}
marginTop={1}
>
<TextInput
focus={effectiveFocusSection === 'settings' && !editingKey}
buffer={searchBuffer}
placeholder={searchPlaceholder}
/>
</Box>
)}
<Box height={1} />
{/* Items list */}
{visibleItems.length === 0 ? (
<Box marginX={1} height={1} flexDirection="column">
<Text color={theme.text.secondary}>No matches found.</Text>
</Box>
) : (
<>
{showScrollUp && (
<Box marginX={1}>
<Text color={theme.text.secondary}>▲</Text>
</Box>
)}
{visibleItems.map((item, idx) => {
const globalIndex = idx + windowStart;
const isActive =
effectiveFocusSection === 'settings' &&
activeIndex === globalIndex;
// Compute display value with edit mode cursor
let displayValue: string;
if (editingKey === item.key) {
// Show edit buffer with cursor highlighting
if (cursorVisible && editCursorPos < cpLen(editBuffer)) {
// Cursor is in the middle or at start of text
const beforeCursor = cpSlice(editBuffer, 0, editCursorPos);
const atCursor = cpSlice(
editBuffer,
editCursorPos,
editCursorPos + 1,
);
const afterCursor = cpSlice(editBuffer, editCursorPos + 1);
displayValue =
beforeCursor + chalk.inverse(atCursor) + afterCursor;
} else if (editCursorPos >= cpLen(editBuffer)) {
// Cursor is at the end - show inverted space
displayValue =
editBuffer + (cursorVisible ? chalk.inverse(' ') : ' ');
} else {
// Cursor not visible
displayValue = editBuffer;
}
} else {
displayValue = item.displayValue;
}
return (
<React.Fragment key={item.key}>
<Box
marginX={1}
flexDirection="row"
alignItems="flex-start"
backgroundColor={
isActive ? theme.background.focus : undefined
}
>
<Box minWidth={2} flexShrink={0}>
<Text
color={isActive ? theme.ui.focus : theme.text.secondary}
>
{isActive ? '●' : ''}
</Text>
</Box>
<Box
flexDirection="row"
flexGrow={1}
minWidth={0}
alignItems="flex-start"
>
<Box
flexDirection="column"
width={maxLabelWidth}
minWidth={0}
>
<Text
color={isActive ? theme.ui.focus : theme.text.primary}
>
{item.label}
{item.scopeMessage && (
<Text color={theme.text.secondary}>
{' '}
{item.scopeMessage}
</Text>
)}
</Text>
<Text color={theme.text.secondary} wrap="truncate">
{item.description ?? ''}
</Text>
</Box>
<Box minWidth={3} />
<Box flexShrink={0}>
<Text
color={
isActive
? theme.ui.focus
: item.isGreyedOut
? theme.text.secondary
: theme.text.primary
}
terminalCursorFocus={
editingKey === item.key && cursorVisible
}
terminalCursorPosition={cpIndexToOffset(
editBuffer,
editCursorPos,
)}
>
{displayValue}
</Text>
</Box>
</Box>
</Box>
<Box height={1} />
</React.Fragment>
);
})}
{showScrollDown && (
<Box marginX={1}>
<Text color={theme.text.secondary}>▼</Text>
</Box>
)}
</>
)}
<Box height={1} />
{/* Scope Selection */}
{finalShowScopeSelector && (
<Box marginX={1} flexDirection="column">
<Text bold={effectiveFocusSection === 'scope'} wrap="truncate">
{effectiveFocusSection === 'scope' ? '> ' : ' '}Apply To
</Text>
<RadioButtonSelect
items={scopeItems}
initialIndex={scopeItems.findIndex(
(item) => item.value === selectedScope,
)}
onSelect={handleScopeChange}
onHighlight={handleScopeChange}
isFocused={effectiveFocusSection === 'scope'}
showNumbers={effectiveFocusSection === 'scope'}
priority={effectiveFocusSection === 'scope'}
/>
</Box>
)}
<Box height={1} />
{/* Help text */}
<Box marginX={1}>
<Text color={theme.text.secondary}>
(Use Enter to select, {formatCommand(Command.CLEAR_SCREEN)} to reset
{finalShowScopeSelector ? ', Tab to change focus' : ''}, Esc to
close)
</Text>
</Box>
{/* Footer content (e.g., restart prompt) */}
{footer && <Box marginX={1}>{footer.content}</Box>}
</Box>
</Box>
);
}
|