AI に ink と ink-testing-library の使い方を自分に説明させるために書いたテキスト
React Inkは、CLIアプリケーションをReactコンポーネントとして構築できるライブラリです。
pnpm add ink react
pnpm add -D @types/react ink-testing-library
<Text color="green" bold>緑色の太字</Text>
<Text backgroundColor="blue" dimColor>背景色付き</Text>
// レイアウト
<Box flexDirection="column" gap={1}>
<Text>上</Text>
<Text>下</Text>
</Box>
// ボーダー
<Box borderStyle="round" borderColor="green" padding={1}>
<Text>枠線付き</Text>
</Box>
ボーダースタイル: single
, double
, round
, bold
, singleDouble
, doubleSingle
, classic
useInput((input, key) => {
if (input === 'q') exit();
if (key.leftArrow) moveLeft();
if (key.return) confirm();
});
const { exit } = useApp();
exit(); // アプリケーション終了
const GameUI = () => {
const [hp, setHp] = useState(100);
useInput((input) => {
if (input === 'a') attack();
if (input === 'q') exit();
});
return (
<Box flexDirection="column">
<Box borderStyle="single" padding={1}>
<Text bold>ステータス</Text>
</Box>
<Text color="red">HP: {hp}/100</Text>
<Text dimColor>[A] 攻撃 [Q] 終了</Text>
</Box>
);
};
import { render } from 'ink-testing-library';
// 基本的なテスト
const { lastFrame } = render(<Text>Hello</Text>);
expect(lastFrame()).toBe('Hello');
// 再レンダリング
const { lastFrame, rerender } = render(<Counter value={1} />);
expect(lastFrame()).toBe('Count: 1');
rerender(<Counter value={2} />);
expect(lastFrame()).toBe('Count: 2');
// キー入力シミュレーション(useInputを使用するコンポーネントでは動作しない)
const { stdin } = render(<App />);
stdin.write('a'); // 通常の文字
stdin.write('\x1B[A'); // 上矢印
stdin.write('\r'); // Enter
Enter: '\r'
Escape: '\x1B'
上矢印: '\x1B[A'
下矢印: '\x1B[B'
右矢印: '\x1B[C'
左矢印: '\x1B[D'
Backspace: '\x7F'
Delete: '\x1B[3~'
Tab: '\t'
beforeAll(() => {
process.stdin.isTTY = true;
process.stdout.isTTY = true;
});
- Raw Modeは一部の環境でサポートされない
- 頻繁な再レンダリングはちらつきの原因になる
console.log
は使用できない(デバッグはファイル出力で)
/docs/lib/ink-usages.test.tsx
- テストコードの実例