2026-05-10 23:12:26 +02:00
|
|
|
|
/* @jsxImportSource react */
|
|
|
|
|
|
import { Box, Text, render as renderInkRuntime, useApp, useInput } from 'ink';
|
2026-05-13 13:33:28 +02:00
|
|
|
|
import { type ReactNode, useEffect, useMemo, useRef, useState } from 'react';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
import {
|
|
|
|
|
|
filterTree,
|
|
|
|
|
|
flattenSelection,
|
2026-05-14 14:35:58 +02:00
|
|
|
|
hasPartialChildren,
|
2026-05-10 23:12:26 +02:00
|
|
|
|
isAncestorChecked,
|
|
|
|
|
|
reducer,
|
|
|
|
|
|
visibleNodeIds,
|
|
|
|
|
|
type PickerCommand,
|
|
|
|
|
|
type PickerState,
|
2026-05-13 18:41:44 -04:00
|
|
|
|
} from './tree-picker-state.js';
|
2026-05-13 15:04:50 +02:00
|
|
|
|
import type { KtxCliIo } from './cli-runtime.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
|
|
const COLOR_THEME = {
|
|
|
|
|
|
text: 'white',
|
|
|
|
|
|
muted: 'gray',
|
|
|
|
|
|
active: 'cyan',
|
2026-05-13 17:28:48 -04:00
|
|
|
|
selected: 'green',
|
2026-05-10 23:12:26 +02:00
|
|
|
|
warning: 'yellow',
|
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
|
|
const NO_COLOR_THEME = {
|
|
|
|
|
|
text: 'white',
|
|
|
|
|
|
muted: 'white',
|
|
|
|
|
|
active: 'white',
|
2026-05-13 17:28:48 -04:00
|
|
|
|
selected: 'white',
|
2026-05-10 23:12:26 +02:00
|
|
|
|
warning: 'white',
|
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
type TreePickerTheme = Record<keyof typeof COLOR_THEME, string>;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const DEFAULT_TREE_PICKER_HELP_TEXT =
|
|
|
|
|
|
'Right Arrow to expand, Up/Down to move, Space to select or unselect, Slash to filter, Enter to confirm, Escape to go back, or Ctrl+C to exit.';
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_SKIP_EMPTY_MESSAGE =
|
|
|
|
|
|
'Nothing selected. Skip this step? Press Enter to skip or Escape to go back.';
|
|
|
|
|
|
|
|
|
|
|
|
export interface TreePickerTuiIo extends KtxCliIo {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
stdin?: { isTTY?: boolean; setRawMode?(value: boolean): void };
|
2026-05-10 23:51:24 +02:00
|
|
|
|
stdout: KtxCliIo['stdout'] & { isTTY?: boolean; columns?: number; rows?: number };
|
2026-05-10 23:12:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface InkKey {
|
|
|
|
|
|
leftArrow?: boolean;
|
|
|
|
|
|
rightArrow?: boolean;
|
|
|
|
|
|
upArrow?: boolean;
|
|
|
|
|
|
downArrow?: boolean;
|
|
|
|
|
|
return?: boolean;
|
|
|
|
|
|
escape?: boolean;
|
|
|
|
|
|
ctrl?: boolean;
|
|
|
|
|
|
backspace?: boolean;
|
|
|
|
|
|
delete?: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export type TreePickerResult = { kind: 'save'; selectedIds: string[] } | { kind: 'quit' };
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export interface TreePickerChrome {
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
helpText?: string;
|
|
|
|
|
|
subtitleLines?: readonly string[];
|
|
|
|
|
|
warningLines?: readonly string[];
|
|
|
|
|
|
confirmSaveMessage?: (state: PickerState) => string;
|
|
|
|
|
|
skipEmptyMessage?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface TreePickerRenderInput {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
initialState: PickerState;
|
2026-05-13 18:41:44 -04:00
|
|
|
|
chrome: TreePickerChrome;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
interface TreePickerAppProps extends TreePickerRenderInput {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
terminalRows?: number;
|
|
|
|
|
|
terminalWidth?: number;
|
|
|
|
|
|
env?: NodeJS.ProcessEnv;
|
2026-05-13 18:41:44 -04:00
|
|
|
|
onExit(result: TreePickerResult): void;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export interface TreePickerInkInstance {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
rerender(tree: ReactNode): void;
|
|
|
|
|
|
unmount(): void;
|
|
|
|
|
|
waitUntilExit(): Promise<void>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export interface TreePickerInkRenderOptions {
|
|
|
|
|
|
stdin?: TreePickerTuiIo['stdin'];
|
|
|
|
|
|
stdout: TreePickerTuiIo['stdout'];
|
|
|
|
|
|
stderr: TreePickerTuiIo['stderr'];
|
2026-05-10 23:12:26 +02:00
|
|
|
|
exitOnCtrlC: boolean;
|
|
|
|
|
|
patchConsole: boolean;
|
|
|
|
|
|
maxFps: number;
|
|
|
|
|
|
alternateScreen: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
function resolveTheme(env: NodeJS.ProcessEnv = process.env): TreePickerTheme {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
return env.NO_COLOR || env.TERM === 'dumb' ? NO_COLOR_THEME : COLOR_THEME;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export function resolveTreePickerWidth(columns: number | undefined): number {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const resolvedColumns = columns ?? 100;
|
|
|
|
|
|
return Math.max(60, Math.min(120, resolvedColumns - 4));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function rowMatchesSearch(state: PickerState, nodeId: string): boolean {
|
|
|
|
|
|
const query = state.search.query.trim().toLocaleLowerCase();
|
|
|
|
|
|
if (!query) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const node = state.byId.get(nodeId);
|
|
|
|
|
|
if (!node) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return node.title.toLocaleLowerCase().includes(query) || node.path.toLocaleLowerCase().includes(query);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export function sanitizeTreePickerTuiError(error: unknown): string {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
|
|
|
|
return message
|
|
|
|
|
|
.replace(/[a-z][a-z0-9+.-]*:\/\/[^\s]+/gi, '[redacted-url]')
|
|
|
|
|
|
.replace(/\b(api[_-]?key|password|token|secret)=\S+/gi, '[redacted]');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function windowOffset(count: number, selected: number, visible: number): number {
|
|
|
|
|
|
if (count <= visible) return 0;
|
|
|
|
|
|
return Math.max(0, Math.min(count - visible, selected - Math.floor(visible / 2)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function windowItems<T>(items: T[], selected: number, visible: number): { items: T[]; offset: number } {
|
|
|
|
|
|
const offset = windowOffset(items.length, selected, visible);
|
|
|
|
|
|
return { items: items.slice(offset, offset + visible), offset };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function truncateText(value: string, width: number): string {
|
|
|
|
|
|
if (value.length <= width) return value;
|
|
|
|
|
|
if (width <= 3) return value.slice(0, width);
|
|
|
|
|
|
return `${value.slice(0, width - 3)}...`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export function treePickerCommandForInkInput(
|
2026-05-10 23:12:26 +02:00
|
|
|
|
input: string,
|
|
|
|
|
|
key: InkKey,
|
|
|
|
|
|
search: PickerState['search'],
|
|
|
|
|
|
pendingConfirm: PickerState['pendingConfirm'],
|
|
|
|
|
|
): PickerCommand | null {
|
|
|
|
|
|
if (pendingConfirm) {
|
|
|
|
|
|
if (input === 'y' || key.return) return 'save-confirm';
|
|
|
|
|
|
if (input === 'n' || key.escape) return 'save-cancel';
|
|
|
|
|
|
if (key.ctrl === true && input === 'c') return 'quit';
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (search.editing) {
|
|
|
|
|
|
if (key.escape) return 'search-cancel';
|
|
|
|
|
|
if (key.return) return 'search-submit';
|
|
|
|
|
|
if (key.backspace || key.delete) return 'search-backspace';
|
|
|
|
|
|
if (key.downArrow) return 'cursor-down';
|
|
|
|
|
|
if (key.upArrow) return 'cursor-up';
|
2026-05-13 18:41:44 -04:00
|
|
|
|
if (input.length === 1 && input >= ' ' && input !== '') return { type: 'search-input', value: input };
|
2026-05-10 23:12:26 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (key.ctrl === true && input === 'c') return 'quit';
|
|
|
|
|
|
if (key.upArrow) return 'cursor-up';
|
|
|
|
|
|
if (key.downArrow) return 'cursor-down';
|
|
|
|
|
|
if (key.leftArrow) return 'cursor-left';
|
|
|
|
|
|
if (key.rightArrow) return 'cursor-right';
|
2026-05-13 17:28:48 -04:00
|
|
|
|
if (key.return) return 'save-request';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
if (input === ' ') return 'toggle-check';
|
|
|
|
|
|
if (input === '/') return 'search-start';
|
2026-05-14 14:35:58 +02:00
|
|
|
|
if (input === 'a') return 'toggle-select-all-visible';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
if (input === 'n') return 'select-none';
|
2026-05-13 17:28:48 -04:00
|
|
|
|
if (key.escape) return 'quit';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
function PickerRow(props: { state: PickerState; nodeId: string; width: number; theme: TreePickerTheme }): ReactNode {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const node = props.state.byId.get(props.nodeId);
|
|
|
|
|
|
if (!node) return null;
|
|
|
|
|
|
const focused = props.state.cursorId === node.id;
|
|
|
|
|
|
const locked = isAncestorChecked(node.id, props.state.checked, props.state.byId);
|
|
|
|
|
|
const checked = props.state.checked.has(node.id);
|
2026-05-13 17:28:48 -04:00
|
|
|
|
const isSelected = checked || locked;
|
2026-05-14 14:35:58 +02:00
|
|
|
|
const partial = !isSelected && hasPartialChildren(node.id, props.state.checked, props.state.byId);
|
|
|
|
|
|
const glyph = isSelected ? '◼' : partial ? '◧' : '◻';
|
|
|
|
|
|
const glyphColor = isSelected || partial ? props.theme.selected : props.theme.muted;
|
2026-05-13 17:28:48 -04:00
|
|
|
|
const childAffordance =
|
2026-05-10 23:12:26 +02:00
|
|
|
|
node.childIds.length > 0 ? (props.state.expanded.has(node.id) ? ' ▾' : ` ▸ (${node.childIds.length})`) : '';
|
2026-05-13 17:28:48 -04:00
|
|
|
|
const indent = ' '.repeat(node.depth * 2);
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const titleColor = focused ? props.theme.active : props.theme.text;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const inverse = rowMatchesSearch(props.state, node.id);
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const prefixWidth = indent.length + 2 + childAffordance.length;
|
|
|
|
|
|
const title = truncateText(node.title, Math.max(10, props.width - prefixWidth));
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
|
|
return (
|
2026-05-13 17:28:48 -04:00
|
|
|
|
<Text>
|
|
|
|
|
|
<Text color={glyphColor}>
|
|
|
|
|
|
{indent}
|
|
|
|
|
|
{glyph}
|
|
|
|
|
|
</Text>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
<Text color={titleColor} strikethrough={node.archived} bold={focused}>
|
2026-05-13 17:28:48 -04:00
|
|
|
|
{' '}
|
|
|
|
|
|
<Text inverse={inverse}>{title}</Text>
|
|
|
|
|
|
</Text>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
{childAffordance.length > 0 ? <Text color={props.theme.muted}>{childAffordance}</Text> : null}
|
2026-05-10 23:12:26 +02:00
|
|
|
|
</Text>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export function TreePickerApp(props: TreePickerAppProps): ReactNode {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const app = useApp();
|
|
|
|
|
|
const [state, setState] = useState(props.initialState);
|
|
|
|
|
|
const stateRef = useRef(state);
|
|
|
|
|
|
const theme = useMemo(() => resolveTheme(props.env), [props.env]);
|
|
|
|
|
|
const visibleIds = visibleNodeIds(state);
|
|
|
|
|
|
const selectedIndex = Math.max(0, visibleIds.indexOf(state.cursorId));
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const reservedRows = state.pendingConfirm === 'save-confirm' ? 10 : 9;
|
2026-05-13 17:28:48 -04:00
|
|
|
|
const visibleRows = Math.max(5, Math.min(12, (props.terminalRows ?? 24) - reservedRows));
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const rows = windowItems(visibleIds, selectedIndex, visibleRows);
|
|
|
|
|
|
const hiddenAbove = rows.offset;
|
|
|
|
|
|
const hiddenBelow = Math.max(0, visibleIds.length - rows.offset - rows.items.length);
|
|
|
|
|
|
const searchMatchCount = filterTree(state).visibleIds.size;
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const width = resolveTreePickerWidth(props.terminalWidth);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
const showSearch = state.search.editing || state.search.query.trim().length > 0;
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const helpText = props.chrome.helpText ?? DEFAULT_TREE_PICKER_HELP_TEXT;
|
|
|
|
|
|
const skipEmptyMessage = props.chrome.skipEmptyMessage ?? DEFAULT_SKIP_EMPTY_MESSAGE;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
|
|
stateRef.current = state;
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const hint = state.transientHint;
|
|
|
|
|
|
if (!hint) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const clearHint = () => {
|
|
|
|
|
|
setState((current) => {
|
|
|
|
|
|
const { next } = reducer(current, 'clear-transient-hint');
|
|
|
|
|
|
stateRef.current = next;
|
|
|
|
|
|
return next;
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
const delay = hint.expiresAt - Date.now();
|
|
|
|
|
|
if (delay <= 0) {
|
|
|
|
|
|
clearHint();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const timeout = setTimeout(clearHint, delay);
|
|
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timeout);
|
|
|
|
|
|
}, [state.transientHint?.expiresAt]);
|
|
|
|
|
|
|
|
|
|
|
|
useInput((input, key) => {
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const command = treePickerCommandForInkInput(input, key, stateRef.current.search, stateRef.current.pendingConfirm);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
if (!command) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const { next, effect } = reducer(stateRef.current, command);
|
|
|
|
|
|
stateRef.current = next;
|
|
|
|
|
|
setState(next);
|
|
|
|
|
|
if (effect === 'save') {
|
2026-05-13 18:41:44 -04:00
|
|
|
|
props.onExit({ kind: 'save', selectedIds: flattenSelection(next.checked, next.byId) });
|
2026-05-10 23:12:26 +02:00
|
|
|
|
app.exit();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (effect === 'quit-without-save') {
|
|
|
|
|
|
props.onExit({ kind: 'quit' });
|
|
|
|
|
|
app.exit();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Box flexDirection="column">
|
2026-05-13 17:28:48 -04:00
|
|
|
|
<Text>
|
|
|
|
|
|
<Text color={theme.active}>◆</Text>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
<Text bold> {props.chrome.title}</Text>
|
2026-05-13 17:28:48 -04:00
|
|
|
|
</Text>
|
|
|
|
|
|
<Box
|
|
|
|
|
|
flexDirection="column"
|
|
|
|
|
|
borderStyle="single"
|
|
|
|
|
|
borderTop={false}
|
|
|
|
|
|
borderRight={false}
|
|
|
|
|
|
borderBottom={false}
|
|
|
|
|
|
borderColor={theme.active}
|
|
|
|
|
|
paddingLeft={1}
|
|
|
|
|
|
>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
<Text color={theme.muted}>{helpText}</Text>
|
2026-05-13 17:28:48 -04:00
|
|
|
|
<Text> </Text>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
{(props.chrome.subtitleLines ?? []).map((line, idx) => (
|
|
|
|
|
|
<Text key={`subtitle-${idx}`} color={theme.muted}>
|
|
|
|
|
|
{line}
|
|
|
|
|
|
</Text>
|
|
|
|
|
|
))}
|
|
|
|
|
|
{(props.chrome.warningLines ?? []).map((line, idx) => (
|
|
|
|
|
|
<Text key={`chromewarn-${idx}`} color={theme.warning}>
|
|
|
|
|
|
{line}
|
|
|
|
|
|
</Text>
|
|
|
|
|
|
))}
|
2026-05-13 17:28:48 -04:00
|
|
|
|
{state.preLoadWarnings.map((warning) => (
|
|
|
|
|
|
<Text key={warning} color={theme.warning}>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
{warning}
|
2026-05-13 17:28:48 -04:00
|
|
|
|
</Text>
|
|
|
|
|
|
))}
|
|
|
|
|
|
{showSearch ? (
|
|
|
|
|
|
<Text>
|
|
|
|
|
|
<Text color={theme.muted}>/ </Text>
|
|
|
|
|
|
<Text>
|
|
|
|
|
|
{state.search.query}
|
|
|
|
|
|
{state.search.editing ? '█' : ''}
|
|
|
|
|
|
</Text>
|
|
|
|
|
|
<Text color={theme.muted}> ({searchMatchCount} matches)</Text>
|
|
|
|
|
|
</Text>
|
|
|
|
|
|
) : null}
|
2026-05-13 18:41:44 -04:00
|
|
|
|
<Text> </Text>
|
2026-05-10 23:12:26 +02:00
|
|
|
|
{hiddenAbove > 0 ? <Text color={theme.muted}>↑ {hiddenAbove} more</Text> : null}
|
|
|
|
|
|
{rows.items.map((nodeId) => (
|
|
|
|
|
|
<PickerRow key={nodeId} state={state} nodeId={nodeId} width={width} theme={theme} />
|
|
|
|
|
|
))}
|
|
|
|
|
|
{hiddenBelow > 0 ? <Text color={theme.muted}>↓ {hiddenBelow} more</Text> : null}
|
2026-05-13 18:41:44 -04:00
|
|
|
|
{state.pendingConfirm === 'save-confirm' ? (
|
2026-05-13 17:28:48 -04:00
|
|
|
|
<Text color={theme.warning}>
|
2026-05-13 18:41:44 -04:00
|
|
|
|
{props.chrome.confirmSaveMessage
|
|
|
|
|
|
? props.chrome.confirmSaveMessage(state)
|
|
|
|
|
|
: 'Confirm save? Press Enter to confirm or Escape to go back.'}
|
2026-05-13 17:28:48 -04:00
|
|
|
|
</Text>
|
|
|
|
|
|
) : null}
|
2026-05-13 18:41:44 -04:00
|
|
|
|
{state.pendingConfirm === 'skip-empty' ? <Text color={theme.warning}>{skipEmptyMessage}</Text> : null}
|
2026-05-13 17:28:48 -04:00
|
|
|
|
{state.transientHint ? <Text color={theme.warning}>{state.transientHint.text}</Text> : null}
|
2026-05-10 23:12:26 +02:00
|
|
|
|
</Box>
|
2026-05-13 17:28:48 -04:00
|
|
|
|
<Text color={theme.active}>└</Text>
|
2026-05-10 23:12:26 +02:00
|
|
|
|
</Box>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
function renderInk(tree: ReactNode, options: TreePickerInkRenderOptions): TreePickerInkInstance {
|
2026-05-10 23:12:26 +02:00
|
|
|
|
return renderInkRuntime(tree, {
|
|
|
|
|
|
stdin: options.stdin as NodeJS.ReadStream | undefined,
|
|
|
|
|
|
stdout: options.stdout as NodeJS.WriteStream,
|
|
|
|
|
|
stderr: options.stderr as NodeJS.WriteStream,
|
|
|
|
|
|
exitOnCtrlC: options.exitOnCtrlC,
|
|
|
|
|
|
patchConsole: options.patchConsole,
|
|
|
|
|
|
maxFps: options.maxFps,
|
|
|
|
|
|
alternateScreen: options.alternateScreen,
|
2026-05-13 18:41:44 -04:00
|
|
|
|
}) as TreePickerInkInstance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface RenderTreePickerOptions {
|
|
|
|
|
|
renderInk?: (tree: ReactNode, options: TreePickerInkRenderOptions) => TreePickerInkInstance;
|
|
|
|
|
|
scriptedModeHint?: string;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 18:41:44 -04:00
|
|
|
|
export async function renderTreePickerTui(
|
|
|
|
|
|
input: TreePickerRenderInput,
|
|
|
|
|
|
io: TreePickerTuiIo,
|
|
|
|
|
|
options: RenderTreePickerOptions = {},
|
|
|
|
|
|
): Promise<TreePickerResult> {
|
|
|
|
|
|
let result: TreePickerResult = { kind: 'quit' };
|
|
|
|
|
|
let instance: TreePickerInkInstance | null = null;
|
2026-05-10 23:12:26 +02:00
|
|
|
|
try {
|
|
|
|
|
|
instance = (options.renderInk ?? renderInk)(
|
2026-05-13 18:41:44 -04:00
|
|
|
|
<TreePickerApp
|
2026-05-10 23:12:26 +02:00
|
|
|
|
{...input}
|
|
|
|
|
|
terminalRows={(io.stdout as { rows?: number }).rows ?? process.stdout.rows ?? 24}
|
|
|
|
|
|
terminalWidth={io.stdout.columns ?? process.stdout.columns}
|
|
|
|
|
|
onExit={(next) => {
|
|
|
|
|
|
result = next;
|
|
|
|
|
|
instance?.unmount();
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>,
|
|
|
|
|
|
{
|
|
|
|
|
|
stdin: io.stdin,
|
|
|
|
|
|
stdout: io.stdout,
|
|
|
|
|
|
stderr: io.stderr,
|
|
|
|
|
|
exitOnCtrlC: false,
|
|
|
|
|
|
patchConsole: false,
|
|
|
|
|
|
maxFps: 30,
|
2026-05-13 17:28:48 -04:00
|
|
|
|
alternateScreen: false,
|
2026-05-10 23:12:26 +02:00
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
await instance.waitUntilExit();
|
|
|
|
|
|
instance.unmount();
|
|
|
|
|
|
return result;
|
|
|
|
|
|
} catch (error) {
|
2026-05-13 18:41:44 -04:00
|
|
|
|
const hint = options.scriptedModeHint ?? 'Picker requires a TTY.';
|
|
|
|
|
|
io.stderr.write(`${hint} ${sanitizeTreePickerTuiError(error)}\n`);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
return { kind: 'quit' };
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|