explorbot 0.0.1 → 0.1.0
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 +80 -26
- package/bin/explorbot-cli.ts +680 -0
- package/boat/api-tester/src/ai/chief/styles.ts +15 -0
- package/boat/api-tester/src/ai/chief.ts +335 -0
- package/boat/api-tester/src/ai/curler-tools.ts +278 -0
- package/boat/api-tester/src/ai/curler.ts +306 -0
- package/boat/api-tester/src/api-client.ts +28 -0
- package/boat/api-tester/src/apibot.ts +203 -0
- package/boat/api-tester/src/cli.ts +301 -0
- package/boat/api-tester/src/config.ts +190 -0
- package/dist/bin/explorbot-cli.js +23 -101
- package/dist/boat/api-tester/bin/apibot-cli.js +0 -1
- package/dist/boat/api-tester/src/ai/chief/styles.js +0 -1
- package/dist/boat/api-tester/src/ai/chief.js +0 -1
- package/dist/boat/api-tester/src/ai/curler-tools.js +0 -1
- package/dist/boat/api-tester/src/ai/curler.js +0 -1
- package/dist/boat/api-tester/src/api-client.js +0 -1
- package/dist/boat/api-tester/src/apibot.js +0 -1
- package/dist/boat/api-tester/src/cli.js +0 -1
- package/dist/boat/api-tester/src/config.js +0 -1
- package/dist/src/action-result.js +0 -1
- package/dist/src/action.js +14 -12
- package/dist/src/activity.js +0 -1
- package/dist/src/ai/agent.js +0 -1
- package/dist/src/ai/bosun.js +0 -1
- package/dist/src/ai/captain/idle-mode.js +0 -1
- package/dist/src/ai/captain/mixin.js +0 -1
- package/dist/src/ai/captain/test-mode.js +0 -1
- package/dist/src/ai/captain/web-mode.js +0 -1
- package/dist/src/ai/captain.js +0 -1
- package/dist/src/ai/conversation.js +0 -1
- package/dist/src/ai/experience-compactor.js +0 -1
- package/dist/src/ai/fisherman-tools.js +0 -1
- package/dist/src/ai/fisherman.js +0 -1
- package/dist/src/ai/historian.js +0 -1
- package/dist/src/ai/navigator.js +0 -1
- package/dist/src/ai/pilot.js +0 -1
- package/dist/src/ai/planner/session-dedup.js +0 -1
- package/dist/src/ai/planner/styles.js +0 -1
- package/dist/src/ai/planner/subpages.js +42 -7
- package/dist/src/ai/planner.js +15 -4
- package/dist/src/ai/provider.js +0 -1
- package/dist/src/ai/quartermaster.js +0 -1
- package/dist/src/ai/researcher/cache.js +13 -9
- package/dist/src/ai/researcher/coordinates.js +4 -3
- package/dist/src/ai/researcher/deep-analysis.js +16 -20
- package/dist/src/ai/researcher/fingerprint-worker.js +0 -1
- package/dist/src/ai/researcher/focus.js +0 -1
- package/dist/src/ai/researcher/locators.js +1 -2
- package/dist/src/ai/researcher/mixin.js +0 -1
- package/dist/src/ai/researcher/parser.js +4 -4
- package/dist/src/ai/researcher/research-result.js +2 -1
- package/dist/src/ai/researcher.js +6 -6
- package/dist/src/ai/rules.js +0 -1
- package/dist/src/ai/task-agent.js +0 -1
- package/dist/src/ai/tester.js +0 -1
- package/dist/src/ai/tools.js +4 -1
- package/dist/src/api/api-client.js +0 -1
- package/dist/src/api/request-result.js +0 -1
- package/dist/src/api/request-store.js +0 -1
- package/dist/src/api/spec-reader.js +0 -1
- package/dist/src/api/xhr-capture.js +0 -1
- package/dist/src/browser-server.js +0 -1
- package/dist/src/command-handler.js +0 -1
- package/dist/src/commands/add-rule-command.js +0 -1
- package/dist/src/commands/base-command.js +0 -1
- package/dist/src/commands/clean-command.js +0 -1
- package/dist/src/commands/context-aria-command.js +0 -1
- package/dist/src/commands/context-command.js +2 -3
- package/dist/src/commands/context-data-command.js +0 -1
- package/dist/src/commands/context-experience-command.js +0 -1
- package/dist/src/commands/context-html-command.js +0 -1
- package/dist/src/commands/context-knowledge-command.js +0 -1
- package/dist/src/commands/debug-command.js +0 -1
- package/dist/src/commands/drill-command.js +0 -1
- package/dist/src/commands/exit-command.js +0 -1
- package/dist/src/commands/explore-command.js +3 -3
- package/dist/src/commands/freesail-command.js +0 -1
- package/dist/src/commands/help-command.js +0 -1
- package/dist/src/commands/index.js +0 -1
- package/dist/src/commands/init-command.js +117 -0
- package/dist/src/commands/knows-command.js +0 -1
- package/dist/src/commands/learn-command.js +0 -1
- package/dist/src/commands/navigate-command.js +0 -1
- package/dist/src/commands/path-command.js +0 -1
- package/dist/src/commands/plan-clear-command.js +0 -1
- package/dist/src/commands/plan-command.js +6 -2
- package/dist/src/commands/plan-edit-command.js +0 -1
- package/dist/src/commands/plan-load-command.js +0 -1
- package/dist/src/commands/plan-reload-command.js +0 -1
- package/dist/src/commands/plan-save-command.js +0 -1
- package/dist/src/commands/research-command.js +0 -1
- package/dist/src/commands/start-command.js +0 -1
- package/dist/src/commands/status-command.js +0 -1
- package/dist/src/commands/test-command.js +0 -1
- package/dist/src/components/ActivityPane.js +0 -1
- package/dist/src/components/AddKnowledge.js +0 -1
- package/dist/src/components/AddRule.js +0 -1
- package/dist/src/components/App.js +0 -1
- package/dist/src/components/Autocomplete.js +0 -1
- package/dist/src/components/InputPane.js +0 -1
- package/dist/src/components/InputReadline.js +0 -1
- package/dist/src/components/LogPane.js +0 -1
- package/dist/src/components/PlanEditor.js +0 -1
- package/dist/src/components/PlanPane.js +0 -1
- package/dist/src/components/SessionTimer.js +0 -1
- package/dist/src/components/StateTransitionPane.js +0 -1
- package/dist/src/components/StatusPane.js +0 -1
- package/dist/src/components/TaskPane.js +0 -1
- package/dist/src/components/Welcome.js +0 -1
- package/dist/src/components/WelcomeChecklist.js +0 -1
- package/dist/src/components/WelcomeCommands.js +0 -1
- package/dist/src/components/autocomplete-store.js +0 -1
- package/dist/src/components/parse-keypress.js +0 -1
- package/dist/src/config.js +0 -1
- package/dist/src/execution-controller.js +0 -1
- package/dist/src/experience-tracker.js +0 -1
- package/dist/src/explorbot.js +1 -2
- package/dist/src/explorer.js +58 -17
- package/dist/src/index.js +0 -1
- package/dist/src/knowledge-tracker.js +2 -2
- package/dist/src/observability.js +0 -1
- package/dist/src/reporter.js +0 -1
- package/dist/src/state-manager.js +0 -1
- package/dist/src/stats.js +0 -1
- package/dist/src/test-plan.js +0 -1
- package/dist/src/utils/aria.js +0 -1
- package/dist/src/utils/cli-name.js +16 -0
- package/dist/src/utils/code-extractor.js +0 -1
- package/dist/src/utils/context-formatter.js +0 -1
- package/dist/src/utils/error-page.js +0 -1
- package/dist/src/utils/expandable.js +0 -1
- package/dist/src/utils/hooks-runner.js +0 -1
- package/dist/src/utils/html-diff.js +0 -1
- package/dist/src/utils/html.js +0 -1
- package/dist/src/utils/logger.js +0 -1
- package/dist/src/utils/loop.js +0 -1
- package/dist/src/utils/markdown-parser.js +0 -1
- package/dist/src/utils/markdown-query.js +0 -1
- package/dist/src/utils/markdown-terminal.js +0 -1
- package/dist/src/utils/research-parser.js +0 -1
- package/dist/src/utils/retry.js +0 -1
- package/dist/src/utils/rules-loader.js +0 -1
- package/dist/src/utils/strings.js +0 -1
- package/dist/src/utils/test-plan-markdown.js +0 -1
- package/dist/src/utils/throttle.js +0 -1
- package/dist/src/utils/unique-names.js +0 -1
- package/dist/src/utils/url-matcher.js +0 -1
- package/dist/src/utils/web-element.js +6 -5
- package/dist/src/utils/xpath.js +0 -1
- package/package.json +28 -4
- package/src/action-result.ts +694 -0
- package/src/action.ts +449 -0
- package/src/activity.ts +111 -0
- package/src/ai/agent.ts +3 -0
- package/src/ai/bosun.ts +557 -0
- package/src/ai/captain/idle-mode.ts +116 -0
- package/src/ai/captain/mixin.ts +22 -0
- package/src/ai/captain/test-mode.ts +262 -0
- package/src/ai/captain/web-mode.ts +136 -0
- package/src/ai/captain.ts +504 -0
- package/src/ai/conversation.ts +205 -0
- package/src/ai/experience-compactor.ts +284 -0
- package/src/ai/fisherman-tools.ts +181 -0
- package/src/ai/fisherman.ts +223 -0
- package/src/ai/historian.ts +457 -0
- package/src/ai/navigator.ts +572 -0
- package/src/ai/pilot.ts +776 -0
- package/src/ai/planner/session-dedup.ts +35 -0
- package/src/ai/planner/styles.ts +17 -0
- package/src/ai/planner/subpages.ts +171 -0
- package/src/ai/planner.ts +549 -0
- package/src/ai/provider.ts +613 -0
- package/src/ai/quartermaster.ts +286 -0
- package/src/ai/researcher/cache.ts +109 -0
- package/src/ai/researcher/coordinates.ts +239 -0
- package/src/ai/researcher/deep-analysis.ts +412 -0
- package/src/ai/researcher/fingerprint-worker.ts +59 -0
- package/src/ai/researcher/focus.ts +42 -0
- package/src/ai/researcher/locators.ts +282 -0
- package/src/ai/researcher/mixin.ts +4 -0
- package/src/ai/researcher/parser.ts +186 -0
- package/src/ai/researcher/research-result.ts +116 -0
- package/src/ai/researcher.ts +858 -0
- package/src/ai/rules.ts +376 -0
- package/src/ai/task-agent.ts +141 -0
- package/src/ai/tester.ts +939 -0
- package/src/ai/tools.ts +1122 -0
- package/src/api/api-client.ts +109 -0
- package/src/api/request-result.ts +212 -0
- package/src/api/request-store.ts +130 -0
- package/src/api/spec-reader.ts +174 -0
- package/src/api/xhr-capture.ts +100 -0
- package/src/browser-server.ts +74 -0
- package/src/command-handler.ts +454 -0
- package/src/commands/add-rule-command.ts +63 -0
- package/src/commands/base-command.ts +27 -0
- package/src/commands/clean-command.ts +73 -0
- package/src/commands/context-aria-command.ts +22 -0
- package/src/commands/context-command.ts +67 -0
- package/src/commands/context-data-command.ts +30 -0
- package/src/commands/context-experience-command.ts +48 -0
- package/src/commands/context-html-command.ts +33 -0
- package/src/commands/context-knowledge-command.ts +43 -0
- package/src/commands/debug-command.ts +13 -0
- package/src/commands/drill-command.ts +34 -0
- package/src/commands/exit-command.ts +32 -0
- package/src/commands/explore-command.ts +129 -0
- package/src/commands/freesail-command.ts +95 -0
- package/src/commands/help-command.ts +8 -0
- package/src/commands/index.ts +69 -0
- package/src/commands/init-command.ts +131 -0
- package/src/commands/knows-command.ts +68 -0
- package/src/commands/learn-command.ts +44 -0
- package/src/commands/navigate-command.ts +18 -0
- package/src/commands/path-command.ts +83 -0
- package/src/commands/plan-clear-command.ts +14 -0
- package/src/commands/plan-command.ts +46 -0
- package/src/commands/plan-edit-command.ts +9 -0
- package/src/commands/plan-load-command.ts +18 -0
- package/src/commands/plan-reload-command.ts +28 -0
- package/src/commands/plan-save-command.ts +25 -0
- package/src/commands/research-command.ts +45 -0
- package/src/commands/start-command.ts +13 -0
- package/src/commands/status-command.tsx +23 -0
- package/src/commands/test-command.ts +84 -0
- package/src/components/ActivityPane.tsx +80 -0
- package/src/components/AddKnowledge.tsx +169 -0
- package/src/components/AddRule.tsx +174 -0
- package/src/components/App.tsx +377 -0
- package/src/components/Autocomplete.tsx +63 -0
- package/src/components/InputPane.tsx +259 -0
- package/src/components/InputReadline.tsx +704 -0
- package/src/components/LogPane.tsx +187 -0
- package/src/components/PlanEditor.tsx +150 -0
- package/src/components/PlanPane.tsx +71 -0
- package/src/components/SessionTimer.tsx +35 -0
- package/src/components/StateTransitionPane.tsx +149 -0
- package/src/components/StatusPane.tsx +62 -0
- package/src/components/TaskPane.tsx +119 -0
- package/src/components/Welcome.tsx +83 -0
- package/src/components/WelcomeChecklist.tsx +118 -0
- package/src/components/WelcomeCommands.tsx +102 -0
- package/src/components/autocomplete-store.ts +35 -0
- package/src/components/parse-keypress.ts +170 -0
- package/src/config.ts +491 -0
- package/src/execution-controller.ts +109 -0
- package/src/experience-tracker.ts +350 -0
- package/src/explorbot.ts +405 -0
- package/src/explorer.ts +760 -0
- package/src/index.tsx +62 -0
- package/src/knowledge-tracker.ts +230 -0
- package/src/observability.ts +150 -0
- package/src/reporter.ts +224 -0
- package/src/state-manager.ts +556 -0
- package/src/stats.ts +53 -0
- package/src/test-plan.ts +432 -0
- package/src/utils/aria.ts +629 -0
- package/src/utils/cli-name.ts +13 -0
- package/src/utils/code-extractor.ts +22 -0
- package/src/utils/context-formatter.ts +239 -0
- package/src/utils/error-page.ts +23 -0
- package/src/utils/expandable.ts +38 -0
- package/src/utils/hooks-runner.ts +79 -0
- package/src/utils/html-diff.ts +918 -0
- package/src/utils/html.ts +1316 -0
- package/src/utils/logger.ts +534 -0
- package/src/utils/loop.ts +176 -0
- package/src/utils/markdown-parser.ts +127 -0
- package/src/utils/markdown-query.ts +466 -0
- package/src/utils/markdown-terminal.ts +43 -0
- package/src/utils/research-parser.ts +11 -0
- package/src/utils/retry.ts +73 -0
- package/src/utils/rules-loader.ts +118 -0
- package/src/utils/strings.ts +13 -0
- package/src/utils/test-plan-markdown.ts +332 -0
- package/src/utils/throttle.ts +18 -0
- package/src/utils/unique-names.ts +14 -0
- package/src/utils/url-matcher.ts +45 -0
- package/src/utils/web-element.ts +147 -0
- package/src/utils/xpath.ts +129 -0
- package/dist/bin/explorbot-cli.js.map +0 -1
- package/dist/boat/api-tester/bin/apibot-cli.js.map +0 -1
- package/dist/boat/api-tester/example/apibot.config.js +0 -31
- package/dist/boat/api-tester/example/apibot.config.js.map +0 -1
- package/dist/boat/api-tester/src/ai/chief/styles.js.map +0 -1
- package/dist/boat/api-tester/src/ai/chief.js.map +0 -1
- package/dist/boat/api-tester/src/ai/curler-tools.js.map +0 -1
- package/dist/boat/api-tester/src/ai/curler.js.map +0 -1
- package/dist/boat/api-tester/src/api-client.js.map +0 -1
- package/dist/boat/api-tester/src/apibot.js.map +0 -1
- package/dist/boat/api-tester/src/cli.js.map +0 -1
- package/dist/boat/api-tester/src/config.js.map +0 -1
- package/dist/prompts/audit-rules.md +0 -124
- package/dist/src/action-result.js.map +0 -1
- package/dist/src/action.js.map +0 -1
- package/dist/src/activity.js.map +0 -1
- package/dist/src/ai/agent.js.map +0 -1
- package/dist/src/ai/bosun.js.map +0 -1
- package/dist/src/ai/captain/idle-mode.js.map +0 -1
- package/dist/src/ai/captain/mixin.js.map +0 -1
- package/dist/src/ai/captain/test-mode.js.map +0 -1
- package/dist/src/ai/captain/web-mode.js.map +0 -1
- package/dist/src/ai/captain.js.map +0 -1
- package/dist/src/ai/conversation.js.map +0 -1
- package/dist/src/ai/experience-compactor.js.map +0 -1
- package/dist/src/ai/fisherman-tools.js.map +0 -1
- package/dist/src/ai/fisherman.js.map +0 -1
- package/dist/src/ai/historian.js.map +0 -1
- package/dist/src/ai/navigator.js.map +0 -1
- package/dist/src/ai/pilot.js.map +0 -1
- package/dist/src/ai/planner/session-dedup.js.map +0 -1
- package/dist/src/ai/planner/styles.js.map +0 -1
- package/dist/src/ai/planner/subpages.js.map +0 -1
- package/dist/src/ai/planner.js.map +0 -1
- package/dist/src/ai/provider.js.map +0 -1
- package/dist/src/ai/quartermaster.js.map +0 -1
- package/dist/src/ai/researcher/cache.js.map +0 -1
- package/dist/src/ai/researcher/coordinates.js.map +0 -1
- package/dist/src/ai/researcher/deep-analysis.js.map +0 -1
- package/dist/src/ai/researcher/fingerprint-worker.js.map +0 -1
- package/dist/src/ai/researcher/focus.js.map +0 -1
- package/dist/src/ai/researcher/locators.js.map +0 -1
- package/dist/src/ai/researcher/mixin.js.map +0 -1
- package/dist/src/ai/researcher/parser.js.map +0 -1
- package/dist/src/ai/researcher/research-result.js.map +0 -1
- package/dist/src/ai/researcher.js.map +0 -1
- package/dist/src/ai/rules.js.map +0 -1
- package/dist/src/ai/task-agent.js.map +0 -1
- package/dist/src/ai/tester.js.map +0 -1
- package/dist/src/ai/tools.js.map +0 -1
- package/dist/src/api/api-client.js.map +0 -1
- package/dist/src/api/request-result.js.map +0 -1
- package/dist/src/api/request-store.js.map +0 -1
- package/dist/src/api/spec-reader.js.map +0 -1
- package/dist/src/api/xhr-capture.js.map +0 -1
- package/dist/src/browser-server.js.map +0 -1
- package/dist/src/command-handler.js.map +0 -1
- package/dist/src/commands/add-rule-command.js.map +0 -1
- package/dist/src/commands/base-command.js.map +0 -1
- package/dist/src/commands/clean-command.js.map +0 -1
- package/dist/src/commands/context-aria-command.js.map +0 -1
- package/dist/src/commands/context-command.js.map +0 -1
- package/dist/src/commands/context-data-command.js.map +0 -1
- package/dist/src/commands/context-experience-command.js.map +0 -1
- package/dist/src/commands/context-html-command.js.map +0 -1
- package/dist/src/commands/context-knowledge-command.js.map +0 -1
- package/dist/src/commands/debug-command.js.map +0 -1
- package/dist/src/commands/drill-command.js.map +0 -1
- package/dist/src/commands/exit-command.js.map +0 -1
- package/dist/src/commands/explore-command.js.map +0 -1
- package/dist/src/commands/freesail-command.js.map +0 -1
- package/dist/src/commands/help-command.js.map +0 -1
- package/dist/src/commands/index.js.map +0 -1
- package/dist/src/commands/knows-command.js.map +0 -1
- package/dist/src/commands/learn-command.js.map +0 -1
- package/dist/src/commands/navigate-command.js.map +0 -1
- package/dist/src/commands/path-command.js.map +0 -1
- package/dist/src/commands/plan-clear-command.js.map +0 -1
- package/dist/src/commands/plan-command.js.map +0 -1
- package/dist/src/commands/plan-edit-command.js.map +0 -1
- package/dist/src/commands/plan-load-command.js.map +0 -1
- package/dist/src/commands/plan-reload-command.js.map +0 -1
- package/dist/src/commands/plan-save-command.js.map +0 -1
- package/dist/src/commands/research-command.js.map +0 -1
- package/dist/src/commands/start-command.js.map +0 -1
- package/dist/src/commands/status-command.js.map +0 -1
- package/dist/src/commands/test-command.js.map +0 -1
- package/dist/src/components/ActivityPane.js.map +0 -1
- package/dist/src/components/AddKnowledge.js.map +0 -1
- package/dist/src/components/AddRule.js.map +0 -1
- package/dist/src/components/App.js.map +0 -1
- package/dist/src/components/Autocomplete.js.map +0 -1
- package/dist/src/components/InputPane.js.map +0 -1
- package/dist/src/components/InputReadline.js.map +0 -1
- package/dist/src/components/LogPane.js.map +0 -1
- package/dist/src/components/PlanEditor.js.map +0 -1
- package/dist/src/components/PlanPane.js.map +0 -1
- package/dist/src/components/SessionTimer.js.map +0 -1
- package/dist/src/components/StateTransitionPane.js.map +0 -1
- package/dist/src/components/StatusPane.js.map +0 -1
- package/dist/src/components/TaskPane.js.map +0 -1
- package/dist/src/components/Welcome.js.map +0 -1
- package/dist/src/components/WelcomeChecklist.js.map +0 -1
- package/dist/src/components/WelcomeCommands.js.map +0 -1
- package/dist/src/components/autocomplete-store.js.map +0 -1
- package/dist/src/components/parse-keypress.js.map +0 -1
- package/dist/src/config.js.map +0 -1
- package/dist/src/execution-controller.js.map +0 -1
- package/dist/src/experience-tracker.js.map +0 -1
- package/dist/src/explorbot.js.map +0 -1
- package/dist/src/explorer.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/knowledge-tracker.js.map +0 -1
- package/dist/src/observability.js.map +0 -1
- package/dist/src/reporter.js.map +0 -1
- package/dist/src/state-manager.js.map +0 -1
- package/dist/src/stats.js.map +0 -1
- package/dist/src/test-plan.js.map +0 -1
- package/dist/src/utils/aria.js.map +0 -1
- package/dist/src/utils/code-extractor.js.map +0 -1
- package/dist/src/utils/context-formatter.js.map +0 -1
- package/dist/src/utils/error-page.js.map +0 -1
- package/dist/src/utils/expandable.js.map +0 -1
- package/dist/src/utils/hooks-runner.js.map +0 -1
- package/dist/src/utils/html-diff.js.map +0 -1
- package/dist/src/utils/html.js.map +0 -1
- package/dist/src/utils/logger.js.map +0 -1
- package/dist/src/utils/loop.js.map +0 -1
- package/dist/src/utils/markdown-parser.js.map +0 -1
- package/dist/src/utils/markdown-query.js.map +0 -1
- package/dist/src/utils/markdown-terminal.js.map +0 -1
- package/dist/src/utils/research-parser.js.map +0 -1
- package/dist/src/utils/retry.js.map +0 -1
- package/dist/src/utils/rules-loader.js.map +0 -1
- package/dist/src/utils/strings.js.map +0 -1
- package/dist/src/utils/test-plan-markdown.js.map +0 -1
- package/dist/src/utils/throttle.js.map +0 -1
- package/dist/src/utils/unique-names.js.map +0 -1
- package/dist/src/utils/url-matcher.js.map +0 -1
- package/dist/src/utils/web-element.js.map +0 -1
- package/dist/src/utils/xpath.js.map +0 -1
- package/prompts/audit-rules.md +0 -124
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { Box, Text, useStdin } from 'ink';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import type { CommandAutocompleteSuggestion, CommandHandler } from '../command-handler.js';
|
|
5
|
+
import { setAutocompleteState } from './autocomplete-store.js';
|
|
6
|
+
import parseKeypress, { nonAlphanumericKeys } from './parse-keypress.js';
|
|
7
|
+
|
|
8
|
+
interface InputReadlineProps {
|
|
9
|
+
commandHandler?: CommandHandler;
|
|
10
|
+
exitOnEmptyInput?: boolean;
|
|
11
|
+
onSubmit?: (value: string) => void | Promise<void>;
|
|
12
|
+
onChange?: (value: string) => void;
|
|
13
|
+
onCommandStart?: () => void;
|
|
14
|
+
onCommandComplete?: () => void;
|
|
15
|
+
isActive?: boolean;
|
|
16
|
+
visible?: boolean;
|
|
17
|
+
value?: string;
|
|
18
|
+
placeholder?: string;
|
|
19
|
+
showPrompt?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface InputState {
|
|
23
|
+
value: string;
|
|
24
|
+
cursor: number;
|
|
25
|
+
showAutocomplete: boolean;
|
|
26
|
+
selectedIndex: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const InputReadline: React.FC<InputReadlineProps> = React.memo(({ commandHandler, exitOnEmptyInput = false, onSubmit, onChange, onCommandStart, onCommandComplete, isActive = true, visible = true, value: controlledValue, placeholder = '', showPrompt = true }) => {
|
|
30
|
+
const isControlled = controlledValue !== undefined;
|
|
31
|
+
const [inputState, setInputState] = useState<InputState>({
|
|
32
|
+
value: controlledValue || '',
|
|
33
|
+
cursor: controlledValue?.length || 0,
|
|
34
|
+
showAutocomplete: false,
|
|
35
|
+
selectedIndex: 0,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const { value: inputValue, cursor: cursorPosition, showAutocomplete, selectedIndex } = inputState;
|
|
39
|
+
const displayValue = isControlled ? controlledValue : inputValue;
|
|
40
|
+
|
|
41
|
+
const inputRef = useRef(displayValue);
|
|
42
|
+
const cursorRef = useRef(cursorPosition);
|
|
43
|
+
const showAutocompleteRef = useRef(showAutocomplete);
|
|
44
|
+
const selectedIndexRef = useRef(selectedIndex);
|
|
45
|
+
const historyRef = useRef<string[]>([]);
|
|
46
|
+
const historyIndexRef = useRef(-1);
|
|
47
|
+
const historyDraftRef = useRef('');
|
|
48
|
+
const wasActiveRef = useRef(isActive);
|
|
49
|
+
const autocompleteStateRef = useRef({ suggestions: [] as CommandAutocompleteSuggestion[], argumentHint: undefined as string | undefined, selectedIndex: 0, visible: false });
|
|
50
|
+
|
|
51
|
+
const onSubmitRef = useRef(onSubmit);
|
|
52
|
+
const onChangeRef = useRef(onChange);
|
|
53
|
+
const onCommandStartRef = useRef(onCommandStart);
|
|
54
|
+
const onCommandCompleteRef = useRef(onCommandComplete);
|
|
55
|
+
const exitOnEmptyInputRef = useRef(exitOnEmptyInput);
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
onSubmitRef.current = onSubmit;
|
|
59
|
+
onChangeRef.current = onChange;
|
|
60
|
+
onCommandStartRef.current = onCommandStart;
|
|
61
|
+
onCommandCompleteRef.current = onCommandComplete;
|
|
62
|
+
exitOnEmptyInputRef.current = exitOnEmptyInput;
|
|
63
|
+
}, [onSubmit, onChange, onCommandStart, onCommandComplete, exitOnEmptyInput]);
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (isControlled) {
|
|
67
|
+
inputRef.current = controlledValue;
|
|
68
|
+
if (cursorRef.current > controlledValue.length) {
|
|
69
|
+
cursorRef.current = controlledValue.length;
|
|
70
|
+
setInputState((prev) => ({ ...prev, value: controlledValue, cursor: controlledValue.length }));
|
|
71
|
+
} else {
|
|
72
|
+
setInputState((prev) => ({ ...prev, value: controlledValue }));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}, [controlledValue, isControlled]);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const wasInactive = !wasActiveRef.current;
|
|
79
|
+
const isNowActive = isActive;
|
|
80
|
+
wasActiveRef.current = isActive;
|
|
81
|
+
|
|
82
|
+
if (wasInactive && isNowActive && !isControlled) {
|
|
83
|
+
inputRef.current = '';
|
|
84
|
+
cursorRef.current = 0;
|
|
85
|
+
showAutocompleteRef.current = false;
|
|
86
|
+
selectedIndexRef.current = 0;
|
|
87
|
+
historyIndexRef.current = -1;
|
|
88
|
+
historyDraftRef.current = '';
|
|
89
|
+
setInputState({ value: '', cursor: 0, showAutocomplete: false, selectedIndex: 0 });
|
|
90
|
+
}
|
|
91
|
+
}, [isActive, isControlled]);
|
|
92
|
+
|
|
93
|
+
const addLog = useCallback((entry: string) => {
|
|
94
|
+
console.log(entry);
|
|
95
|
+
}, []);
|
|
96
|
+
const { internal_eventEmitter, setRawMode } = useStdin();
|
|
97
|
+
|
|
98
|
+
const getAutocomplete = useCallback(
|
|
99
|
+
(value: string, cursor: number) => {
|
|
100
|
+
if (!commandHandler) {
|
|
101
|
+
return {
|
|
102
|
+
suggestions: [],
|
|
103
|
+
replaceFrom: cursor,
|
|
104
|
+
replaceTo: cursor,
|
|
105
|
+
visible: false,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return commandHandler.getAutocomplete(value, cursor);
|
|
109
|
+
},
|
|
110
|
+
[commandHandler]
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const updateCursor = useCallback((nextCursor: number) => {
|
|
114
|
+
const safeCursor = Math.max(0, Math.min(nextCursor, inputRef.current.length));
|
|
115
|
+
cursorRef.current = safeCursor;
|
|
116
|
+
setInputState((prev) => ({ ...prev, cursor: safeCursor }));
|
|
117
|
+
}, []);
|
|
118
|
+
|
|
119
|
+
const findLineStart = useCallback((value: string, cursor: number) => {
|
|
120
|
+
if (cursor <= 0) {
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
return value.lastIndexOf('\n', cursor - 1) + 1;
|
|
124
|
+
}, []);
|
|
125
|
+
|
|
126
|
+
const findLineEnd = useCallback((value: string, cursor: number) => {
|
|
127
|
+
const lineEnd = value.indexOf('\n', cursor);
|
|
128
|
+
if (lineEnd === -1) {
|
|
129
|
+
return value.length;
|
|
130
|
+
}
|
|
131
|
+
return lineEnd;
|
|
132
|
+
}, []);
|
|
133
|
+
|
|
134
|
+
const updateValue = useCallback(
|
|
135
|
+
(newValue: string, newCursor: number) => {
|
|
136
|
+
const safeCursor = Math.max(0, Math.min(newCursor, newValue.length));
|
|
137
|
+
const autocomplete = getAutocomplete(newValue, safeCursor);
|
|
138
|
+
const nextShowAutocomplete = autocomplete.visible || Boolean(autocomplete.argumentHint);
|
|
139
|
+
inputRef.current = newValue;
|
|
140
|
+
cursorRef.current = safeCursor;
|
|
141
|
+
showAutocompleteRef.current = nextShowAutocomplete;
|
|
142
|
+
selectedIndexRef.current = 0;
|
|
143
|
+
historyIndexRef.current = -1;
|
|
144
|
+
historyDraftRef.current = '';
|
|
145
|
+
|
|
146
|
+
if (isControlled) {
|
|
147
|
+
onChangeRef.current?.(newValue);
|
|
148
|
+
setInputState((prev) => ({ ...prev, cursor: safeCursor, showAutocomplete: nextShowAutocomplete, selectedIndex: 0 }));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
setInputState({ value: newValue, cursor: safeCursor, showAutocomplete: nextShowAutocomplete, selectedIndex: 0 });
|
|
152
|
+
},
|
|
153
|
+
[getAutocomplete, isControlled]
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const handleSubmit = useCallback(
|
|
157
|
+
async (value: string) => {
|
|
158
|
+
const trimmedValue = value.trim();
|
|
159
|
+
|
|
160
|
+
if (!trimmedValue) {
|
|
161
|
+
if (exitOnEmptyInputRef.current) {
|
|
162
|
+
console.log('\nExiting...');
|
|
163
|
+
process.exit(0);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (commandHandler) {
|
|
169
|
+
const history = historyRef.current;
|
|
170
|
+
if (history[history.length - 1] !== trimmedValue) {
|
|
171
|
+
history.push(trimmedValue);
|
|
172
|
+
}
|
|
173
|
+
historyIndexRef.current = -1;
|
|
174
|
+
historyDraftRef.current = '';
|
|
175
|
+
|
|
176
|
+
onCommandStartRef.current?.();
|
|
177
|
+
|
|
178
|
+
if (onSubmitRef.current) {
|
|
179
|
+
await onSubmitRef.current(trimmedValue);
|
|
180
|
+
}
|
|
181
|
+
onCommandCompleteRef.current?.();
|
|
182
|
+
|
|
183
|
+
updateValue('', 0);
|
|
184
|
+
} else {
|
|
185
|
+
onSubmitRef.current?.(trimmedValue);
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
[commandHandler, updateValue]
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const insertText = useCallback(
|
|
192
|
+
(text: string) => {
|
|
193
|
+
const normalized = text.replaceAll('\u001b[200~', '').replaceAll('\u001b[201~', '').replace(/\r/g, '\n');
|
|
194
|
+
if (!normalized) return;
|
|
195
|
+
const currentValue = inputRef.current;
|
|
196
|
+
const currentCursor = cursorRef.current;
|
|
197
|
+
const newValue = currentValue.slice(0, currentCursor) + normalized + currentValue.slice(currentCursor);
|
|
198
|
+
const nextCursor = currentCursor + normalized.length;
|
|
199
|
+
updateValue(newValue, nextCursor);
|
|
200
|
+
},
|
|
201
|
+
[updateValue]
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const applyAutocomplete = useCallback(
|
|
205
|
+
(index: number) => {
|
|
206
|
+
if (!commandHandler) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
const autocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
210
|
+
const suggestion = autocomplete.suggestions[index] || autocomplete.suggestions[0];
|
|
211
|
+
if (!suggestion) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
const newValue = inputRef.current.slice(0, autocomplete.replaceFrom) + suggestion.value + inputRef.current.slice(autocomplete.replaceTo);
|
|
215
|
+
const nextCursor = autocomplete.replaceFrom + suggestion.value.length;
|
|
216
|
+
updateValue(newValue, nextCursor);
|
|
217
|
+
return newValue;
|
|
218
|
+
},
|
|
219
|
+
[commandHandler, getAutocomplete, updateValue]
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const handleInput = useCallback(
|
|
223
|
+
(input: string, key: { [key: string]: boolean }) => {
|
|
224
|
+
const isBackspaceKey = key.backspace;
|
|
225
|
+
const isDeleteKey = key.delete;
|
|
226
|
+
|
|
227
|
+
if (key.ctrl && input === 'c') {
|
|
228
|
+
console.log('\n🛑 Received Ctrl-C, exiting...');
|
|
229
|
+
process.exit(0);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (key.escape) {
|
|
234
|
+
if (showAutocompleteRef.current) {
|
|
235
|
+
showAutocompleteRef.current = false;
|
|
236
|
+
selectedIndexRef.current = 0;
|
|
237
|
+
setInputState((prev) => ({ ...prev, showAutocomplete: false, selectedIndex: 0 }));
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (key.return) {
|
|
243
|
+
if (key.shift) {
|
|
244
|
+
insertText('\n');
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (commandHandler && showAutocompleteRef.current) {
|
|
248
|
+
const autocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
249
|
+
if (autocomplete.visible && autocomplete.suggestions.length > 0) {
|
|
250
|
+
const chosen = applyAutocomplete(selectedIndexRef.current);
|
|
251
|
+
if (chosen) {
|
|
252
|
+
handleSubmit(chosen);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
handleSubmit(inputRef.current);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const isWordChar = (char: string) => /[A-Za-z0-9_]/.test(char);
|
|
262
|
+
|
|
263
|
+
if (key.ctrl && key.leftArrow) {
|
|
264
|
+
const value = inputRef.current;
|
|
265
|
+
let nextCursor = cursorRef.current;
|
|
266
|
+
if (nextCursor === 0) return;
|
|
267
|
+
nextCursor -= 1;
|
|
268
|
+
while (nextCursor > 0 && !isWordChar(value[nextCursor])) nextCursor -= 1;
|
|
269
|
+
while (nextCursor > 0 && isWordChar(value[nextCursor - 1])) nextCursor -= 1;
|
|
270
|
+
updateCursor(nextCursor);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (key.ctrl && key.rightArrow) {
|
|
275
|
+
const value = inputRef.current;
|
|
276
|
+
let nextCursor = cursorRef.current;
|
|
277
|
+
if (nextCursor >= value.length) return;
|
|
278
|
+
while (nextCursor < value.length && !isWordChar(value[nextCursor])) nextCursor += 1;
|
|
279
|
+
while (nextCursor < value.length && isWordChar(value[nextCursor])) nextCursor += 1;
|
|
280
|
+
updateCursor(nextCursor);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (key.meta && input === 'b') {
|
|
285
|
+
const value = inputRef.current;
|
|
286
|
+
let nextCursor = cursorRef.current;
|
|
287
|
+
if (nextCursor === 0) return;
|
|
288
|
+
nextCursor -= 1;
|
|
289
|
+
while (nextCursor > 0 && !isWordChar(value[nextCursor])) nextCursor -= 1;
|
|
290
|
+
while (nextCursor > 0 && isWordChar(value[nextCursor - 1])) nextCursor -= 1;
|
|
291
|
+
updateCursor(nextCursor);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (key.meta && input === 'f') {
|
|
296
|
+
const value = inputRef.current;
|
|
297
|
+
let nextCursor = cursorRef.current;
|
|
298
|
+
if (nextCursor >= value.length) return;
|
|
299
|
+
while (nextCursor < value.length && !isWordChar(value[nextCursor])) nextCursor += 1;
|
|
300
|
+
while (nextCursor < value.length && isWordChar(value[nextCursor])) nextCursor += 1;
|
|
301
|
+
updateCursor(nextCursor);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if ((key.ctrl && input === 'a') || key.home) {
|
|
306
|
+
updateCursor(findLineStart(inputRef.current, cursorRef.current));
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if ((key.ctrl && input === 'e') || key.end) {
|
|
311
|
+
updateCursor(findLineEnd(inputRef.current, cursorRef.current));
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if ((key.ctrl && input === 'w') || (key.ctrl && isBackspaceKey)) {
|
|
316
|
+
const value = inputRef.current;
|
|
317
|
+
const currentCursor = cursorRef.current;
|
|
318
|
+
if (currentCursor === 0) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
let nextCursor = currentCursor - 1;
|
|
322
|
+
while (nextCursor > 0 && !isWordChar(value[nextCursor])) nextCursor -= 1;
|
|
323
|
+
while (nextCursor > 0 && isWordChar(value[nextCursor - 1])) nextCursor -= 1;
|
|
324
|
+
const newValue = value.slice(0, nextCursor) + value.slice(currentCursor);
|
|
325
|
+
updateValue(newValue, nextCursor);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (key.ctrl && key.delete) {
|
|
330
|
+
const value = inputRef.current;
|
|
331
|
+
const currentCursor = cursorRef.current;
|
|
332
|
+
if (currentCursor >= value.length) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
let nextCursor = currentCursor;
|
|
336
|
+
while (nextCursor < value.length && !isWordChar(value[nextCursor])) nextCursor += 1;
|
|
337
|
+
while (nextCursor < value.length && isWordChar(value[nextCursor])) nextCursor += 1;
|
|
338
|
+
const newValue = value.slice(0, currentCursor) + value.slice(nextCursor);
|
|
339
|
+
updateValue(newValue, currentCursor);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (key.ctrl && input === 'u') {
|
|
344
|
+
const currentCursor = cursorRef.current;
|
|
345
|
+
const lineStart = findLineStart(inputRef.current, currentCursor);
|
|
346
|
+
if (lineStart === currentCursor) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const newValue = inputRef.current.slice(0, lineStart) + inputRef.current.slice(currentCursor);
|
|
350
|
+
updateValue(newValue, lineStart);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (key.ctrl && input === 'k') {
|
|
355
|
+
const currentCursor = cursorRef.current;
|
|
356
|
+
const lineEnd = findLineEnd(inputRef.current, currentCursor);
|
|
357
|
+
if (lineEnd === currentCursor) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const newValue = inputRef.current.slice(0, currentCursor) + inputRef.current.slice(lineEnd);
|
|
361
|
+
updateValue(newValue, currentCursor);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (key.ctrl) return;
|
|
366
|
+
if (key.meta && (input === 'b' || input === 'f')) return;
|
|
367
|
+
|
|
368
|
+
if (key.leftArrow) {
|
|
369
|
+
updateCursor(Math.max(0, cursorRef.current - 1));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (key.rightArrow) {
|
|
374
|
+
updateCursor(Math.min(inputRef.current.length, cursorRef.current + 1));
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (commandHandler && showAutocompleteRef.current && (key.upArrow || (key.shift && key.leftArrow))) {
|
|
379
|
+
const autocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
380
|
+
if (autocomplete.visible && autocomplete.suggestions.length > 0) {
|
|
381
|
+
const nextIndex = selectedIndexRef.current > 0 ? selectedIndexRef.current - 1 : autocomplete.suggestions.length - 1;
|
|
382
|
+
selectedIndexRef.current = nextIndex;
|
|
383
|
+
setInputState((prev) => ({ ...prev, selectedIndex: nextIndex }));
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (commandHandler && showAutocompleteRef.current && (key.downArrow || (key.shift && key.rightArrow))) {
|
|
389
|
+
const autocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
390
|
+
if (autocomplete.visible && autocomplete.suggestions.length > 0) {
|
|
391
|
+
const nextIndex = selectedIndexRef.current < autocomplete.suggestions.length - 1 ? selectedIndexRef.current + 1 : 0;
|
|
392
|
+
selectedIndexRef.current = nextIndex;
|
|
393
|
+
setInputState((prev) => ({ ...prev, selectedIndex: nextIndex }));
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const hasNewlines = inputRef.current.includes('\n');
|
|
399
|
+
|
|
400
|
+
if (key.upArrow && hasNewlines) {
|
|
401
|
+
const value = inputRef.current;
|
|
402
|
+
const currentCursor = cursorRef.current;
|
|
403
|
+
const before = value.slice(0, currentCursor);
|
|
404
|
+
const currentLineIndex = before.split('\n').length - 1;
|
|
405
|
+
if (currentLineIndex === 0) return;
|
|
406
|
+
const lineStart = before.lastIndexOf('\n') + 1;
|
|
407
|
+
const column = currentCursor - lineStart;
|
|
408
|
+
const prevLineStart = before.lastIndexOf('\n', lineStart - 2) + 1;
|
|
409
|
+
const prevLineEnd = lineStart - 1;
|
|
410
|
+
const prevLineLength = prevLineEnd - prevLineStart;
|
|
411
|
+
const nextCursor = prevLineStart + Math.min(column, prevLineLength);
|
|
412
|
+
updateCursor(nextCursor);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
if (key.downArrow && hasNewlines) {
|
|
417
|
+
const value = inputRef.current;
|
|
418
|
+
const currentCursor = cursorRef.current;
|
|
419
|
+
const before = value.slice(0, currentCursor);
|
|
420
|
+
const currentLineIndex = before.split('\n').length - 1;
|
|
421
|
+
const lines = value.split('\n');
|
|
422
|
+
if (currentLineIndex >= lines.length - 1) return;
|
|
423
|
+
const lineStart = before.lastIndexOf('\n') + 1;
|
|
424
|
+
const column = currentCursor - lineStart;
|
|
425
|
+
const nextLineStart = lineStart + lines[currentLineIndex].length + 1;
|
|
426
|
+
const nextLineLength = lines[currentLineIndex + 1].length;
|
|
427
|
+
const nextCursor = nextLineStart + Math.min(column, nextLineLength);
|
|
428
|
+
updateCursor(nextCursor);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (commandHandler && key.upArrow) {
|
|
433
|
+
const currentAutocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
434
|
+
if (showAutocompleteRef.current && currentAutocomplete.visible && currentAutocomplete.suggestions.length > 0) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const history = historyRef.current;
|
|
438
|
+
if (history.length === 0) return;
|
|
439
|
+
if (historyIndexRef.current === -1) {
|
|
440
|
+
historyDraftRef.current = inputRef.current;
|
|
441
|
+
historyIndexRef.current = history.length - 1;
|
|
442
|
+
} else if (historyIndexRef.current > 0) {
|
|
443
|
+
historyIndexRef.current -= 1;
|
|
444
|
+
}
|
|
445
|
+
const nextValue = history[historyIndexRef.current] || '';
|
|
446
|
+
const nextAutocomplete = getAutocomplete(nextValue, nextValue.length);
|
|
447
|
+
const nextShowAutocomplete = nextAutocomplete.visible || Boolean(nextAutocomplete.argumentHint);
|
|
448
|
+
inputRef.current = nextValue;
|
|
449
|
+
cursorRef.current = nextValue.length;
|
|
450
|
+
selectedIndexRef.current = 0;
|
|
451
|
+
showAutocompleteRef.current = nextShowAutocomplete;
|
|
452
|
+
setInputState({ value: nextValue, cursor: nextValue.length, selectedIndex: 0, showAutocomplete: nextShowAutocomplete });
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (commandHandler && key.downArrow) {
|
|
457
|
+
const currentAutocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
458
|
+
if (showAutocompleteRef.current && currentAutocomplete.visible && currentAutocomplete.suggestions.length > 0) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const history = historyRef.current;
|
|
462
|
+
if (history.length === 0) return;
|
|
463
|
+
if (historyIndexRef.current === -1) return;
|
|
464
|
+
if (historyIndexRef.current >= history.length - 1) {
|
|
465
|
+
historyIndexRef.current = -1;
|
|
466
|
+
const nextValue = historyDraftRef.current || '';
|
|
467
|
+
const nextAutocomplete = getAutocomplete(nextValue, nextValue.length);
|
|
468
|
+
const nextShowAutocomplete = nextAutocomplete.visible || Boolean(nextAutocomplete.argumentHint);
|
|
469
|
+
inputRef.current = nextValue;
|
|
470
|
+
cursorRef.current = nextValue.length;
|
|
471
|
+
selectedIndexRef.current = 0;
|
|
472
|
+
showAutocompleteRef.current = nextShowAutocomplete;
|
|
473
|
+
setInputState({ value: nextValue, cursor: nextValue.length, selectedIndex: 0, showAutocomplete: nextShowAutocomplete });
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
historyIndexRef.current += 1;
|
|
477
|
+
const nextValue = history[historyIndexRef.current] || '';
|
|
478
|
+
const nextAutocomplete = getAutocomplete(nextValue, nextValue.length);
|
|
479
|
+
const nextShowAutocomplete = nextAutocomplete.visible || Boolean(nextAutocomplete.argumentHint);
|
|
480
|
+
inputRef.current = nextValue;
|
|
481
|
+
cursorRef.current = nextValue.length;
|
|
482
|
+
selectedIndexRef.current = 0;
|
|
483
|
+
showAutocompleteRef.current = nextShowAutocomplete;
|
|
484
|
+
setInputState({ value: nextValue, cursor: nextValue.length, selectedIndex: 0, showAutocomplete: nextShowAutocomplete });
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (commandHandler && key.tab) {
|
|
489
|
+
const autocomplete = getAutocomplete(inputRef.current, cursorRef.current);
|
|
490
|
+
if (autocomplete.visible && autocomplete.suggestions.length > 0) {
|
|
491
|
+
const chosen = applyAutocomplete(selectedIndexRef.current);
|
|
492
|
+
if (chosen) {
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (isBackspaceKey) {
|
|
499
|
+
if (cursorRef.current > 0) {
|
|
500
|
+
const currentValue = inputRef.current;
|
|
501
|
+
const currentCursor = cursorRef.current;
|
|
502
|
+
const newValue = currentValue.slice(0, currentCursor - 1) + currentValue.slice(currentCursor);
|
|
503
|
+
const nextCursor = Math.max(0, currentCursor - 1);
|
|
504
|
+
updateValue(newValue, nextCursor);
|
|
505
|
+
}
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (isDeleteKey) {
|
|
510
|
+
if (cursorRef.current >= inputRef.current.length) {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const currentValue = inputRef.current;
|
|
514
|
+
const currentCursor = cursorRef.current;
|
|
515
|
+
const newValue = currentValue.slice(0, currentCursor) + currentValue.slice(currentCursor + 1);
|
|
516
|
+
updateValue(newValue, currentCursor);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (input && input.length >= 1 && !input.startsWith('\x1b') && !key.meta) {
|
|
521
|
+
insertText(input);
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
[applyAutocomplete, commandHandler, findLineEnd, findLineStart, getAutocomplete, handleSubmit, insertText, updateCursor, updateValue]
|
|
525
|
+
);
|
|
526
|
+
|
|
527
|
+
useEffect(() => {
|
|
528
|
+
if (!isActive) {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
setRawMode(true);
|
|
533
|
+
return () => {
|
|
534
|
+
setRawMode(false);
|
|
535
|
+
};
|
|
536
|
+
}, [isActive, setRawMode]);
|
|
537
|
+
|
|
538
|
+
useEffect(() => {
|
|
539
|
+
if (!isActive) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const handleData = (data: string) => {
|
|
544
|
+
const keypress = parseKeypress(data);
|
|
545
|
+
const normalizedName = keypress.sequence === '\x7f' || keypress.sequence === '\x1b\x7f' ? 'backspace' : keypress.name;
|
|
546
|
+
|
|
547
|
+
if (normalizedName !== 'backspace' && normalizedName !== 'delete' && keypress.sequence.includes('\x7f')) {
|
|
548
|
+
const deleteCount = keypress.sequence.match(/\x7f/g)?.length || 0;
|
|
549
|
+
let currentValue = inputRef.current;
|
|
550
|
+
let currentCursor = cursorRef.current;
|
|
551
|
+
|
|
552
|
+
for (let index = 0; index < deleteCount; index += 1) {
|
|
553
|
+
if (currentCursor === 0) {
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
currentValue = currentValue.slice(0, currentCursor - 1) + currentValue.slice(currentCursor);
|
|
557
|
+
currentCursor -= 1;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (currentValue !== inputRef.current) {
|
|
561
|
+
updateValue(currentValue, currentCursor);
|
|
562
|
+
}
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const key = {
|
|
567
|
+
upArrow: normalizedName === 'up',
|
|
568
|
+
downArrow: normalizedName === 'down',
|
|
569
|
+
leftArrow: normalizedName === 'left',
|
|
570
|
+
rightArrow: normalizedName === 'right',
|
|
571
|
+
pageDown: normalizedName === 'pagedown',
|
|
572
|
+
pageUp: normalizedName === 'pageup',
|
|
573
|
+
return: normalizedName === 'return',
|
|
574
|
+
escape: normalizedName === 'escape',
|
|
575
|
+
ctrl: keypress.ctrl,
|
|
576
|
+
shift: keypress.shift,
|
|
577
|
+
tab: normalizedName === 'tab',
|
|
578
|
+
backspace: normalizedName === 'backspace',
|
|
579
|
+
delete: normalizedName === 'delete',
|
|
580
|
+
meta: keypress.meta || normalizedName === 'escape' || keypress.option,
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
let input = keypress.ctrl ? keypress.name : keypress.sequence;
|
|
584
|
+
if (normalizedName !== keypress.name && keypress.ctrl) {
|
|
585
|
+
input = normalizedName;
|
|
586
|
+
}
|
|
587
|
+
if (nonAlphanumericKeys.includes(normalizedName)) {
|
|
588
|
+
input = '';
|
|
589
|
+
}
|
|
590
|
+
if (input.startsWith('\u001b')) {
|
|
591
|
+
input = input.slice(1);
|
|
592
|
+
}
|
|
593
|
+
if (input.length === 1 && /[A-Z]/.test(input)) {
|
|
594
|
+
key.shift = true;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
handleInput(input, key);
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
internal_eventEmitter?.on('input', handleData);
|
|
601
|
+
return () => {
|
|
602
|
+
internal_eventEmitter?.removeListener('input', handleData);
|
|
603
|
+
};
|
|
604
|
+
}, [handleInput, internal_eventEmitter, isActive, updateValue]);
|
|
605
|
+
|
|
606
|
+
useEffect(() => {
|
|
607
|
+
if (commandHandler) {
|
|
608
|
+
const unregister = commandHandler.registerInputPane(addLog, handleSubmit);
|
|
609
|
+
return unregister;
|
|
610
|
+
}
|
|
611
|
+
}, [commandHandler, addLog, handleSubmit]);
|
|
612
|
+
|
|
613
|
+
useEffect(() => {
|
|
614
|
+
if (commandHandler) {
|
|
615
|
+
commandHandler.setExitOnEmptyInput(exitOnEmptyInput);
|
|
616
|
+
}
|
|
617
|
+
}, [commandHandler, exitOnEmptyInput]);
|
|
618
|
+
|
|
619
|
+
const beforeCursor = displayValue.slice(0, cursorPosition);
|
|
620
|
+
const afterCursor = displayValue.slice(cursorPosition);
|
|
621
|
+
const beforeLines = beforeCursor.split('\n');
|
|
622
|
+
const afterLines = afterCursor.split('\n');
|
|
623
|
+
const lines = displayValue.split('\n');
|
|
624
|
+
const cursorLineIndex = beforeLines.length - 1;
|
|
625
|
+
const cursorLineBefore = beforeLines[cursorLineIndex] || '';
|
|
626
|
+
const cursorLineAfter = afterLines[0] || '';
|
|
627
|
+
const showPlaceholder = !displayValue && placeholder && isActive;
|
|
628
|
+
const activeCursorText = showPlaceholder ? placeholder : cursorLineAfter;
|
|
629
|
+
const [cursorGlyph = ' '] = Array.from(activeCursorText);
|
|
630
|
+
const cursorSuffix = activeCursorText.slice(cursorGlyph.length);
|
|
631
|
+
|
|
632
|
+
const autocomplete = useMemo(() => {
|
|
633
|
+
return getAutocomplete(displayValue, cursorPosition);
|
|
634
|
+
}, [cursorPosition, displayValue, getAutocomplete]);
|
|
635
|
+
|
|
636
|
+
useEffect(() => {
|
|
637
|
+
if (!commandHandler) return;
|
|
638
|
+
const nextVisible = visible && showAutocomplete && autocomplete.visible;
|
|
639
|
+
const nextSuggestions = nextVisible ? autocomplete.suggestions : [];
|
|
640
|
+
const nextArgumentHint = visible && showAutocomplete ? autocomplete.argumentHint : undefined;
|
|
641
|
+
const nextIndex = nextVisible ? selectedIndex : 0;
|
|
642
|
+
const prev = autocompleteStateRef.current;
|
|
643
|
+
if (
|
|
644
|
+
prev.visible === nextVisible &&
|
|
645
|
+
prev.selectedIndex === nextIndex &&
|
|
646
|
+
prev.argumentHint === nextArgumentHint &&
|
|
647
|
+
prev.suggestions.length === nextSuggestions.length &&
|
|
648
|
+
prev.suggestions.every((suggestion, index) => suggestion.display === nextSuggestions[index]?.display && suggestion.value === nextSuggestions[index]?.value)
|
|
649
|
+
) {
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
autocompleteStateRef.current = { suggestions: nextSuggestions, argumentHint: nextArgumentHint, selectedIndex: nextIndex, visible: nextVisible };
|
|
653
|
+
setAutocompleteState(autocompleteStateRef.current);
|
|
654
|
+
}, [autocomplete.argumentHint, autocomplete.suggestions, autocomplete.visible, commandHandler, selectedIndex, showAutocomplete, visible]);
|
|
655
|
+
|
|
656
|
+
if (!visible) {
|
|
657
|
+
return null;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (!showPrompt) {
|
|
661
|
+
return (
|
|
662
|
+
<Box>
|
|
663
|
+
<Text>{cursorLineBefore}</Text>
|
|
664
|
+
{isActive ? (
|
|
665
|
+
<Text backgroundColor="white" color="black">
|
|
666
|
+
{cursorGlyph}
|
|
667
|
+
</Text>
|
|
668
|
+
) : (
|
|
669
|
+
<Text />
|
|
670
|
+
)}
|
|
671
|
+
<Text>{cursorSuffix}</Text>
|
|
672
|
+
{!displayValue && placeholder && !isActive && <Text color="gray">{placeholder}</Text>}
|
|
673
|
+
</Box>
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
return (
|
|
678
|
+
<Box flexDirection="column">
|
|
679
|
+
{lines.map((line, index) => {
|
|
680
|
+
const prefix = index === 0 ? '> ' : ' ';
|
|
681
|
+
if (index === cursorLineIndex) {
|
|
682
|
+
return (
|
|
683
|
+
<Box key={`line-${index}`}>
|
|
684
|
+
<Text color="green">{prefix}</Text>
|
|
685
|
+
<Text>{cursorLineBefore}</Text>
|
|
686
|
+
<Text backgroundColor="white" color="black">
|
|
687
|
+
{cursorGlyph}
|
|
688
|
+
</Text>
|
|
689
|
+
<Text>{cursorSuffix}</Text>
|
|
690
|
+
</Box>
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
return (
|
|
694
|
+
<Box key={`line-${index}`}>
|
|
695
|
+
<Text color="green">{prefix}</Text>
|
|
696
|
+
<Text>{line}</Text>
|
|
697
|
+
</Box>
|
|
698
|
+
);
|
|
699
|
+
})}
|
|
700
|
+
</Box>
|
|
701
|
+
);
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
export default InputReadline;
|