名称: ink-component-generator 描述: 为终端用户界面生成Ink(CLI的React)组件,包含钩子、状态管理和布局组件。 允许使用的工具: 读取、写入、编辑、Bash、Glob、Grep
Ink 组件生成器
为终端用户界面生成Ink(React)组件。
能力
- 生成Ink React组件
- 为CLI状态创建自定义钩子
- 设置布局组件(Box,Text)
- 实现输入处理
- 创建加载和进度组件
- 使用ink-testing-library设置测试
使用方法
在以下情况时调用此技能:
- 使用React模式构建终端UI
- 创建交互式CLI组件
- 实现有状态的终端界面
- 设置Ink项目结构
输入参数
| 参数 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| projectName | 字符串 | 是 | 项目名称 |
| components | 数组 | 是 | 组件定义 |
| includeHooks | 布尔值 | 否 | 生成自定义钩子 |
组件结构
{
"components": [
{
"name": "SelectList",
"type": "interactive",
"props": ["items", "onSelect"],
"state": ["selectedIndex"]
}
]
}
生成模式
选择列表组件
import React, { useState, useCallback } from 'react';
import { Box, Text, useInput, useApp } from 'ink';
interface SelectListProps {
items: string[];
onSelect: (item: string, index: number) => void;
}
export const SelectList: React.FC<SelectListProps> = ({ items, onSelect }) => {
const [selectedIndex, setSelectedIndex] = useState(0);
const { exit } = useApp();
useInput((input, key) => {
if (key.upArrow) {
setSelectedIndex((prev) => (prev > 0 ? prev - 1 : items.length - 1));
} else if (key.downArrow) {
setSelectedIndex((prev) => (prev < items.length - 1 ? prev + 1 : 0));
} else if (key.return) {
onSelect(items[selectedIndex], selectedIndex);
} else if (input === 'q' || key.escape) {
exit();
}
});
return (
<Box flexDirection="column">
{items.map((item, index) => (
<Box key={item}>
<Text color={index === selectedIndex ? 'green' : undefined}>
{index === selectedIndex ? '> ' : ' '}
{item}
</Text>
</Box>
))}
<Box marginTop={1}>
<Text dimColor>使用方向键导航,回车键选择,q键退出</Text>
</Box>
</Box>
);
};
文本输入组件
import React, { useState } from 'react';
import { Box, Text, useInput } from 'ink';
interface TextInputProps {
placeholder?: string;
onSubmit: (value: string) => void;
mask?: string;
}
export const TextInput: React.FC<TextInputProps> = ({
placeholder = '',
onSubmit,
mask,
}) => {
const [value, setValue] = useState('');
const [cursor, setCursor] = useState(0);
useInput((input, key) => {
if (key.return) {
onSubmit(value);
return;
}
if (key.backspace || key.delete) {
setValue((prev) => prev.slice(0, -1));
setCursor((prev) => Math.max(0, prev - 1));
return;
}
if (!key.ctrl && !key.meta && input) {
setValue((prev) => prev + input);
setCursor((prev) => prev + 1);
}
});
const displayValue = mask ? mask.repeat(value.length) : value;
return (
<Box>
<Text>
{displayValue || <Text dimColor>{placeholder}</Text>}
<Text backgroundColor="white"> </Text>
</Text>
</Box>
);
};
旋转器组件
import React, { useState, useEffect } from 'react';
import { Text } from 'ink';
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
interface SpinnerProps {
label?: string;
}
export const Spinner: React.FC<SpinnerProps> = ({ label }) => {
const [frame, setFrame] = useState(0);
useEffect(() => {
const timer = setInterval(() => {
setFrame((prev) => (prev + 1) % frames.length);
}, 80);
return () => clearInterval(timer);
}, []);
return (
<Text>
<Text color="green">{frames[frame]}</Text>
{label && <Text> {label}</Text>}
</Text>
);
};
自定义钩子 - useAsync
import { useState, useEffect, useCallback } from 'react';
interface AsyncState<T> {
data: T | null;
loading: boolean;
error: Error | null;
}
export function useAsync<T>(
asyncFn: () => Promise<T>,
deps: any[] = []
): AsyncState<T> & { refetch: () => void } {
const [state, setState] = useState<AsyncState<T>>({
data: null,
loading: true,
error: null,
});
const execute = useCallback(async () => {
setState({ data: null, loading: true, error: null });
try {
const data = await asyncFn();
setState({ data, loading: false, error: null });
} catch (error) {
setState({ data: null, loading: false, error: error as Error });
}
}, deps);
useEffect(() => {
execute();
}, [execute]);
return { ...state, refetch: execute };
}
依赖项
{
"dependencies": {
"ink": "^4.0.0",
"react": "^18.0.0"
},
"devDependencies": {
"@types/react": "^18.0.0",
"ink-testing-library": "^3.0.0"
}
}
目标流程
- tui-application-framework
- interactive-form-implementation
- dashboard-monitoring-tui