@daniel156161/prism 0.2.101 → 0.2.105
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/README.md +2 -2
- package/dist/pi/pi-env.d.ts +1 -1
- package/dist/pi/pi-env.js +1 -1
- package/dist/prism-extensions/integrations/ai-memory-system.js +16 -9
- package/dist/prism-extensions/integrations/ai-memory-system.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent-loop.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/agent-loop.js +23 -25
- package/node_modules/@earendil-works/pi-agent-core/dist/agent-loop.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/env/nodejs.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/env/nodejs.js +3 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/env/nodejs.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/types.d.ts +3 -2
- package/node_modules/@earendil-works/pi-agent-core/dist/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
- package/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +38 -6
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts +75 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js +75 -0
- package/node_modules/@earendil-works/pi-ai/dist/image-models.generated.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-ai-gateway.d.ts +3 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-ai-gateway.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/cloudflare-ai-gateway.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/amazon-bedrock.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/azure-openai-responses.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/baseten.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/deepseek.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/github-copilot.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/huggingface.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/minimax-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/minimax.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/mistral.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/nvidia.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/together.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/zai-coding-cn.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/zai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.d.ts +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/package.json +2 -2
- package/node_modules/@earendil-works/pi-client/CHANGELOG.md +1 -1
- package/node_modules/@earendil-works/pi-client/package.json +2 -2
- package/node_modules/@earendil-works/pi-coding-agent/CHANGELOG.md +47 -3
- package/node_modules/@earendil-works/pi-coding-agent/README.md +5 -4
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/{chunk-GPPBJGBU.js → chunk-2KVJKXS2.js} +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/{chunk-E5KXRMZK.js → chunk-OMWWHBTG.js} +69 -68
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/github-copilot.js +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/{google-generative-ai-XDKMGBCJ.js → google-generative-ai-YMUPJBKR.js} +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/{google-vertex-D5FGEO3Y.js → google-vertex-MPWMV4OF.js} +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/https-proxy-agent-2VXB7436.js +13 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/{mistral-conversations-YK73UAOZ.js → mistral-conversations-Q3AWZJAZ.js} +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/{openai-completions-JD4WAC3R.js → openai-completions-ERMU2SS7.js} +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/index.js +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/rpc-entry.js +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/cli/startup-ui.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/cli/startup-ui.js +2 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/cli/startup-ui.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.d.ts +12 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.js +77 -9
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/agent-session.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/branch-summarization.js +4 -3
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/branch-summarization.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/compaction.d.ts +5 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/compaction.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/compaction.js +19 -5
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/compaction/compaction.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/index.d.ts +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/runner.d.ts +5 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/runner.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/runner.js +45 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/runner.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/types.d.ts +18 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/session-manager.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/session-manager.js +4 -2
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/session-manager.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/settings-manager.d.ts +8 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/settings-manager.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/settings-manager.js +17 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/core/settings-manager.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/client.d.ts +6 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/client.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/client.js +7 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/client.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/provider.d.ts +3 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/provider.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/provider.js +27 -6
- package/node_modules/@earendil-works/pi-coding-agent/dist/extensions/llama/provider.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/index.d.ts +2 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/index.js +1 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/main.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/main.js +2 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/main.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/settings-selector.d.ts +2 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/settings-selector.js +10 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/interactive-mode.d.ts +4 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/interactive-mode.js +55 -31
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-client.d.ts +7 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-client.js +7 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-client.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-mode.js +3 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-types.d.ts +12 -0
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/modes/rpc/rpc-types.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/utils/shell.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/dist/utils/shell.js +6 -4
- package/node_modules/@earendil-works/pi-coding-agent/dist/utils/shell.js.map +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/docs/compaction.md +2 -0
- package/node_modules/@earendil-works/pi-coding-agent/docs/custom-provider.md +7 -7
- package/node_modules/@earendil-works/pi-coding-agent/docs/environment-variables.md +3 -0
- package/node_modules/@earendil-works/pi-coding-agent/docs/extensions.md +18 -0
- package/node_modules/@earendil-works/pi-coding-agent/docs/keybindings.md +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/docs/quickstart.md +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/docs/rpc.md +23 -0
- package/node_modules/@earendil-works/pi-coding-agent/docs/settings.md +12 -4
- package/node_modules/@earendil-works/pi-coding-agent/docs/terminal-setup.md +12 -0
- package/node_modules/@earendil-works/pi-coding-agent/docs/usage.md +4 -3
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/gondolin/package-lock.json +2 -2
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/gondolin/package.json +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/sandbox/package-lock.json +2 -2
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/sandbox/package.json +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/with-deps/package-lock.json +2 -2
- package/node_modules/@earendil-works/pi-coding-agent/examples/extensions/with-deps/package.json +1 -1
- package/node_modules/@earendil-works/pi-coding-agent/package.json +6 -6
- package/node_modules/@earendil-works/pi-protocol/package.json +1 -1
- package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/README.md +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.d.ts +1 -0
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.js +36 -3
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/index.d.ts +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/index.js +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.d.ts +2 -0
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.js +37 -2
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui-alt-screen.d.ts +11 -0
- package/node_modules/@earendil-works/pi-tui/dist/tui-alt-screen.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui-alt-screen.js +58 -15
- package/node_modules/@earendil-works/pi-tui/dist/tui-alt-screen.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui-main-screen.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui-main-screen.js +105 -45
- package/node_modules/@earendil-works/pi-tui/dist/tui-main-screen.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/node_modules/@types/node/README.md +1 -1
- package/node_modules/@types/node/buffer.buffer.d.ts +5 -0
- package/node_modules/@types/node/crypto.d.ts +0 -2
- package/node_modules/@types/node/dgram.d.ts +119 -4
- package/node_modules/@types/node/fs/promises.d.ts +61 -65
- package/node_modules/@types/node/fs.d.ts +86 -76
- package/node_modules/@types/node/http.d.ts +1 -1
- package/node_modules/@types/node/http2.d.ts +2 -2
- package/node_modules/@types/node/index.d.ts +1 -0
- package/node_modules/@types/node/inspector.d.ts +2 -1
- package/node_modules/@types/node/net.d.ts +108 -16
- package/node_modules/@types/node/package.json +2 -2
- package/node_modules/@types/node/quic.d.ts +37 -11
- package/node_modules/@types/node/stream/iter.d.ts +4 -4
- package/node_modules/@types/node/tls.d.ts +24 -0
- package/node_modules/@types/node/ts5.6/buffer.buffer.d.ts +5 -0
- package/node_modules/@types/node/ts5.6/index.d.ts +1 -0
- package/node_modules/@types/node/ts5.7/index.d.ts +1 -0
- package/node_modules/@types/node/tty.d.ts +4 -3
- package/node_modules/@types/node/vfs.d.ts +210 -0
- package/node_modules/@types/node/vm.d.ts +1 -1
- package/package.json +9 -9
- package/src/prism-extensions/integrations/ai-memory-system.ts +14 -9
- package/node_modules/@earendil-works/pi-coding-agent/dist/bundle/chunks/dist-RDWOYWHR.js +0 -13
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tui-main-screen.d.ts","sourceRoot":"","sources":["../src/tui-main-screen.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,GAAG,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AA0ClE,MAAM,WAAW,wBAAwB;IACxC,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,gBAAgB,EAAE,MAAM,CAAC;IACzB,mBAAmB,EAAE,MAAM,CAAC;CAC5B;AAED,sFAAsF;AACtF,qBAAa,aAAc,SAAQ,OAAQ,YAAW,GAAG;IACxD,QAAQ,CAAC,IAAI,YAAsB;IACnC,OAAO,CAAC,aAAa,CAAgB;IACrC,OAAO,CAAC,qBAAqB,CAAqB;IAClD,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,gBAAgB,CAAK;IAC7B,OAAO,CAAC,mBAAmB,CAAK;IAEhC,kBAAkB,IAAI,wBAAwB,CAU7C;IAED,kBAAkB,CAAC,KAAK,EAAE,wBAAwB,GAAG,IAAI,CASxD;IAED,UAAmB,gBAAgB,IAAI,IAAI,CAQ1C;IAED,UAAmB,kBAAkB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAQnE;IAED,OAAO,CAAC,oBAAoB;IAU5B,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,yBAAyB;IAcjC,OAAO,CAAC,gCAAgC;IAuBxC,OAAO,CAAC,wBAAwB;IAchC,SAAS,CAAC,QAAQ,IAAI,IAAI,CA+WzB;IAED;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;CAgC9B","sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { deleteKittyImage, isImageLine } from \"./terminal-image.ts\";\nimport { type TUI, TuiBase, type TuiStopOptions } from \"./tui.ts\";\nimport { visibleWidth } from \"./utils.ts\";\n\nconst KITTY_SEQUENCE_PREFIX = \"\\x1b_G\";\n\ninterface KittyImageHeader {\n\tids: number[];\n\trows: number;\n}\n\nfunction parseKittyImageHeader(line: string): KittyImageHeader | undefined {\n\tconst sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);\n\tif (sequenceStart === -1) return undefined;\n\tconst paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length;\n\tconst paramsEnd = line.indexOf(\";\", paramsStart);\n\tif (paramsEnd === -1) return undefined;\n\n\tconst ids: number[] = [];\n\tlet rows = 1;\n\tfor (const param of line.slice(paramsStart, paramsEnd).split(\",\")) {\n\t\tconst [key, value] = param.split(\"=\", 2);\n\t\tif (value === undefined) continue;\n\t\tconst numberValue = Number(value);\n\t\tif (!Number.isInteger(numberValue) || numberValue <= 0 || numberValue > 0xffffffff) continue;\n\t\tif (key === \"i\") ids.push(numberValue);\n\t\telse if (key === \"r\") rows = numberValue;\n\t}\n\treturn { ids, rows };\n}\n\nfunction extractKittyImageIds(line: string): number[] {\n\treturn parseKittyImageHeader(line)?.ids ?? [];\n}\n\nfunction extractKittyImageRows(line: string): number {\n\treturn parseKittyImageHeader(line)?.rows ?? 1;\n}\n\nfunction isTermuxSession(): boolean {\n\treturn Boolean(process.env.TERMUX_VERSION);\n}\n\nexport interface TuiMainScreenRenderState {\n\tpreviousLines: string[];\n\tpreviousWidth: number;\n\tpreviousHeight: number;\n\tcursorRow: number;\n\thardwareCursorRow: number;\n\tmaxLinesRendered: number;\n\tpreviousViewportTop: number;\n}\n\n/** TUI implementation that renders into the terminal's main screen and scrollback. */\nexport class TuiMainScreen extends TuiBase implements TUI {\n\treadonly mode = \"regular\" as const;\n\tprivate previousLines: string[] = [];\n\tprivate previousKittyImageIds = new Set<number>();\n\tprivate previousWidth = 0;\n\tprivate previousHeight = 0;\n\tprivate cursorRow = 0;\n\tprivate hardwareCursorRow = 0;\n\tprivate maxLinesRendered = 0;\n\tprivate previousViewportTop = 0;\n\n\tcaptureRenderState(): TuiMainScreenRenderState {\n\t\treturn {\n\t\t\tpreviousLines: [...this.previousLines],\n\t\t\tpreviousWidth: this.previousWidth,\n\t\t\tpreviousHeight: this.previousHeight,\n\t\t\tcursorRow: this.cursorRow,\n\t\t\thardwareCursorRow: this.hardwareCursorRow,\n\t\t\tmaxLinesRendered: this.maxLinesRendered,\n\t\t\tpreviousViewportTop: this.previousViewportTop,\n\t\t};\n\t}\n\n\trestoreRenderState(state: TuiMainScreenRenderState): void {\n\t\tthis.previousLines = state.previousLines.map((line) => (isImageLine(line) ? \"\" : line));\n\t\tthis.previousKittyImageIds = new Set();\n\t\tthis.previousWidth = state.previousWidth;\n\t\tthis.previousHeight = state.previousHeight;\n\t\tthis.cursorRow = state.cursorRow;\n\t\tthis.hardwareCursorRow = state.hardwareCursorRow;\n\t\tthis.maxLinesRendered = state.maxLinesRendered;\n\t\tthis.previousViewportTop = state.previousViewportTop;\n\t}\n\n\tprotected override resetRenderState(): void {\n\t\tthis.previousLines = [];\n\t\tthis.previousWidth = -1;\n\t\tthis.previousHeight = -1;\n\t\tthis.cursorRow = 0;\n\t\tthis.hardwareCursorRow = 0;\n\t\tthis.maxLinesRendered = 0;\n\t\tthis.previousViewportTop = 0;\n\t}\n\n\tprotected override beforeTerminalStop(options: TuiStopOptions): void {\n\t\tif (options.preserveScreen || this.previousLines.length === 0) return;\n\t\tthis.terminal.write(\" \");\n\t\tconst targetRow = this.previousLines.length;\n\t\tconst lineDiff = targetRow - this.hardwareCursorRow;\n\t\tif (lineDiff > 0) this.terminal.write(`\\x1b[${lineDiff}B`);\n\t\telse if (lineDiff < 0) this.terminal.write(`\\x1b[${-lineDiff}A`);\n\t\tthis.terminal.write(\"\\r\\n\");\n\t}\n\n\tprivate collectKittyImageIds(lines: string[]): Set<number> {\n\t\tconst ids = new Set<number>();\n\t\tfor (const line of lines) {\n\t\t\tfor (const id of extractKittyImageIds(line)) {\n\t\t\t\tids.add(id);\n\t\t\t}\n\t\t}\n\t\treturn ids;\n\t}\n\n\tprivate deleteKittyImages(ids: Iterable<number>): string {\n\t\tlet buffer = \"\";\n\t\tfor (const id of ids) {\n\t\t\tbuffer += deleteKittyImage(id);\n\t\t}\n\t\treturn buffer;\n\t}\n\n\tprivate getKittyImageReservedRows(lines: string[], index: number, maxIndex = lines.length - 1): number {\n\t\tconst rows = extractKittyImageRows(lines[index] ?? \"\");\n\t\tif (rows <= 1) return 1;\n\n\t\tconst maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index);\n\t\tlet reservedRows = 1;\n\t\twhile (reservedRows < maxRows) {\n\t\t\tconst line = lines[index + reservedRows] ?? \"\";\n\t\t\tif (isImageLine(line) || visibleWidth(line) > 0) break;\n\t\t\treservedRows++;\n\t\t}\n\t\treturn reservedRows;\n\t}\n\n\tprivate expandChangedRangeForKittyImages(\n\t\tfirstChanged: number,\n\t\tlastChanged: number,\n\t\tnewLines: string[],\n\t): { firstChanged: number; lastChanged: number } {\n\t\tlet expandedFirstChanged = firstChanged;\n\t\tlet expandedLastChanged = lastChanged;\n\t\tconst expandForLines = (lines: string[]): void => {\n\t\t\tfor (let i = 0; i < lines.length; i++) {\n\t\t\t\tif (extractKittyImageIds(lines[i]).length === 0) continue;\n\t\t\t\tconst blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1;\n\t\t\t\tif (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) {\n\t\t\t\t\texpandedFirstChanged = Math.min(expandedFirstChanged, i);\n\t\t\t\t\texpandedLastChanged = Math.max(expandedLastChanged, blockEnd);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\texpandForLines(this.previousLines);\n\t\texpandForLines(newLines);\n\t\treturn { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged };\n\t}\n\n\tprivate deleteChangedKittyImages(firstChanged: number, lastChanged: number): string {\n\t\tif (firstChanged < 0 || lastChanged < firstChanged) return \"\";\n\n\t\tconst ids = new Set<number>();\n\t\tconst maxLine = Math.min(lastChanged, this.previousLines.length - 1);\n\t\tfor (let i = firstChanged; i <= maxLine; i++) {\n\t\t\tfor (const id of extractKittyImageIds(this.previousLines[i] ?? \"\")) {\n\t\t\t\tids.add(id);\n\t\t\t}\n\t\t}\n\n\t\treturn this.deleteKittyImages(ids);\n\t}\n\n\tprotected doRender(): void {\n\t\tif (this.stopped) return;\n\t\tconst width = this.terminal.columns;\n\t\tconst height = this.terminal.rows;\n\t\tconst widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;\n\t\tconst heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;\n\t\tconst previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;\n\t\tlet prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;\n\t\tlet viewportTop = prevViewportTop;\n\t\tlet hardwareCursorRow = this.hardwareCursorRow;\n\t\tconst computeLineDiff = (targetRow: number): number => {\n\t\t\tconst currentScreenRow = hardwareCursorRow - prevViewportTop;\n\t\t\tconst targetScreenRow = targetRow - viewportTop;\n\t\t\treturn targetScreenRow - currentScreenRow;\n\t\t};\n\n\t\t// Render all components to get new lines\n\t\tlet newLines = this.render(width);\n\n\t\t// Composite overlays into the rendered lines (before differential compare)\n\t\tif (this.hasOverlayEntries) {\n\t\t\tnewLines = this.compositeOverlays(newLines, width, height);\n\t\t}\n\n\t\t// Extract cursor position before applying line resets (marker must be found first)\n\t\tconst cursorPos = this.extractCursorPosition(newLines, height);\n\n\t\tnewLines = this.applyLineResets(newLines);\n\n\t\t// Helper to clear scrollback and viewport and render all new lines\n\t\tconst fullRender = (clear: boolean): void => {\n\t\t\tthis.fullRedrawCount += 1;\n\t\t\tlet buffer = \"\\x1b[?2026h\"; // Begin synchronized output\n\t\t\tif (clear) {\n\t\t\t\tbuffer += this.deleteKittyImages(this.previousKittyImageIds);\n\t\t\t\tbuffer += \"\\x1b[2J\\x1b[H\\x1b[3J\"; // Clear screen, home, then clear scrollback\n\t\t\t}\n\t\t\tfor (let i = 0; i < newLines.length; i++) {\n\t\t\t\tif (i > 0) buffer += \"\\r\\n\";\n\t\t\t\tconst line = newLines[i];\n\t\t\t\tconst isImage = isImageLine(line);\n\t\t\t\tconst imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;\n\t\t\t\tif (imageReservedRows > 1 && imageReservedRows <= height) {\n\t\t\t\t\tfor (let row = 1; row < imageReservedRows; row++) {\n\t\t\t\t\t\tbuffer += \"\\r\\n\";\n\t\t\t\t\t}\n\t\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}A`;\n\t\t\t\t\tbuffer += line;\n\t\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}B`;\n\t\t\t\t\ti += imageReservedRows - 1;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tbuffer += line;\n\t\t\t}\n\t\t\tbuffer += \"\\x1b[?2026l\"; // End synchronized output\n\t\t\tthis.terminal.write(buffer);\n\t\t\tthis.cursorRow = Math.max(0, newLines.length - 1);\n\t\t\tthis.hardwareCursorRow = this.cursorRow;\n\t\t\t// Reset max lines when clearing, otherwise track growth\n\t\t\tif (clear) {\n\t\t\t\tthis.maxLinesRendered = newLines.length;\n\t\t\t} else {\n\t\t\t\tthis.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);\n\t\t\t}\n\t\t\tconst bufferLength = Math.max(height, newLines.length);\n\t\t\tthis.previousViewportTop = Math.max(0, bufferLength - height);\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousLines = newLines;\n\t\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\t\tthis.previousWidth = width;\n\t\t\tthis.previousHeight = height;\n\t\t};\n\n\t\tconst debugRedraw = process.env.PI_DEBUG_REDRAW === \"1\";\n\t\tconst logRedraw = (reason: string): void => {\n\t\t\tif (!debugRedraw) return;\n\t\t\tconst logPath = path.join(this.logDirectory, \"pi-debug.log\");\n\t\t\tconst msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\\n`;\n\t\t\tfs.mkdirSync(path.dirname(logPath), { recursive: true });\n\t\t\tfs.appendFileSync(logPath, msg);\n\t\t};\n\n\t\t// First render - just output everything without clearing (assumes clean screen)\n\t\tif (this.previousLines.length === 0 && !widthChanged && !heightChanged) {\n\t\t\tlogRedraw(\"first render\");\n\t\t\tfullRender(false);\n\t\t\treturn;\n\t\t}\n\n\t\t// Width changes always need a full re-render because wrapping changes.\n\t\tif (widthChanged) {\n\t\t\tlogRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Height changes normally need a full re-render to keep the visible viewport aligned,\n\t\t// but Termux changes height when the software keyboard shows or hides.\n\t\t// In that environment, a full redraw causes the entire history to replay on every toggle.\n\t\tif (heightChanged && !isTermuxSession()) {\n\t\t\tlogRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Content shrunk below the working area and no overlays - re-render to clear empty rows\n\t\t// (overlays need the padding, so only do this when no overlays are active)\n\t\t// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var\n\t\tif (this.getClearOnShrink() && newLines.length < this.maxLinesRendered && !this.hasOverlayEntries) {\n\t\t\tlogRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Find first and last changed lines\n\t\tlet firstChanged = -1;\n\t\tlet lastChanged = -1;\n\t\tconst maxLines = Math.max(newLines.length, this.previousLines.length);\n\t\tfor (let i = 0; i < maxLines; i++) {\n\t\t\tconst oldLine = i < this.previousLines.length ? this.previousLines[i] : \"\";\n\t\t\tconst newLine = i < newLines.length ? newLines[i] : \"\";\n\n\t\t\tif (oldLine !== newLine) {\n\t\t\t\tif (firstChanged === -1) {\n\t\t\t\t\tfirstChanged = i;\n\t\t\t\t}\n\t\t\t\tlastChanged = i;\n\t\t\t}\n\t\t}\n\t\tconst appendedLines = newLines.length > this.previousLines.length;\n\t\tif (appendedLines) {\n\t\t\tif (firstChanged === -1) {\n\t\t\t\tfirstChanged = this.previousLines.length;\n\t\t\t}\n\t\t\tlastChanged = newLines.length - 1;\n\t\t}\n\t\tif (firstChanged !== -1) {\n\t\t\tconst expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines);\n\t\t\tfirstChanged = expandedRange.firstChanged;\n\t\t\tlastChanged = expandedRange.lastChanged;\n\t\t}\n\t\tconst appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;\n\n\t\t// No changes - but still need to update hardware cursor position if it moved\n\t\tif (firstChanged === -1) {\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousViewportTop = prevViewportTop;\n\t\t\tthis.previousHeight = height;\n\t\t\treturn;\n\t\t}\n\n\t\t// All changes are in deleted lines (nothing to render, just clear)\n\t\tif (firstChanged >= newLines.length) {\n\t\t\tif (this.previousLines.length > newLines.length) {\n\t\t\t\tlet buffer = \"\\x1b[?2026h\";\n\t\t\t\tbuffer += this.deleteChangedKittyImages(firstChanged, lastChanged);\n\t\t\t\t// Move to end of new content (clamp to 0 for empty content)\n\t\t\t\tconst targetRow = Math.max(0, newLines.length - 1);\n\t\t\t\tif (targetRow < prevViewportTop) {\n\t\t\t\t\tlogRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst lineDiff = computeLineDiff(targetRow);\n\t\t\t\tif (lineDiff > 0) buffer += `\\x1b[${lineDiff}B`;\n\t\t\t\telse if (lineDiff < 0) buffer += `\\x1b[${-lineDiff}A`;\n\t\t\t\tbuffer += \"\\r\";\n\t\t\t\t// Clear extra lines without scrolling\n\t\t\t\tconst extraLines = this.previousLines.length - newLines.length;\n\t\t\t\tif (extraLines > height) {\n\t\t\t\t\tlogRedraw(`extraLines > height (${extraLines} > ${height})`);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst clearStartOffset = newLines.length === 0 ? 0 : 1;\n\t\t\t\tif (extraLines > 0 && clearStartOffset > 0) {\n\t\t\t\t\tbuffer += `\\x1b[${clearStartOffset}B`;\n\t\t\t\t}\n\t\t\t\tfor (let i = 0; i < extraLines; i++) {\n\t\t\t\t\tbuffer += \"\\r\\x1b[2K\";\n\t\t\t\t\tif (i < extraLines - 1) buffer += \"\\x1b[1B\";\n\t\t\t\t}\n\t\t\t\tconst moveBack = Math.max(0, extraLines - 1 + clearStartOffset);\n\t\t\t\tif (moveBack > 0) {\n\t\t\t\t\tbuffer += `\\x1b[${moveBack}A`;\n\t\t\t\t}\n\t\t\t\tbuffer += \"\\x1b[?2026l\";\n\t\t\t\tthis.terminal.write(buffer);\n\t\t\t\tthis.cursorRow = targetRow;\n\t\t\t\tthis.hardwareCursorRow = targetRow;\n\t\t\t}\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousLines = newLines;\n\t\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\t\tthis.previousWidth = width;\n\t\t\tthis.previousHeight = height;\n\t\t\tthis.previousViewportTop = prevViewportTop;\n\t\t\treturn;\n\t\t}\n\n\t\t// Differential rendering can only touch what was actually visible.\n\t\t// If the first changed line is above the previous viewport, we need a full redraw.\n\t\tif (firstChanged < prevViewportTop) {\n\t\t\tlogRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Render from first changed line to end\n\t\t// Build buffer with all updates wrapped in synchronized output\n\t\tlet buffer = \"\\x1b[?2026h\"; // Begin synchronized output\n\t\tbuffer += this.deleteChangedKittyImages(firstChanged, lastChanged);\n\t\tconst prevViewportBottom = prevViewportTop + height - 1;\n\t\tconst moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;\n\t\tif (moveTargetRow > prevViewportBottom) {\n\t\t\tconst currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));\n\t\t\tconst moveToBottom = height - 1 - currentScreenRow;\n\t\t\tif (moveToBottom > 0) {\n\t\t\t\tbuffer += `\\x1b[${moveToBottom}B`;\n\t\t\t}\n\t\t\tconst scroll = moveTargetRow - prevViewportBottom;\n\t\t\tbuffer += \"\\r\\n\".repeat(scroll);\n\t\t\tprevViewportTop += scroll;\n\t\t\tviewportTop += scroll;\n\t\t\thardwareCursorRow = moveTargetRow;\n\t\t}\n\n\t\t// Move cursor to first changed line (use hardwareCursorRow for actual position)\n\t\tconst lineDiff = computeLineDiff(moveTargetRow);\n\t\tif (lineDiff > 0) {\n\t\t\tbuffer += `\\x1b[${lineDiff}B`; // Move down\n\t\t} else if (lineDiff < 0) {\n\t\t\tbuffer += `\\x1b[${-lineDiff}A`; // Move up\n\t\t}\n\n\t\tbuffer += appendStart ? \"\\r\\n\" : \"\\r\"; // Move to column 0\n\n\t\t// Only render changed lines (firstChanged to lastChanged), not all lines to end\n\t\t// This reduces flicker when only a single line changes (e.g., spinner animation)\n\t\tconst renderEnd = Math.min(lastChanged, newLines.length - 1);\n\t\tfor (let i = firstChanged; i <= renderEnd; i++) {\n\t\t\tif (i > firstChanged) buffer += \"\\r\\n\";\n\t\t\tconst line = newLines[i];\n\t\t\tconst isImage = isImageLine(line);\n\t\t\tconst imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;\n\t\t\tif (imageReservedRows > 1) {\n\t\t\t\tconst imageStartScreenRow = i - viewportTop;\n\t\t\t\tif (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) {\n\t\t\t\t\tlogRedraw(\n\t\t\t\t\t\t`kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`,\n\t\t\t\t\t);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tbuffer += \"\\x1b[2K\";\n\t\t\t\tfor (let row = 1; row < imageReservedRows; row++) {\n\t\t\t\t\tbuffer += \"\\r\\n\\x1b[2K\";\n\t\t\t\t}\n\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}A`;\n\t\t\t\tbuffer += line;\n\t\t\t\tbuffer += `\\x1b[${imageReservedRows - 1}B`;\n\t\t\t\ti += imageReservedRows - 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tbuffer += \"\\x1b[2K\"; // Clear current line\n\t\t\tif (!isImage && visibleWidth(line) > width) {\n\t\t\t\t// Log all lines to crash file for debugging\n\t\t\t\tconst crashLogPath = path.join(this.logDirectory, \"pi-crash.log\");\n\t\t\t\tconst crashData = [\n\t\t\t\t\t`Crash at ${new Date().toISOString()}`,\n\t\t\t\t\t`Terminal width: ${width}`,\n\t\t\t\t\t`Line ${i} visible width: ${visibleWidth(line)}`,\n\t\t\t\t\t\"\",\n\t\t\t\t\t\"=== All rendered lines ===\",\n\t\t\t\t\t...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),\n\t\t\t\t\t\"\",\n\t\t\t\t].join(\"\\n\");\n\t\t\t\tfs.mkdirSync(path.dirname(crashLogPath), { recursive: true });\n\t\t\t\tfs.writeFileSync(crashLogPath, crashData);\n\n\t\t\t\t// Clean up terminal state before throwing\n\t\t\t\tthis.stop();\n\n\t\t\t\tconst errorMsg = [\n\t\t\t\t\t`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,\n\t\t\t\t\t\"\",\n\t\t\t\t\t\"This is likely caused by a custom TUI component not truncating its output.\",\n\t\t\t\t\t\"Use visibleWidth() to measure and truncateToWidth() to truncate lines.\",\n\t\t\t\t\t\"\",\n\t\t\t\t\t`Debug log written to: ${crashLogPath}`,\n\t\t\t\t].join(\"\\n\");\n\t\t\t\tthrow new Error(errorMsg);\n\t\t\t}\n\t\t\tbuffer += line;\n\t\t}\n\n\t\t// Track where cursor ended up after rendering\n\t\tlet finalCursorRow = renderEnd;\n\n\t\t// If we had more lines before, clear them and move cursor back\n\t\tif (this.previousLines.length > newLines.length) {\n\t\t\t// Move to end of new content first if we stopped before it\n\t\t\tif (renderEnd < newLines.length - 1) {\n\t\t\t\tconst moveDown = newLines.length - 1 - renderEnd;\n\t\t\t\tbuffer += `\\x1b[${moveDown}B`;\n\t\t\t\tfinalCursorRow = newLines.length - 1;\n\t\t\t}\n\t\t\tconst extraLines = this.previousLines.length - newLines.length;\n\t\t\tfor (let i = newLines.length; i < this.previousLines.length; i++) {\n\t\t\t\tbuffer += \"\\r\\n\\x1b[2K\";\n\t\t\t}\n\t\t\t// Move cursor back to end of new content\n\t\t\tbuffer += `\\x1b[${extraLines}A`;\n\t\t}\n\n\t\tbuffer += \"\\x1b[?2026l\"; // End synchronized output\n\n\t\tif (process.env.PI_TUI_DEBUG === \"1\") {\n\t\t\tconst debugDir = \"/tmp/tui\";\n\t\t\tfs.mkdirSync(debugDir, { recursive: true });\n\t\t\tconst debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);\n\t\t\tconst debugData = [\n\t\t\t\t`firstChanged: ${firstChanged}`,\n\t\t\t\t`viewportTop: ${viewportTop}`,\n\t\t\t\t`cursorRow: ${this.cursorRow}`,\n\t\t\t\t`height: ${height}`,\n\t\t\t\t`lineDiff: ${lineDiff}`,\n\t\t\t\t`hardwareCursorRow: ${hardwareCursorRow}`,\n\t\t\t\t`renderEnd: ${renderEnd}`,\n\t\t\t\t`finalCursorRow: ${finalCursorRow}`,\n\t\t\t\t`cursorPos: ${JSON.stringify(cursorPos)}`,\n\t\t\t\t`newLines.length: ${newLines.length}`,\n\t\t\t\t`previousLines.length: ${this.previousLines.length}`,\n\t\t\t\t\"\",\n\t\t\t\t\"=== newLines ===\",\n\t\t\t\tJSON.stringify(newLines, null, 2),\n\t\t\t\t\"\",\n\t\t\t\t\"=== previousLines ===\",\n\t\t\t\tJSON.stringify(this.previousLines, null, 2),\n\t\t\t\t\"\",\n\t\t\t\t\"=== buffer ===\",\n\t\t\t\tJSON.stringify(buffer),\n\t\t\t].join(\"\\n\");\n\t\t\tfs.writeFileSync(debugPath, debugData);\n\t\t}\n\n\t\t// Write entire buffer at once\n\t\tthis.terminal.write(buffer);\n\n\t\t// Track cursor position for next render\n\t\t// cursorRow tracks end of content (for viewport calculation)\n\t\t// hardwareCursorRow tracks actual terminal cursor position (for movement)\n\t\tthis.cursorRow = Math.max(0, newLines.length - 1);\n\t\tthis.hardwareCursorRow = finalCursorRow;\n\t\t// Track terminal's working area (grows but doesn't shrink unless cleared)\n\t\tthis.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);\n\t\tthis.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);\n\n\t\t// Position hardware cursor for IME\n\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\n\t\tthis.previousLines = newLines;\n\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\tthis.previousWidth = width;\n\t\tthis.previousHeight = height;\n\t}\n\n\t/**\n\t * Position the hardware cursor for IME candidate window.\n\t * @param cursorPos The cursor position extracted from rendered output, or null\n\t * @param totalLines Total number of rendered lines\n\t */\n\tprivate positionHardwareCursor(cursorPos: { row: number; col: number } | null, totalLines: number): void {\n\t\tif (!cursorPos || totalLines <= 0) {\n\t\t\tthis.terminal.hideCursor();\n\t\t\treturn;\n\t\t}\n\n\t\t// Clamp cursor position to valid range\n\t\tconst targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));\n\t\tconst targetCol = Math.max(0, cursorPos.col);\n\n\t\t// Move cursor from current position to target\n\t\tconst rowDelta = targetRow - this.hardwareCursorRow;\n\t\tlet buffer = \"\";\n\t\tif (rowDelta > 0) {\n\t\t\tbuffer += `\\x1b[${rowDelta}B`; // Move down\n\t\t} else if (rowDelta < 0) {\n\t\t\tbuffer += `\\x1b[${-rowDelta}A`; // Move up\n\t\t}\n\t\t// Move to absolute column (1-indexed)\n\t\tbuffer += `\\x1b[${targetCol + 1}G`;\n\n\t\tif (buffer) {\n\t\t\tthis.terminal.write(buffer);\n\t\t}\n\n\t\tthis.hardwareCursorRow = targetRow;\n\t\tif (this.getShowHardwareCursor()) {\n\t\t\tthis.terminal.showCursor();\n\t\t} else {\n\t\t\tthis.terminal.hideCursor();\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"tui-main-screen.d.ts","sourceRoot":"","sources":["../src/tui-main-screen.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,GAAG,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AA4GlE,MAAM,WAAW,wBAAwB;IACxC,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,gBAAgB,EAAE,MAAM,CAAC;IACzB,mBAAmB,EAAE,MAAM,CAAC;CAC5B;AAED,sFAAsF;AACtF,qBAAa,aAAc,SAAQ,OAAQ,YAAW,GAAG;IACxD,QAAQ,CAAC,IAAI,YAAsB;IACnC,OAAO,CAAC,aAAa,CAAgB;IACrC,OAAO,CAAC,qBAAqB,CAAqB;IAClD,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,iBAAiB,CAAK;IAC9B,OAAO,CAAC,gBAAgB,CAAK;IAC7B,OAAO,CAAC,mBAAmB,CAAK;IAEhC,kBAAkB,IAAI,wBAAwB,CAU7C;IAED,kBAAkB,CAAC,KAAK,EAAE,wBAAwB,GAAG,IAAI,CASxD;IAED,UAAmB,gBAAgB,IAAI,IAAI,CAQ1C;IAED,UAAmB,kBAAkB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAQnE;IAED,OAAO,CAAC,oBAAoB;IAU5B,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,yBAAyB;IAcjC,OAAO,CAAC,gCAAgC;IAuBxC,OAAO,CAAC,wBAAwB;IAchC,SAAS,CAAC,QAAQ,IAAI,IAAI,CAiXzB;IAED;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;CAgC9B","sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { deleteKittyImage, isImageLine } from \"./terminal-image.ts\";\nimport { type TUI, TuiBase, type TuiStopOptions } from \"./tui.ts\";\nimport { visibleWidth } from \"./utils.ts\";\n\nconst KITTY_SEQUENCE_PREFIX = \"\\x1b_G\";\nconst MAX_RENDER_WRITE_CHARS = 1024 * 1024;\n\n/**\n * Streams terminal output in 1 MiB chunks so a full render never forms one string large enough to exceed V8's limit.\n *\n * `append()` fills the current chunk and flushes it when full. Oversized input is split at chunk boundaries, preserving\n * surrogate pairs so each write remains valid UTF-16. Callers append synchronized-output begin/end sequences themselves;\n * the final `flush()` writes any remainder, including the end sequence.\n */\nclass BoundedTerminalWriter {\n\tprivate buffer = \"\";\n\tprivate writtenChars = 0;\n\tprivate readonly write: (data: string) => void;\n\n\tconstructor(write: (data: string) => void) {\n\t\tthis.write = write;\n\t}\n\n\t/**\n\t * Append terminal data, flushing full chunks as needed. Callers must call `flush()` after the final append.\n\t * @param value Terminal data to write in order; oversized values are split without splitting surrogate pairs.\n\t */\n\tappend(value: string): void {\n\t\tlet offset = 0;\n\t\twhile (offset < value.length) {\n\t\t\tconst capacity = MAX_RENDER_WRITE_CHARS - this.buffer.length;\n\t\t\tif (capacity === 0) {\n\t\t\t\tthis.flush();\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet end = Math.min(value.length, offset + capacity);\n\t\t\tif (\n\t\t\t\tend < value.length &&\n\t\t\t\tvalue.charCodeAt(end - 1) >= 0xd800 &&\n\t\t\t\tvalue.charCodeAt(end - 1) <= 0xdbff &&\n\t\t\t\tvalue.charCodeAt(end) >= 0xdc00 &&\n\t\t\t\tvalue.charCodeAt(end) <= 0xdfff\n\t\t\t) {\n\t\t\t\tend--;\n\t\t\t}\n\t\t\tif (end === offset) {\n\t\t\t\tthis.flush();\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tthis.buffer += value.slice(offset, end);\n\t\t\toffset = end;\n\t\t\tif (this.buffer.length === MAX_RENDER_WRITE_CHARS) {\n\t\t\t\tthis.flush();\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Write the current chunk, if any, and retain only its character count for debug output. */\n\tflush(): void {\n\t\tif (!this.buffer) return;\n\t\tthis.write(this.buffer);\n\t\tthis.writtenChars += this.buffer.length;\n\t\tthis.buffer = \"\";\n\t}\n\n\tget length(): number {\n\t\treturn this.writtenChars + this.buffer.length;\n\t}\n}\n\ninterface KittyImageHeader {\n\tids: number[];\n\trows: number;\n}\n\nfunction parseKittyImageHeader(line: string): KittyImageHeader | undefined {\n\tconst sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);\n\tif (sequenceStart === -1) return undefined;\n\tconst paramsStart = sequenceStart + KITTY_SEQUENCE_PREFIX.length;\n\tconst paramsEnd = line.indexOf(\";\", paramsStart);\n\tif (paramsEnd === -1) return undefined;\n\n\tconst ids: number[] = [];\n\tlet rows = 1;\n\tfor (const param of line.slice(paramsStart, paramsEnd).split(\",\")) {\n\t\tconst [key, value] = param.split(\"=\", 2);\n\t\tif (value === undefined) continue;\n\t\tconst numberValue = Number(value);\n\t\tif (!Number.isInteger(numberValue) || numberValue <= 0 || numberValue > 0xffffffff) continue;\n\t\tif (key === \"i\") ids.push(numberValue);\n\t\telse if (key === \"r\") rows = numberValue;\n\t}\n\treturn { ids, rows };\n}\n\nfunction extractKittyImageIds(line: string): number[] {\n\treturn parseKittyImageHeader(line)?.ids ?? [];\n}\n\nfunction extractKittyImageRows(line: string): number {\n\treturn parseKittyImageHeader(line)?.rows ?? 1;\n}\n\nfunction isTermuxSession(): boolean {\n\treturn Boolean(process.env.TERMUX_VERSION);\n}\n\nexport interface TuiMainScreenRenderState {\n\tpreviousLines: string[];\n\tpreviousWidth: number;\n\tpreviousHeight: number;\n\tcursorRow: number;\n\thardwareCursorRow: number;\n\tmaxLinesRendered: number;\n\tpreviousViewportTop: number;\n}\n\n/** TUI implementation that renders into the terminal's main screen and scrollback. */\nexport class TuiMainScreen extends TuiBase implements TUI {\n\treadonly mode = \"regular\" as const;\n\tprivate previousLines: string[] = [];\n\tprivate previousKittyImageIds = new Set<number>();\n\tprivate previousWidth = 0;\n\tprivate previousHeight = 0;\n\tprivate cursorRow = 0;\n\tprivate hardwareCursorRow = 0;\n\tprivate maxLinesRendered = 0;\n\tprivate previousViewportTop = 0;\n\n\tcaptureRenderState(): TuiMainScreenRenderState {\n\t\treturn {\n\t\t\tpreviousLines: [...this.previousLines],\n\t\t\tpreviousWidth: this.previousWidth,\n\t\t\tpreviousHeight: this.previousHeight,\n\t\t\tcursorRow: this.cursorRow,\n\t\t\thardwareCursorRow: this.hardwareCursorRow,\n\t\t\tmaxLinesRendered: this.maxLinesRendered,\n\t\t\tpreviousViewportTop: this.previousViewportTop,\n\t\t};\n\t}\n\n\trestoreRenderState(state: TuiMainScreenRenderState): void {\n\t\tthis.previousLines = state.previousLines.map((line) => (isImageLine(line) ? \"\" : line));\n\t\tthis.previousKittyImageIds = new Set();\n\t\tthis.previousWidth = state.previousWidth;\n\t\tthis.previousHeight = state.previousHeight;\n\t\tthis.cursorRow = state.cursorRow;\n\t\tthis.hardwareCursorRow = state.hardwareCursorRow;\n\t\tthis.maxLinesRendered = state.maxLinesRendered;\n\t\tthis.previousViewportTop = state.previousViewportTop;\n\t}\n\n\tprotected override resetRenderState(): void {\n\t\tthis.previousLines = [];\n\t\tthis.previousWidth = -1;\n\t\tthis.previousHeight = -1;\n\t\tthis.cursorRow = 0;\n\t\tthis.hardwareCursorRow = 0;\n\t\tthis.maxLinesRendered = 0;\n\t\tthis.previousViewportTop = 0;\n\t}\n\n\tprotected override beforeTerminalStop(options: TuiStopOptions): void {\n\t\tif (options.preserveScreen || this.previousLines.length === 0) return;\n\t\tthis.terminal.write(\" \");\n\t\tconst targetRow = this.previousLines.length;\n\t\tconst lineDiff = targetRow - this.hardwareCursorRow;\n\t\tif (lineDiff > 0) this.terminal.write(`\\x1b[${lineDiff}B`);\n\t\telse if (lineDiff < 0) this.terminal.write(`\\x1b[${-lineDiff}A`);\n\t\tthis.terminal.write(\"\\r\\n\");\n\t}\n\n\tprivate collectKittyImageIds(lines: string[]): Set<number> {\n\t\tconst ids = new Set<number>();\n\t\tfor (const line of lines) {\n\t\t\tfor (const id of extractKittyImageIds(line)) {\n\t\t\t\tids.add(id);\n\t\t\t}\n\t\t}\n\t\treturn ids;\n\t}\n\n\tprivate deleteKittyImages(ids: Iterable<number>): string {\n\t\tlet buffer = \"\";\n\t\tfor (const id of ids) {\n\t\t\tbuffer += deleteKittyImage(id);\n\t\t}\n\t\treturn buffer;\n\t}\n\n\tprivate getKittyImageReservedRows(lines: string[], index: number, maxIndex = lines.length - 1): number {\n\t\tconst rows = extractKittyImageRows(lines[index] ?? \"\");\n\t\tif (rows <= 1) return 1;\n\n\t\tconst maxRows = Math.min(rows, maxIndex - index + 1, lines.length - index);\n\t\tlet reservedRows = 1;\n\t\twhile (reservedRows < maxRows) {\n\t\t\tconst line = lines[index + reservedRows] ?? \"\";\n\t\t\tif (isImageLine(line) || visibleWidth(line) > 0) break;\n\t\t\treservedRows++;\n\t\t}\n\t\treturn reservedRows;\n\t}\n\n\tprivate expandChangedRangeForKittyImages(\n\t\tfirstChanged: number,\n\t\tlastChanged: number,\n\t\tnewLines: string[],\n\t): { firstChanged: number; lastChanged: number } {\n\t\tlet expandedFirstChanged = firstChanged;\n\t\tlet expandedLastChanged = lastChanged;\n\t\tconst expandForLines = (lines: string[]): void => {\n\t\t\tfor (let i = 0; i < lines.length; i++) {\n\t\t\t\tif (extractKittyImageIds(lines[i]).length === 0) continue;\n\t\t\t\tconst blockEnd = i + this.getKittyImageReservedRows(lines, i) - 1;\n\t\t\t\tif (i >= firstChanged || (i <= lastChanged && blockEnd >= firstChanged)) {\n\t\t\t\t\texpandedFirstChanged = Math.min(expandedFirstChanged, i);\n\t\t\t\t\texpandedLastChanged = Math.max(expandedLastChanged, blockEnd);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\texpandForLines(this.previousLines);\n\t\texpandForLines(newLines);\n\t\treturn { firstChanged: expandedFirstChanged, lastChanged: expandedLastChanged };\n\t}\n\n\tprivate deleteChangedKittyImages(firstChanged: number, lastChanged: number): string {\n\t\tif (firstChanged < 0 || lastChanged < firstChanged) return \"\";\n\n\t\tconst ids = new Set<number>();\n\t\tconst maxLine = Math.min(lastChanged, this.previousLines.length - 1);\n\t\tfor (let i = firstChanged; i <= maxLine; i++) {\n\t\t\tfor (const id of extractKittyImageIds(this.previousLines[i] ?? \"\")) {\n\t\t\t\tids.add(id);\n\t\t\t}\n\t\t}\n\n\t\treturn this.deleteKittyImages(ids);\n\t}\n\n\tprotected doRender(): void {\n\t\tif (this.stopped) return;\n\t\tconst width = this.terminal.columns;\n\t\tconst height = this.terminal.rows;\n\t\tconst widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;\n\t\tconst heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;\n\t\tconst previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;\n\t\tlet prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;\n\t\tlet viewportTop = prevViewportTop;\n\t\tlet hardwareCursorRow = this.hardwareCursorRow;\n\t\tconst computeLineDiff = (targetRow: number): number => {\n\t\t\tconst currentScreenRow = hardwareCursorRow - prevViewportTop;\n\t\t\tconst targetScreenRow = targetRow - viewportTop;\n\t\t\treturn targetScreenRow - currentScreenRow;\n\t\t};\n\n\t\t// Render all components to get new lines\n\t\tlet newLines = this.render(width);\n\n\t\t// Composite overlays into the rendered lines (before differential compare)\n\t\tif (this.hasOverlayEntries) {\n\t\t\tnewLines = this.compositeOverlays(newLines, width, height);\n\t\t}\n\n\t\t// Extract cursor position before applying line resets (marker must be found first)\n\t\tconst cursorPos = this.extractCursorPosition(newLines, height);\n\n\t\tnewLines = this.applyLineResets(newLines);\n\n\t\t// Helper to clear scrollback and viewport and render all new lines\n\t\tconst fullRender = (clear: boolean): void => {\n\t\t\tthis.fullRedrawCount += 1;\n\t\t\tconst output = new BoundedTerminalWriter((data) => this.terminal.write(data));\n\t\t\toutput.append(\"\\x1b[?2026h\"); // Begin synchronized output\n\t\t\tif (clear) {\n\t\t\t\toutput.append(this.deleteKittyImages(this.previousKittyImageIds));\n\t\t\t\toutput.append(\"\\x1b[2J\\x1b[H\\x1b[3J\"); // Clear screen, home, then clear scrollback\n\t\t\t}\n\t\t\tfor (let i = 0; i < newLines.length; i++) {\n\t\t\t\tif (i > 0) output.append(\"\\r\\n\");\n\t\t\t\tconst line = newLines[i];\n\t\t\t\tconst isImage = isImageLine(line);\n\t\t\t\tconst imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;\n\t\t\t\tif (imageReservedRows > 1 && imageReservedRows <= height) {\n\t\t\t\t\tfor (let row = 1; row < imageReservedRows; row++) {\n\t\t\t\t\t\toutput.append(\"\\r\\n\");\n\t\t\t\t\t}\n\t\t\t\t\toutput.append(`\\x1b[${imageReservedRows - 1}A`);\n\t\t\t\t\toutput.append(line);\n\t\t\t\t\toutput.append(`\\x1b[${imageReservedRows - 1}B`);\n\t\t\t\t\ti += imageReservedRows - 1;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\toutput.append(line);\n\t\t\t}\n\t\t\toutput.append(\"\\x1b[?2026l\"); // End synchronized output\n\t\t\toutput.flush();\n\t\t\tthis.cursorRow = Math.max(0, newLines.length - 1);\n\t\t\tthis.hardwareCursorRow = this.cursorRow;\n\t\t\t// Reset max lines when clearing, otherwise track growth\n\t\t\tif (clear) {\n\t\t\t\tthis.maxLinesRendered = newLines.length;\n\t\t\t} else {\n\t\t\t\tthis.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);\n\t\t\t}\n\t\t\tconst bufferLength = Math.max(height, newLines.length);\n\t\t\tthis.previousViewportTop = Math.max(0, bufferLength - height);\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousLines = newLines;\n\t\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\t\tthis.previousWidth = width;\n\t\t\tthis.previousHeight = height;\n\t\t};\n\n\t\tconst debugRedraw = process.env.PI_DEBUG_REDRAW === \"1\";\n\t\tconst logRedraw = (reason: string): void => {\n\t\t\tif (!debugRedraw) return;\n\t\t\tconst logPath = path.join(this.logDirectory, \"pi-debug.log\");\n\t\t\tconst msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\\n`;\n\t\t\tfs.mkdirSync(path.dirname(logPath), { recursive: true });\n\t\t\tfs.appendFileSync(logPath, msg);\n\t\t};\n\n\t\t// First render - just output everything without clearing (assumes clean screen)\n\t\tif (this.previousLines.length === 0 && !widthChanged && !heightChanged) {\n\t\t\tlogRedraw(\"first render\");\n\t\t\tfullRender(false);\n\t\t\treturn;\n\t\t}\n\n\t\t// Width changes always need a full re-render because wrapping changes.\n\t\tif (widthChanged) {\n\t\t\tlogRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Height changes normally need a full re-render to keep the visible viewport aligned,\n\t\t// but Termux changes height when the software keyboard shows or hides.\n\t\t// In that environment, a full redraw causes the entire history to replay on every toggle.\n\t\tif (heightChanged && !isTermuxSession()) {\n\t\t\tlogRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Content shrunk below the working area and no overlays - re-render to clear empty rows\n\t\t// (overlays need the padding, so only do this when no overlays are active)\n\t\t// Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var\n\t\tif (this.getClearOnShrink() && newLines.length < this.maxLinesRendered && !this.hasOverlayEntries) {\n\t\t\tlogRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Find first and last changed lines\n\t\tlet firstChanged = -1;\n\t\tlet lastChanged = -1;\n\t\tconst maxLines = Math.max(newLines.length, this.previousLines.length);\n\t\tfor (let i = 0; i < maxLines; i++) {\n\t\t\tconst oldLine = i < this.previousLines.length ? this.previousLines[i] : \"\";\n\t\t\tconst newLine = i < newLines.length ? newLines[i] : \"\";\n\n\t\t\tif (oldLine !== newLine) {\n\t\t\t\tif (firstChanged === -1) {\n\t\t\t\t\tfirstChanged = i;\n\t\t\t\t}\n\t\t\t\tlastChanged = i;\n\t\t\t}\n\t\t}\n\t\tconst appendedLines = newLines.length > this.previousLines.length;\n\t\tif (appendedLines) {\n\t\t\tif (firstChanged === -1) {\n\t\t\t\tfirstChanged = this.previousLines.length;\n\t\t\t}\n\t\t\tlastChanged = newLines.length - 1;\n\t\t}\n\t\tif (firstChanged !== -1) {\n\t\t\tconst expandedRange = this.expandChangedRangeForKittyImages(firstChanged, lastChanged, newLines);\n\t\t\tfirstChanged = expandedRange.firstChanged;\n\t\t\tlastChanged = expandedRange.lastChanged;\n\t\t}\n\t\tconst appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;\n\n\t\t// No changes - but still need to update hardware cursor position if it moved\n\t\tif (firstChanged === -1) {\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousViewportTop = prevViewportTop;\n\t\t\tthis.previousHeight = height;\n\t\t\treturn;\n\t\t}\n\n\t\t// All changes are in deleted lines (nothing to render, just clear)\n\t\tif (firstChanged >= newLines.length) {\n\t\t\tif (this.previousLines.length > newLines.length) {\n\t\t\t\tconst output = new BoundedTerminalWriter((data) => this.terminal.write(data));\n\t\t\t\toutput.append(\"\\x1b[?2026h\");\n\t\t\t\toutput.append(this.deleteChangedKittyImages(firstChanged, lastChanged));\n\t\t\t\t// Move to end of new content (clamp to 0 for empty content)\n\t\t\t\tconst targetRow = Math.max(0, newLines.length - 1);\n\t\t\t\tif (targetRow < prevViewportTop) {\n\t\t\t\t\tlogRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst lineDiff = computeLineDiff(targetRow);\n\t\t\t\tif (lineDiff > 0) output.append(`\\x1b[${lineDiff}B`);\n\t\t\t\telse if (lineDiff < 0) output.append(`\\x1b[${-lineDiff}A`);\n\t\t\t\toutput.append(\"\\r\");\n\t\t\t\t// Clear extra lines without scrolling\n\t\t\t\tconst extraLines = this.previousLines.length - newLines.length;\n\t\t\t\tif (extraLines > height) {\n\t\t\t\t\tlogRedraw(`extraLines > height (${extraLines} > ${height})`);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst clearStartOffset = newLines.length === 0 ? 0 : 1;\n\t\t\t\tif (extraLines > 0 && clearStartOffset > 0) {\n\t\t\t\t\toutput.append(`\\x1b[${clearStartOffset}B`);\n\t\t\t\t}\n\t\t\t\tfor (let i = 0; i < extraLines; i++) {\n\t\t\t\t\toutput.append(\"\\r\\x1b[2K\");\n\t\t\t\t\tif (i < extraLines - 1) output.append(\"\\x1b[1B\");\n\t\t\t\t}\n\t\t\t\tconst moveBack = Math.max(0, extraLines - 1 + clearStartOffset);\n\t\t\t\tif (moveBack > 0) {\n\t\t\t\t\toutput.append(`\\x1b[${moveBack}A`);\n\t\t\t\t}\n\t\t\t\toutput.append(\"\\x1b[?2026l\");\n\t\t\t\toutput.flush();\n\t\t\t\tthis.cursorRow = targetRow;\n\t\t\t\tthis.hardwareCursorRow = targetRow;\n\t\t\t}\n\t\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\t\t\tthis.previousLines = newLines;\n\t\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\t\tthis.previousWidth = width;\n\t\t\tthis.previousHeight = height;\n\t\t\tthis.previousViewportTop = prevViewportTop;\n\t\t\treturn;\n\t\t}\n\n\t\t// Differential rendering can only touch what was actually visible.\n\t\t// If the first changed line is above the previous viewport, we need a full redraw.\n\t\tif (firstChanged < prevViewportTop) {\n\t\t\tlogRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);\n\t\t\tfullRender(true);\n\t\t\treturn;\n\t\t}\n\n\t\t// Render from first changed line to end\n\t\t// Keep updates wrapped in synchronized output while writing bounded chunks.\n\t\tconst output = new BoundedTerminalWriter((data) => this.terminal.write(data));\n\t\toutput.append(\"\\x1b[?2026h\"); // Begin synchronized output\n\t\toutput.append(this.deleteChangedKittyImages(firstChanged, lastChanged));\n\t\tconst prevViewportBottom = prevViewportTop + height - 1;\n\t\tconst moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;\n\t\tif (moveTargetRow > prevViewportBottom) {\n\t\t\tconst currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));\n\t\t\tconst moveToBottom = height - 1 - currentScreenRow;\n\t\t\tif (moveToBottom > 0) {\n\t\t\t\toutput.append(`\\x1b[${moveToBottom}B`);\n\t\t\t}\n\t\t\tconst scroll = moveTargetRow - prevViewportBottom;\n\t\t\toutput.append(\"\\r\\n\".repeat(scroll));\n\t\t\tprevViewportTop += scroll;\n\t\t\tviewportTop += scroll;\n\t\t\thardwareCursorRow = moveTargetRow;\n\t\t}\n\n\t\t// Move cursor to first changed line (use hardwareCursorRow for actual position)\n\t\tconst lineDiff = computeLineDiff(moveTargetRow);\n\t\tif (lineDiff > 0) {\n\t\t\toutput.append(`\\x1b[${lineDiff}B`); // Move down\n\t\t} else if (lineDiff < 0) {\n\t\t\toutput.append(`\\x1b[${-lineDiff}A`); // Move up\n\t\t}\n\n\t\toutput.append(appendStart ? \"\\r\\n\" : \"\\r\"); // Move to column 0\n\n\t\t// Only render changed lines (firstChanged to lastChanged), not all lines to end\n\t\t// This reduces flicker when only a single line changes (e.g., spinner animation)\n\t\tconst renderEnd = Math.min(lastChanged, newLines.length - 1);\n\t\tfor (let i = firstChanged; i <= renderEnd; i++) {\n\t\t\tif (i > firstChanged) output.append(\"\\r\\n\");\n\t\t\tconst line = newLines[i];\n\t\t\tconst isImage = isImageLine(line);\n\t\t\tconst imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;\n\t\t\tif (imageReservedRows > 1) {\n\t\t\t\tconst imageStartScreenRow = i - viewportTop;\n\t\t\t\tif (imageStartScreenRow < 0 || imageStartScreenRow + imageReservedRows > height) {\n\t\t\t\t\tlogRedraw(\n\t\t\t\t\t\t`kitty image pre-clear would scroll (${imageStartScreenRow} + ${imageReservedRows} > ${height})`,\n\t\t\t\t\t);\n\t\t\t\t\tfullRender(true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\toutput.append(\"\\x1b[2K\");\n\t\t\t\tfor (let row = 1; row < imageReservedRows; row++) {\n\t\t\t\t\toutput.append(\"\\r\\n\\x1b[2K\");\n\t\t\t\t}\n\t\t\t\toutput.append(`\\x1b[${imageReservedRows - 1}A`);\n\t\t\t\toutput.append(line);\n\t\t\t\toutput.append(`\\x1b[${imageReservedRows - 1}B`);\n\t\t\t\ti += imageReservedRows - 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\toutput.append(\"\\x1b[2K\"); // Clear current line\n\t\t\tif (!isImage && visibleWidth(line) > width) {\n\t\t\t\t// Log all lines to crash file for debugging\n\t\t\t\tconst crashLogPath = path.join(this.logDirectory, \"pi-crash.log\");\n\t\t\t\tconst crashData = [\n\t\t\t\t\t`Crash at ${new Date().toISOString()}`,\n\t\t\t\t\t`Terminal width: ${width}`,\n\t\t\t\t\t`Line ${i} visible width: ${visibleWidth(line)}`,\n\t\t\t\t\t\"\",\n\t\t\t\t\t\"=== All rendered lines ===\",\n\t\t\t\t\t...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),\n\t\t\t\t\t\"\",\n\t\t\t\t].join(\"\\n\");\n\t\t\t\tfs.mkdirSync(path.dirname(crashLogPath), { recursive: true });\n\t\t\t\tfs.writeFileSync(crashLogPath, crashData);\n\n\t\t\t\t// Clean up terminal state before throwing\n\t\t\t\tthis.stop();\n\n\t\t\t\tconst errorMsg = [\n\t\t\t\t\t`Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,\n\t\t\t\t\t\"\",\n\t\t\t\t\t\"This is likely caused by a custom TUI component not truncating its output.\",\n\t\t\t\t\t\"Use visibleWidth() to measure and truncateToWidth() to truncate lines.\",\n\t\t\t\t\t\"\",\n\t\t\t\t\t`Debug log written to: ${crashLogPath}`,\n\t\t\t\t].join(\"\\n\");\n\t\t\t\tthrow new Error(errorMsg);\n\t\t\t}\n\t\t\toutput.append(line);\n\t\t}\n\n\t\t// Track where cursor ended up after rendering\n\t\tlet finalCursorRow = renderEnd;\n\n\t\t// If we had more lines before, clear them and move cursor back\n\t\tif (this.previousLines.length > newLines.length) {\n\t\t\t// Move to end of new content first if we stopped before it\n\t\t\tif (renderEnd < newLines.length - 1) {\n\t\t\t\tconst moveDown = newLines.length - 1 - renderEnd;\n\t\t\t\toutput.append(`\\x1b[${moveDown}B`);\n\t\t\t\tfinalCursorRow = newLines.length - 1;\n\t\t\t}\n\t\t\tconst extraLines = this.previousLines.length - newLines.length;\n\t\t\tfor (let i = newLines.length; i < this.previousLines.length; i++) {\n\t\t\t\toutput.append(\"\\r\\n\\x1b[2K\");\n\t\t\t}\n\t\t\t// Move cursor back to end of new content\n\t\t\toutput.append(`\\x1b[${extraLines}A`);\n\t\t}\n\n\t\toutput.append(\"\\x1b[?2026l\"); // End synchronized output\n\n\t\tif (process.env.PI_TUI_DEBUG === \"1\") {\n\t\t\tconst debugDir = \"/tmp/tui\";\n\t\t\tfs.mkdirSync(debugDir, { recursive: true });\n\t\t\tconst debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);\n\t\t\tconst debugData = [\n\t\t\t\t`firstChanged: ${firstChanged}`,\n\t\t\t\t`viewportTop: ${viewportTop}`,\n\t\t\t\t`cursorRow: ${this.cursorRow}`,\n\t\t\t\t`height: ${height}`,\n\t\t\t\t`lineDiff: ${lineDiff}`,\n\t\t\t\t`hardwareCursorRow: ${hardwareCursorRow}`,\n\t\t\t\t`renderEnd: ${renderEnd}`,\n\t\t\t\t`finalCursorRow: ${finalCursorRow}`,\n\t\t\t\t`cursorPos: ${JSON.stringify(cursorPos)}`,\n\t\t\t\t`newLines.length: ${newLines.length}`,\n\t\t\t\t`previousLines.length: ${this.previousLines.length}`,\n\t\t\t\t\"\",\n\t\t\t\t\"=== newLines ===\",\n\t\t\t\tJSON.stringify(newLines, null, 2),\n\t\t\t\t\"\",\n\t\t\t\t\"=== previousLines ===\",\n\t\t\t\tJSON.stringify(this.previousLines, null, 2),\n\t\t\t\t\"\",\n\t\t\t\t\"=== buffer ===\",\n\t\t\t\t`[${output.length} chars written in bounded chunks]`,\n\t\t\t].join(\"\\n\");\n\t\t\tfs.writeFileSync(debugPath, debugData);\n\t\t}\n\n\t\toutput.flush();\n\n\t\t// Track cursor position for next render\n\t\t// cursorRow tracks end of content (for viewport calculation)\n\t\t// hardwareCursorRow tracks actual terminal cursor position (for movement)\n\t\tthis.cursorRow = Math.max(0, newLines.length - 1);\n\t\tthis.hardwareCursorRow = finalCursorRow;\n\t\t// Track terminal's working area (grows but doesn't shrink unless cleared)\n\t\tthis.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);\n\t\tthis.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);\n\n\t\t// Position hardware cursor for IME\n\t\tthis.positionHardwareCursor(cursorPos, newLines.length);\n\n\t\tthis.previousLines = newLines;\n\t\tthis.previousKittyImageIds = this.collectKittyImageIds(newLines);\n\t\tthis.previousWidth = width;\n\t\tthis.previousHeight = height;\n\t}\n\n\t/**\n\t * Position the hardware cursor for IME candidate window.\n\t * @param cursorPos The cursor position extracted from rendered output, or null\n\t * @param totalLines Total number of rendered lines\n\t */\n\tprivate positionHardwareCursor(cursorPos: { row: number; col: number } | null, totalLines: number): void {\n\t\tif (!cursorPos || totalLines <= 0) {\n\t\t\tthis.terminal.hideCursor();\n\t\t\treturn;\n\t\t}\n\n\t\t// Clamp cursor position to valid range\n\t\tconst targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));\n\t\tconst targetCol = Math.max(0, cursorPos.col);\n\n\t\t// Move cursor from current position to target\n\t\tconst rowDelta = targetRow - this.hardwareCursorRow;\n\t\tlet buffer = \"\";\n\t\tif (rowDelta > 0) {\n\t\t\tbuffer += `\\x1b[${rowDelta}B`; // Move down\n\t\t} else if (rowDelta < 0) {\n\t\t\tbuffer += `\\x1b[${-rowDelta}A`; // Move up\n\t\t}\n\t\t// Move to absolute column (1-indexed)\n\t\tbuffer += `\\x1b[${targetCol + 1}G`;\n\n\t\tif (buffer) {\n\t\t\tthis.terminal.write(buffer);\n\t\t}\n\n\t\tthis.hardwareCursorRow = targetRow;\n\t\tif (this.getShowHardwareCursor()) {\n\t\t\tthis.terminal.showCursor();\n\t\t} else {\n\t\t\tthis.terminal.hideCursor();\n\t\t}\n\t}\n}\n"]}
|
|
@@ -4,6 +4,64 @@ import { deleteKittyImage, isImageLine } from "./terminal-image.js";
|
|
|
4
4
|
import { TuiBase } from "./tui.js";
|
|
5
5
|
import { visibleWidth } from "./utils.js";
|
|
6
6
|
const KITTY_SEQUENCE_PREFIX = "\x1b_G";
|
|
7
|
+
const MAX_RENDER_WRITE_CHARS = 1024 * 1024;
|
|
8
|
+
/**
|
|
9
|
+
* Streams terminal output in 1 MiB chunks so a full render never forms one string large enough to exceed V8's limit.
|
|
10
|
+
*
|
|
11
|
+
* `append()` fills the current chunk and flushes it when full. Oversized input is split at chunk boundaries, preserving
|
|
12
|
+
* surrogate pairs so each write remains valid UTF-16. Callers append synchronized-output begin/end sequences themselves;
|
|
13
|
+
* the final `flush()` writes any remainder, including the end sequence.
|
|
14
|
+
*/
|
|
15
|
+
class BoundedTerminalWriter {
|
|
16
|
+
buffer = "";
|
|
17
|
+
writtenChars = 0;
|
|
18
|
+
write;
|
|
19
|
+
constructor(write) {
|
|
20
|
+
this.write = write;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Append terminal data, flushing full chunks as needed. Callers must call `flush()` after the final append.
|
|
24
|
+
* @param value Terminal data to write in order; oversized values are split without splitting surrogate pairs.
|
|
25
|
+
*/
|
|
26
|
+
append(value) {
|
|
27
|
+
let offset = 0;
|
|
28
|
+
while (offset < value.length) {
|
|
29
|
+
const capacity = MAX_RENDER_WRITE_CHARS - this.buffer.length;
|
|
30
|
+
if (capacity === 0) {
|
|
31
|
+
this.flush();
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let end = Math.min(value.length, offset + capacity);
|
|
35
|
+
if (end < value.length &&
|
|
36
|
+
value.charCodeAt(end - 1) >= 0xd800 &&
|
|
37
|
+
value.charCodeAt(end - 1) <= 0xdbff &&
|
|
38
|
+
value.charCodeAt(end) >= 0xdc00 &&
|
|
39
|
+
value.charCodeAt(end) <= 0xdfff) {
|
|
40
|
+
end--;
|
|
41
|
+
}
|
|
42
|
+
if (end === offset) {
|
|
43
|
+
this.flush();
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
this.buffer += value.slice(offset, end);
|
|
47
|
+
offset = end;
|
|
48
|
+
if (this.buffer.length === MAX_RENDER_WRITE_CHARS) {
|
|
49
|
+
this.flush();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Write the current chunk, if any, and retain only its character count for debug output. */
|
|
54
|
+
flush() {
|
|
55
|
+
if (!this.buffer)
|
|
56
|
+
return;
|
|
57
|
+
this.write(this.buffer);
|
|
58
|
+
this.writtenChars += this.buffer.length;
|
|
59
|
+
this.buffer = "";
|
|
60
|
+
}
|
|
61
|
+
get length() {
|
|
62
|
+
return this.writtenChars + this.buffer.length;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
7
65
|
function parseKittyImageHeader(line) {
|
|
8
66
|
const sequenceStart = line.indexOf(KITTY_SEQUENCE_PREFIX);
|
|
9
67
|
if (sequenceStart === -1)
|
|
@@ -178,31 +236,32 @@ export class TuiMainScreen extends TuiBase {
|
|
|
178
236
|
// Helper to clear scrollback and viewport and render all new lines
|
|
179
237
|
const fullRender = (clear) => {
|
|
180
238
|
this.fullRedrawCount += 1;
|
|
181
|
-
|
|
239
|
+
const output = new BoundedTerminalWriter((data) => this.terminal.write(data));
|
|
240
|
+
output.append("\x1b[?2026h"); // Begin synchronized output
|
|
182
241
|
if (clear) {
|
|
183
|
-
|
|
184
|
-
|
|
242
|
+
output.append(this.deleteKittyImages(this.previousKittyImageIds));
|
|
243
|
+
output.append("\x1b[2J\x1b[H\x1b[3J"); // Clear screen, home, then clear scrollback
|
|
185
244
|
}
|
|
186
245
|
for (let i = 0; i < newLines.length; i++) {
|
|
187
246
|
if (i > 0)
|
|
188
|
-
|
|
247
|
+
output.append("\r\n");
|
|
189
248
|
const line = newLines[i];
|
|
190
249
|
const isImage = isImageLine(line);
|
|
191
250
|
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i) : 1;
|
|
192
251
|
if (imageReservedRows > 1 && imageReservedRows <= height) {
|
|
193
252
|
for (let row = 1; row < imageReservedRows; row++) {
|
|
194
|
-
|
|
253
|
+
output.append("\r\n");
|
|
195
254
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
255
|
+
output.append(`\x1b[${imageReservedRows - 1}A`);
|
|
256
|
+
output.append(line);
|
|
257
|
+
output.append(`\x1b[${imageReservedRows - 1}B`);
|
|
199
258
|
i += imageReservedRows - 1;
|
|
200
259
|
continue;
|
|
201
260
|
}
|
|
202
|
-
|
|
261
|
+
output.append(line);
|
|
203
262
|
}
|
|
204
|
-
|
|
205
|
-
|
|
263
|
+
output.append("\x1b[?2026l"); // End synchronized output
|
|
264
|
+
output.flush();
|
|
206
265
|
this.cursorRow = Math.max(0, newLines.length - 1);
|
|
207
266
|
this.hardwareCursorRow = this.cursorRow;
|
|
208
267
|
// Reset max lines when clearing, otherwise track growth
|
|
@@ -294,8 +353,9 @@ export class TuiMainScreen extends TuiBase {
|
|
|
294
353
|
// All changes are in deleted lines (nothing to render, just clear)
|
|
295
354
|
if (firstChanged >= newLines.length) {
|
|
296
355
|
if (this.previousLines.length > newLines.length) {
|
|
297
|
-
|
|
298
|
-
|
|
356
|
+
const output = new BoundedTerminalWriter((data) => this.terminal.write(data));
|
|
357
|
+
output.append("\x1b[?2026h");
|
|
358
|
+
output.append(this.deleteChangedKittyImages(firstChanged, lastChanged));
|
|
299
359
|
// Move to end of new content (clamp to 0 for empty content)
|
|
300
360
|
const targetRow = Math.max(0, newLines.length - 1);
|
|
301
361
|
if (targetRow < prevViewportTop) {
|
|
@@ -305,10 +365,10 @@ export class TuiMainScreen extends TuiBase {
|
|
|
305
365
|
}
|
|
306
366
|
const lineDiff = computeLineDiff(targetRow);
|
|
307
367
|
if (lineDiff > 0)
|
|
308
|
-
|
|
368
|
+
output.append(`\x1b[${lineDiff}B`);
|
|
309
369
|
else if (lineDiff < 0)
|
|
310
|
-
|
|
311
|
-
|
|
370
|
+
output.append(`\x1b[${-lineDiff}A`);
|
|
371
|
+
output.append("\r");
|
|
312
372
|
// Clear extra lines without scrolling
|
|
313
373
|
const extraLines = this.previousLines.length - newLines.length;
|
|
314
374
|
if (extraLines > height) {
|
|
@@ -318,19 +378,19 @@ export class TuiMainScreen extends TuiBase {
|
|
|
318
378
|
}
|
|
319
379
|
const clearStartOffset = newLines.length === 0 ? 0 : 1;
|
|
320
380
|
if (extraLines > 0 && clearStartOffset > 0) {
|
|
321
|
-
|
|
381
|
+
output.append(`\x1b[${clearStartOffset}B`);
|
|
322
382
|
}
|
|
323
383
|
for (let i = 0; i < extraLines; i++) {
|
|
324
|
-
|
|
384
|
+
output.append("\r\x1b[2K");
|
|
325
385
|
if (i < extraLines - 1)
|
|
326
|
-
|
|
386
|
+
output.append("\x1b[1B");
|
|
327
387
|
}
|
|
328
388
|
const moveBack = Math.max(0, extraLines - 1 + clearStartOffset);
|
|
329
389
|
if (moveBack > 0) {
|
|
330
|
-
|
|
390
|
+
output.append(`\x1b[${moveBack}A`);
|
|
331
391
|
}
|
|
332
|
-
|
|
333
|
-
|
|
392
|
+
output.append("\x1b[?2026l");
|
|
393
|
+
output.flush();
|
|
334
394
|
this.cursorRow = targetRow;
|
|
335
395
|
this.hardwareCursorRow = targetRow;
|
|
336
396
|
}
|
|
@@ -350,19 +410,20 @@ export class TuiMainScreen extends TuiBase {
|
|
|
350
410
|
return;
|
|
351
411
|
}
|
|
352
412
|
// Render from first changed line to end
|
|
353
|
-
//
|
|
354
|
-
|
|
355
|
-
|
|
413
|
+
// Keep updates wrapped in synchronized output while writing bounded chunks.
|
|
414
|
+
const output = new BoundedTerminalWriter((data) => this.terminal.write(data));
|
|
415
|
+
output.append("\x1b[?2026h"); // Begin synchronized output
|
|
416
|
+
output.append(this.deleteChangedKittyImages(firstChanged, lastChanged));
|
|
356
417
|
const prevViewportBottom = prevViewportTop + height - 1;
|
|
357
418
|
const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
|
|
358
419
|
if (moveTargetRow > prevViewportBottom) {
|
|
359
420
|
const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
|
|
360
421
|
const moveToBottom = height - 1 - currentScreenRow;
|
|
361
422
|
if (moveToBottom > 0) {
|
|
362
|
-
|
|
423
|
+
output.append(`\x1b[${moveToBottom}B`);
|
|
363
424
|
}
|
|
364
425
|
const scroll = moveTargetRow - prevViewportBottom;
|
|
365
|
-
|
|
426
|
+
output.append("\r\n".repeat(scroll));
|
|
366
427
|
prevViewportTop += scroll;
|
|
367
428
|
viewportTop += scroll;
|
|
368
429
|
hardwareCursorRow = moveTargetRow;
|
|
@@ -370,18 +431,18 @@ export class TuiMainScreen extends TuiBase {
|
|
|
370
431
|
// Move cursor to first changed line (use hardwareCursorRow for actual position)
|
|
371
432
|
const lineDiff = computeLineDiff(moveTargetRow);
|
|
372
433
|
if (lineDiff > 0) {
|
|
373
|
-
|
|
434
|
+
output.append(`\x1b[${lineDiff}B`); // Move down
|
|
374
435
|
}
|
|
375
436
|
else if (lineDiff < 0) {
|
|
376
|
-
|
|
437
|
+
output.append(`\x1b[${-lineDiff}A`); // Move up
|
|
377
438
|
}
|
|
378
|
-
|
|
439
|
+
output.append(appendStart ? "\r\n" : "\r"); // Move to column 0
|
|
379
440
|
// Only render changed lines (firstChanged to lastChanged), not all lines to end
|
|
380
441
|
// This reduces flicker when only a single line changes (e.g., spinner animation)
|
|
381
442
|
const renderEnd = Math.min(lastChanged, newLines.length - 1);
|
|
382
443
|
for (let i = firstChanged; i <= renderEnd; i++) {
|
|
383
444
|
if (i > firstChanged)
|
|
384
|
-
|
|
445
|
+
output.append("\r\n");
|
|
385
446
|
const line = newLines[i];
|
|
386
447
|
const isImage = isImageLine(line);
|
|
387
448
|
const imageReservedRows = isImage ? this.getKittyImageReservedRows(newLines, i, renderEnd) : 1;
|
|
@@ -392,17 +453,17 @@ export class TuiMainScreen extends TuiBase {
|
|
|
392
453
|
fullRender(true);
|
|
393
454
|
return;
|
|
394
455
|
}
|
|
395
|
-
|
|
456
|
+
output.append("\x1b[2K");
|
|
396
457
|
for (let row = 1; row < imageReservedRows; row++) {
|
|
397
|
-
|
|
458
|
+
output.append("\r\n\x1b[2K");
|
|
398
459
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
460
|
+
output.append(`\x1b[${imageReservedRows - 1}A`);
|
|
461
|
+
output.append(line);
|
|
462
|
+
output.append(`\x1b[${imageReservedRows - 1}B`);
|
|
402
463
|
i += imageReservedRows - 1;
|
|
403
464
|
continue;
|
|
404
465
|
}
|
|
405
|
-
|
|
466
|
+
output.append("\x1b[2K"); // Clear current line
|
|
406
467
|
if (!isImage && visibleWidth(line) > width) {
|
|
407
468
|
// Log all lines to crash file for debugging
|
|
408
469
|
const crashLogPath = path.join(this.logDirectory, "pi-crash.log");
|
|
@@ -429,7 +490,7 @@ export class TuiMainScreen extends TuiBase {
|
|
|
429
490
|
].join("\n");
|
|
430
491
|
throw new Error(errorMsg);
|
|
431
492
|
}
|
|
432
|
-
|
|
493
|
+
output.append(line);
|
|
433
494
|
}
|
|
434
495
|
// Track where cursor ended up after rendering
|
|
435
496
|
let finalCursorRow = renderEnd;
|
|
@@ -438,17 +499,17 @@ export class TuiMainScreen extends TuiBase {
|
|
|
438
499
|
// Move to end of new content first if we stopped before it
|
|
439
500
|
if (renderEnd < newLines.length - 1) {
|
|
440
501
|
const moveDown = newLines.length - 1 - renderEnd;
|
|
441
|
-
|
|
502
|
+
output.append(`\x1b[${moveDown}B`);
|
|
442
503
|
finalCursorRow = newLines.length - 1;
|
|
443
504
|
}
|
|
444
505
|
const extraLines = this.previousLines.length - newLines.length;
|
|
445
506
|
for (let i = newLines.length; i < this.previousLines.length; i++) {
|
|
446
|
-
|
|
507
|
+
output.append("\r\n\x1b[2K");
|
|
447
508
|
}
|
|
448
509
|
// Move cursor back to end of new content
|
|
449
|
-
|
|
510
|
+
output.append(`\x1b[${extraLines}A`);
|
|
450
511
|
}
|
|
451
|
-
|
|
512
|
+
output.append("\x1b[?2026l"); // End synchronized output
|
|
452
513
|
if (process.env.PI_TUI_DEBUG === "1") {
|
|
453
514
|
const debugDir = "/tmp/tui";
|
|
454
515
|
fs.mkdirSync(debugDir, { recursive: true });
|
|
@@ -473,12 +534,11 @@ export class TuiMainScreen extends TuiBase {
|
|
|
473
534
|
JSON.stringify(this.previousLines, null, 2),
|
|
474
535
|
"",
|
|
475
536
|
"=== buffer ===",
|
|
476
|
-
|
|
537
|
+
`[${output.length} chars written in bounded chunks]`,
|
|
477
538
|
].join("\n");
|
|
478
539
|
fs.writeFileSync(debugPath, debugData);
|
|
479
540
|
}
|
|
480
|
-
|
|
481
|
-
this.terminal.write(buffer);
|
|
541
|
+
output.flush();
|
|
482
542
|
// Track cursor position for next render
|
|
483
543
|
// cursorRow tracks end of content (for viewport calculation)
|
|
484
544
|
// hardwareCursorRow tracks actual terminal cursor position (for movement)
|