# TerminalOutput

> 展示只读进程输出、stdout/stderr、ANSI 色彩与流式跟随。

网页来源：/components/terminal-output

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

`QuireTerminalOutput` 按 `chunks` 的顺序展示进程输出。宿主提供稳定 `id`、`stream`、原始 `text` 和运行状态；组件不启动进程、不接收 stdin，也不是交互式 PTY。只解释有限的 ANSI 颜色与文字样式，控制序列不会作为 HTML 或终端命令执行。
状态、退出码等界面文案跟随 [Locale Provider](/raw/components/locale-provider.md)；截断说明由宿主通过 `truncated` 提供。

## 流式输出

追加 chunk 时，视图默认跟随底部；用户向上阅读后暂停跟随，可用“回到底部”恢复。长行横向滚动，`maxVisibleCharacters` 只限制 DOM 展示的尾部字符；完整原文仍由输入数据提供并可复制。

### 示例：terminal-output/streaming

文件：`streaming.vue`

```vue
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import type { QuireKitTerminalOutputChunk, QuireKitTerminalOutputState } from '@quirekit/ui';
import { QuireButton, QuireTerminalOutput } from '@quirekit/ui/vue';

const chunks = ref<QuireKitTerminalOutputChunk[]>([
  { id: '1', stream: 'stdout', text: '$ pnpm check\n\u001b[36mPreparing packages…\u001b[0m\n' },
  { id: '2', stream: 'stdout', text: '✓ ui typecheck\n' }
]);
const state = ref<QuireKitTerminalOutputState>('running');
let timer: ReturnType<typeof setInterval> | undefined;
let index = 0;
const next = [
  { stream: 'stdout', text: '✓ preset build\n' },
  { stream: 'stderr', text: '\u001b[33mWarning: one optional renderer was skipped.\u001b[0m\n' },
  { stream: 'stdout', text: '✓ design docs:check\n\n3 checks passed.\n' }
] as const;

/** Append one visible process fragment. */
function append(): void {
  const item = next[index++];
  if (!item) { state.value = 'succeeded'; stop(); return; }
  chunks.value = [...chunks.value, { id: `step-${index}`, ...item }];
}

/** Stop the local demonstration timer. */
function stop(): void {
  if (timer) clearInterval(timer);
  timer = undefined;
}

/** Simulate a host appending stdout and stderr. */
function play(): void {
  stop();
  chunks.value = [
    { id: '1', stream: 'stdout', text: '$ pnpm check\n\u001b[36mPreparing packages…\u001b[0m\n' },
    { id: '2', stream: 'stdout', text: '✓ ui typecheck\n' }
  ];
  state.value = 'running';
  index = 0;
  timer = setInterval(append, 650);
}

/** Add enough host output to exercise follow and reading-history behavior. */
function fill(): void {
  stop();
  const start = chunks.value.length;
  chunks.value = [...chunks.value, ...Array.from({ length: 24 }, (_, offset) => ({
    id: `line-${start + offset}`, stream: 'stdout' as const,
    text: `检查文件 ${String(start + offset).padStart(2, '0')} · 通过\n`
  }))];
  state.value = 'succeeded';
}
onBeforeUnmount(stop);
</script>

<template>
  <div class="grid min-w-0 gap-qk-group p-qk-surface" data-terminal-output-streaming>
    <div class="flex gap-qk-inline"><QuireButton size="sm" color="primary" variant="soft" @click="play">模拟流式输出</QuireButton><QuireButton size="sm" variant="ghost" @click="fill">追加长输出</QuireButton></div>
    <QuireTerminalOutput :chunks="chunks" :state="state" :max-height="220" label="构建输出" />
  </div>
</template>
```

## 与工具过程组合

工具的执行、授权和状态投影仍归宿主。可以通过 `QuireChatTool` 的 `output` slot 放入 TerminalOutput；上游已经截断时传入 `truncated` 提示，组件不再提供“复制完整输出”。“原文”视图复用 CodeBlock，便于检查转义序列。

### 示例：terminal-output/tool

文件：`tool.vue`

```vue
<script setup lang="ts">
import type { QuireKitChatToolPart } from '@quirekit/ui';
import { QuireChatTool, QuireTerminalOutput } from '@quirekit/ui/vue';

const part: QuireKitChatToolPart = {
  id: 'build-1', kind: 'tool', title: '构建组件包', icon: 'terminal', state: 'failed',
  summary: '脚本退出，日志保留供检查'
};
const chunks = [
  { id: '1', stream: 'stdout' as const, text: '$ pnpm build\nBundling ui…\n' },
  { id: '2', stream: 'stderr' as const, text: '\u001b[31mError: missing export\u001b[0m\n' }
];
</script>

<template>
  <div class="min-w-0 p-qk-surface" data-terminal-output-tool>
    <QuireChatTool :part="part" :default-open="true">
      <template #output><QuireTerminalOutput :chunks="chunks" state="failed" :exit-code="1" label="构建日志" /></template>
    </QuireChatTool>
  </div>
</template>
```

## API

### QuireTerminalOutput

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| chunks | 是 | — | Chronological output chunks; their raw concatenation is the copy authority. |
| state | 否 | "running" | Host-provided process state. |
| exitCode | 否 | — | Host-provided exit code, when known. |
| label | 否 | — | Accessible output name; the language pack supplies the default. |
| maxHeight | 否 | 360 | Maximum viewport height in CSS pixels. |
| maxVisibleCharacters | 否 | 80000 | Bounded rendered tail in UTF-16 code units; source data remains intact. |
| truncated | 否 | — | Upstream truncation notice. When set, copying is disabled because the source is incomplete. |

**chunks**

```ts
readonly QuireKitTerminalOutputChunk[]
```

```ts
readonly ({
  /** Stable identity; append new chunks instead of replacing earlier identities. */
  id: string;
  /** The originating process stream. */
  stream: "stdout" | "stderr";
  /** Unmodified text, including any ANSI escape sequences. */
  text: string;
})[]
```

**state**

```ts
QuireKitTerminalOutputState | undefined
```

```ts
undefined | "running" | "succeeded" | "failed" | "cancelled"
```

**exitCode**

```ts
number | undefined
```

```ts
undefined | number
```

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**maxHeight**

```ts
number | undefined
```

```ts
undefined | number
```

**maxVisibleCharacters**

```ts
number | undefined
```

```ts
undefined | number
```

**truncated**

```ts
string | undefined
```

```ts
undefined | string
```

#### Events

无。

#### Slots

无。

#### 公开实例成员

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| scrollToLatest | 否 | — |  |

**scrollToLatest**

```ts
() => void
```

```ts
{ (): void; }
```

#### 相关类型

```ts
type QuireKitTerminalOutputChunk = {
  /** Stable identity; append new chunks instead of replacing earlier identities. */
  id: string;
  /** The originating process stream. */
  stream: "stdout" | "stderr";
  /** Unmodified text, including any ANSI escape sequences. */
  text: string;
};
```
