@armadra/agent 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/README.md +426 -2
- package/dist/agent/agent.d.ts +56 -0
- package/dist/agent/agent.js +129 -0
- package/dist/agent/loop.d.ts +51 -0
- package/dist/agent/loop.js +158 -0
- package/dist/agent/queue.d.ts +25 -0
- package/dist/agent/queue.js +55 -0
- package/dist/agent/retry.d.ts +22 -0
- package/dist/agent/retry.js +92 -0
- package/dist/agent/schema.d.ts +27 -0
- package/dist/agent/schema.js +186 -0
- package/dist/agent/session-cache.d.ts +100 -0
- package/dist/agent/session-cache.js +511 -0
- package/dist/agent/session-compaction.d.ts +49 -0
- package/dist/agent/session-compaction.js +251 -0
- package/dist/agent/session-core.d.ts +103 -0
- package/dist/agent/session-core.js +7 -0
- package/dist/agent/session-run.d.ts +55 -0
- package/dist/agent/session-run.js +246 -0
- package/dist/agent/session-state.d.ts +39 -0
- package/dist/agent/session-state.js +118 -0
- package/dist/agent/session-subagent.d.ts +54 -0
- package/dist/agent/session-subagent.js +157 -0
- package/dist/agent/session-sync.d.ts +20 -0
- package/dist/agent/session-sync.js +104 -0
- package/dist/agent/session-tools.d.ts +24 -0
- package/dist/agent/session-tools.js +277 -0
- package/dist/agent/session.d.ts +117 -0
- package/dist/agent/session.js +484 -0
- package/dist/agent/system-prompt.d.ts +49 -0
- package/dist/agent/system-prompt.js +121 -0
- package/dist/agent/tool-runner.d.ts +64 -0
- package/dist/agent/tool-runner.js +309 -0
- package/dist/agent/transform.d.ts +26 -0
- package/dist/agent/transform.js +171 -0
- package/dist/agent/types.d.ts +378 -0
- package/dist/agent/types.js +21 -0
- package/dist/ai/apis/anthropic-messages.d.ts +26 -0
- package/dist/ai/apis/anthropic-messages.js +214 -0
- package/dist/ai/apis/anthropic-request.d.ts +42 -0
- package/dist/ai/apis/anthropic-request.js +282 -0
- package/dist/ai/apis/api.d.ts +28 -0
- package/dist/ai/apis/api.js +102 -0
- package/dist/ai/apis/cache-params.d.ts +48 -0
- package/dist/ai/apis/cache-params.js +163 -0
- package/dist/ai/apis/google-generative-ai.d.ts +32 -0
- package/dist/ai/apis/google-generative-ai.js +249 -0
- package/dist/ai/apis/google-request.d.ts +40 -0
- package/dist/ai/apis/google-request.js +255 -0
- package/dist/ai/apis/openai-compat.d.ts +35 -0
- package/dist/ai/apis/openai-compat.js +142 -0
- package/dist/ai/apis/openai-completions.d.ts +28 -0
- package/dist/ai/apis/openai-completions.js +246 -0
- package/dist/ai/apis/openai-request.d.ts +35 -0
- package/dist/ai/apis/openai-request.js +303 -0
- package/dist/ai/apis/openai-responses-request.d.ts +52 -0
- package/dist/ai/apis/openai-responses-request.js +266 -0
- package/dist/ai/apis/openai-responses.d.ts +28 -0
- package/dist/ai/apis/openai-responses.js +303 -0
- package/dist/ai/apis/shared.d.ts +56 -0
- package/dist/ai/apis/shared.js +179 -0
- package/dist/ai/cache/economics.d.ts +24 -0
- package/dist/ai/cache/economics.js +69 -0
- package/dist/ai/cache/fingerprint.d.ts +17 -0
- package/dist/ai/cache/fingerprint.js +39 -0
- package/dist/ai/cache/miss.d.ts +49 -0
- package/dist/ai/cache/miss.js +96 -0
- package/dist/ai/cache/reporting.d.ts +39 -0
- package/dist/ai/cache/reporting.js +102 -0
- package/dist/ai/cache/types.d.ts +94 -0
- package/dist/ai/cache/types.js +19 -0
- package/dist/ai/cache/warmer.d.ts +94 -0
- package/dist/ai/cache/warmer.js +224 -0
- package/dist/ai/context.d.ts +35 -0
- package/dist/ai/context.js +114 -0
- package/dist/ai/cost.d.ts +21 -0
- package/dist/ai/cost.js +60 -0
- package/dist/ai/event-stream.d.ts +33 -0
- package/dist/ai/event-stream.js +101 -0
- package/dist/ai/fake/fake-provider.d.ts +68 -0
- package/dist/ai/fake/fake-provider.js +278 -0
- package/dist/ai/fake/fake-script.d.ts +69 -0
- package/dist/ai/fake/fake-script.js +142 -0
- package/dist/ai/http.d.ts +54 -0
- package/dist/ai/http.js +212 -0
- package/dist/ai/json-partial.d.ts +17 -0
- package/dist/ai/json-partial.js +243 -0
- package/dist/ai/overflow.d.ts +22 -0
- package/dist/ai/overflow.js +61 -0
- package/dist/ai/providers/auth.d.ts +71 -0
- package/dist/ai/providers/auth.js +202 -0
- package/dist/ai/providers/builtin.d.ts +23 -0
- package/dist/ai/providers/builtin.js +148 -0
- package/dist/ai/providers/catalog-data.d.ts +5 -0
- package/dist/ai/providers/catalog-data.js +19 -0
- package/dist/ai/providers/catalog.d.ts +28 -0
- package/dist/ai/providers/catalog.js +166 -0
- package/dist/ai/providers/registry.d.ts +83 -0
- package/dist/ai/providers/registry.js +305 -0
- package/dist/ai/sse.d.ts +32 -0
- package/dist/ai/sse.js +140 -0
- package/dist/ai/thinking.d.ts +42 -0
- package/dist/ai/thinking.js +101 -0
- package/dist/ai/types.d.ts +428 -0
- package/dist/ai/types.js +19 -0
- package/dist/bundle/ama-sandbox.cjs +324 -0
- package/dist/bundle/ama.cjs +25770 -0
- package/dist/bundle.d.ts +9 -0
- package/dist/bundle.js +15 -0
- package/dist/cli/args.d.ts +82 -0
- package/dist/cli/args.js +381 -0
- package/dist/cli/bootstrap.d.ts +18 -0
- package/dist/cli/bootstrap.js +398 -0
- package/dist/cli/compose-providers.d.ts +37 -0
- package/dist/cli/compose-providers.js +104 -0
- package/dist/cli/compose-session.d.ts +69 -0
- package/dist/cli/compose-session.js +370 -0
- package/dist/cli/compose-store.d.ts +24 -0
- package/dist/cli/compose-store.js +125 -0
- package/dist/cli/compose.d.ts +106 -0
- package/dist/cli/compose.js +206 -0
- package/dist/cli/default-model.d.ts +16 -0
- package/dist/cli/default-model.js +24 -0
- package/dist/cli/deps.d.ts +205 -0
- package/dist/cli/deps.js +12 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +40 -0
- package/dist/cli/main.d.ts +26 -0
- package/dist/cli/main.js +165 -0
- package/dist/cli/runtime.d.ts +90 -0
- package/dist/cli/runtime.js +8 -0
- package/dist/cli/startup-screen.d.ts +15 -0
- package/dist/cli/startup-screen.js +56 -0
- package/dist/cli/startup-steps.d.ts +35 -0
- package/dist/cli/startup-steps.js +197 -0
- package/dist/cli/subcommands/auth.d.ts +12 -0
- package/dist/cli/subcommands/auth.js +91 -0
- package/dist/cli/subcommands/config.d.ts +44 -0
- package/dist/cli/subcommands/config.js +201 -0
- package/dist/cli/subcommands/context.d.ts +21 -0
- package/dist/cli/subcommands/context.js +41 -0
- package/dist/cli/subcommands/doctor.d.ts +11 -0
- package/dist/cli/subcommands/doctor.js +246 -0
- package/dist/cli/subcommands/models-cache-probe.d.ts +34 -0
- package/dist/cli/subcommands/models-cache-probe.js +190 -0
- package/dist/cli/subcommands/models-discover.d.ts +40 -0
- package/dist/cli/subcommands/models-discover.js +204 -0
- package/dist/cli/subcommands/models.d.ts +40 -0
- package/dist/cli/subcommands/models.js +144 -0
- package/dist/cli/subcommands/sessions.d.ts +11 -0
- package/dist/cli/subcommands/sessions.js +100 -0
- package/dist/codemode/capability.d.ts +37 -0
- package/dist/codemode/capability.js +55 -0
- package/dist/codemode/declarations.d.ts +44 -0
- package/dist/codemode/declarations.js +111 -0
- package/dist/codemode/host-side.d.ts +59 -0
- package/dist/codemode/host-side.js +253 -0
- package/dist/codemode/modes.d.ts +23 -0
- package/dist/codemode/modes.js +40 -0
- package/dist/codemode/protocol.d.ts +91 -0
- package/dist/codemode/protocol.js +114 -0
- package/dist/codemode/sandbox-entry.d.ts +25 -0
- package/dist/codemode/sandbox-entry.js +323 -0
- package/dist/codemode/store.d.ts +26 -0
- package/dist/codemode/store.js +53 -0
- package/dist/codemode/tool.d.ts +105 -0
- package/dist/codemode/tool.js +283 -0
- package/dist/compaction/branch-summary.d.ts +30 -0
- package/dist/compaction/branch-summary.js +79 -0
- package/dist/compaction/breaker.d.ts +32 -0
- package/dist/compaction/breaker.js +65 -0
- package/dist/compaction/cut-point.d.ts +23 -0
- package/dist/compaction/cut-point.js +70 -0
- package/dist/compaction/estimate.d.ts +30 -0
- package/dist/compaction/estimate.js +114 -0
- package/dist/compaction/prune-tier.d.ts +31 -0
- package/dist/compaction/prune-tier.js +83 -0
- package/dist/compaction/serialize.d.ts +20 -0
- package/dist/compaction/serialize.js +115 -0
- package/dist/compaction/summarize-tier.d.ts +111 -0
- package/dist/compaction/summarize-tier.js +305 -0
- package/dist/config/auth-file.d.ts +39 -0
- package/dist/config/auth-file.js +92 -0
- package/dist/config/context-files.d.ts +30 -0
- package/dist/config/context-files.js +77 -0
- package/dist/config/load.d.ts +45 -0
- package/dist/config/load.js +97 -0
- package/dist/config/merge.d.ts +73 -0
- package/dist/config/merge.js +248 -0
- package/dist/config/paths.d.ts +38 -0
- package/dist/config/paths.js +108 -0
- package/dist/config/profile.d.ts +27 -0
- package/dist/config/profile.js +65 -0
- package/dist/config/schema.d.ts +34 -0
- package/dist/config/schema.js +413 -0
- package/dist/config/trust.d.ts +42 -0
- package/dist/config/trust.js +122 -0
- package/dist/config/types.d.ts +172 -0
- package/dist/config/types.js +27 -0
- package/dist/config/write.d.ts +10 -0
- package/dist/config/write.js +25 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +30 -0
- package/dist/hooks/config.d.ts +49 -0
- package/dist/hooks/config.js +102 -0
- package/dist/hooks/dispatcher.d.ts +41 -0
- package/dist/hooks/dispatcher.js +104 -0
- package/dist/hooks/matcher.d.ts +22 -0
- package/dist/hooks/matcher.js +108 -0
- package/dist/hooks/protocol.d.ts +33 -0
- package/dist/hooks/protocol.js +240 -0
- package/dist/hooks/runner.d.ts +30 -0
- package/dist/hooks/runner.js +143 -0
- package/dist/hooks/types.d.ts +147 -0
- package/dist/hooks/types.js +20 -0
- package/dist/host/api-impl.d.ts +75 -0
- package/dist/host/api-impl.js +173 -0
- package/dist/host/loader.d.ts +31 -0
- package/dist/host/loader.js +147 -0
- package/dist/host/types.d.ts +169 -0
- package/dist/host/types.js +14 -0
- package/dist/host.d.ts +5 -0
- package/dist/host.js +4 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +28 -0
- package/dist/modes/commands-core.d.ts +44 -0
- package/dist/modes/commands-core.js +171 -0
- package/dist/modes/interactive/approval-dialog.d.ts +48 -0
- package/dist/modes/interactive/approval-dialog.js +202 -0
- package/dist/modes/interactive/commands.d.ts +41 -0
- package/dist/modes/interactive/commands.js +202 -0
- package/dist/modes/interactive/completion.d.ts +53 -0
- package/dist/modes/interactive/completion.js +157 -0
- package/dist/modes/interactive/interactive-mode.d.ts +46 -0
- package/dist/modes/interactive/interactive-mode.js +487 -0
- package/dist/modes/interactive/key-dispatch.d.ts +29 -0
- package/dist/modes/interactive/key-dispatch.js +107 -0
- package/dist/modes/interactive/line/line-editor.d.ts +50 -0
- package/dist/modes/interactive/line/line-editor.js +232 -0
- package/dist/modes/interactive/line/line-mode.d.ts +20 -0
- package/dist/modes/interactive/line/line-mode.js +216 -0
- package/dist/modes/interactive/line/line-render.d.ts +35 -0
- package/dist/modes/interactive/line/line-render.js +171 -0
- package/dist/modes/interactive/line/paste-state.d.ts +26 -0
- package/dist/modes/interactive/line/paste-state.js +72 -0
- package/dist/modes/interactive/message-view.d.ts +77 -0
- package/dist/modes/interactive/message-view.js +269 -0
- package/dist/modes/interactive/pickers.d.ts +42 -0
- package/dist/modes/interactive/pickers.js +115 -0
- package/dist/modes/interactive/startup-ui.d.ts +28 -0
- package/dist/modes/interactive/startup-ui.js +238 -0
- package/dist/modes/interactive/status-bar.d.ts +48 -0
- package/dist/modes/interactive/status-bar.js +139 -0
- package/dist/modes/interactive/tool-view.d.ts +95 -0
- package/dist/modes/interactive/tool-view.js +344 -0
- package/dist/modes/print/json-event.d.ts +15 -0
- package/dist/modes/print/json-event.js +39 -0
- package/dist/modes/print/print-mode.d.ts +15 -0
- package/dist/modes/print/print-mode.js +96 -0
- package/dist/modes/rpc/commands.d.ts +40 -0
- package/dist/modes/rpc/commands.js +258 -0
- package/dist/modes/rpc/jsonl.d.ts +16 -0
- package/dist/modes/rpc/jsonl.js +55 -0
- package/dist/modes/rpc/rpc-mode.d.ts +22 -0
- package/dist/modes/rpc/rpc-mode.js +153 -0
- package/dist/modes/session-report.d.ts +55 -0
- package/dist/modes/session-report.js +278 -0
- package/dist/modes/shared.d.ts +13 -0
- package/dist/modes/shared.js +52 -0
- package/dist/modes/startup-ui-text.d.ts +16 -0
- package/dist/modes/startup-ui-text.js +103 -0
- package/dist/permissions/broker.d.ts +47 -0
- package/dist/permissions/broker.js +107 -0
- package/dist/permissions/dangerous.d.ts +47 -0
- package/dist/permissions/dangerous.js +370 -0
- package/dist/permissions/pipeline.d.ts +51 -0
- package/dist/permissions/pipeline.js +181 -0
- package/dist/permissions/preview.d.ts +43 -0
- package/dist/permissions/preview.js +463 -0
- package/dist/permissions/rules.d.ts +58 -0
- package/dist/permissions/rules.js +249 -0
- package/dist/permissions/types.d.ts +100 -0
- package/dist/permissions/types.js +18 -0
- package/dist/rpc.d.ts +171 -0
- package/dist/rpc.js +13 -0
- package/dist/sdk.d.ts +98 -0
- package/dist/sdk.js +195 -0
- package/dist/session/manager.d.ts +70 -0
- package/dist/session/manager.js +278 -0
- package/dist/session/migrate.d.ts +19 -0
- package/dist/session/migrate.js +53 -0
- package/dist/session/projection.d.ts +50 -0
- package/dist/session/projection.js +180 -0
- package/dist/session/store.d.ts +47 -0
- package/dist/session/store.js +210 -0
- package/dist/session/tree.d.ts +21 -0
- package/dist/session/tree.js +88 -0
- package/dist/session/types.d.ts +199 -0
- package/dist/session/types.js +18 -0
- package/dist/skills/discover.d.ts +60 -0
- package/dist/skills/discover.js +160 -0
- package/dist/skills/expand.d.ts +35 -0
- package/dist/skills/expand.js +46 -0
- package/dist/skills/frontmatter.d.ts +27 -0
- package/dist/skills/frontmatter.js +157 -0
- package/dist/skills/index-prompt.d.ts +13 -0
- package/dist/skills/index-prompt.js +42 -0
- package/dist/skills/templates.d.ts +52 -0
- package/dist/skills/templates.js +177 -0
- package/dist/tools/bash.d.ts +49 -0
- package/dist/tools/bash.js +222 -0
- package/dist/tools/edit-fuzzy.d.ts +36 -0
- package/dist/tools/edit-fuzzy.js +129 -0
- package/dist/tools/edit.d.ts +38 -0
- package/dist/tools/edit.js +241 -0
- package/dist/tools/file-mutex.d.ts +8 -0
- package/dist/tools/file-mutex.js +29 -0
- package/dist/tools/glob.d.ts +44 -0
- package/dist/tools/glob.js +217 -0
- package/dist/tools/grep.d.ts +39 -0
- package/dist/tools/grep.js +179 -0
- package/dist/tools/ignore.d.ts +40 -0
- package/dist/tools/ignore.js +174 -0
- package/dist/tools/ls.d.ts +21 -0
- package/dist/tools/ls.js +96 -0
- package/dist/tools/output-accumulator.d.ts +48 -0
- package/dist/tools/output-accumulator.js +106 -0
- package/dist/tools/paths.d.ts +20 -0
- package/dist/tools/paths.js +58 -0
- package/dist/tools/presets.d.ts +71 -0
- package/dist/tools/presets.js +128 -0
- package/dist/tools/process-tree.d.ts +35 -0
- package/dist/tools/process-tree.js +125 -0
- package/dist/tools/read.d.ts +28 -0
- package/dist/tools/read.js +181 -0
- package/dist/tools/registry.d.ts +43 -0
- package/dist/tools/registry.js +106 -0
- package/dist/tools/shell.d.ts +38 -0
- package/dist/tools/shell.js +91 -0
- package/dist/tools/task.d.ts +29 -0
- package/dist/tools/task.js +107 -0
- package/dist/tools/todo.d.ts +28 -0
- package/dist/tools/todo.js +107 -0
- package/dist/tools/truncate.d.ts +44 -0
- package/dist/tools/truncate.js +175 -0
- package/dist/tools/types.d.ts +135 -0
- package/dist/tools/types.js +19 -0
- package/dist/tools/write.d.ts +20 -0
- package/dist/tools/write.js +84 -0
- package/dist/tui/ansi.d.ts +29 -0
- package/dist/tui/ansi.js +420 -0
- package/dist/tui/component.d.ts +38 -0
- package/dist/tui/component.js +13 -0
- package/dist/tui/components/box.d.ts +26 -0
- package/dist/tui/components/box.js +65 -0
- package/dist/tui/components/container.d.ts +14 -0
- package/dist/tui/components/container.js +39 -0
- package/dist/tui/components/editor-buffer.d.ts +67 -0
- package/dist/tui/components/editor-buffer.js +300 -0
- package/dist/tui/components/editor-paste.d.ts +29 -0
- package/dist/tui/components/editor-paste.js +64 -0
- package/dist/tui/components/editor.d.ts +114 -0
- package/dist/tui/components/editor.js +530 -0
- package/dist/tui/components/key-value.d.ts +29 -0
- package/dist/tui/components/key-value.js +48 -0
- package/dist/tui/components/loader.d.ts +35 -0
- package/dist/tui/components/loader.js +71 -0
- package/dist/tui/components/markdown.d.ts +90 -0
- package/dist/tui/components/markdown.js +462 -0
- package/dist/tui/components/meter.d.ts +29 -0
- package/dist/tui/components/meter.js +47 -0
- package/dist/tui/components/overlay.d.ts +24 -0
- package/dist/tui/components/overlay.js +52 -0
- package/dist/tui/components/select-list.d.ts +57 -0
- package/dist/tui/components/select-list.js +179 -0
- package/dist/tui/components/spacer.d.ts +11 -0
- package/dist/tui/components/spacer.js +16 -0
- package/dist/tui/components/text.d.ts +30 -0
- package/dist/tui/components/text.js +65 -0
- package/dist/tui/keybindings.d.ts +66 -0
- package/dist/tui/keybindings.js +120 -0
- package/dist/tui/keys.d.ts +38 -0
- package/dist/tui/keys.js +275 -0
- package/dist/tui/stdin-buffer.d.ts +41 -0
- package/dist/tui/stdin-buffer.js +206 -0
- package/dist/tui/terminal.d.ts +79 -0
- package/dist/tui/terminal.js +153 -0
- package/dist/tui/theme.d.ts +31 -0
- package/dist/tui/theme.js +199 -0
- package/dist/tui/tui.d.ts +103 -0
- package/dist/tui/tui.js +363 -0
- package/dist/tui/vt-screen.d.ts +46 -0
- package/dist/tui/vt-screen.js +251 -0
- package/dist/tui.d.ts +30 -0
- package/dist/tui.js +31 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +24 -0
- package/docs/codemode.md +102 -0
- package/docs/hooks.md +196 -0
- package/docs/host-api.md +161 -0
- package/docs/providers.md +316 -0
- package/docs/rpc.md +253 -0
- package/docs/session-format.md +119 -0
- package/docs/tui.md +168 -0
- package/package.json +101 -4
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 多行编辑器组件(设计 §12.3、§12.4)。[B4]
|
|
3
|
+
*
|
|
4
|
+
* - 渲染:上下边框之间按列宽折行(不切开字素与折叠标记);超过 `maxVisibleLines` 时视窗跟随光标,
|
|
5
|
+
* 边框上提示上 / 下方隐藏的行数;获焦时在光标处输出 CURSOR_MARKER 并反显光标格。
|
|
6
|
+
* - 键位:`tui.editor.*`(见 keybindings.ts)。Enter 提交(`disableSubmit` 时忽略);Shift+Enter / Ctrl+J 换行;
|
|
7
|
+
* Up/Down 在首 / 末视觉行时浏览历史(单行文本或空文本或已在浏览中),否则按视觉行移动。
|
|
8
|
+
* - 括号粘贴(`ESC[200~…ESC[201~`,由 StdinBuffer 合成一次输入):> 10 行或 > 1 000 字符折叠为
|
|
9
|
+
* `[paste #N +M lines]`,提交时展开;粘贴期间不触发补全;粘贴后紧随的 `\r` 是普通 Enter(提交)。
|
|
10
|
+
* - 补全:提供者由外部注入(`/` 命令、`@` 文件等由 B7 实现);同步或异步返回候选与替换起点。
|
|
11
|
+
* - 历史:会话内 + 可选历史文件(JSONL,每行一个 JSON 字符串,保留最近 500 条)。
|
|
12
|
+
*/
|
|
13
|
+
import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { dirname } from "node:path";
|
|
15
|
+
import { CURSOR_MARKER } from "../component.js";
|
|
16
|
+
import { truncateToWidth, visibleWidth } from "../ansi.js";
|
|
17
|
+
import { defaultKeybindings } from "../keybindings.js";
|
|
18
|
+
import { isPasteData, isPrintableText, unwrapPaste } from "../keys.js";
|
|
19
|
+
import { plainTheme } from "../theme.js";
|
|
20
|
+
import { EditorBuffer } from "./editor-buffer.js";
|
|
21
|
+
import { PasteStore } from "./editor-paste.js";
|
|
22
|
+
import { SelectList } from "./select-list.js";
|
|
23
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
24
|
+
export function loadHistoryFile(path, limit = 500) {
|
|
25
|
+
let text;
|
|
26
|
+
try {
|
|
27
|
+
text = readFileSync(path, "utf8");
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
const entries = [];
|
|
33
|
+
for (const line of text.split("\n")) {
|
|
34
|
+
if (line.trim() === "")
|
|
35
|
+
continue;
|
|
36
|
+
try {
|
|
37
|
+
const value = JSON.parse(line);
|
|
38
|
+
if (typeof value === "string" && value !== "")
|
|
39
|
+
entries.push(value);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// 损坏的行跳过
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return entries.slice(-limit);
|
|
46
|
+
}
|
|
47
|
+
function appendHistoryFile(path, entry, limit) {
|
|
48
|
+
try {
|
|
49
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
50
|
+
appendFileSync(path, JSON.stringify(entry) + "\n", { mode: 0o600 });
|
|
51
|
+
const all = loadHistoryFile(path, Number.MAX_SAFE_INTEGER);
|
|
52
|
+
if (all.length > limit * 2) {
|
|
53
|
+
writeFileSync(path, all
|
|
54
|
+
.slice(-limit)
|
|
55
|
+
.map((e) => JSON.stringify(e) + "\n")
|
|
56
|
+
.join(""));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
// 历史写失败不影响编辑
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export class Editor {
|
|
64
|
+
options;
|
|
65
|
+
focused = false;
|
|
66
|
+
/** 审批对话框期间置 true:Enter 不提交。 */
|
|
67
|
+
disableSubmit = false;
|
|
68
|
+
buffer;
|
|
69
|
+
pastes = new PasteStore();
|
|
70
|
+
keys;
|
|
71
|
+
theme;
|
|
72
|
+
history = [];
|
|
73
|
+
historyIndex = -1;
|
|
74
|
+
draft = "";
|
|
75
|
+
lastWidth = 80;
|
|
76
|
+
scrollTop = 0;
|
|
77
|
+
preferredCol = null;
|
|
78
|
+
completion = null;
|
|
79
|
+
completionToken = 0;
|
|
80
|
+
constructor(options = {}) {
|
|
81
|
+
this.options = options;
|
|
82
|
+
this.buffer = new EditorBuffer({ atomic: (line) => this.pastes.ranges(line) });
|
|
83
|
+
this.keys = options.keybindings ?? defaultKeybindings;
|
|
84
|
+
this.theme = options.theme ?? plainTheme();
|
|
85
|
+
if (options.historyFile)
|
|
86
|
+
this.history = loadHistoryFile(options.historyFile, this.historyLimit);
|
|
87
|
+
}
|
|
88
|
+
get historyLimit() {
|
|
89
|
+
return this.options.historyLimit ?? 500;
|
|
90
|
+
}
|
|
91
|
+
// ---- 公共 API -------------------------------------------------------------
|
|
92
|
+
/** 编辑器原文(折叠标记未展开)。 */
|
|
93
|
+
getText() {
|
|
94
|
+
return this.buffer.getText();
|
|
95
|
+
}
|
|
96
|
+
/** 展开折叠标记后的文本(即提交内容)。 */
|
|
97
|
+
getExpandedText() {
|
|
98
|
+
return this.pastes.expand(this.buffer.getText());
|
|
99
|
+
}
|
|
100
|
+
setText(text) {
|
|
101
|
+
this.buffer.setText(text);
|
|
102
|
+
this.afterEdit(false);
|
|
103
|
+
}
|
|
104
|
+
insertText(text) {
|
|
105
|
+
this.buffer.insert(text);
|
|
106
|
+
this.afterEdit(false);
|
|
107
|
+
}
|
|
108
|
+
clear() {
|
|
109
|
+
this.buffer.reset();
|
|
110
|
+
this.pastes.clear();
|
|
111
|
+
this.closeCompletion();
|
|
112
|
+
this.historyIndex = -1;
|
|
113
|
+
this.options.onChange?.("");
|
|
114
|
+
}
|
|
115
|
+
isEmpty() {
|
|
116
|
+
return this.buffer.isEmpty();
|
|
117
|
+
}
|
|
118
|
+
get cursor() {
|
|
119
|
+
return this.buffer.cursor;
|
|
120
|
+
}
|
|
121
|
+
get isCompletionOpen() {
|
|
122
|
+
return this.completion !== null;
|
|
123
|
+
}
|
|
124
|
+
getHistory() {
|
|
125
|
+
return this.history;
|
|
126
|
+
}
|
|
127
|
+
setHistory(entries) {
|
|
128
|
+
this.history = entries.slice(-this.historyLimit);
|
|
129
|
+
this.historyIndex = -1;
|
|
130
|
+
}
|
|
131
|
+
addToHistory(text) {
|
|
132
|
+
if (text.trim() === "" || this.history[this.history.length - 1] === text)
|
|
133
|
+
return;
|
|
134
|
+
this.history.push(text);
|
|
135
|
+
if (this.history.length > this.historyLimit)
|
|
136
|
+
this.history.shift();
|
|
137
|
+
if (this.options.historyFile)
|
|
138
|
+
appendHistoryFile(this.options.historyFile, text, this.historyLimit);
|
|
139
|
+
}
|
|
140
|
+
/** 取出提交内容(展开、记历史、清空),不调用 onSubmit;空文本返回 null。供 followUp 等使用。 */
|
|
141
|
+
takeSubmission() {
|
|
142
|
+
const text = this.getExpandedText();
|
|
143
|
+
if (text.trim() === "")
|
|
144
|
+
return null;
|
|
145
|
+
this.addToHistory(text);
|
|
146
|
+
this.clear();
|
|
147
|
+
return text;
|
|
148
|
+
}
|
|
149
|
+
submit() {
|
|
150
|
+
if (this.disableSubmit)
|
|
151
|
+
return;
|
|
152
|
+
const text = this.takeSubmission();
|
|
153
|
+
if (text !== null)
|
|
154
|
+
this.options.onSubmit?.(text);
|
|
155
|
+
}
|
|
156
|
+
// ---- 输入 -----------------------------------------------------------------
|
|
157
|
+
handleInput(data) {
|
|
158
|
+
if (isPasteData(data)) {
|
|
159
|
+
this.closeCompletion();
|
|
160
|
+
this.buffer.insert(this.pastes.accept(unwrapPaste(data)));
|
|
161
|
+
this.afterEdit(false);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (this.completion && this.handleCompletionKey(data))
|
|
165
|
+
return;
|
|
166
|
+
if (this.handleAction(data))
|
|
167
|
+
return;
|
|
168
|
+
if (isPrintableText(data)) {
|
|
169
|
+
this.buffer.insert(data);
|
|
170
|
+
this.afterEdit(true);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
handleCompletionKey(data) {
|
|
174
|
+
const k = this.keys;
|
|
175
|
+
const list = this.completion.list;
|
|
176
|
+
if (k.matches(data, "tui.select.up"))
|
|
177
|
+
list.moveSelection(-1);
|
|
178
|
+
else if (k.matches(data, "tui.select.down"))
|
|
179
|
+
list.moveSelection(1);
|
|
180
|
+
else if (k.matches(data, "tui.editor.submit")) {
|
|
181
|
+
// Enter:接受候选;候选与已输入完全相同(没有可补的了)则直接提交
|
|
182
|
+
const before = this.buffer.getText();
|
|
183
|
+
this.acceptCompletion();
|
|
184
|
+
if (this.buffer.getText() === before)
|
|
185
|
+
this.submit();
|
|
186
|
+
}
|
|
187
|
+
else if (k.matches(data, "tui.select.confirm"))
|
|
188
|
+
this.acceptCompletion();
|
|
189
|
+
else if (k.matches(data, "tui.select.cancel"))
|
|
190
|
+
this.closeCompletion();
|
|
191
|
+
else
|
|
192
|
+
return false;
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
handleAction(data) {
|
|
196
|
+
const k = this.keys;
|
|
197
|
+
const b = this.buffer;
|
|
198
|
+
const edit = (fn) => {
|
|
199
|
+
fn();
|
|
200
|
+
this.afterEdit(true);
|
|
201
|
+
return true;
|
|
202
|
+
};
|
|
203
|
+
const move = (fn) => {
|
|
204
|
+
fn();
|
|
205
|
+
this.preferredCol = null;
|
|
206
|
+
this.closeCompletion();
|
|
207
|
+
return true;
|
|
208
|
+
};
|
|
209
|
+
if (k.matches(data, "tui.editor.submit")) {
|
|
210
|
+
this.submit();
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
if (k.matches(data, "tui.editor.newLine"))
|
|
214
|
+
return edit(() => b.newLine());
|
|
215
|
+
if (k.matches(data, "tui.editor.deleteBackward"))
|
|
216
|
+
return edit(() => b.backspace());
|
|
217
|
+
if (k.matches(data, "tui.editor.deleteForward"))
|
|
218
|
+
return edit(() => b.deleteForward());
|
|
219
|
+
if (k.matches(data, "tui.editor.deleteWordBackward"))
|
|
220
|
+
return edit(() => b.deleteWordBackward());
|
|
221
|
+
if (k.matches(data, "tui.editor.deleteWordForward"))
|
|
222
|
+
return edit(() => b.deleteWordForward());
|
|
223
|
+
if (k.matches(data, "tui.editor.deleteToLineStart"))
|
|
224
|
+
return edit(() => b.deleteToLineStart());
|
|
225
|
+
if (k.matches(data, "tui.editor.deleteToLineEnd"))
|
|
226
|
+
return edit(() => b.deleteToLineEnd());
|
|
227
|
+
if (k.matches(data, "tui.editor.undo"))
|
|
228
|
+
return edit(() => b.undo());
|
|
229
|
+
if (k.matches(data, "tui.editor.cursorLeft"))
|
|
230
|
+
return move(() => b.moveLeft());
|
|
231
|
+
if (k.matches(data, "tui.editor.cursorRight"))
|
|
232
|
+
return move(() => b.moveRight());
|
|
233
|
+
if (k.matches(data, "tui.editor.wordLeft"))
|
|
234
|
+
return move(() => b.moveWordLeft());
|
|
235
|
+
if (k.matches(data, "tui.editor.wordRight"))
|
|
236
|
+
return move(() => b.moveWordRight());
|
|
237
|
+
if (k.matches(data, "tui.editor.lineStart"))
|
|
238
|
+
return move(() => b.moveLineStart());
|
|
239
|
+
if (k.matches(data, "tui.editor.lineEnd"))
|
|
240
|
+
return move(() => b.moveLineEnd());
|
|
241
|
+
if (k.matches(data, "tui.editor.cursorUp")) {
|
|
242
|
+
this.closeCompletion();
|
|
243
|
+
this.verticalMove(-1);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
if (k.matches(data, "tui.editor.cursorDown")) {
|
|
247
|
+
this.closeCompletion();
|
|
248
|
+
this.verticalMove(1);
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
if (k.matches(data, "tui.editor.complete") && this.options.autocomplete) {
|
|
252
|
+
this.requestCompletion(true);
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
afterEdit(triggerCompletion) {
|
|
258
|
+
this.historyIndex = -1;
|
|
259
|
+
this.preferredCol = null;
|
|
260
|
+
this.options.onChange?.(this.buffer.getText());
|
|
261
|
+
if (triggerCompletion)
|
|
262
|
+
this.requestCompletion(false);
|
|
263
|
+
else
|
|
264
|
+
this.closeCompletion();
|
|
265
|
+
}
|
|
266
|
+
// ---- 历史与纵向移动 -------------------------------------------------------
|
|
267
|
+
verticalMove(direction) {
|
|
268
|
+
const rows = this.layout(this.contentWidth(this.lastWidth));
|
|
269
|
+
const at = this.cursorRowIndex(rows);
|
|
270
|
+
const singleLine = !this.buffer.getText().includes("\n");
|
|
271
|
+
const browsing = this.historyIndex !== -1;
|
|
272
|
+
if (direction === -1 && at === 0) {
|
|
273
|
+
if (this.history.length > 0 && (this.buffer.isEmpty() || singleLine || browsing)) {
|
|
274
|
+
this.historyStep(-1);
|
|
275
|
+
}
|
|
276
|
+
else
|
|
277
|
+
this.buffer.moveLineStart();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (direction === 1 && at === rows.length - 1) {
|
|
281
|
+
if (browsing)
|
|
282
|
+
this.historyStep(1);
|
|
283
|
+
else
|
|
284
|
+
this.buffer.moveLineEnd();
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const row = rows[at];
|
|
288
|
+
const line = this.buffer.getLines()[row.line];
|
|
289
|
+
const col = this.preferredCol ?? visibleWidth(line.slice(row.start, this.buffer.cursor.col));
|
|
290
|
+
this.preferredCol = col;
|
|
291
|
+
const target = rows[at + direction];
|
|
292
|
+
this.buffer.setCursor({ line: target.line, col: this.colAtWidth(target, col) });
|
|
293
|
+
}
|
|
294
|
+
historyStep(direction) {
|
|
295
|
+
if (direction === -1) {
|
|
296
|
+
if (this.historyIndex === -1) {
|
|
297
|
+
this.draft = this.buffer.getText();
|
|
298
|
+
this.historyIndex = this.history.length - 1;
|
|
299
|
+
}
|
|
300
|
+
else if (this.historyIndex > 0)
|
|
301
|
+
this.historyIndex--;
|
|
302
|
+
else
|
|
303
|
+
return;
|
|
304
|
+
this.buffer.setText(this.history[this.historyIndex], false);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
this.historyIndex++;
|
|
308
|
+
if (this.historyIndex >= this.history.length) {
|
|
309
|
+
this.historyIndex = -1;
|
|
310
|
+
this.buffer.setText(this.draft, false);
|
|
311
|
+
}
|
|
312
|
+
else
|
|
313
|
+
this.buffer.setText(this.history[this.historyIndex], false);
|
|
314
|
+
}
|
|
315
|
+
this.preferredCol = null;
|
|
316
|
+
this.options.onChange?.(this.buffer.getText());
|
|
317
|
+
}
|
|
318
|
+
colAtWidth(row, targetWidth) {
|
|
319
|
+
const line = this.buffer.getLines()[row.line];
|
|
320
|
+
let col = row.start;
|
|
321
|
+
let width = 0;
|
|
322
|
+
for (const { segment, index } of segmenter.segment(line.slice(row.start, row.end))) {
|
|
323
|
+
const w = visibleWidth(segment);
|
|
324
|
+
if (width + w > targetWidth)
|
|
325
|
+
break;
|
|
326
|
+
width += w;
|
|
327
|
+
col = row.start + index + segment.length;
|
|
328
|
+
}
|
|
329
|
+
// 不能停在下一段的起点之后(非末段时 end 属于下一行)
|
|
330
|
+
if (!row.last && col >= row.end)
|
|
331
|
+
col = this.prevGrapheme(line, row.end);
|
|
332
|
+
return col;
|
|
333
|
+
}
|
|
334
|
+
prevGrapheme(line, col) {
|
|
335
|
+
let prev = 0;
|
|
336
|
+
for (const { index } of segmenter.segment(line)) {
|
|
337
|
+
if (index >= col)
|
|
338
|
+
break;
|
|
339
|
+
prev = index;
|
|
340
|
+
}
|
|
341
|
+
return prev;
|
|
342
|
+
}
|
|
343
|
+
// ---- 补全 -----------------------------------------------------------------
|
|
344
|
+
requestCompletion(force) {
|
|
345
|
+
const provider = this.options.autocomplete;
|
|
346
|
+
if (!provider)
|
|
347
|
+
return;
|
|
348
|
+
const token = ++this.completionToken;
|
|
349
|
+
const lines = this.buffer.getLines();
|
|
350
|
+
const context = {
|
|
351
|
+
line: lines[this.buffer.cursor.line],
|
|
352
|
+
textBeforeCursor: this.buffer.textBeforeCursor(),
|
|
353
|
+
text: this.buffer.getText(),
|
|
354
|
+
force,
|
|
355
|
+
};
|
|
356
|
+
const result = provider.getSuggestions(context);
|
|
357
|
+
if (result instanceof Promise) {
|
|
358
|
+
result.then((r) => {
|
|
359
|
+
if (token !== this.completionToken)
|
|
360
|
+
return;
|
|
361
|
+
this.applyCompletion(r, force);
|
|
362
|
+
this.options.requestRender?.();
|
|
363
|
+
}, () => undefined);
|
|
364
|
+
}
|
|
365
|
+
else
|
|
366
|
+
this.applyCompletion(result, force);
|
|
367
|
+
}
|
|
368
|
+
applyCompletion(result, force) {
|
|
369
|
+
if (!result || result.items.length === 0) {
|
|
370
|
+
this.closeCompletion();
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (force && result.items.length === 1) {
|
|
374
|
+
this.buffer.replaceBeforeCursor(result.from, result.items[0].value);
|
|
375
|
+
this.closeCompletion();
|
|
376
|
+
this.options.onChange?.(this.buffer.getText());
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const items = result.items.map((item) => item.description === undefined
|
|
380
|
+
? { value: item.value, label: item.label }
|
|
381
|
+
: { value: item.value, label: item.label, description: item.description });
|
|
382
|
+
this.completion = {
|
|
383
|
+
list: new SelectList(items, { maxVisible: 8, theme: this.theme, keybindings: this.keys }),
|
|
384
|
+
from: result.from,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
acceptCompletion() {
|
|
388
|
+
const c = this.completion;
|
|
389
|
+
if (!c)
|
|
390
|
+
return;
|
|
391
|
+
const item = c.list.getSelected();
|
|
392
|
+
this.closeCompletion();
|
|
393
|
+
if (!item)
|
|
394
|
+
return;
|
|
395
|
+
this.buffer.replaceBeforeCursor(c.from, item.value);
|
|
396
|
+
this.historyIndex = -1;
|
|
397
|
+
this.options.onChange?.(this.buffer.getText());
|
|
398
|
+
}
|
|
399
|
+
closeCompletion() {
|
|
400
|
+
this.completion = null;
|
|
401
|
+
this.completionToken++;
|
|
402
|
+
}
|
|
403
|
+
// ---- 渲染 -----------------------------------------------------------------
|
|
404
|
+
contentWidth(width) {
|
|
405
|
+
return Math.max(1, width - 1);
|
|
406
|
+
}
|
|
407
|
+
/** 逻辑行 → 视觉行(按列宽折行,不切开字素与折叠标记)。 */
|
|
408
|
+
layout(width) {
|
|
409
|
+
const rows = [];
|
|
410
|
+
this.buffer.getLines().forEach((text, line) => {
|
|
411
|
+
const atomic = this.pastes.ranges(text);
|
|
412
|
+
let start = 0;
|
|
413
|
+
let used = 0;
|
|
414
|
+
let pos = 0;
|
|
415
|
+
const graphemes = [...segmenter.segment(text)];
|
|
416
|
+
let gi = 0;
|
|
417
|
+
while (gi < graphemes.length) {
|
|
418
|
+
const g = graphemes[gi];
|
|
419
|
+
pos = g.index;
|
|
420
|
+
const range = atomic.find(([s]) => s === pos);
|
|
421
|
+
const unitEnd = range ? range[1] : pos + g.segment.length;
|
|
422
|
+
const unitWidth = visibleWidth(text.slice(pos, unitEnd));
|
|
423
|
+
if (used > 0 && used + unitWidth > width) {
|
|
424
|
+
rows.push({ line, start, end: pos, last: false });
|
|
425
|
+
start = pos;
|
|
426
|
+
used = 0;
|
|
427
|
+
}
|
|
428
|
+
if (range && unitWidth > width) {
|
|
429
|
+
// 标记比整行还宽:按字素拆开显示(逻辑上仍不可分割)
|
|
430
|
+
used += visibleWidth(g.segment);
|
|
431
|
+
gi++;
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
used += unitWidth;
|
|
435
|
+
while (gi < graphemes.length && graphemes[gi].index < unitEnd)
|
|
436
|
+
gi++;
|
|
437
|
+
}
|
|
438
|
+
rows.push({ line, start, end: text.length, last: true });
|
|
439
|
+
});
|
|
440
|
+
return rows;
|
|
441
|
+
}
|
|
442
|
+
cursorRowIndex(rows) {
|
|
443
|
+
const { line, col } = this.buffer.cursor;
|
|
444
|
+
for (let i = 0; i < rows.length; i++) {
|
|
445
|
+
const r = rows[i];
|
|
446
|
+
if (r.line === line && col >= r.start && (col < r.end || r.last))
|
|
447
|
+
return i;
|
|
448
|
+
}
|
|
449
|
+
return rows.length - 1;
|
|
450
|
+
}
|
|
451
|
+
renderRow(row, withCursor) {
|
|
452
|
+
const text = this.buffer.getLines()[row.line];
|
|
453
|
+
const atomic = this.pastes.ranges(text);
|
|
454
|
+
const cursorCol = withCursor ? this.buffer.cursor.col : -1;
|
|
455
|
+
let out = "";
|
|
456
|
+
let pos = row.start;
|
|
457
|
+
while (pos < row.end || pos === cursorCol) {
|
|
458
|
+
if (pos === cursorCol) {
|
|
459
|
+
const g = pos < row.end ? nextGrapheme(text, pos) : " ";
|
|
460
|
+
out += CURSOR_MARKER + `\x1b[7m${g}\x1b[27m`;
|
|
461
|
+
if (pos >= row.end)
|
|
462
|
+
break;
|
|
463
|
+
pos += g.length;
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const range = atomic.find(([s, e]) => pos >= s && pos < e);
|
|
467
|
+
if (range) {
|
|
468
|
+
const end = Math.min(range[1], row.end, cursorCol > pos ? cursorCol : Infinity);
|
|
469
|
+
out += this.theme.fg("accent", text.slice(pos, end));
|
|
470
|
+
pos = end;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
let end = row.end;
|
|
474
|
+
if (cursorCol > pos && cursorCol < end)
|
|
475
|
+
end = cursorCol;
|
|
476
|
+
const nextAtomic = atomic.find(([s]) => s > pos && s < end);
|
|
477
|
+
if (nextAtomic)
|
|
478
|
+
end = nextAtomic[0];
|
|
479
|
+
out += text.slice(pos, end);
|
|
480
|
+
pos = end;
|
|
481
|
+
}
|
|
482
|
+
return out;
|
|
483
|
+
}
|
|
484
|
+
render(width) {
|
|
485
|
+
this.lastWidth = width;
|
|
486
|
+
const theme = this.theme;
|
|
487
|
+
const contentWidth = this.contentWidth(width);
|
|
488
|
+
const rows = this.layout(contentWidth);
|
|
489
|
+
const cursorRow = this.cursorRowIndex(rows);
|
|
490
|
+
const maxVisible = Math.max(1, this.options.maxVisibleLines ?? 10);
|
|
491
|
+
if (cursorRow < this.scrollTop)
|
|
492
|
+
this.scrollTop = cursorRow;
|
|
493
|
+
if (cursorRow >= this.scrollTop + maxVisible)
|
|
494
|
+
this.scrollTop = cursorRow - maxVisible + 1;
|
|
495
|
+
this.scrollTop = Math.max(0, Math.min(this.scrollTop, rows.length - maxVisible));
|
|
496
|
+
const end = Math.min(rows.length, this.scrollTop + maxVisible);
|
|
497
|
+
const lines = [this.border(width, this.scrollTop > 0 ? `↑ ${this.scrollTop}` : "")];
|
|
498
|
+
if (this.buffer.isEmpty() && this.options.placeholder && !this.focused) {
|
|
499
|
+
lines.push(truncateToWidth(theme.fg("dim", this.options.placeholder), width));
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
for (let i = this.scrollTop; i < end; i++) {
|
|
503
|
+
lines.push(this.renderRow(rows[i], this.focused && i === cursorRow));
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
lines.push(this.border(width, end < rows.length ? `↓ ${rows.length - end}` : ""));
|
|
507
|
+
if (this.completion) {
|
|
508
|
+
for (const line of this.completion.list.render(width))
|
|
509
|
+
lines.push(line);
|
|
510
|
+
}
|
|
511
|
+
return lines;
|
|
512
|
+
}
|
|
513
|
+
border(width, label) {
|
|
514
|
+
if (label === "" || width < label.length + 6)
|
|
515
|
+
return this.theme.fg("border", "─".repeat(width));
|
|
516
|
+
const tail = "─".repeat(width - label.length - 5);
|
|
517
|
+
return (this.theme.fg("border", "─── ") +
|
|
518
|
+
this.theme.fg("dim", label) +
|
|
519
|
+
this.theme.fg("border", " " + tail));
|
|
520
|
+
}
|
|
521
|
+
invalidate() {
|
|
522
|
+
this.completion?.list.invalidate();
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
function nextGrapheme(text, pos) {
|
|
526
|
+
const first = segmenter
|
|
527
|
+
.segment(text.slice(pos, pos + 32))[Symbol.iterator]()
|
|
528
|
+
.next();
|
|
529
|
+
return first.done ? " " : first.value.segment;
|
|
530
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 两列键值表(第三波 A8,供 `/session` 等面板)。[W3-B9a-2]
|
|
3
|
+
*
|
|
4
|
+
* - 键列宽 = 最长键的可见宽度,但不超过总宽的 `maxKeyRatio`(缺省 0.4,至少 4 列),超出的键截断;
|
|
5
|
+
* - 值在剩余宽度内截断(不换行,保留 ANSI 样式);值里的换行折成 ` ⏎ `;
|
|
6
|
+
* - 宽度连键列都放不下时退化为只显示截断后的键;空行(`key` 为空串)原样输出空行,可作分组间隔。
|
|
7
|
+
*/
|
|
8
|
+
import type { Component, SemanticColor, Theme } from "../component.js";
|
|
9
|
+
export interface KeyValueRow {
|
|
10
|
+
key: string;
|
|
11
|
+
value: string;
|
|
12
|
+
}
|
|
13
|
+
export interface KeyValueOptions {
|
|
14
|
+
theme?: Theme;
|
|
15
|
+
/** 键的颜色(需要 theme),缺省 dim。 */
|
|
16
|
+
keyColor?: SemanticColor;
|
|
17
|
+
/** 两列之间的空白,缺省 2。 */
|
|
18
|
+
gap?: number;
|
|
19
|
+
maxKeyRatio?: number;
|
|
20
|
+
}
|
|
21
|
+
export declare class KeyValue implements Component {
|
|
22
|
+
private rows;
|
|
23
|
+
private readonly options;
|
|
24
|
+
private cache;
|
|
25
|
+
constructor(rows?: readonly KeyValueRow[], options?: KeyValueOptions);
|
|
26
|
+
setRows(rows: readonly KeyValueRow[]): void;
|
|
27
|
+
render(width: number): string[];
|
|
28
|
+
invalidate(): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 两列键值表(第三波 A8,供 `/session` 等面板)。[W3-B9a-2]
|
|
3
|
+
*
|
|
4
|
+
* - 键列宽 = 最长键的可见宽度,但不超过总宽的 `maxKeyRatio`(缺省 0.4,至少 4 列),超出的键截断;
|
|
5
|
+
* - 值在剩余宽度内截断(不换行,保留 ANSI 样式);值里的换行折成 ` ⏎ `;
|
|
6
|
+
* - 宽度连键列都放不下时退化为只显示截断后的键;空行(`key` 为空串)原样输出空行,可作分组间隔。
|
|
7
|
+
*/
|
|
8
|
+
import { padToWidth, truncateToWidth, visibleWidth } from "../ansi.js";
|
|
9
|
+
/** 键列至少保留的宽度(键更短时取键宽);再窄就只显示键。 */
|
|
10
|
+
const MIN_KEY_WIDTH = 4;
|
|
11
|
+
export class KeyValue {
|
|
12
|
+
rows;
|
|
13
|
+
options;
|
|
14
|
+
cache = null;
|
|
15
|
+
constructor(rows = [], options = {}) {
|
|
16
|
+
this.rows = rows;
|
|
17
|
+
this.options = options;
|
|
18
|
+
}
|
|
19
|
+
setRows(rows) {
|
|
20
|
+
this.rows = rows;
|
|
21
|
+
this.cache = null;
|
|
22
|
+
}
|
|
23
|
+
render(width) {
|
|
24
|
+
if (this.cache?.width === width)
|
|
25
|
+
return this.cache.lines;
|
|
26
|
+
const gap = this.options.gap ?? 2;
|
|
27
|
+
const longest = Math.max(0, ...this.rows.map((r) => visibleWidth(r.key)));
|
|
28
|
+
const share = Math.floor(width * (this.options.maxKeyRatio ?? 0.4));
|
|
29
|
+
const keyWidth = Math.min(longest, Math.max(share, MIN_KEY_WIDTH));
|
|
30
|
+
const valueWidth = width - keyWidth - gap;
|
|
31
|
+
const { theme, keyColor = "dim" } = this.options;
|
|
32
|
+
const lines = this.rows.map(({ key, value }) => {
|
|
33
|
+
if (key === "" && value === "")
|
|
34
|
+
return "";
|
|
35
|
+
const shownKey = truncateToWidth(key, Math.max(keyWidth, 1));
|
|
36
|
+
const styledKey = theme ? theme.fg(keyColor, shownKey) : shownKey;
|
|
37
|
+
if (valueWidth < 1)
|
|
38
|
+
return truncateToWidth(styledKey, width);
|
|
39
|
+
const flat = value.replace(/\r?\n/g, " ⏎ ");
|
|
40
|
+
return padToWidth(styledKey, keyWidth) + " ".repeat(gap) + truncateToWidth(flat, valueWidth);
|
|
41
|
+
});
|
|
42
|
+
this.cache = { width, lines };
|
|
43
|
+
return lines;
|
|
44
|
+
}
|
|
45
|
+
invalidate() {
|
|
46
|
+
this.cache = null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 旋转指示器(设计 §12.1、§12.6:运行中显示 Loader 与已用时)。[B4]
|
|
3
|
+
*
|
|
4
|
+
* 帧由定时器推进并调用 `requestRender`;`stop()` 后不再触发渲染。时钟可注入便于测试。
|
|
5
|
+
*/
|
|
6
|
+
import type { Component, Theme } from "../component.js";
|
|
7
|
+
export declare const LOADER_FRAMES: readonly ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
8
|
+
export interface LoaderOptions {
|
|
9
|
+
message?: string;
|
|
10
|
+
theme?: Theme;
|
|
11
|
+
/** 帧间隔(ms),缺省 80。 */
|
|
12
|
+
intervalMs?: number;
|
|
13
|
+
/** 显示已用时(`12s`、`1m05s`),缺省 true。 */
|
|
14
|
+
showElapsed?: boolean;
|
|
15
|
+
now?: () => number;
|
|
16
|
+
}
|
|
17
|
+
export declare function formatElapsed(ms: number): string;
|
|
18
|
+
export declare class Loader implements Component {
|
|
19
|
+
private readonly requestRender;
|
|
20
|
+
private readonly options;
|
|
21
|
+
private frame;
|
|
22
|
+
private timer;
|
|
23
|
+
private startedAt;
|
|
24
|
+
private message;
|
|
25
|
+
private readonly now;
|
|
26
|
+
constructor(requestRender: () => void, options?: LoaderOptions);
|
|
27
|
+
get running(): boolean;
|
|
28
|
+
setMessage(message: string): void;
|
|
29
|
+
start(): void;
|
|
30
|
+
stop(): void;
|
|
31
|
+
/** 推进一帧(定时器调用;测试可直接调用)。 */
|
|
32
|
+
tick(): void;
|
|
33
|
+
render(width: number): string[];
|
|
34
|
+
invalidate(): void;
|
|
35
|
+
}
|