@golba98/codexa 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +320 -0
- package/bin/codexa.js +445 -0
- package/package.json +45 -0
- package/scripts/audit-codexa-capabilities.mjs +466 -0
- package/scripts/smoke-terminal-bench.mjs +35 -0
- package/src/app.tsx +4561 -0
- package/src/appRenderStability.test.ts +131 -0
- package/src/commands/handler.test.ts +643 -0
- package/src/commands/handler.ts +875 -0
- package/src/config/launchArgs.test.ts +158 -0
- package/src/config/launchArgs.ts +186 -0
- package/src/config/layeredConfig.test.ts +143 -0
- package/src/config/layeredConfig.ts +836 -0
- package/src/config/persistence.test.ts +110 -0
- package/src/config/persistence.ts +311 -0
- package/src/config/runtimeConfig.test.ts +218 -0
- package/src/config/runtimeConfig.ts +554 -0
- package/src/config/settings.test.ts +155 -0
- package/src/config/settings.ts +401 -0
- package/src/config/toml-serialize.ts +98 -0
- package/src/config/trustStore.test.ts +29 -0
- package/src/config/trustStore.ts +68 -0
- package/src/core/attachments.test.ts +155 -0
- package/src/core/attachments.ts +71 -0
- package/src/core/auth/codexAuth.test.ts +68 -0
- package/src/core/auth/codexAuth.ts +359 -0
- package/src/core/cleanupFastFail.test.ts +76 -0
- package/src/core/cleanupFastFail.ts +67 -0
- package/src/core/clipboard.ts +24 -0
- package/src/core/codex.ts +124 -0
- package/src/core/codexExecArgs.test.ts +195 -0
- package/src/core/codexExecArgs.ts +152 -0
- package/src/core/codexLaunch.test.ts +205 -0
- package/src/core/codexLaunch.ts +162 -0
- package/src/core/codexPrompt.test.ts +252 -0
- package/src/core/codexPrompt.ts +428 -0
- package/src/core/executables/claudeExecutable.ts +63 -0
- package/src/core/executables/codexExecutable.test.ts +212 -0
- package/src/core/executables/codexExecutable.ts +159 -0
- package/src/core/executables/executableResolver.test.ts +129 -0
- package/src/core/executables/executableResolver.ts +138 -0
- package/src/core/executables/geminiExecutable.test.ts +116 -0
- package/src/core/executables/geminiExecutable.ts +78 -0
- package/src/core/executables/pathSanityScan.test.ts +47 -0
- package/src/core/githubDiagnostics.test.ts +92 -0
- package/src/core/githubDiagnostics.ts +222 -0
- package/src/core/hollowResponseFormat.test.ts +58 -0
- package/src/core/hollowResponseFormat.ts +39 -0
- package/src/core/inputDebug.ts +51 -0
- package/src/core/launchContext.test.ts +157 -0
- package/src/core/launchContext.ts +266 -0
- package/src/core/models/codexCapabilities.test.ts +45 -0
- package/src/core/models/codexCapabilities.ts +95 -0
- package/src/core/models/codexModelCapabilities.test.ts +246 -0
- package/src/core/models/codexModelCapabilities.ts +571 -0
- package/src/core/models/modelSpecs.test.ts +283 -0
- package/src/core/models/modelSpecs.ts +300 -0
- package/src/core/perf/profiler.ts +125 -0
- package/src/core/perf/renderDebug.test.ts +230 -0
- package/src/core/perf/renderDebug.ts +373 -0
- package/src/core/planStorage.test.ts +143 -0
- package/src/core/planStorage.ts +141 -0
- package/src/core/process/CommandRunner.test.ts +105 -0
- package/src/core/process/CommandRunner.ts +269 -0
- package/src/core/process/processValidation.ts +101 -0
- package/src/core/projectInstructions.test.ts +50 -0
- package/src/core/projectInstructions.ts +54 -0
- package/src/core/providerLauncher/launcher.test.ts +238 -0
- package/src/core/providerLauncher/launcher.ts +203 -0
- package/src/core/providerLauncher/registry.test.ts +324 -0
- package/src/core/providerLauncher/registry.ts +253 -0
- package/src/core/providerLauncher/types.ts +84 -0
- package/src/core/providerLauncher/workspaceConfig.test.ts +638 -0
- package/src/core/providerLauncher/workspaceConfig.ts +407 -0
- package/src/core/providerRuntime/anthropic.test.ts +1120 -0
- package/src/core/providerRuntime/anthropic.ts +576 -0
- package/src/core/providerRuntime/capabilityProfile.test.ts +311 -0
- package/src/core/providerRuntime/capabilityProfile.ts +288 -0
- package/src/core/providerRuntime/claudeCodeDiscovery.ts +446 -0
- package/src/core/providerRuntime/contextMetadata.test.ts +468 -0
- package/src/core/providerRuntime/contextMetadata.ts +409 -0
- package/src/core/providerRuntime/gemini.test.ts +437 -0
- package/src/core/providerRuntime/gemini.ts +784 -0
- package/src/core/providerRuntime/lmstudio.test.ts +168 -0
- package/src/core/providerRuntime/lmstudio.ts +118 -0
- package/src/core/providerRuntime/local.test.ts +787 -0
- package/src/core/providerRuntime/local.ts +754 -0
- package/src/core/providerRuntime/models.ts +150 -0
- package/src/core/providerRuntime/reasoning.ts +17 -0
- package/src/core/providerRuntime/registry.test.ts +233 -0
- package/src/core/providerRuntime/registry.ts +203 -0
- package/src/core/providerRuntime/types.ts +103 -0
- package/src/core/providers/codexJsonStream.test.ts +148 -0
- package/src/core/providers/codexJsonStream.ts +305 -0
- package/src/core/providers/codexSubprocess.test.ts +68 -0
- package/src/core/providers/codexSubprocess.ts +372 -0
- package/src/core/providers/codexTranscript.test.ts +284 -0
- package/src/core/providers/codexTranscript.ts +695 -0
- package/src/core/providers/openaiNative.ts +13 -0
- package/src/core/providers/registry.ts +21 -0
- package/src/core/providers/types.ts +59 -0
- package/src/core/terminal/terminalCapabilities.test.ts +93 -0
- package/src/core/terminal/terminalCapabilities.ts +100 -0
- package/src/core/terminal/terminalControl.test.ts +75 -0
- package/src/core/terminal/terminalControl.ts +147 -0
- package/src/core/terminal/terminalSanitize.test.ts +22 -0
- package/src/core/terminal/terminalSanitize.ts +147 -0
- package/src/core/terminal/terminalSelection.test.ts +42 -0
- package/src/core/terminal/terminalSelection.ts +66 -0
- package/src/core/terminal/terminalTitle.test.ts +328 -0
- package/src/core/terminal/terminalTitle.ts +483 -0
- package/src/core/workspaceActivity.test.ts +163 -0
- package/src/core/workspaceActivity.ts +380 -0
- package/src/core/workspaceGuard.test.ts +151 -0
- package/src/core/workspaceGuard.ts +288 -0
- package/src/core/workspaceRoot.test.ts +23 -0
- package/src/core/workspaceRoot.ts +47 -0
- package/src/exec.test.ts +13 -0
- package/src/exec.ts +72 -0
- package/src/headless/execArgs.test.ts +147 -0
- package/src/headless/execArgs.ts +294 -0
- package/src/headless/execRunner.test.ts +434 -0
- package/src/headless/execRunner.ts +304 -0
- package/src/index.test.tsx +618 -0
- package/src/index.tsx +296 -0
- package/src/session/appSession.test.ts +897 -0
- package/src/session/appSession.ts +761 -0
- package/src/session/chatLifecycle.test.ts +64 -0
- package/src/session/chatLifecycle.ts +951 -0
- package/src/session/liveRenderScheduler.test.ts +201 -0
- package/src/session/liveRenderScheduler.ts +214 -0
- package/src/session/planFlow.test.ts +103 -0
- package/src/session/planFlow.ts +149 -0
- package/src/session/planTranscript.test.ts +65 -0
- package/src/session/planTranscript.ts +15 -0
- package/src/session/promptRunSchedule.test.ts +36 -0
- package/src/session/promptRunSchedule.ts +26 -0
- package/src/session/types.ts +228 -0
- package/src/test/runtimeTestUtils.ts +14 -0
- package/src/types/react-dom.d.ts +3 -0
- package/src/ui/ActionRequiredBlock.tsx +38 -0
- package/src/ui/ActivityBars.tsx +68 -0
- package/src/ui/ActivityIndicator.test.tsx +58 -0
- package/src/ui/ActivityIndicator.tsx +58 -0
- package/src/ui/AgentBlock.test.ts +6 -0
- package/src/ui/AgentBlock.tsx +130 -0
- package/src/ui/AnimatedStatusText.test.ts +16 -0
- package/src/ui/AnimatedStatusText.tsx +69 -0
- package/src/ui/AppShell.test.tsx +1739 -0
- package/src/ui/AppShell.tsx +698 -0
- package/src/ui/AttachmentImportPanel.test.tsx +204 -0
- package/src/ui/AttachmentImportPanel.tsx +98 -0
- package/src/ui/AuthPanel.tsx +113 -0
- package/src/ui/BackendPicker.tsx +28 -0
- package/src/ui/BottomComposer.test.ts +674 -0
- package/src/ui/BottomComposer.tsx +1028 -0
- package/src/ui/CodexLogo.tsx +55 -0
- package/src/ui/DashCard.tsx +82 -0
- package/src/ui/Markdown.test.ts +157 -0
- package/src/ui/Markdown.tsx +310 -0
- package/src/ui/ModePicker.tsx +27 -0
- package/src/ui/ModelPicker.tsx +31 -0
- package/src/ui/ModelPickerProviderScope.test.tsx +411 -0
- package/src/ui/ModelPickerScreen.test.tsx +99 -0
- package/src/ui/ModelPickerScreen.tsx +416 -0
- package/src/ui/ModelPickerState.test.tsx +151 -0
- package/src/ui/ModelReasoningPicker.test.tsx +447 -0
- package/src/ui/ModelReasoningPicker.tsx +458 -0
- package/src/ui/Panel.tsx +51 -0
- package/src/ui/PermissionsPanel.tsx +78 -0
- package/src/ui/PlanActionPicker.tsx +119 -0
- package/src/ui/PlanReviewPanel.test.tsx +267 -0
- package/src/ui/PlanReviewPanel.tsx +212 -0
- package/src/ui/PromptCardBorder.test.tsx +161 -0
- package/src/ui/ProviderPicker.test.tsx +289 -0
- package/src/ui/ProviderPicker.tsx +321 -0
- package/src/ui/ProviderShortcut.test.tsx +143 -0
- package/src/ui/ReasoningPicker.tsx +46 -0
- package/src/ui/RunFooter.tsx +65 -0
- package/src/ui/SelectionPanel.tsx +67 -0
- package/src/ui/SettingsPanel.test.tsx +233 -0
- package/src/ui/SettingsPanel.tsx +156 -0
- package/src/ui/Spinner.tsx +25 -0
- package/src/ui/StaticIntroItem.tsx +54 -0
- package/src/ui/StaticTranscriptItem.tsx +56 -0
- package/src/ui/TextEntryPanel.tsx +139 -0
- package/src/ui/ThemePicker.tsx +31 -0
- package/src/ui/ThinkingBlock.tsx +100 -0
- package/src/ui/Timeline.test.ts +2067 -0
- package/src/ui/Timeline.tsx +1472 -0
- package/src/ui/TimelineNavigation.test.tsx +201 -0
- package/src/ui/TopHeader.test.tsx +239 -0
- package/src/ui/TopHeader.tsx +257 -0
- package/src/ui/TurnGroup.test.tsx +365 -0
- package/src/ui/TurnGroup.tsx +657 -0
- package/src/ui/busyStatusAnimation.test.ts +30 -0
- package/src/ui/busyStatusAnimation.ts +11 -0
- package/src/ui/commandNormalize.test.ts +142 -0
- package/src/ui/commandNormalize.ts +66 -0
- package/src/ui/diffRenderer.test.ts +102 -0
- package/src/ui/diffRenderer.ts +116 -0
- package/src/ui/focus.ts +61 -0
- package/src/ui/focusFlow.test.tsx +1098 -0
- package/src/ui/inputBuffer.test.ts +151 -0
- package/src/ui/inputBuffer.ts +203 -0
- package/src/ui/layout.test.ts +145 -0
- package/src/ui/layout.ts +287 -0
- package/src/ui/modeDisplay.test.ts +42 -0
- package/src/ui/modeDisplay.ts +52 -0
- package/src/ui/outputPipeline.ts +64 -0
- package/src/ui/progressEntries.ts +156 -0
- package/src/ui/runActivityView.test.ts +89 -0
- package/src/ui/runActivityView.ts +37 -0
- package/src/ui/runLifecycleView.test.tsx +237 -0
- package/src/ui/slashCommands.ts +41 -0
- package/src/ui/statusRenderIsolation.test.tsx +654 -0
- package/src/ui/terminalAnswerFormat.test.ts +19 -0
- package/src/ui/terminalAnswerFormat.ts +128 -0
- package/src/ui/textLayout.test.ts +18 -0
- package/src/ui/textLayout.ts +338 -0
- package/src/ui/theme.tsx +395 -0
- package/src/ui/themeFlow.test.ts +53 -0
- package/src/ui/themeFlow.ts +41 -0
- package/src/ui/timelineMeasure.ts +3088 -0
- package/src/ui/timelineMeasureCache.test.ts +986 -0
- package/src/ui/useThrottledValue.ts +31 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
clampScrollToCursor,
|
|
5
|
+
createInputViewport,
|
|
6
|
+
deleteInputBackward,
|
|
7
|
+
deleteInputForward,
|
|
8
|
+
insertInputText,
|
|
9
|
+
locateCursor,
|
|
10
|
+
moveCursorLeft,
|
|
11
|
+
moveCursorRight,
|
|
12
|
+
normalizeInputText,
|
|
13
|
+
stripMouseEscapes,
|
|
14
|
+
wrapInputRows,
|
|
15
|
+
} from "./inputBuffer.js";
|
|
16
|
+
|
|
17
|
+
test("normalizes windows line endings for the composer buffer", () => {
|
|
18
|
+
assert.equal(normalizeInputText("a\r\nb\rc"), "a\nb\nc");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("wraps multiline input into stable viewport rows", () => {
|
|
22
|
+
const rows = wrapInputRows("alpha\nbeta gamma", 5);
|
|
23
|
+
assert.deepEqual(rows.map((row) => row.text), ["alpha", "beta ", "gamma"]);
|
|
24
|
+
assert.deepEqual(rows.map((row) => row.breakType), ["hard", "soft", "end"]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("keeps the cursor visible by scrolling the composer viewport", () => {
|
|
28
|
+
assert.equal(clampScrollToCursor(0, 7, 4), 4);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("keeps cursor mapping stable at hard newlines and soft wrap boundaries", () => {
|
|
32
|
+
const multilineRows = wrapInputRows("alpha\nbeta", 20);
|
|
33
|
+
assert.deepEqual(locateCursor(multilineRows, 5), { row: 0, column: 5 });
|
|
34
|
+
assert.deepEqual(locateCursor(multilineRows, 6), { row: 1, column: 0 });
|
|
35
|
+
|
|
36
|
+
const wrappedRows = wrapInputRows("alpha beta", 5);
|
|
37
|
+
assert.deepEqual(locateCursor(wrappedRows, 5), { row: 0, column: 5 });
|
|
38
|
+
assert.deepEqual(locateCursor(wrappedRows, 6), { row: 1, column: 1 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("uses code-point-safe cursor movement and deletion", () => {
|
|
42
|
+
const text = "A😀B";
|
|
43
|
+
assert.equal(moveCursorRight(text, 0), 1);
|
|
44
|
+
assert.equal(moveCursorRight(text, 1), 3);
|
|
45
|
+
assert.equal(moveCursorLeft(text, 3), 1);
|
|
46
|
+
|
|
47
|
+
const inserted = insertInputText({ value: "A", cursorOffset: 1, text: "\nB" });
|
|
48
|
+
assert.deepEqual(inserted, { value: "A\nB", cursorOffset: 3 });
|
|
49
|
+
|
|
50
|
+
const deleted = deleteInputBackward({ value: text, cursorOffset: 3 });
|
|
51
|
+
assert.deepEqual(deleted, { value: "AB", cursorOffset: 1 });
|
|
52
|
+
|
|
53
|
+
const forwardDeleted = deleteInputForward({ value: text, cursorOffset: 1 });
|
|
54
|
+
assert.deepEqual(forwardDeleted, { value: "AB", cursorOffset: 1 });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("creates a bounded input viewport for large pasted content", () => {
|
|
58
|
+
const viewport = createInputViewport({
|
|
59
|
+
text: Array.from({ length: 12 }, (_, index) => `line-${index + 1}`).join("\n"),
|
|
60
|
+
cursorOffset: "line-1\nline-2\nline-3\nline-4\nline-5\nline-6\nline-7\nline-8\nline-9\nline-10\nline-11\nline-12".length,
|
|
61
|
+
width: 20,
|
|
62
|
+
maxVisibleRows: 4,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
assert.equal(viewport.visibleRows.length, 4);
|
|
66
|
+
assert.equal(viewport.visibleRows[0]?.text, "line-9");
|
|
67
|
+
assert.equal(viewport.visibleRows[3]?.text, "line-12");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("reclamps scroll state when resize reduces the wrapped row count", () => {
|
|
71
|
+
const narrowViewport = createInputViewport({
|
|
72
|
+
text: "alpha beta gamma delta epsilon",
|
|
73
|
+
cursorOffset: "alpha beta gamma delta epsilon".length,
|
|
74
|
+
width: 5,
|
|
75
|
+
maxVisibleRows: 2,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const wideViewport = createInputViewport({
|
|
79
|
+
text: "alpha beta gamma delta epsilon",
|
|
80
|
+
cursorOffset: "alpha beta gamma delta epsilon".length,
|
|
81
|
+
width: 40,
|
|
82
|
+
maxVisibleRows: 2,
|
|
83
|
+
scrollRow: narrowViewport.scrollRow,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
assert.equal(narrowViewport.scrollRow > 0, true);
|
|
87
|
+
assert.equal(wideViewport.scrollRow, 0);
|
|
88
|
+
assert.deepEqual(wideViewport.visibleRows.map((row) => row.text), ["alpha beta gamma delta epsilon"]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("strips leaked SGR mouse escape sequence fragments from input", () => {
|
|
92
|
+
const leaked = "[<0;26;24M[<0;26;24m";
|
|
93
|
+
assert.equal(normalizeInputText(leaked), "");
|
|
94
|
+
|
|
95
|
+
const partial = "text[<0;26;24Mmore";
|
|
96
|
+
assert.equal(normalizeInputText(partial), "textmore");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("stripMouseEscapes: removes complete SGR sequences (ESC-prefixed)", () => {
|
|
100
|
+
assert.equal(stripMouseEscapes("\x1b[<0;83;19M"), "");
|
|
101
|
+
assert.equal(stripMouseEscapes("\x1b[<64;83;19M"), "");
|
|
102
|
+
assert.equal(stripMouseEscapes("\x1b[<64;83;19m"), ""); // lowercase m
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("stripMouseEscapes: removes leaked SGR fragments (ESC already stripped by readline)", () => {
|
|
106
|
+
assert.equal(stripMouseEscapes("[<0;83;19M"), "");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("stripMouseEscapes: strips mouse sequences embedded in surrounding text", () => {
|
|
110
|
+
assert.equal(stripMouseEscapes("hello\x1b[<0;83;19M"), "hello");
|
|
111
|
+
assert.equal(stripMouseEscapes("\x1b[<0;83;19Mhello"), "hello");
|
|
112
|
+
assert.equal(stripMouseEscapes("hello[<64;83;19Mworld"), "helloworld");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("stripMouseEscapes: preserves normal text and returns it unchanged", () => {
|
|
116
|
+
assert.equal(stripMouseEscapes("hello world"), "hello world");
|
|
117
|
+
assert.equal(stripMouseEscapes("git status"), "git status");
|
|
118
|
+
assert.equal(stripMouseEscapes(""), "");
|
|
119
|
+
assert.equal(stripMouseEscapes("I"), "I");
|
|
120
|
+
assert.equal(stripMouseEscapes("yes"), "yes");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// NOTE: Partial chunk splitting (e.g. "\x1b[<0;" then "83;19M" as two separate writes)
|
|
124
|
+
// is not filterable at the string level — each chunk alone does not match the pattern.
|
|
125
|
+
// Robust partial-chunk handling requires stateful stdin buffering (not implemented here
|
|
126
|
+
// or in BottomComposer). In practice, terminals send mouse sequences as atomic stdin
|
|
127
|
+
// writes so this case does not arise in normal usage.
|
|
128
|
+
|
|
129
|
+
test("robustness: rapid sequential typing and deletion", () => {
|
|
130
|
+
let state = { value: "", cursorOffset: 0 };
|
|
131
|
+
|
|
132
|
+
// Simulate typing "hello"
|
|
133
|
+
for (const char of "hello") {
|
|
134
|
+
state = insertInputText({ ...state, text: char });
|
|
135
|
+
}
|
|
136
|
+
assert.deepEqual(state, { value: "hello", cursorOffset: 5 });
|
|
137
|
+
|
|
138
|
+
// Simulate backspacing "o" and "l"
|
|
139
|
+
state = deleteInputBackward(state);
|
|
140
|
+
state = deleteInputBackward(state);
|
|
141
|
+
assert.deepEqual(state, { value: "hel", cursorOffset: 3 });
|
|
142
|
+
|
|
143
|
+
// Simulate inserting "p" in the middle
|
|
144
|
+
state.cursorOffset = 2; // after "e"
|
|
145
|
+
state = insertInputText({ ...state, text: "p" });
|
|
146
|
+
assert.deepEqual(state, { value: "hepl", cursorOffset: 3 });
|
|
147
|
+
|
|
148
|
+
// Simulate forward delete of "l"
|
|
149
|
+
state = deleteInputForward(state);
|
|
150
|
+
assert.deepEqual(state, { value: "hep", cursorOffset: 3 });
|
|
151
|
+
});
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { WrappedTextRow } from "./textLayout.js";
|
|
2
|
+
import { getTextWidth, normalizeLineBreaks, wrapTextRows } from "./textLayout.js";
|
|
3
|
+
import { sanitizeTerminalInput } from "../core/terminal/terminalSanitize.js";
|
|
4
|
+
|
|
5
|
+
export type WrappedInputRow = WrappedTextRow;
|
|
6
|
+
|
|
7
|
+
export interface InputViewport {
|
|
8
|
+
rows: WrappedInputRow[];
|
|
9
|
+
visibleRows: WrappedInputRow[];
|
|
10
|
+
cursorRow: number;
|
|
11
|
+
cursorColumn: number;
|
|
12
|
+
scrollRow: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isHighSurrogate(code: number): boolean {
|
|
16
|
+
return code >= 0xd800 && code <= 0xdbff;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isLowSurrogate(code: number): boolean {
|
|
20
|
+
return code >= 0xdc00 && code <= 0xdfff;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const LEAKED_SGR_MOUSE_PATTERN = /(?:\x1b)?\[<\d+;\d+;\d+[Mm]/g;
|
|
24
|
+
|
|
25
|
+
export function normalizeInputText(text: string): string {
|
|
26
|
+
if (!text) return "";
|
|
27
|
+
// Strip out leaked SGR mouse coordinates that might have been emitted by
|
|
28
|
+
// the terminal without their ESC prefix (swallowed by readline).
|
|
29
|
+
const withoutMouse = text.replace(LEAKED_SGR_MOUSE_PATTERN, "");
|
|
30
|
+
return normalizeLineBreaks(sanitizeTerminalInput(withoutMouse));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function stripMouseEscapes(input: string): string {
|
|
34
|
+
return input.replace(LEAKED_SGR_MOUSE_PATTERN, "");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function normalizeCursorOffset(text: string, cursorOffset: number): number {
|
|
38
|
+
const safeCursor = Math.max(0, Math.min(cursorOffset, text.length));
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
safeCursor > 0
|
|
42
|
+
&& safeCursor < text.length
|
|
43
|
+
&& isHighSurrogate(text.charCodeAt(safeCursor - 1))
|
|
44
|
+
&& isLowSurrogate(text.charCodeAt(safeCursor))
|
|
45
|
+
) {
|
|
46
|
+
return safeCursor - 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return safeCursor;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ─── Cursor movement ─────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
export function moveCursorLeft(text: string, cursorOffset: number): number {
|
|
55
|
+
const safeCursor = normalizeCursorOffset(text, cursorOffset);
|
|
56
|
+
if (safeCursor <= 0) return 0;
|
|
57
|
+
|
|
58
|
+
let nextCursor = safeCursor - 1;
|
|
59
|
+
if (nextCursor > 0 && isLowSurrogate(text.charCodeAt(nextCursor)) && isHighSurrogate(text.charCodeAt(nextCursor - 1))) {
|
|
60
|
+
nextCursor -= 1;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return nextCursor;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function moveCursorRight(text: string, cursorOffset: number): number {
|
|
67
|
+
const safeCursor = normalizeCursorOffset(text, cursorOffset);
|
|
68
|
+
if (safeCursor >= text.length) return text.length;
|
|
69
|
+
|
|
70
|
+
if (
|
|
71
|
+
isHighSurrogate(text.charCodeAt(safeCursor))
|
|
72
|
+
&& safeCursor + 1 < text.length
|
|
73
|
+
&& isLowSurrogate(text.charCodeAt(safeCursor + 1))
|
|
74
|
+
) {
|
|
75
|
+
return safeCursor + 2;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return safeCursor + 1;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ─── Text mutations ───────────────────────────────────────────────────────────
|
|
82
|
+
|
|
83
|
+
export function insertInputText(params: {
|
|
84
|
+
value: string;
|
|
85
|
+
cursorOffset: number;
|
|
86
|
+
text: string;
|
|
87
|
+
}): { value: string; cursorOffset: number } {
|
|
88
|
+
const value = normalizeInputText(params.value);
|
|
89
|
+
const safeCursor = normalizeCursorOffset(value, params.cursorOffset);
|
|
90
|
+
const insertedText = normalizeInputText(params.text);
|
|
91
|
+
return {
|
|
92
|
+
value: value.slice(0, safeCursor) + insertedText + value.slice(safeCursor),
|
|
93
|
+
cursorOffset: safeCursor + insertedText.length,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function deleteInputBackward(params: {
|
|
98
|
+
value: string;
|
|
99
|
+
cursorOffset: number;
|
|
100
|
+
}): { value: string; cursorOffset: number } {
|
|
101
|
+
const value = normalizeInputText(params.value);
|
|
102
|
+
const safeCursor = normalizeCursorOffset(value, params.cursorOffset);
|
|
103
|
+
|
|
104
|
+
if (safeCursor <= 0) {
|
|
105
|
+
return { value, cursorOffset: 0 };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const previousCursor = moveCursorLeft(value, safeCursor);
|
|
109
|
+
return {
|
|
110
|
+
value: value.slice(0, previousCursor) + value.slice(safeCursor),
|
|
111
|
+
cursorOffset: previousCursor,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function deleteInputForward(params: {
|
|
116
|
+
value: string;
|
|
117
|
+
cursorOffset: number;
|
|
118
|
+
}): { value: string; cursorOffset: number } {
|
|
119
|
+
const value = normalizeInputText(params.value);
|
|
120
|
+
const safeCursor = normalizeCursorOffset(value, params.cursorOffset);
|
|
121
|
+
|
|
122
|
+
if (safeCursor >= value.length) {
|
|
123
|
+
return { value, cursorOffset: safeCursor };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const nextCursor = moveCursorRight(value, safeCursor);
|
|
127
|
+
return {
|
|
128
|
+
value: value.slice(0, safeCursor) + value.slice(nextCursor),
|
|
129
|
+
cursorOffset: safeCursor,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ─── Viewport ────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
export function wrapInputRows(text: string, width: number): WrappedInputRow[] {
|
|
136
|
+
return wrapTextRows(normalizeInputText(text), width);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function locateCursor(rows: WrappedInputRow[], cursorOffset: number) {
|
|
140
|
+
const safeCursor = Math.max(0, cursorOffset);
|
|
141
|
+
|
|
142
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
143
|
+
const row = rows[index]!;
|
|
144
|
+
if (safeCursor <= row.end) {
|
|
145
|
+
const relative = Math.max(0, Math.min(safeCursor, row.end) - row.start);
|
|
146
|
+
return {
|
|
147
|
+
row: index,
|
|
148
|
+
column: getTextWidth(row.text.slice(0, relative)),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const lastRow = rows[rows.length - 1]!;
|
|
154
|
+
return {
|
|
155
|
+
row: rows.length - 1,
|
|
156
|
+
column: getTextWidth(lastRow.text),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function clampScrollToCursor(
|
|
161
|
+
scrollRow: number,
|
|
162
|
+
cursorRow: number,
|
|
163
|
+
visibleRowCount: number,
|
|
164
|
+
totalRowCount = cursorRow + 1,
|
|
165
|
+
): number {
|
|
166
|
+
const safeVisibleRows = Math.max(1, visibleRowCount);
|
|
167
|
+
const maxScroll = Math.max(0, totalRowCount - safeVisibleRows);
|
|
168
|
+
let nextScrollRow = Math.max(0, Math.min(scrollRow, maxScroll));
|
|
169
|
+
|
|
170
|
+
if (cursorRow < nextScrollRow) {
|
|
171
|
+
nextScrollRow = cursorRow;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (cursorRow >= nextScrollRow + safeVisibleRows) {
|
|
175
|
+
nextScrollRow = cursorRow - safeVisibleRows + 1;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return Math.max(0, Math.min(nextScrollRow, maxScroll));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function createInputViewport(params: {
|
|
182
|
+
text: string;
|
|
183
|
+
cursorOffset: number;
|
|
184
|
+
width: number;
|
|
185
|
+
maxVisibleRows: number;
|
|
186
|
+
scrollRow?: number;
|
|
187
|
+
}): InputViewport {
|
|
188
|
+
const rows = wrapInputRows(params.text, params.width);
|
|
189
|
+
const cursor = locateCursor(rows, params.cursorOffset);
|
|
190
|
+
const nextScrollRow = clampScrollToCursor(params.scrollRow ?? 0, cursor.row, params.maxVisibleRows, rows.length);
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
rows,
|
|
194
|
+
visibleRows: rows.slice(nextScrollRow, nextScrollRow + Math.max(1, params.maxVisibleRows)),
|
|
195
|
+
cursorRow: cursor.row,
|
|
196
|
+
cursorColumn: cursor.column,
|
|
197
|
+
scrollRow: nextScrollRow,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function getComposerBodyWidth(totalWidth: number): number {
|
|
202
|
+
return Math.max(4, totalWidth - 4);
|
|
203
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
advanceTerminalViewport,
|
|
5
|
+
clampVisualText,
|
|
6
|
+
createTerminalViewport,
|
|
7
|
+
createLayoutSnapshot,
|
|
8
|
+
getShellHeight,
|
|
9
|
+
getShellWidth,
|
|
10
|
+
getUsableShellWidth,
|
|
11
|
+
getVisualWidth,
|
|
12
|
+
resolveStartupHeaderMode,
|
|
13
|
+
} from "./layout.js";
|
|
14
|
+
import { getHeaderHeroLayout, measureTopHeaderRows } from "./TopHeader.js";
|
|
15
|
+
|
|
16
|
+
test("leaves a one-column gutter to avoid edge-triggered scrollbars", () => {
|
|
17
|
+
assert.equal(getShellWidth(120), 119);
|
|
18
|
+
assert.equal(getShellWidth(80), 79);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("keeps a sensible minimum shell width on tiny terminals", () => {
|
|
22
|
+
assert.equal(getShellWidth(20), 20);
|
|
23
|
+
assert.equal(getShellWidth(10), 20);
|
|
24
|
+
assert.equal(getShellWidth(undefined), 119);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("derives usable shell width from the guttered viewport width", () => {
|
|
28
|
+
assert.equal(getUsableShellWidth(120, 6), 113);
|
|
29
|
+
assert.equal(getUsableShellWidth(20, 30), 1);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("leaves a one-row gutter to avoid edge-triggered bottom scrolling", () => {
|
|
33
|
+
assert.equal(getShellHeight(40), 39);
|
|
34
|
+
assert.equal(getShellHeight(24), 23);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("keeps a sensible minimum shell height on tiny terminals", () => {
|
|
38
|
+
assert.equal(getShellHeight(10), 10);
|
|
39
|
+
assert.equal(getShellHeight(5), 10);
|
|
40
|
+
assert.equal(getShellHeight(undefined), 23);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("keeps breakpoint modes stable at the edges", () => {
|
|
44
|
+
assert.equal(createLayoutSnapshot(110, 24).mode, "full");
|
|
45
|
+
assert.equal(createLayoutSnapshot(109, 24).mode, "compact");
|
|
46
|
+
assert.equal(createLayoutSnapshot(60, 24).mode, "compact");
|
|
47
|
+
assert.equal(createLayoutSnapshot(59, 24).mode, "micro");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("chooses startup header mode from measured row budget", () => {
|
|
51
|
+
assert.equal(resolveStartupHeaderMode({
|
|
52
|
+
cols: 120,
|
|
53
|
+
rows: 30,
|
|
54
|
+
introRows: 7,
|
|
55
|
+
composerRows: 6,
|
|
56
|
+
}), "large");
|
|
57
|
+
|
|
58
|
+
assert.equal(resolveStartupHeaderMode({
|
|
59
|
+
cols: 120,
|
|
60
|
+
rows: 16,
|
|
61
|
+
introRows: 7,
|
|
62
|
+
composerRows: 6,
|
|
63
|
+
}), "compact");
|
|
64
|
+
|
|
65
|
+
assert.equal(resolveStartupHeaderMode({
|
|
66
|
+
cols: 100,
|
|
67
|
+
rows: 24,
|
|
68
|
+
introRows: 7,
|
|
69
|
+
composerRows: 5,
|
|
70
|
+
}), "compact");
|
|
71
|
+
|
|
72
|
+
assert.equal(resolveStartupHeaderMode({
|
|
73
|
+
cols: 39,
|
|
74
|
+
rows: 24,
|
|
75
|
+
introRows: 7,
|
|
76
|
+
composerRows: 5,
|
|
77
|
+
}), "tiny");
|
|
78
|
+
|
|
79
|
+
assert.equal(resolveStartupHeaderMode({
|
|
80
|
+
cols: 100,
|
|
81
|
+
rows: 13,
|
|
82
|
+
introRows: 7,
|
|
83
|
+
composerRows: 5,
|
|
84
|
+
}), "tiny");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("measures the header rows for full and compact layouts", () => {
|
|
88
|
+
assert.equal(measureTopHeaderRows(createLayoutSnapshot(120, 30)), 8);
|
|
89
|
+
assert.equal(measureTopHeaderRows(createLayoutSnapshot(80, 24)), 1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("header hero switches from stacked to wide only when metadata has room", () => {
|
|
93
|
+
assert.equal(getHeaderHeroLayout(createLayoutSnapshot(110, 30)).mode, "stacked");
|
|
94
|
+
assert.equal(getHeaderHeroLayout(createLayoutSnapshot(130, 30)).mode, "wide");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("preserves the previous layout when resize values are invalid", () => {
|
|
98
|
+
const previous = createLayoutSnapshot(80, 30);
|
|
99
|
+
const next = createLayoutSnapshot(0, -1, previous);
|
|
100
|
+
|
|
101
|
+
assert.deepEqual(next, previous);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("marks undersized restore samples as unstable without discarding the last stable layout", () => {
|
|
105
|
+
const stable = createTerminalViewport(120, 40);
|
|
106
|
+
const invalidSamples = [
|
|
107
|
+
advanceTerminalViewport(stable, 1, 1),
|
|
108
|
+
advanceTerminalViewport(stable, 2, 1),
|
|
109
|
+
advanceTerminalViewport(stable, 1, 24),
|
|
110
|
+
advanceTerminalViewport(stable, 24, 1),
|
|
111
|
+
// Medium-invalid: pass old <=1 check but fail MIN_VIEWPORT thresholds
|
|
112
|
+
advanceTerminalViewport(stable, 15, 8),
|
|
113
|
+
advanceTerminalViewport(stable, 19, 24),
|
|
114
|
+
advanceTerminalViewport(stable, 120, 9),
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
for (const sample of invalidSamples) {
|
|
118
|
+
assert.equal(sample.unstable, true);
|
|
119
|
+
assert.equal(sample.cols, stable.cols);
|
|
120
|
+
assert.equal(sample.rows, stable.rows);
|
|
121
|
+
assert.equal(sample.layoutEpoch, stable.layoutEpoch);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("bumps the layout epoch when the terminal recovers from an unstable restore", () => {
|
|
126
|
+
const stable = createTerminalViewport(120, 40);
|
|
127
|
+
const unstable = advanceTerminalViewport(stable, 1, 1);
|
|
128
|
+
const recovered = advanceTerminalViewport(unstable, 100, 30);
|
|
129
|
+
|
|
130
|
+
assert.equal(unstable.unstable, true);
|
|
131
|
+
assert.equal(recovered.unstable, false);
|
|
132
|
+
assert.equal(recovered.cols, 100);
|
|
133
|
+
assert.equal(recovered.rows, 30);
|
|
134
|
+
assert.equal(recovered.layoutEpoch, stable.layoutEpoch + 1);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("measures visual width instead of raw string length", () => {
|
|
138
|
+
assert.equal("⚡".length, 1);
|
|
139
|
+
assert.equal(getVisualWidth("⚡"), 2);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("clamps text to a visual width budget", () => {
|
|
143
|
+
assert.equal(clampVisualText("ACTION REQUIRED", 8), "ACTION …");
|
|
144
|
+
assert.equal(clampVisualText("⚡⚡", 3), "⚡…");
|
|
145
|
+
});
|