@pi-kaush/pi-content-layout 0.1.11 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +21 -25
- package/package.json +1 -1
- package/src/index.ts +65 -23
- package/src/intercom-message.ts +99 -0
- package/src/render.ts +7 -144
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- Restyle the `intercom_message` box even when `pi-intercom` wins Pi's first-registration-wins renderer lookup: a chat-container hook replaces the box Pi built with this package's frame, so the surface no longer depends on `packages` order. The renderer registration stays as the cheaper path when this package loads first.
|
|
6
|
+
|
|
7
|
+
- Restore Pi's native active editor border and terminal background, so its embedded status interrupts the top rule as in native Pi. Submitted user messages keep their darker railed shell. Older Pi versions keep the aligned transcript-row status fallback.
|
|
8
|
+
|
|
5
9
|
- Roll back a chat-container hook that mutates the child list and then throws.
|
|
6
10
|
Its edit used to stick, dropping those rows from every later render. The
|
|
7
11
|
contract is shared by copy with pi-tool-call-markers, so both copies changed
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# pi-content-layout
|
|
2
2
|
|
|
3
|
-
A small Pi extension that insets the main conversation,
|
|
3
|
+
A small Pi extension that insets the main conversation, leaves the active editor in Pi's native style, and gives submitted prompts a compact rail shell.
|
|
4
4
|
|
|
5
5
|
### Install
|
|
6
6
|
|
|
@@ -14,10 +14,10 @@ Restart Pi or run `/reload`.
|
|
|
14
14
|
Assistant output
|
|
15
15
|
Text wraps inside the inset width.
|
|
16
16
|
|
|
17
|
-
Active prompt
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
Active prompt (native Pi border and terminal background)
|
|
18
|
+
── ⠋ Working ───────────────────────────────────
|
|
19
|
+
prompt text
|
|
20
|
+
────────────────────────────────────────────────
|
|
21
21
|
|
|
22
22
|
Submitted prompt
|
|
23
23
|
▎ darker background
|
|
@@ -25,7 +25,7 @@ Submitted prompt
|
|
|
25
25
|
▎ darker background
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
Assistant and submitted-message content keep the two-column outer inset. The active prompt
|
|
28
|
+
Assistant and submitted-message content keep the two-column outer inset. The active prompt uses its editor's native border, terminal background, and padding. Pi embeds its status indicator in the top border when supported. The submitted prompt keeps Pi's message padding, with the thin rail outside its darker body.
|
|
29
29
|
|
|
30
30
|
## Scope boundary
|
|
31
31
|
|
|
@@ -64,19 +64,20 @@ When [`pi-intercom`](https://www.npmjs.com/package/pi-intercom) is installed,
|
|
|
64
64
|
this package restyles its inbound `intercom_message` box onto the shared
|
|
65
65
|
transcript columns: the frame's left border sits on the tool-marker column
|
|
66
66
|
(the outer inset), and the `From:` title and body text share the text column
|
|
67
|
-
used by tool rows and Thought labels. The frame
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
themes). The redundant
|
|
67
|
+
used by tool rows and Thought labels. The frame and the sender name ride
|
|
68
|
+
`customMessageLabel` (Pi's label tone for extension messages), and every
|
|
69
|
+
line inside the frame stays muted. The redundant
|
|
71
70
|
`Ctrl+O expands` title hint is dropped (the meta line inside the box keeps
|
|
72
71
|
`Ctrl+O to expand`). Content is unchanged, and expanded view keeps
|
|
73
72
|
pi-intercom's full body, attachment, and reply sections.
|
|
74
73
|
|
|
75
|
-
Pi resolves
|
|
76
|
-
registration wins), so
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
74
|
+
Pi resolves a customType's renderer in extension load order (first
|
|
75
|
+
registration wins), so `pi-intercom` would otherwise shadow this package's
|
|
76
|
+
box. A chat-container hook swaps the restyled box in for whichever
|
|
77
|
+
component Pi built, so the surface holds in either order; listing this
|
|
78
|
+
package before `pi-intercom` in `packages` keeps the cheaper registration
|
|
79
|
+
path. Without pi-intercom installed both paths are inert, and malformed
|
|
80
|
+
message payloads fall back to Pi's default custom-message box.
|
|
80
81
|
|
|
81
82
|
## Manual configuration
|
|
82
83
|
|
|
@@ -90,20 +91,15 @@ If you manage Pi package sources directly, add the package to `settings.json`:
|
|
|
90
91
|
|
|
91
92
|
## Theme ownership
|
|
92
93
|
|
|
93
|
-
The active editor
|
|
94
|
-
near-black `#071312` background (`PROMPT_SURFACE_BG`) so both user-input
|
|
95
|
-
surfaces stay identical; the theme's `selectedBg` token continues to serve
|
|
96
|
-
selections and dialogs. Active input uses the theme's `userMessageText`
|
|
97
|
-
foreground and restores it after embedded ANSI resets without overriding
|
|
98
|
-
inline identifier colors. The extension also consumes `accent` for the
|
|
99
|
-
submitted-prompt rail and `muted` for editor scroll hints.
|
|
94
|
+
The active editor uses Pi's native terminal background (light with a light theme, dark with a dark theme). Submitted message bodies paint with the theme's `userMessageBg` token, and their rails use `borderAccent`.
|
|
100
95
|
|
|
101
|
-
Themes control colors
|
|
102
|
-
shape; a theme cannot provide the layout itself.
|
|
96
|
+
Themes control colors; this extension controls transcript spacing and the submitted-prompt shape.
|
|
103
97
|
|
|
104
98
|
## Composition and compatibility
|
|
105
99
|
|
|
106
|
-
The editor factory wraps the currently configured custom editor, or creates Pi's `CustomEditor` when none exists. It
|
|
100
|
+
The editor factory wraps the currently configured custom editor, or creates Pi's `CustomEditor` when none exists. It preserves the editor's native rendering and input behavior. The wrapper only prevents transcript-specific status indentation from affecting a status embedded in the editor border.
|
|
101
|
+
|
|
102
|
+
The default editor opts into Pi's `embedWorkingStatus` behavior when the host supports it, so status indicators interrupt the active prompt's top border. Older Pi versions keep the separate status row, which this package still aligns with the transcript.
|
|
107
103
|
|
|
108
104
|
Pi does not currently expose public renderers for native user and assistant messages or queued steering previews. Their layout and styling use guarded adapters around Pi's exported components. The adapters restore the original renderers on shutdown and fall back to native rendering when the expected component shape changes.
|
|
109
105
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
CustomEditor,
|
|
4
4
|
type ExtensionAPI,
|
|
5
5
|
type KeybindingsManager,
|
|
6
|
+
type MessageRenderer,
|
|
6
7
|
type Theme,
|
|
7
8
|
UserMessageComponent,
|
|
8
9
|
} from "@earendil-works/pi-coding-agent";
|
|
@@ -15,14 +16,17 @@ import {
|
|
|
15
16
|
visibleWidth,
|
|
16
17
|
} from "@earendil-works/pi-tui";
|
|
17
18
|
import {
|
|
19
|
+
type ChatContainerHook,
|
|
18
20
|
chatContainerHooks,
|
|
19
21
|
runChatContainerHooks,
|
|
20
22
|
} from "./container-hooks.ts";
|
|
21
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
interceptIntercomMessages,
|
|
25
|
+
renderIntercomMessage,
|
|
26
|
+
} from "./intercom-message.ts";
|
|
22
27
|
import {
|
|
23
28
|
contentInset,
|
|
24
29
|
insetLines,
|
|
25
|
-
renderActiveEditor,
|
|
26
30
|
renderSubmittedUserLines,
|
|
27
31
|
} from "./render.ts";
|
|
28
32
|
|
|
@@ -46,6 +50,15 @@ type EditorFactory = (
|
|
|
46
50
|
keybindings: KeybindingsManager,
|
|
47
51
|
) => EditorComponent;
|
|
48
52
|
|
|
53
|
+
type EmbeddedStatusEditorOptions = NonNullable<
|
|
54
|
+
ConstructorParameters<typeof CustomEditor>[3]
|
|
55
|
+
> & { embedWorkingStatus: true };
|
|
56
|
+
|
|
57
|
+
// Newer Pi consumes this option; older editors keep the transcript status row.
|
|
58
|
+
const EMBEDDED_STATUS_EDITOR_OPTIONS: EmbeddedStatusEditorOptions = {
|
|
59
|
+
embedWorkingStatus: true,
|
|
60
|
+
};
|
|
61
|
+
|
|
49
62
|
type ThemeGetter = () => Theme | undefined;
|
|
50
63
|
|
|
51
64
|
type RenderRow = {
|
|
@@ -249,6 +262,9 @@ const knownChatContainers = new WeakSet<object>();
|
|
|
249
262
|
|
|
250
263
|
let systemTextHookActive = 0;
|
|
251
264
|
|
|
265
|
+
// Embedded status indicators call Loader.render() from inside the editor border.
|
|
266
|
+
let embeddedStatusRenderDepth = 0;
|
|
267
|
+
|
|
252
268
|
// The system-text inset decorates matching Text instances in place instead of
|
|
253
269
|
// intercepting the container's child list. Other whole-container wrappers
|
|
254
270
|
// (pi-tool-call-markers' grouping) run the shared hooks without delegating to
|
|
@@ -305,8 +321,12 @@ function statusLoaderDecorator(
|
|
|
305
321
|
row: RenderRow,
|
|
306
322
|
width: number,
|
|
307
323
|
): string[] {
|
|
308
|
-
if (
|
|
324
|
+
if (
|
|
325
|
+
embeddedStatusRenderDepth > 0 ||
|
|
326
|
+
typeof (row as LoaderRow).kind !== "string"
|
|
327
|
+
) {
|
|
309
328
|
return original.call(row, width);
|
|
329
|
+
}
|
|
310
330
|
const nativeInset = Math.max(0, Number((row as LoaderRow).paddingX) || 0);
|
|
311
331
|
const inset = Math.max(0, contentInset(width) - nativeInset);
|
|
312
332
|
if (inset === 0) return original.call(row, width);
|
|
@@ -401,22 +421,31 @@ function userDecorator(
|
|
|
401
421
|
return decorated;
|
|
402
422
|
}
|
|
403
423
|
|
|
404
|
-
function
|
|
405
|
-
editor
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const target = editor as EditorComponent & {
|
|
409
|
-
borderColor?: (text: string) => string;
|
|
424
|
+
function supportsEmbeddedWorkingStatus(editor: EditorComponent): boolean {
|
|
425
|
+
const candidate = editor as EditorComponent & {
|
|
426
|
+
embedWorkingStatus?: unknown;
|
|
427
|
+
setWorkingStatusIndicator?: unknown;
|
|
410
428
|
};
|
|
429
|
+
return (
|
|
430
|
+
candidate.embedWorkingStatus === true &&
|
|
431
|
+
typeof candidate.setWorkingStatusIndicator === "function"
|
|
432
|
+
);
|
|
433
|
+
}
|
|
411
434
|
|
|
412
|
-
|
|
435
|
+
function decorateEditor(editor: EditorComponent): EditorComponent {
|
|
436
|
+
const embedsWorkingStatus = supportsEmbeddedWorkingStatus(editor);
|
|
437
|
+
|
|
438
|
+
return new Proxy(editor, {
|
|
413
439
|
get(component, property) {
|
|
414
440
|
if (property === "render") {
|
|
415
441
|
return (width: number) => {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
442
|
+
if (!embedsWorkingStatus) return component.render(width);
|
|
443
|
+
embeddedStatusRenderDepth += 1;
|
|
444
|
+
try {
|
|
445
|
+
return component.render(width);
|
|
446
|
+
} finally {
|
|
447
|
+
embeddedStatusRenderDepth -= 1;
|
|
448
|
+
}
|
|
420
449
|
};
|
|
421
450
|
}
|
|
422
451
|
const value = Reflect.get(component, property, component) as unknown;
|
|
@@ -427,15 +456,15 @@ function decorateEditor(
|
|
|
427
456
|
},
|
|
428
457
|
});
|
|
429
458
|
}
|
|
459
|
+
// Pi resolves custom message renderers in extension load order (first
|
|
460
|
+
// registration wins), so this registration only wins when this package loads
|
|
461
|
+
// before pi-intercom; the intercomMessageHook installed below covers the
|
|
462
|
+
// order where pi-intercom wins. Without pi-intercom installed both are inert.
|
|
463
|
+
const renderIntercom: MessageRenderer = (message, options, theme) =>
|
|
464
|
+
renderIntercomMessage(message.details, options.expanded, theme);
|
|
430
465
|
|
|
431
466
|
export default function contentLayout(pi: ExtensionAPI): void {
|
|
432
|
-
|
|
433
|
-
// transcript columns. Pi resolves message renderers in extension load
|
|
434
|
-
// order (first registration wins), so this must load before pi-intercom
|
|
435
|
-
// to take effect; without pi-intercom installed the registration is inert.
|
|
436
|
-
pi.registerMessageRenderer("intercom_message", (message, options, theme) =>
|
|
437
|
-
renderIntercomMessage(message.details, options.expanded, theme),
|
|
438
|
-
);
|
|
467
|
+
pi.registerMessageRenderer("intercom_message", renderIntercom);
|
|
439
468
|
|
|
440
469
|
const owner = {};
|
|
441
470
|
let activeTheme: Theme | undefined;
|
|
@@ -443,6 +472,7 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
443
472
|
let userPatch: RenderPatchState | undefined;
|
|
444
473
|
let systemContainerPatch: RenderPatchState | undefined;
|
|
445
474
|
let statusLoaderPatch: RenderPatchState | undefined;
|
|
475
|
+
let intercomMessageHook: ChatContainerHook | undefined;
|
|
446
476
|
let steeringMessagePatch: RenderPatchState | undefined;
|
|
447
477
|
let editorRegistration:
|
|
448
478
|
| {
|
|
@@ -478,6 +508,9 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
478
508
|
const hostContainerPrototype = Object.getPrototypeOf(
|
|
479
509
|
AssistantMessageComponent.prototype,
|
|
480
510
|
) as PatchableRenderPrototype;
|
|
511
|
+
intercomMessageHook = (_container, children) =>
|
|
512
|
+
interceptIntercomMessages(children, getTheme(), renderIntercom);
|
|
513
|
+
chatContainerHooks().add(intercomMessageHook);
|
|
481
514
|
chatContainerHooks().add(systemTextHook);
|
|
482
515
|
systemContainerPatch = installRenderPatch(
|
|
483
516
|
hostContainerPrototype,
|
|
@@ -506,14 +539,23 @@ export default function contentLayout(pi: ExtensionAPI): void {
|
|
|
506
539
|
const factory: EditorFactory = (tui, editorTheme, keybindings) => {
|
|
507
540
|
const editor = previous
|
|
508
541
|
? previous(tui, editorTheme, keybindings)
|
|
509
|
-
: new CustomEditor(
|
|
510
|
-
|
|
542
|
+
: new CustomEditor(
|
|
543
|
+
tui,
|
|
544
|
+
editorTheme,
|
|
545
|
+
keybindings,
|
|
546
|
+
EMBEDDED_STATUS_EDITOR_OPTIONS,
|
|
547
|
+
);
|
|
548
|
+
return decorateEditor(editor);
|
|
511
549
|
};
|
|
512
550
|
editorRegistration = { factory, previous };
|
|
513
551
|
ctx.ui.setEditorComponent(factory);
|
|
514
552
|
});
|
|
515
553
|
|
|
516
554
|
pi.on("session_shutdown", (_event, ctx) => {
|
|
555
|
+
if (intercomMessageHook) {
|
|
556
|
+
chatContainerHooks().delete(intercomMessageHook);
|
|
557
|
+
intercomMessageHook = undefined;
|
|
558
|
+
}
|
|
517
559
|
chatContainerHooks().delete(systemTextHook);
|
|
518
560
|
if (
|
|
519
561
|
editorRegistration &&
|
package/src/intercom-message.ts
CHANGED
|
@@ -171,3 +171,102 @@ export function renderIntercomMessage(
|
|
|
171
171
|
if (typeof text !== "string") return undefined;
|
|
172
172
|
return new IntercomMessageComponent(intercom, expanded, theme);
|
|
173
173
|
}
|
|
174
|
+
|
|
175
|
+
// Structural mirror of Pi's CustomMessageComponent: the fields this package
|
|
176
|
+
// reads to restyle a box Pi already built with another renderer.
|
|
177
|
+
type IntercomMessageHost = {
|
|
178
|
+
message?: { customType?: unknown; details?: unknown };
|
|
179
|
+
customRenderer?: unknown;
|
|
180
|
+
_expanded?: unknown;
|
|
181
|
+
render(width: number): string[];
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
function isIntercomMessageHost(
|
|
185
|
+
child: unknown,
|
|
186
|
+
ownRenderer: unknown,
|
|
187
|
+
): child is IntercomMessageHost {
|
|
188
|
+
if (!child || typeof child !== "object") return false;
|
|
189
|
+
const host = child as IntercomMessageHost;
|
|
190
|
+
if (typeof host.render !== "function") return false;
|
|
191
|
+
// Pi picks one renderer per customType by load order; when this package's
|
|
192
|
+
// registration won, Pi already renders the styled box and there is nothing
|
|
193
|
+
// left to intercept.
|
|
194
|
+
if (host.customRenderer === ownRenderer) return false;
|
|
195
|
+
return host.message?.customType === "intercom_message";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Pi re-renders the transcript on every keystroke, so a box rebuilt per frame
|
|
199
|
+
// would re-wrap the body text each time. Cache per host component instead,
|
|
200
|
+
// keyed on the inputs that change what the box looks like.
|
|
201
|
+
const intercomBoxes = new WeakMap<
|
|
202
|
+
object,
|
|
203
|
+
{
|
|
204
|
+
theme: Theme;
|
|
205
|
+
details: unknown;
|
|
206
|
+
expanded: boolean;
|
|
207
|
+
component: Component;
|
|
208
|
+
}
|
|
209
|
+
>();
|
|
210
|
+
|
|
211
|
+
function intercomBoxFor(
|
|
212
|
+
host: IntercomMessageHost,
|
|
213
|
+
details: unknown,
|
|
214
|
+
expanded: boolean,
|
|
215
|
+
theme: Theme,
|
|
216
|
+
): Component | undefined {
|
|
217
|
+
const cached = intercomBoxes.get(host);
|
|
218
|
+
if (
|
|
219
|
+
cached &&
|
|
220
|
+
cached.theme === theme &&
|
|
221
|
+
cached.details === details &&
|
|
222
|
+
cached.expanded === expanded
|
|
223
|
+
) {
|
|
224
|
+
return cached.component;
|
|
225
|
+
}
|
|
226
|
+
const component = renderIntercomMessage(details, expanded, theme);
|
|
227
|
+
if (!component) {
|
|
228
|
+
intercomBoxes.delete(host);
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
intercomBoxes.set(host, { theme, details, expanded, component });
|
|
232
|
+
return component;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Swap this package's box into an intercom message Pi rendered with another
|
|
237
|
+
* extension's renderer. Pi resolves a customType's renderer by extension load
|
|
238
|
+
* order (first registration wins), so the registration above only wins when
|
|
239
|
+
* this package loads before pi-intercom; this hook makes the restyle hold in
|
|
240
|
+
* either order.
|
|
241
|
+
*
|
|
242
|
+
* Returns the undo for the current render pass, or undefined when no child
|
|
243
|
+
* needed intercepting. Malformed payloads fall back to the render Pi built.
|
|
244
|
+
*/
|
|
245
|
+
export function interceptIntercomMessages(
|
|
246
|
+
children: unknown[],
|
|
247
|
+
theme: Theme | undefined,
|
|
248
|
+
ownRenderer: unknown,
|
|
249
|
+
): (() => void) | undefined {
|
|
250
|
+
if (!theme || !Array.isArray(children)) return undefined;
|
|
251
|
+
const restores: Array<() => void> = [];
|
|
252
|
+
for (const child of children) {
|
|
253
|
+
if (!isIntercomMessageHost(child, ownRenderer)) continue;
|
|
254
|
+
const originalRender = child.render;
|
|
255
|
+
child.render = (width: number): string[] => {
|
|
256
|
+
const box = intercomBoxFor(
|
|
257
|
+
child,
|
|
258
|
+
child.message?.details,
|
|
259
|
+
child._expanded === true,
|
|
260
|
+
theme,
|
|
261
|
+
);
|
|
262
|
+
return box ? box.render(width) : originalRender.call(child, width);
|
|
263
|
+
};
|
|
264
|
+
restores.push(() => {
|
|
265
|
+
delete (child as { render?: unknown }).render;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
if (restores.length === 0) return undefined;
|
|
269
|
+
return () => {
|
|
270
|
+
for (const restore of restores.reverse()) restore();
|
|
271
|
+
};
|
|
272
|
+
}
|
package/src/render.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { EditorComponent } from "@earendil-works/pi-tui";
|
|
3
2
|
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
4
3
|
|
|
5
4
|
export const OUTER_INSET = 2;
|
|
6
5
|
export const PROMPT_RAIL = "▎";
|
|
7
|
-
|
|
6
|
+
const SUBMITTED_SIDE_PADDING = 1;
|
|
8
7
|
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// theme token; the legacy hex remains only as a fallback for theme lookalikes
|
|
13
|
-
// that cannot resolve it (test fakes, older hosts).
|
|
8
|
+
// Submitted message bodies paint with the theme's userMessageBg token.
|
|
9
|
+
// The active editor uses Pi's native terminal background. The legacy hex
|
|
10
|
+
// remains only as a fallback for theme lookalikes that cannot resolve it.
|
|
14
11
|
const PROMPT_SURFACE_BG_FALLBACK = "\x1b[48;2;7;19;18m"; // #071312
|
|
15
12
|
|
|
16
13
|
export function promptSurfaceBg(theme: Theme): string {
|
|
@@ -21,15 +18,9 @@ export function promptSurfaceBg(theme: Theme): string {
|
|
|
21
18
|
}
|
|
22
19
|
}
|
|
23
20
|
|
|
24
|
-
const BORDER_SENTINEL = "\x1b]133;P;pi-content-layout\x07";
|
|
25
21
|
const SGR_PATTERN = /\x1b\[([0-9;]*)m/g;
|
|
26
22
|
const BACKGROUND_PATTERN =
|
|
27
23
|
/\x1b\[(?:4[0-8]|10[0-7]|48;5;\d{1,3}|48;2;\d{1,3};\d{1,3};\d{1,3})m/g;
|
|
28
|
-
const DISPLAY_ANSI_PATTERN = /\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g;
|
|
29
|
-
|
|
30
|
-
type BorderEditor = EditorComponent & {
|
|
31
|
-
borderColor?: (text: string) => string;
|
|
32
|
-
};
|
|
33
24
|
|
|
34
25
|
export function contentInset(width: number): number {
|
|
35
26
|
return width > OUTER_INSET * 2 ? OUTER_INSET : 0;
|
|
@@ -97,41 +88,6 @@ function reapplyBackgroundAfterReset(
|
|
|
97
88
|
});
|
|
98
89
|
}
|
|
99
90
|
|
|
100
|
-
function leavesForegroundReset(rawParameters: string): boolean {
|
|
101
|
-
const parameters =
|
|
102
|
-
rawParameters === "" ? [0] : rawParameters.split(";").map(Number);
|
|
103
|
-
let foregroundIsDefault = false;
|
|
104
|
-
for (let index = 0; index < parameters.length; index++) {
|
|
105
|
-
const parameter = parameters[index];
|
|
106
|
-
if (parameter === 38) {
|
|
107
|
-
foregroundIsDefault = false;
|
|
108
|
-
const mode = parameters[index + 1];
|
|
109
|
-
if (mode === 5) index += 2;
|
|
110
|
-
if (mode === 2) index += 4;
|
|
111
|
-
} else if (parameter === 0 || parameter === 39) {
|
|
112
|
-
foregroundIsDefault = true;
|
|
113
|
-
} else if (
|
|
114
|
-
parameter !== undefined &&
|
|
115
|
-
((parameter >= 30 && parameter <= 37) ||
|
|
116
|
-
(parameter >= 90 && parameter <= 97))
|
|
117
|
-
) {
|
|
118
|
-
foregroundIsDefault = false;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return foregroundIsDefault;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function paintForeground(text: string, foregroundAnsi: string): string {
|
|
125
|
-
const content = text.replace(
|
|
126
|
-
SGR_PATTERN,
|
|
127
|
-
(sequence, rawParameters: string) =>
|
|
128
|
-
leavesForegroundReset(rawParameters)
|
|
129
|
-
? `${sequence}${foregroundAnsi}`
|
|
130
|
-
: sequence,
|
|
131
|
-
);
|
|
132
|
-
return `${foregroundAnsi}${content}\x1b[39m`;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
91
|
export function paintBackground(
|
|
136
92
|
line: string,
|
|
137
93
|
width: number,
|
|
@@ -142,99 +98,6 @@ export function paintBackground(
|
|
|
142
98
|
return `${backgroundAnsi}${reapplyBackgroundAfterReset(fitted, backgroundAnsi)}\x1b[49m`;
|
|
143
99
|
}
|
|
144
100
|
|
|
145
|
-
function stripDisplayAnsi(text: string): string {
|
|
146
|
-
return text.replace(DISPLAY_ANSI_PATTERN, "");
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function scrollHint(line: string): string | undefined {
|
|
150
|
-
return stripDisplayAnsi(line).match(/[↑↓] \d+ more/)?.[0];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function activeBlockLine(
|
|
154
|
-
line: string,
|
|
155
|
-
isBoundary: boolean,
|
|
156
|
-
width: number,
|
|
157
|
-
theme: Theme,
|
|
158
|
-
): string {
|
|
159
|
-
const hint = isBoundary ? scrollHint(line) : undefined;
|
|
160
|
-
const body = hint
|
|
161
|
-
? theme.fg("muted", ` ${hint}`)
|
|
162
|
-
: isBoundary
|
|
163
|
-
? ""
|
|
164
|
-
: paintForeground(line, theme.getFgAnsi("userMessageText"));
|
|
165
|
-
const innerWidth = Math.max(0, width - ACTIVE_SIDE_PADDING * 2);
|
|
166
|
-
const padding = " ".repeat(ACTIVE_SIDE_PADDING);
|
|
167
|
-
return paintBackground(
|
|
168
|
-
`${padding}${fitLine(body, innerWidth)}${padding}`,
|
|
169
|
-
width,
|
|
170
|
-
promptSurfaceBg(theme),
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function markEditorBoundaries(editor: BorderEditor): () => void {
|
|
175
|
-
const original = editor.borderColor;
|
|
176
|
-
if (typeof original !== "function") return () => {};
|
|
177
|
-
editor.borderColor = (text: string) =>
|
|
178
|
-
`${BORDER_SENTINEL}${original(text)}${BORDER_SENTINEL}`;
|
|
179
|
-
return () => {
|
|
180
|
-
editor.borderColor = original;
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export function renderActiveEditor(
|
|
185
|
-
editor: BorderEditor,
|
|
186
|
-
width: number,
|
|
187
|
-
theme: Theme,
|
|
188
|
-
): string[] {
|
|
189
|
-
const editorWidth = width - ACTIVE_SIDE_PADDING * 2;
|
|
190
|
-
if (editorWidth < 1 || typeof editor.borderColor !== "function") {
|
|
191
|
-
return editor.render(width);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const restoreBorder = markEditorBoundaries(editor);
|
|
195
|
-
let markedLines: string[];
|
|
196
|
-
try {
|
|
197
|
-
markedLines = editor.render(editorWidth);
|
|
198
|
-
} finally {
|
|
199
|
-
restoreBorder();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
const boundaries = markedLines
|
|
204
|
-
.map((line, index) => (line.includes(BORDER_SENTINEL) ? index : -1))
|
|
205
|
-
.filter((index) => index >= 0);
|
|
206
|
-
if (boundaries.length !== 2 || boundaries[0] !== 0) {
|
|
207
|
-
return editor.render(width);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const bottomBoundary = boundaries[1];
|
|
211
|
-
if (bottomBoundary === undefined || bottomBoundary <= 0) {
|
|
212
|
-
return editor.render(width);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const cleanLines = markedLines.map((line) =>
|
|
216
|
-
line.split(BORDER_SENTINEL).join(""),
|
|
217
|
-
);
|
|
218
|
-
const block = cleanLines
|
|
219
|
-
.slice(0, bottomBoundary + 1)
|
|
220
|
-
.map((line, index) =>
|
|
221
|
-
activeBlockLine(
|
|
222
|
-
line,
|
|
223
|
-
index === 0 || index === bottomBoundary,
|
|
224
|
-
width,
|
|
225
|
-
theme,
|
|
226
|
-
),
|
|
227
|
-
);
|
|
228
|
-
const suggestions = cleanLines
|
|
229
|
-
.slice(bottomBoundary + 1)
|
|
230
|
-
.map((line) => fitLine(line, width));
|
|
231
|
-
|
|
232
|
-
return [...block, ...suggestions];
|
|
233
|
-
} catch {
|
|
234
|
-
return editor.render(width);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
101
|
function replaceBackground(text: string, backgroundAnsi: string): string {
|
|
239
102
|
return text.replace(BACKGROUND_PATTERN, backgroundAnsi);
|
|
240
103
|
}
|
|
@@ -261,9 +124,9 @@ export function renderSubmittedUserLines(
|
|
|
261
124
|
// The rail glyph (▎) fills only the left quarter of its cell, so one
|
|
262
125
|
// column of left padding already reads as a two-column visual gap.
|
|
263
126
|
// Give the right edge two columns so both sides of the text read equal.
|
|
264
|
-
const leftPadding = " ".repeat(
|
|
265
|
-
const rightPadding = " ".repeat(
|
|
266
|
-
const innerWidth = Math.max(1, bodyWidth -
|
|
127
|
+
const leftPadding = " ".repeat(SUBMITTED_SIDE_PADDING);
|
|
128
|
+
const rightPadding = " ".repeat(SUBMITTED_SIDE_PADDING + 1);
|
|
129
|
+
const innerWidth = Math.max(1, bodyWidth - SUBMITTED_SIDE_PADDING * 2 - 1);
|
|
267
130
|
const body = paintBackground(
|
|
268
131
|
`${leftPadding}${fitLine(recolored, innerWidth)}${rightPadding}`,
|
|
269
132
|
bodyWidth,
|