pi-tui-prompt-box 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/CHANGELOG.md +26 -0
- package/README.md +29 -8
- package/package.json +12 -7
- package/src/index.ts +57 -41
- package/src/terminal-background.ts +33 -0
- package/src/ui/colors.ts +58 -6
- package/src/ui/padded-containers.ts +143 -0
- package/src/ui/padded-footer.ts +122 -0
- package/src/ui/prompt-box-editor.ts +307 -26
- package/src/ui/scrollbar-padding.ts +53 -0
- package/src/ui/status-line.ts +39 -16
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 11.11.2026
|
|
4
|
+
|
|
5
|
+
### New
|
|
6
|
+
* `[ui]` Replace the prompt editor with a filled box and an inner status line.
|
|
7
|
+
* `[ui]` Take the accent bar and status line colors from the current Pi theme.
|
|
8
|
+
* `[ui]` Show `Bash` in the status line for `!` input, `Bash (no context)` for `!!`.
|
|
9
|
+
* `[ui]` Indent Pi's footer by the box margin so it starts in the same column as the box.
|
|
10
|
+
* `[ui]` Remove the model and provider from Pi's built-in footer -> the box shows them.
|
|
11
|
+
* `[ui]` Show Pi's busy indicators in the status line, including retry and compaction.
|
|
12
|
+
* `[ui]` Show only the spinner for Pi's default `Working` message.
|
|
13
|
+
* `[ui]` Indent the chat, queued messages, and widgets by the box margin.
|
|
14
|
+
* `[ui]` Keep the chat clear of Pi's fullscreen `auto` scrollbar; box and footer align.
|
|
15
|
+
* `[ui]` Align the autocomplete item text with the input text.
|
|
16
|
+
* `[ui]` Map mouse clicks on the box to the input text and the autocomplete list.
|
|
17
|
+
* `[ui]` Indent selectors and extension dialogs shown in place of the box like the chat.
|
|
18
|
+
* `[ui]` Show Pi's scroll indicators as `N ↑` / `N ↓` in the box padding rows.
|
|
19
|
+
* `[ui]` Apply Pi's editor `paddingX` setting to the box margins, not the text padding.
|
|
20
|
+
* `[ui]` Show `thinking off` in the status line; hide the level for non-reasoning models.
|
|
21
|
+
* `[ui]` Derive the box background from the terminal background instead of a fixed gray.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## [0.0.1] - 01.11.2026
|
|
25
|
+
|
|
26
|
+
🌱 Proof-of-Concept implementation.
|
package/README.md
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
# pi-tui-prompt-box
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
|
+
<img width="456" src="https://media.githubusercontent.com/media/dimk90/pi-tui-prompt-box/d40318c625f33ef90f99108bb0536a8872174cab/doc/images/pi-tui-prompt-box.png" alt="pi-tui-prompt-box preview">
|
|
5
|
+
<br>
|
|
4
6
|
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square" alt="License: MIT"></a>
|
|
7
|
+
<a href="https://pi.dev/packages/pi-tui-prompt-box"><img src="https://img.shields.io/badge/Pi-Package-6366F1?style=flat-square" alt="Pi Package"></a>
|
|
5
8
|
<a href="https://www.npmjs.com/package/pi-tui-prompt-box"><img src="https://img.shields.io/npm/v/pi-tui-prompt-box?style=flat-square&logoColor=white" alt="npm version"></a>
|
|
6
9
|
</p>
|
|
7
10
|
|
|
8
11
|
<br>
|
|
9
12
|
|
|
10
|
-
[Pi](https://pi.dev)
|
|
11
|
-
box. The box has a left accent bar and a status line inside it with the model,
|
|
12
|
-
the provider, the thinking level, and a busy indicator.
|
|
13
|
+
An alternative style for the prompt input in [Pi](https://pi.dev).
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
It styles the prompt box and nothing else. Other TUI components are only moved
|
|
16
|
+
to line up with the box. Their content and styling stay unchanged.
|
|
17
|
+
|
|
18
|
+
It is a building block, not a full theme: combine it with your favorite footer,
|
|
19
|
+
completion menu, or any other styling extension.
|
|
20
|
+
|
|
21
|
+
## Demo
|
|
22
|
+
|
|
23
|
+

|
|
16
24
|
|
|
17
25
|
## Install
|
|
18
26
|
|
|
@@ -20,6 +28,20 @@ the provider, the thinking level, and a busy indicator.
|
|
|
20
28
|
pi install npm:pi-tui-prompt-box
|
|
21
29
|
```
|
|
22
30
|
|
|
31
|
+
## Side Effects
|
|
32
|
+
|
|
33
|
+
- Moves the chat, widgets, dialogs, and footer right so they line up with the
|
|
34
|
+
prompt box. Their content and styling stay unchanged.
|
|
35
|
+
|
|
36
|
+
- Removes the model, provider, and thinking level from the default footer, as the
|
|
37
|
+
prompt box already shows them. Custom footers stay unchanged.
|
|
38
|
+
|
|
39
|
+
- Moves Pi's busy indicator (spinner and message) into the prompt box. Custom
|
|
40
|
+
spinner widgets stay unchanged.
|
|
41
|
+
|
|
42
|
+
- Replaces Pi's editor, so it does not work together with other extensions that
|
|
43
|
+
replace the editor.
|
|
44
|
+
|
|
23
45
|
## Context Footprint
|
|
24
46
|
|
|
25
47
|
None. The extension only changes the terminal UI: it adds no tools, prompt
|
|
@@ -27,10 +49,9 @@ sections, or messages to the model context.
|
|
|
27
49
|
|
|
28
50
|
## My Other Stuff
|
|
29
51
|
|
|
30
|
-
📌 [S-VHS](https://github.com/dimk90/s-vhs) - terminal recorder used to create the demo GIFs.
|
|
52
|
+
📌 [S-VHS](https://github.com/dimk90/s-vhs) - A terminal recorder used to create the demo GIFs.
|
|
31
53
|
|
|
32
|
-
📌 [pi-context-view](https://github.com/dimk90/pi-context-view) - Pi extension to visualize context usage and inspect
|
|
33
|
-
the hidden parts: base prompt, tool definitions, and extension injections.
|
|
54
|
+
📌 [pi-context-view](https://github.com/dimk90/pi-context-view) - Pi extension to visualize context usage and inspect its hidden parts.
|
|
34
55
|
|
|
35
56
|
## License
|
|
36
57
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-tui-prompt-box",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A prompt box style for Pi that combines with other styling extensions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Dmitry Makarov",
|
|
@@ -15,28 +15,33 @@
|
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
17
17
|
"pi",
|
|
18
|
+
"pi-package",
|
|
18
19
|
"pi-extension",
|
|
19
|
-
"coding-agent"
|
|
20
|
+
"coding-agent",
|
|
21
|
+
"theme"
|
|
20
22
|
],
|
|
21
23
|
"engines": {
|
|
22
24
|
"node": ">=22.19.0"
|
|
23
25
|
},
|
|
24
26
|
"files": [
|
|
25
27
|
"src",
|
|
26
|
-
"README.md"
|
|
28
|
+
"README.md",
|
|
29
|
+
"CHANGELOG.md"
|
|
27
30
|
],
|
|
28
31
|
"pi": {
|
|
29
32
|
"extensions": [
|
|
30
33
|
"./src/index.ts"
|
|
31
|
-
]
|
|
34
|
+
],
|
|
35
|
+
"image": "https://media.githubusercontent.com/media/dimk90/pi-tui-prompt-box/d40318c625f33ef90f99108bb0536a8872174cab/doc/images/pi-tui-prompt-box.png"
|
|
32
36
|
},
|
|
33
37
|
"peerDependencies": {
|
|
34
38
|
"@earendil-works/pi-coding-agent": "*",
|
|
35
39
|
"@earendil-works/pi-tui": "*"
|
|
36
40
|
},
|
|
37
41
|
"devDependencies": {
|
|
38
|
-
"@earendil-works/pi-
|
|
39
|
-
"@earendil-works/pi-
|
|
42
|
+
"@earendil-works/pi-ai": "1.5.2",
|
|
43
|
+
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
44
|
+
"@earendil-works/pi-tui": "1.5.2",
|
|
40
45
|
"@types/node": "^22.20.1",
|
|
41
46
|
"typescript": "^7.0.2"
|
|
42
47
|
},
|
package/src/index.ts
CHANGED
|
@@ -5,60 +5,76 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
8
|
-
import type { TUI } from "@earendil-works/pi-tui";
|
|
8
|
+
import type { RgbColor, TUI } from "@earendil-works/pi-tui";
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { watchTerminalBackground } from "./terminal-background.ts";
|
|
11
|
+
import { computeBoxColors, type ReadBoxColors } from "./ui/colors.ts";
|
|
12
|
+
import { padPiContainers } from "./ui/padded-containers.ts";
|
|
13
|
+
import { findBuiltInFooter, PaddedFooter } from "./ui/padded-footer.ts";
|
|
14
|
+
import { PromptBoxEditor, type RenderStatus } from "./ui/prompt-box-editor.ts";
|
|
15
|
+
import { readScrollbarPadding } from "./ui/scrollbar-padding.ts";
|
|
11
16
|
import { formatStatus } from "./ui/status-line.ts";
|
|
12
17
|
|
|
13
|
-
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
14
|
-
const SPINNER_INTERVAL_MS = 80;
|
|
15
|
-
|
|
16
18
|
/**
|
|
17
19
|
* Registers the prompt box editor for every session.
|
|
18
20
|
*/
|
|
19
21
|
export default function promptBox(pi: ExtensionAPI): void {
|
|
20
|
-
let
|
|
21
|
-
let
|
|
22
|
-
let spinnerTimer: ReturnType<typeof setInterval> | undefined;
|
|
23
|
-
let activeTui: TUI | undefined;
|
|
22
|
+
let restoreContainers: (() => void) | undefined;
|
|
23
|
+
let stopWatchingBackground: (() => void) | undefined;
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
pi.on("session_start", (_event, ctx) => {
|
|
26
|
+
const renderStatus: RenderStatus = (barColor, bashMode) =>
|
|
27
|
+
formatStatus({
|
|
28
|
+
theme: ctx.ui.theme,
|
|
29
|
+
levelColor: barColor,
|
|
30
|
+
bashMode,
|
|
31
|
+
model: ctx.model,
|
|
32
|
+
getProviderName: (provider) => ctx.modelRegistry.getProviderDisplayName(provider),
|
|
33
|
+
thinkingLevel: pi.getThinkingLevel(),
|
|
34
|
+
});
|
|
29
35
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
spinnerTimer = setInterval(() => {
|
|
34
|
-
spinnerIndex = (spinnerIndex + 1) % SPINNER_FRAMES.length;
|
|
35
|
-
activeTui?.requestRender();
|
|
36
|
-
}, SPINNER_INTERVAL_MS);
|
|
37
|
-
activeTui?.requestRender();
|
|
38
|
-
});
|
|
36
|
+
let terminalBackground: RgbColor | undefined;
|
|
37
|
+
const readBoxColors: ReadBoxColors = () =>
|
|
38
|
+
computeBoxColors(terminalBackground, ctx.ui.theme.appearance, ctx.ui.theme.getColorMode());
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
let editorTui: TUI | undefined;
|
|
41
|
+
let editor: PromptBoxEditor | undefined;
|
|
42
|
+
ctx.ui.setEditorComponent((tui, theme, keybindings) => {
|
|
43
|
+
editorTui = tui;
|
|
44
|
+
const readRightPadding = () => readScrollbarPadding(tui);
|
|
45
|
+
editor = new PromptBoxEditor(tui, theme, keybindings, renderStatus, readBoxColors, readRightPadding);
|
|
46
|
+
return editor;
|
|
47
|
+
});
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
// Pi calls the editor factory synchronously, so both are set here in TUI mode only
|
|
50
|
+
if (!editorTui || !editor)
|
|
51
|
+
return;
|
|
52
|
+
|
|
53
|
+
stopWatchingBackground = watchTerminalBackground(editorTui, (background) => {
|
|
54
|
+
terminalBackground = background;
|
|
55
|
+
editorTui?.requestRender();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
restoreContainers = padPiContainers(editorTui, editor);
|
|
59
|
+
|
|
60
|
+
// Search before setFooter(): it removes the current footer from the tree before calling the factory
|
|
61
|
+
const builtInFooter = findBuiltInFooter(editorTui);
|
|
62
|
+
if (!builtInFooter)
|
|
63
|
+
return;
|
|
64
|
+
|
|
65
|
+
const readModelState = () => ({ model: ctx.model, thinkingLevel: pi.getThinkingLevel() });
|
|
66
|
+
ctx.ui.setFooter((tui) => new PaddedFooter(builtInFooter, readModelState, () => readScrollbarPadding(tui)));
|
|
49
67
|
});
|
|
50
68
|
|
|
51
|
-
pi.on("
|
|
52
|
-
|
|
53
|
-
|
|
69
|
+
pi.on("session_shutdown", (event) => {
|
|
70
|
+
stopWatchingBackground?.();
|
|
71
|
+
stopWatchingBackground = undefined;
|
|
54
72
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return new PromptBoxEditor(tui, theme, keybindings, renderStatus);
|
|
62
|
-
});
|
|
73
|
+
// Pi keeps its containers across extension reloads and session changes, so remove the padding for the
|
|
74
|
+
// next extension instance; on quit it stays, as Pi may still print the transcript
|
|
75
|
+
if (event.reason !== "quit")
|
|
76
|
+
restoreContainers?.();
|
|
77
|
+
|
|
78
|
+
restoreContainers = undefined;
|
|
63
79
|
});
|
|
64
80
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal background color, as the terminal reports it, for the box background.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { RgbColor, TerminalColors, TUI } from "@earendil-works/pi-tui";
|
|
6
|
+
|
|
7
|
+
// Pi waits as long for the terminal colors at startup
|
|
8
|
+
const QUERY_TIMEOUT_MS = 100;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Queries the terminal colors now and on every light/dark change the terminal reports, and passes each
|
|
12
|
+
* reported background to `onChange`, also replies that arrive after the timeout. Pi keeps the colors it
|
|
13
|
+
* read private, so the extension asks the terminal itself. Returns a function that stops watching;
|
|
14
|
+
* replies that arrive after it are ignored.
|
|
15
|
+
*/
|
|
16
|
+
export function watchTerminalBackground(tui: TUI, onChange: (background: RgbColor) => void): () => void {
|
|
17
|
+
let stopped = false;
|
|
18
|
+
const report = (colors: TerminalColors) => {
|
|
19
|
+
if (!stopped && colors.background)
|
|
20
|
+
onChange(colors.background);
|
|
21
|
+
};
|
|
22
|
+
const query = () => {
|
|
23
|
+
void tui.queryTerminalColors({ timeoutMs: QUERY_TIMEOUT_MS, onLateReply: report }).then(report);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
query();
|
|
27
|
+
const unsubscribe = tui.onTerminalColorSchemeChange(query);
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
stopped = true;
|
|
31
|
+
unsubscribe();
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/ui/colors.ts
CHANGED
|
@@ -1,10 +1,62 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ANSI color codes shared by the prompt box components; see doc/ui.md.
|
|
2
|
+
* ANSI color codes of the box background, shared by the prompt box components; see doc/ui.md.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import type { ThemeAppearance } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
backgroundAnsi,
|
|
8
|
+
type Color,
|
|
9
|
+
colorToOkhsl,
|
|
10
|
+
foregroundAnsi,
|
|
11
|
+
okhslColor,
|
|
12
|
+
type RgbColor,
|
|
13
|
+
rgbColor,
|
|
14
|
+
type TerminalColorMode,
|
|
15
|
+
} from "@earendil-works/pi-tui";
|
|
16
|
+
|
|
5
17
|
export const RESET = "\x1b[0m";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
|
|
19
|
+
// OKHSL lightness between the terminal background and the box background; a #0a0a0a terminal gets a #1f1f1f box
|
|
20
|
+
const BOX_LIGHTNESS_SHIFT = 0.08;
|
|
21
|
+
// Backgrounds above this OKHSL lightness count as light, so the box gets darker instead of lighter
|
|
22
|
+
const LIGHT_BACKGROUND_LIGHTNESS = 0.5;
|
|
23
|
+
// Pi's guesses for the terminal background when the terminal does not report it
|
|
24
|
+
const GUESSED_BACKGROUNDS = {
|
|
25
|
+
dark: rgbColor(0, 0, 0),
|
|
26
|
+
light: rgbColor(255, 255, 255),
|
|
27
|
+
} satisfies Record<ThemeAppearance, Color>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* ANSI codes that draw the box background.
|
|
31
|
+
*/
|
|
32
|
+
export interface BoxColors {
|
|
33
|
+
/** Background code for the full box rows. */
|
|
34
|
+
background: string;
|
|
35
|
+
/** Foreground code for the half blocks of the bottom edge, drawn on the terminal background. */
|
|
36
|
+
halfBlock: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Function that returns the current box colors; called on every render.
|
|
41
|
+
*/
|
|
42
|
+
export type ReadBoxColors = () => BoxColors;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Computes the box colors from the terminal background: the same hue and saturation, with the OKHSL
|
|
46
|
+
* lightness moved by `BOX_LIGHTNESS_SHIFT` toward the middle, so the box is lighter on dark terminals and
|
|
47
|
+
* darker on light ones. Without a reported background, Pi's guess for the theme `appearance` is used.
|
|
48
|
+
*/
|
|
49
|
+
export function computeBoxColors(
|
|
50
|
+
terminalBackground: RgbColor | undefined,
|
|
51
|
+
appearance: ThemeAppearance,
|
|
52
|
+
mode: TerminalColorMode,
|
|
53
|
+
): BoxColors {
|
|
54
|
+
const background = terminalBackground
|
|
55
|
+
? rgbColor(terminalBackground.r, terminalBackground.g, terminalBackground.b)
|
|
56
|
+
: GUESSED_BACKGROUNDS[appearance];
|
|
57
|
+
const { h, s, l } = colorToOkhsl(background);
|
|
58
|
+
const shift = l > LIGHT_BACKGROUND_LIGHTNESS ? -BOX_LIGHTNESS_SHIFT : BOX_LIGHTNESS_SHIFT;
|
|
59
|
+
const boxColor = okhslColor(h, s, l + shift);
|
|
60
|
+
|
|
61
|
+
return { background: backgroundAnsi(boxColor, mode), halfBlock: foregroundAnsi(boxColor, mode) };
|
|
62
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pi's chat, queued messages, extension widgets, and the dialogs shown in place of the editor moved right by the
|
|
3
|
+
* box margins, so they start in the same column as the prompt box, and kept clear of Pi's fullscreen scrollbar.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Component, TuiMouseEvent, TuiMouseEventResult } from "@earendil-works/pi-tui";
|
|
7
|
+
|
|
8
|
+
import { LEFT_MARGIN, RIGHT_MARGIN } from "./prompt-box-editor.ts";
|
|
9
|
+
import { type ReadRightPadding, readScrollbarPadding } from "./scrollbar-padding.ts";
|
|
10
|
+
|
|
11
|
+
// Pi's TUI children, in this order: document, queued messages, status, widgets above, editor,
|
|
12
|
+
// widgets below, footer
|
|
13
|
+
const ROOT_CHILD_COUNT = 7;
|
|
14
|
+
const EDITOR_INDEX = 4;
|
|
15
|
+
const PADDED_ROOT_INDEXES = [1, 3, 5];
|
|
16
|
+
// Pi's document container holds the header, the loaded resources, and the chat, in this order
|
|
17
|
+
const DOCUMENT_CHILD_COUNT = 3;
|
|
18
|
+
const CHAT_INDEX = 2;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Component with child components, matched by shape like Pi's `Container`.
|
|
22
|
+
*/
|
|
23
|
+
interface ParentComponent extends Component {
|
|
24
|
+
children: Component[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Moves Pi's chat, queued messages, widgets above and below the editor, and the dialogs shown in place of the
|
|
29
|
+
* editor right by the box margins.
|
|
30
|
+
*
|
|
31
|
+
* Pi gives extensions no references to these containers, so they are found by the shape of the TUI tree
|
|
32
|
+
* under `root`, starting from the root child that holds `editor`. Nothing is changed when the shape differs.
|
|
33
|
+
* The editor container is padded only while it does not hold `editor`, which draws its own margins: Pi shows
|
|
34
|
+
* its selectors and extension dialogs in the same container, in place of the editor.
|
|
35
|
+
* In fullscreen mode with the `auto` scrollbar, they are also narrowed by the scrollbar width; see
|
|
36
|
+
* {@link readScrollbarPadding}.
|
|
37
|
+
*
|
|
38
|
+
* @returns A function that removes the padding, or `undefined` when nothing was changed, for example
|
|
39
|
+
* because the containers are already padded.
|
|
40
|
+
*/
|
|
41
|
+
export function padPiContainers(root: Component, editor: Component): (() => void) | undefined {
|
|
42
|
+
const containers = findPiContainers(root, editor);
|
|
43
|
+
if (!containers || [...containers.alwaysPadded, containers.editorContainer].some(isPadded))
|
|
44
|
+
return undefined;
|
|
45
|
+
|
|
46
|
+
const readRightPadding = () => readScrollbarPadding(root);
|
|
47
|
+
const restoreFunctions = containers.alwaysPadded.map((container) => padComponent(container, readRightPadding));
|
|
48
|
+
const { editorContainer } = containers;
|
|
49
|
+
const isDialogShown = () => !editorContainer.children.includes(editor);
|
|
50
|
+
restoreFunctions.push(padComponent(editorContainer, readRightPadding, isDialogShown));
|
|
51
|
+
return () => {
|
|
52
|
+
for (const restore of restoreFunctions) restore();
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Makes `component` render narrower by the box margins and `readRightPadding()`, with every line indented by
|
|
58
|
+
* the left margin.
|
|
59
|
+
*
|
|
60
|
+
* Only this instance is changed, not its class, so Pi keeps its own reference and keeps updating it, in
|
|
61
|
+
* both regular and fullscreen mode. Mouse events are passed in the component coordinates; events on the
|
|
62
|
+
* margins are not handled. `readRightPadding` is called on every render and mouse event, so the padding
|
|
63
|
+
* follows Pi's mode and settings. `shouldPad` is called the same way; while it returns `false`, the component
|
|
64
|
+
* renders and gets mouse events unchanged.
|
|
65
|
+
*
|
|
66
|
+
* @returns A function that removes the padding.
|
|
67
|
+
*/
|
|
68
|
+
export function padComponent(
|
|
69
|
+
component: Component,
|
|
70
|
+
readRightPadding: ReadRightPadding = () => 0,
|
|
71
|
+
shouldPad: () => boolean = () => true,
|
|
72
|
+
): () => void {
|
|
73
|
+
const render = component.render.bind(component);
|
|
74
|
+
const handleMouse = component.handleMouse?.bind(component);
|
|
75
|
+
const readInnerWidth = (width: number) => width - LEFT_MARGIN - RIGHT_MARGIN - readRightPadding();
|
|
76
|
+
|
|
77
|
+
component.render = (width: number): string[] => {
|
|
78
|
+
if (!shouldPad()) return render(width);
|
|
79
|
+
const innerWidth = readInnerWidth(width);
|
|
80
|
+
if (innerWidth < 1) return render(width);
|
|
81
|
+
const leftMargin = " ".repeat(LEFT_MARGIN);
|
|
82
|
+
return render(innerWidth).map((line) => leftMargin + line);
|
|
83
|
+
};
|
|
84
|
+
if (handleMouse) {
|
|
85
|
+
component.handleMouse = (event: TuiMouseEvent): TuiMouseEventResult | undefined => {
|
|
86
|
+
if (!shouldPad()) return handleMouse(event);
|
|
87
|
+
const innerWidth = readInnerWidth(event.width);
|
|
88
|
+
if (innerWidth < 1) return handleMouse(event);
|
|
89
|
+
const x = event.x - LEFT_MARGIN;
|
|
90
|
+
if (x < 0 || x >= innerWidth) return undefined;
|
|
91
|
+
return handleMouse({ ...event, x, width: innerWidth });
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return () => {
|
|
96
|
+
Reflect.deleteProperty(component, "render");
|
|
97
|
+
Reflect.deleteProperty(component, "handleMouse");
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Pi's containers that get the padding.
|
|
103
|
+
*/
|
|
104
|
+
interface PiContainers {
|
|
105
|
+
/** Chat, queued messages, and widgets above and below the editor. */
|
|
106
|
+
alwaysPadded: Component[];
|
|
107
|
+
/** Holds the editor, or a selector or extension dialog shown in its place. */
|
|
108
|
+
editorContainer: ParentComponent;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Finds Pi's chat, queued messages, widget, and editor containers, or returns `undefined` when the tree has
|
|
113
|
+
* another shape.
|
|
114
|
+
*/
|
|
115
|
+
function findPiContainers(root: Component, editor: Component): PiContainers | undefined {
|
|
116
|
+
if (!isParent(root) || root.children.length !== ROOT_CHILD_COUNT) return undefined;
|
|
117
|
+
if (root.children.some((child) => !isParent(child))) return undefined;
|
|
118
|
+
const rootChildren = root.children as ParentComponent[];
|
|
119
|
+
const editorContainer = rootChildren[EDITOR_INDEX];
|
|
120
|
+
if (!editorContainer?.children.includes(editor)) return undefined;
|
|
121
|
+
|
|
122
|
+
const document = rootChildren[0];
|
|
123
|
+
if (!document || document.children.length !== DOCUMENT_CHILD_COUNT) return undefined;
|
|
124
|
+
const chat = document.children[CHAT_INDEX];
|
|
125
|
+
if (!chat || !isParent(chat)) return undefined;
|
|
126
|
+
|
|
127
|
+
const alwaysPadded = [chat, ...PADDED_ROOT_INDEXES.map((index) => rootChildren[index] as Component)];
|
|
128
|
+
return { alwaysPadded, editorContainer };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Checks if `component` was already padded: Pi's containers take `render` from their class.
|
|
133
|
+
*/
|
|
134
|
+
function isPadded(component: Component): boolean {
|
|
135
|
+
return Object.hasOwn(component, "render");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Checks if `component` has a `children` array, as Pi's `Container` has.
|
|
140
|
+
*/
|
|
141
|
+
function isParent(component: Component): component is ParentComponent {
|
|
142
|
+
return "children" in component && Array.isArray(component.children);
|
|
143
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pi's built-in footer moved right by the box margins, so it starts in the same column as the prompt box.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { type Component, stripTerminalSequences, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
7
|
+
|
|
8
|
+
import { LEFT_MARGIN, RIGHT_MARGIN } from "./prompt-box-editor.ts";
|
|
9
|
+
import type { ReadRightPadding } from "./scrollbar-padding.ts";
|
|
10
|
+
|
|
11
|
+
// Index of the footer line with the token stats on the left and the model on the right
|
|
12
|
+
const STATS_LINE_INDEX = 1;
|
|
13
|
+
|
|
14
|
+
// Pi puts at least this many spaces between the stats and the model
|
|
15
|
+
const MIN_MODEL_GAP = 2;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Model state the built-in footer shows; read on every render, as Pi's footer does.
|
|
19
|
+
*/
|
|
20
|
+
export interface FooterModelState {
|
|
21
|
+
model: ExtensionContext["model"];
|
|
22
|
+
thinkingLevel: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Finds Pi's built-in footer in the component tree under `root`.
|
|
27
|
+
*
|
|
28
|
+
* Pi gives extensions no reference to its footer, so the tree is searched. The footer is matched by its
|
|
29
|
+
* methods, not with `instanceof`: Pi itself uses duck typing, as `instanceof` can fail across module copies.
|
|
30
|
+
*/
|
|
31
|
+
export function findBuiltInFooter(root: Component): Component | undefined {
|
|
32
|
+
if (isBuiltInFooter(root))
|
|
33
|
+
return root;
|
|
34
|
+
|
|
35
|
+
if (!("children" in root) || !Array.isArray(root.children))
|
|
36
|
+
return undefined;
|
|
37
|
+
|
|
38
|
+
for (const child of root.children as Component[]) {
|
|
39
|
+
const footer = findBuiltInFooter(child);
|
|
40
|
+
if (footer)
|
|
41
|
+
return footer;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes the model part that Pi's built-in footer right-aligns on its stats line: `(provider) model • level`.
|
|
49
|
+
*
|
|
50
|
+
* The prompt box status line already shows it. The line is changed only when it ends with exactly the text
|
|
51
|
+
* Pi builds from `state`; otherwise, for example when Pi truncated it or shows a routed model, it is
|
|
52
|
+
* returned unchanged.
|
|
53
|
+
*/
|
|
54
|
+
export function removeModelPart(line: string, state: FooterModelState): string {
|
|
55
|
+
const { model, thinkingLevel } = state;
|
|
56
|
+
if (!model)
|
|
57
|
+
return line;
|
|
58
|
+
|
|
59
|
+
let modelPart = model.id;
|
|
60
|
+
if (model.reasoning)
|
|
61
|
+
modelPart += thinkingLevel === "off" ? " • thinking off" : ` • ${thinkingLevel}`;
|
|
62
|
+
|
|
63
|
+
const plainLine = stripTerminalSequences(line);
|
|
64
|
+
const gap = " ".repeat(MIN_MODEL_GAP);
|
|
65
|
+
for (const candidate of [`(${model.provider}) ${modelPart}`, modelPart]) {
|
|
66
|
+
if (!plainLine.endsWith(gap + candidate))
|
|
67
|
+
continue;
|
|
68
|
+
|
|
69
|
+
const statsWidth = visibleWidth(plainLine.slice(0, -candidate.length).trimEnd());
|
|
70
|
+
return truncateToWidth(line, statsWidth, "");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return line;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Renders `footer` narrower by the box margins and `readRightPadding()`, indents every line by the left
|
|
78
|
+
* margin, and removes the model part from the stats line.
|
|
79
|
+
*
|
|
80
|
+
* Pi owns the wrapped footer and keeps updating it, so this component never disposes it.
|
|
81
|
+
*/
|
|
82
|
+
export class PaddedFooter implements Component {
|
|
83
|
+
private readonly footer: Component;
|
|
84
|
+
private readonly readModelState: () => FooterModelState;
|
|
85
|
+
private readonly readRightPadding: ReadRightPadding;
|
|
86
|
+
|
|
87
|
+
constructor(
|
|
88
|
+
footer: Component,
|
|
89
|
+
readModelState: () => FooterModelState,
|
|
90
|
+
readRightPadding: ReadRightPadding = () => 0,
|
|
91
|
+
) {
|
|
92
|
+
this.footer = footer;
|
|
93
|
+
this.readModelState = readModelState;
|
|
94
|
+
this.readRightPadding = readRightPadding;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
render(width: number): string[] {
|
|
98
|
+
const innerWidth = width - LEFT_MARGIN - RIGHT_MARGIN - this.readRightPadding();
|
|
99
|
+
if (innerWidth < 1)
|
|
100
|
+
return this.footer.render(width);
|
|
101
|
+
|
|
102
|
+
const leftMargin = " ".repeat(LEFT_MARGIN);
|
|
103
|
+
const lines = this.footer.render(innerWidth);
|
|
104
|
+
const modelState = this.readModelState();
|
|
105
|
+
|
|
106
|
+
return lines.map((line, index) => {
|
|
107
|
+
const content = index === STATS_LINE_INDEX ? removeModelPart(line, modelState) : line;
|
|
108
|
+
return leftMargin + content;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
invalidate(): void {
|
|
113
|
+
this.footer.invalidate();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Tells Pi's `FooterComponent` apart from other components by the methods only it has.
|
|
119
|
+
*/
|
|
120
|
+
function isBuiltInFooter(component: Component): boolean {
|
|
121
|
+
return "setSession" in component && "setAutoCompactEnabled" in component;
|
|
122
|
+
}
|
|
@@ -3,74 +3,355 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { CustomEditor, type KeybindingsManager } from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
type EditorTheme,
|
|
8
|
+
stripTerminalSequences,
|
|
9
|
+
truncateToWidth,
|
|
10
|
+
type TUI,
|
|
11
|
+
type TuiMouseEvent,
|
|
12
|
+
type TuiMouseEventResult,
|
|
13
|
+
visibleWidth,
|
|
14
|
+
} from "@earendil-works/pi-tui";
|
|
7
15
|
|
|
8
|
-
import {
|
|
16
|
+
import { type BoxColors, type ReadBoxColors, RESET } from "./colors.ts";
|
|
17
|
+
import type { ReadRightPadding } from "./scrollbar-padding.ts";
|
|
18
|
+
import type { BashMode } from "./status-line.ts";
|
|
9
19
|
|
|
10
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Terminal columns left empty on each side of the box; the chat and the footer use them too.
|
|
22
|
+
* Pi's editor `paddingX` setting adds to them only for the box.
|
|
23
|
+
*/
|
|
24
|
+
export const LEFT_MARGIN = 1;
|
|
25
|
+
export const RIGHT_MARGIN = 0;
|
|
11
26
|
const INNER_PADDING = 2;
|
|
12
27
|
const BAR_WIDTH = 1;
|
|
28
|
+
// Pi's select list starts every item with a 2-column prefix (`→ ` or spaces); the list is moved left by it,
|
|
29
|
+
// so the item text starts in the same column as the input text
|
|
30
|
+
const SELECT_PREFIX_WIDTH = 2;
|
|
31
|
+
// Columns between the left margin and the input text
|
|
32
|
+
const TEXT_OFFSET = BAR_WIDTH + INNER_PADDING;
|
|
33
|
+
// Box rows between the text lines and the autocomplete lines (blank, status, half row), where the base
|
|
34
|
+
// editor has only its bottom border
|
|
35
|
+
const ROWS_AFTER_TEXT = 3;
|
|
36
|
+
const BASE_ROWS_AFTER_TEXT = 1;
|
|
37
|
+
// Base editor row of its top border; mouse events on box rows without text are passed on this row
|
|
38
|
+
const BASE_BORDER_ROW = 0;
|
|
39
|
+
// Spaces between the status line text and Pi's busy indicator
|
|
40
|
+
const BUSY_GAP = 2;
|
|
41
|
+
// Pi's default working message, also with the interrupt hint Pi adds after an extension reload
|
|
42
|
+
const DEFAULT_WORKING_MESSAGE = /^Working( \(.+ to interrupt\))?$/;
|
|
43
|
+
// Wide enough for the default working message; Pi pads the rendered indicator to this width
|
|
44
|
+
const MESSAGE_CHECK_WIDTH = 200;
|
|
45
|
+
|
|
46
|
+
// Scroll indicator arrows, as Pi shows them in its borders
|
|
47
|
+
const SCROLL_UP = "↑";
|
|
48
|
+
const SCROLL_DOWN = "↓";
|
|
49
|
+
// Columns between a scroll indicator and the right edge of the box; it extends into the inner padding
|
|
50
|
+
const SCROLL_INDICATOR_GAP = 1;
|
|
13
51
|
|
|
14
52
|
// Returned by renderBottomBorder() so render() can find where the text lines end
|
|
15
53
|
const BOTTOM_BORDER_MARKER = "\x00prompt-box-bottom-border\x00";
|
|
16
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Builds the status line on every render from the editor state Pi does not expose elsewhere.
|
|
57
|
+
*/
|
|
58
|
+
export type RenderStatus = (barColor: (text: string) => string, bashMode: BashMode) => string;
|
|
59
|
+
|
|
60
|
+
// Pi does not export the StatusIndicator type
|
|
61
|
+
type StatusIndicator = NonNullable<Parameters<CustomEditor["setWorkingStatusIndicator"]>[0]>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Box geometry of the last render, used to map mouse events to the base editor layout.
|
|
65
|
+
*/
|
|
66
|
+
interface BoxLayout {
|
|
67
|
+
leftMargin: number;
|
|
68
|
+
textWidth: number;
|
|
69
|
+
textLineCount: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Terminal columns left empty on each side of the box.
|
|
74
|
+
*/
|
|
75
|
+
interface BoxMargins {
|
|
76
|
+
left: number;
|
|
77
|
+
right: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Shared look of the box rows of one render.
|
|
82
|
+
*/
|
|
83
|
+
interface BoxStyle {
|
|
84
|
+
margins: BoxMargins;
|
|
85
|
+
barColor: (text: string) => string;
|
|
86
|
+
colors: BoxColors;
|
|
87
|
+
}
|
|
88
|
+
|
|
17
89
|
/**
|
|
18
90
|
* Main editor drawn as a filled box with a left accent bar and a status line inside.
|
|
19
91
|
*
|
|
20
92
|
* The base editor renders the text lines; this class replaces its borders and
|
|
21
93
|
* wraps every line in the box background. `renderStatus` is called on every
|
|
22
|
-
* render, so the status line always shows the current state.
|
|
94
|
+
* render, so the status line always shows the current state. The accent bar
|
|
95
|
+
* uses `borderColor`, which Pi sets from the thinking level and bash mode.
|
|
96
|
+
* With `embedWorkingStatus`, Pi passes its busy indicators (working, retry,
|
|
97
|
+
* compaction, branch summary) to the editor, which shows them in the status line.
|
|
98
|
+
* `readBoxColors` is called on every render, so the box follows the terminal background.
|
|
99
|
+
* `readRightPadding` is called on every render and adds columns to the right margin.
|
|
100
|
+
* Pi's editor `paddingX` setting adds columns to both margins of the box and moves the autocomplete list
|
|
101
|
+
* with it; the text padding inside the box stays the same.
|
|
102
|
+
* Pi's scroll indicators (`N ↑`, `N ↓`) go right-aligned into the padding rows
|
|
103
|
+
* around the text, in the theme `borderColor` (Pi's `borderMuted`).
|
|
23
104
|
*/
|
|
24
105
|
export class PromptBoxEditor extends CustomEditor {
|
|
25
|
-
private readonly renderStatus:
|
|
106
|
+
private readonly renderStatus: RenderStatus;
|
|
107
|
+
// The base class keeps the theme private; its borderColor stays Pi's `borderMuted` while Pi changes the
|
|
108
|
+
// editor borderColor for the thinking level and bash mode
|
|
109
|
+
private readonly editorTheme: EditorTheme;
|
|
110
|
+
private readonly readBoxColors: ReadBoxColors;
|
|
111
|
+
private readonly readRightPadding: ReadRightPadding;
|
|
112
|
+
private statusIndicator: StatusIndicator | undefined;
|
|
113
|
+
// Pi's editor paddingX setting; the base editor keeps 0, so its text indent inside the box stays the same
|
|
114
|
+
private boxPaddingX = 0;
|
|
115
|
+
// Text lines hidden above and below the visible ones, set by the base render through the border methods
|
|
116
|
+
private hiddenAbove = 0;
|
|
117
|
+
private hiddenBelow = 0;
|
|
118
|
+
// Undefined when the last render fell back to the base editor layout
|
|
119
|
+
private boxLayout: BoxLayout | undefined;
|
|
120
|
+
// Set while the base editor renders its own layout below the box minimum width, so it keeps Pi's borders
|
|
121
|
+
private renderingFallback = false;
|
|
26
122
|
|
|
27
|
-
constructor(
|
|
28
|
-
|
|
123
|
+
constructor(
|
|
124
|
+
tui: TUI,
|
|
125
|
+
theme: EditorTheme,
|
|
126
|
+
keybindings: KeybindingsManager,
|
|
127
|
+
renderStatus: RenderStatus,
|
|
128
|
+
readBoxColors: ReadBoxColors,
|
|
129
|
+
readRightPadding: ReadRightPadding = () => 0,
|
|
130
|
+
) {
|
|
131
|
+
super(tui, theme, keybindings, { paddingX: 0, embedWorkingStatus: true });
|
|
29
132
|
this.renderStatus = renderStatus;
|
|
133
|
+
this.editorTheme = theme;
|
|
134
|
+
this.readBoxColors = readBoxColors;
|
|
135
|
+
this.readRightPadding = readRightPadding;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// The base class keeps the indicator private, so keep a copy for the status line
|
|
139
|
+
override setWorkingStatusIndicator(indicator: StatusIndicator | undefined): void {
|
|
140
|
+
super.setWorkingStatusIndicator(indicator);
|
|
141
|
+
this.statusIndicator = indicator;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Pi copies its editor paddingX setting here; it widens the box margins instead of the base text indent
|
|
145
|
+
override getPaddingX(): number {
|
|
146
|
+
return this.boxPaddingX;
|
|
30
147
|
}
|
|
31
148
|
|
|
32
|
-
|
|
149
|
+
override setPaddingX(padding: number): void {
|
|
150
|
+
const boxPaddingX = Number.isFinite(padding) ? Math.max(0, Math.floor(padding)) : 0;
|
|
151
|
+
if (this.boxPaddingX === boxPaddingX)
|
|
152
|
+
return;
|
|
153
|
+
|
|
154
|
+
this.boxPaddingX = boxPaddingX;
|
|
155
|
+
this.tui.requestRender();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// In the box layout the base top border is dropped; only its hidden line count is kept
|
|
159
|
+
protected override renderTopBorder(width: number, hiddenLineCount: number): string {
|
|
160
|
+
if (this.renderingFallback)
|
|
161
|
+
return super.renderTopBorder(width, hiddenLineCount);
|
|
162
|
+
|
|
163
|
+
this.hiddenAbove = hiddenLineCount;
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
protected override renderBottomBorder(width: number, hiddenLineCount: number): string {
|
|
168
|
+
if (this.renderingFallback)
|
|
169
|
+
return super.renderBottomBorder(width, hiddenLineCount);
|
|
170
|
+
|
|
171
|
+
this.hiddenBelow = hiddenLineCount;
|
|
33
172
|
return BOTTOM_BORDER_MARKER;
|
|
34
173
|
}
|
|
35
174
|
|
|
36
175
|
override render(width: number): string[] {
|
|
37
|
-
const
|
|
38
|
-
|
|
176
|
+
const margins = {
|
|
177
|
+
left: LEFT_MARGIN + this.boxPaddingX,
|
|
178
|
+
right: RIGHT_MARGIN + this.boxPaddingX + this.readRightPadding(),
|
|
179
|
+
};
|
|
180
|
+
const textWidth = width - margins.left - margins.right - BAR_WIDTH - 2 * INNER_PADDING;
|
|
181
|
+
if (textWidth < 1) {
|
|
182
|
+
this.boxLayout = undefined;
|
|
183
|
+
return this.renderFallback(width);
|
|
184
|
+
}
|
|
39
185
|
|
|
40
186
|
// Base layout: top border, text lines, bottom border, autocomplete lines
|
|
41
187
|
const baseLines = super.render(textWidth);
|
|
42
188
|
const bottomBorderIndex = baseLines.indexOf(BOTTOM_BORDER_MARKER);
|
|
43
189
|
const textLines = baseLines.slice(1, bottomBorderIndex);
|
|
44
190
|
const autocompleteLines = baseLines.slice(bottomBorderIndex + 1);
|
|
191
|
+
this.boxLayout = { leftMargin: margins.left, textWidth, textLineCount: textLines.length };
|
|
192
|
+
|
|
193
|
+
const boxWidth = width - margins.left - margins.right - BAR_WIDTH;
|
|
194
|
+
const barColor = this.borderColor;
|
|
195
|
+
const colors = this.readBoxColors();
|
|
196
|
+
const status = this.renderStatusLine(textWidth, barColor);
|
|
197
|
+
|
|
198
|
+
const scrollUp = this.renderScrollIndicator(SCROLL_UP, this.hiddenAbove, textWidth);
|
|
199
|
+
const scrollDown = this.renderScrollIndicator(SCROLL_DOWN, this.hiddenBelow, textWidth);
|
|
200
|
+
|
|
201
|
+
const autocompleteIndent = " ".repeat(margins.left + TEXT_OFFSET - SELECT_PREFIX_WIDTH);
|
|
202
|
+
|
|
203
|
+
const style = { margins, barColor, colors };
|
|
45
204
|
|
|
46
|
-
const boxWidth = width - 2 * OUTER_MARGIN - BAR_WIDTH;
|
|
47
205
|
return [
|
|
48
|
-
|
|
49
|
-
...textLines.map((line) => renderBoxRow(line, textWidth)),
|
|
50
|
-
renderBoxRow(
|
|
51
|
-
renderBoxRow(
|
|
52
|
-
renderHalfRow("╹", "▀", boxWidth),
|
|
53
|
-
...autocompleteLines.map((line) =>
|
|
206
|
+
renderBoxRow(scrollUp, textWidth, style),
|
|
207
|
+
...textLines.map((line) => renderBoxRow(line, textWidth, style)),
|
|
208
|
+
renderBoxRow(scrollDown, textWidth, style),
|
|
209
|
+
renderBoxRow(status, textWidth, style),
|
|
210
|
+
renderHalfRow("╹", "▀", boxWidth, style),
|
|
211
|
+
...autocompleteLines.map((line) => padToWidth(autocompleteIndent + line, width)),
|
|
54
212
|
];
|
|
55
213
|
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Passes the mouse event to the base editor in its own layout, so clicks place the cursor under the pointer
|
|
217
|
+
* and select autocomplete items. Box rows without text (top padding, blank, status, half row) are passed on
|
|
218
|
+
* the base top border row, so the box takes focus there as on Pi's borders.
|
|
219
|
+
*/
|
|
220
|
+
override handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
221
|
+
const layout = this.boxLayout;
|
|
222
|
+
if (!layout)
|
|
223
|
+
return super.handleMouse(event);
|
|
224
|
+
|
|
225
|
+
const autocompleteRow = 1 + layout.textLineCount + ROWS_AFTER_TEXT;
|
|
226
|
+
const rowShift = ROWS_AFTER_TEXT - BASE_ROWS_AFTER_TEXT;
|
|
227
|
+
const baseEvent = { ...event, width: layout.textWidth, height: event.height - rowShift };
|
|
228
|
+
const textIndent = layout.leftMargin + TEXT_OFFSET;
|
|
229
|
+
if (event.y >= autocompleteRow) {
|
|
230
|
+
const x = event.x - textIndent + SELECT_PREFIX_WIDTH;
|
|
231
|
+
return super.handleMouse({ ...baseEvent, x, y: event.y - rowShift });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const y = event.y <= layout.textLineCount ? event.y : BASE_BORDER_ROW;
|
|
235
|
+
|
|
236
|
+
return super.handleMouse({ ...baseEvent, x: event.x - textIndent, y });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Renders the base editor layout with Pi's borders, for widths below the box minimum.
|
|
241
|
+
*/
|
|
242
|
+
private renderFallback(width: number): string[] {
|
|
243
|
+
this.renderingFallback = true;
|
|
244
|
+
try {
|
|
245
|
+
return super.render(width);
|
|
246
|
+
} finally {
|
|
247
|
+
this.renderingFallback = false;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Renders Pi's scroll indicator (`N ↑`) right-aligned `SCROLL_INDICATOR_GAP` columns before the box edge, so
|
|
253
|
+
* it extends into the inner padding, or an empty row content when no lines are hidden. The label is cut from
|
|
254
|
+
* the right when the box is too narrow.
|
|
255
|
+
*/
|
|
256
|
+
private renderScrollIndicator(direction: string, hiddenLineCount: number, textWidth: number): string {
|
|
257
|
+
if (hiddenLineCount < 1)
|
|
258
|
+
return "";
|
|
259
|
+
|
|
260
|
+
const indicatorWidth = textWidth + INNER_PADDING - SCROLL_INDICATOR_GAP;
|
|
261
|
+
const label = truncateToWidth(`${hiddenLineCount} ${direction}`, indicatorWidth, "");
|
|
262
|
+
|
|
263
|
+
return " ".repeat(indicatorWidth - visibleWidth(label)) + this.editorTheme.borderColor(label);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Renders the status line text followed by Pi's busy indicator, if any, within `textWidth`.
|
|
268
|
+
* The text is cut first so the spinner always fits; the indicator message is cut as Pi does in its border.
|
|
269
|
+
* Pi's default working message is dropped, so a normal turn shows only the spinner.
|
|
270
|
+
*/
|
|
271
|
+
private renderStatusLine(textWidth: number, barColor: (text: string) => string): string {
|
|
272
|
+
const text = this.renderStatus(barColor, detectBashMode(this.getText()));
|
|
273
|
+
const indicator = this.statusIndicator;
|
|
274
|
+
if (!indicator)
|
|
275
|
+
return truncateToWidth(text, textWidth, "");
|
|
276
|
+
|
|
277
|
+
const spinnerWidth = visibleWidth(indicator.renderSpinnerInBorder(textWidth));
|
|
278
|
+
const fittedText = truncateToWidth(text, Math.max(0, textWidth - BUSY_GAP - spinnerWidth), "");
|
|
279
|
+
const busyWidth = textWidth - visibleWidth(fittedText) - BUSY_GAP;
|
|
280
|
+
if (busyWidth < 1)
|
|
281
|
+
return fittedText;
|
|
282
|
+
|
|
283
|
+
const busy = hasDefaultWorkingMessage(indicator)
|
|
284
|
+
? indicator.renderSpinnerInBorder(busyWidth)
|
|
285
|
+
: indicator.renderInBorder(busyWidth);
|
|
286
|
+
|
|
287
|
+
return truncateToWidth(`${fittedText}${" ".repeat(BUSY_GAP)}${busy}`, textWidth, "");
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Checks if the indicator shows Pi's default working message, which the box hides to show only the spinner.
|
|
293
|
+
* Pi keeps the message private, so it is read from the rendered indicator.
|
|
294
|
+
* An indicator without a spinner keeps its message, so it never disappears.
|
|
295
|
+
*/
|
|
296
|
+
function hasDefaultWorkingMessage(indicator: StatusIndicator): boolean {
|
|
297
|
+
if (indicator.kind !== "working")
|
|
298
|
+
return false;
|
|
299
|
+
|
|
300
|
+
const spinner = stripTerminalSequences(indicator.renderSpinnerInBorder(MESSAGE_CHECK_WIDTH));
|
|
301
|
+
if (!spinner)
|
|
302
|
+
return false;
|
|
303
|
+
|
|
304
|
+
const rendered = stripTerminalSequences(indicator.renderInBorder(MESSAGE_CHECK_WIDTH));
|
|
305
|
+
const message = rendered.startsWith(`${spinner} `) ? rendered.slice(spinner.length + 1) : rendered;
|
|
306
|
+
|
|
307
|
+
return DEFAULT_WORKING_MESSAGE.test(message);
|
|
56
308
|
}
|
|
57
309
|
|
|
58
310
|
/**
|
|
59
|
-
*
|
|
311
|
+
* Detects bash mode with Pi's rules: `!` runs a command, `!!` also keeps its output out of the context.
|
|
60
312
|
*/
|
|
61
|
-
function
|
|
62
|
-
const
|
|
63
|
-
|
|
313
|
+
function detectBashMode(text: string): BashMode {
|
|
314
|
+
const input = text.trimStart();
|
|
315
|
+
if (input.startsWith("!!"))
|
|
316
|
+
return "no-context";
|
|
317
|
+
|
|
318
|
+
if (input.startsWith("!"))
|
|
319
|
+
return "context";
|
|
320
|
+
|
|
321
|
+
return "off";
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Pads `line` with trailing spaces to `width` columns, so rows outside the box have the full width too.
|
|
326
|
+
*/
|
|
327
|
+
function padToWidth(line: string, width: number): string {
|
|
328
|
+
return line + " ".repeat(Math.max(0, width - visibleWidth(line)));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Renders the thin bottom edge of the box with half blocks.
|
|
333
|
+
*/
|
|
334
|
+
function renderHalfRow(barChar: string, fillChar: string, boxWidth: number, style: BoxStyle): string {
|
|
335
|
+
const { margins, barColor, colors } = style;
|
|
336
|
+
const leftMargin = " ".repeat(margins.left);
|
|
337
|
+
const rightMargin = " ".repeat(margins.right);
|
|
338
|
+
|
|
339
|
+
return `${leftMargin}${barColor(barChar)}${colors.halfBlock}${fillChar.repeat(boxWidth)}${RESET}${rightMargin}`;
|
|
64
340
|
}
|
|
65
341
|
|
|
66
342
|
/**
|
|
67
343
|
* Renders one full-height box row: accent bar, padding and content on the box background.
|
|
344
|
+
* Content wider than `textWidth` extends into the right inner padding; only scroll indicators do that.
|
|
68
345
|
*/
|
|
69
|
-
function renderBoxRow(content: string, textWidth: number): string {
|
|
70
|
-
const
|
|
346
|
+
function renderBoxRow(content: string, textWidth: number, style: BoxStyle): string {
|
|
347
|
+
const { margins, barColor, colors } = style;
|
|
348
|
+
const leftMargin = " ".repeat(margins.left);
|
|
349
|
+
const rightMargin = " ".repeat(margins.right);
|
|
71
350
|
const padding = " ".repeat(INNER_PADDING);
|
|
72
|
-
const fill = " ".repeat(Math.max(0, textWidth - visibleWidth(content)));
|
|
351
|
+
const fill = " ".repeat(Math.max(0, textWidth + INNER_PADDING - visibleWidth(content)));
|
|
73
352
|
// Styled content may reset all attributes, which would also drop the box background
|
|
74
|
-
const boxedContent = content.replaceAll(RESET, RESET +
|
|
75
|
-
|
|
353
|
+
const boxedContent = content.replaceAll(RESET, RESET + colors.background);
|
|
354
|
+
const box = `${colors.background}${padding}${boxedContent}${fill}${RESET}`;
|
|
355
|
+
|
|
356
|
+
return `${leftMargin}${barColor("┃")}${RESET}${box}${rightMargin}`;
|
|
76
357
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Right padding that keeps the chat, the prompt box, and the footer clear of Pi's fullscreen scrollbar.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
6
|
+
|
|
7
|
+
// Pi's fullscreen scrollbar in `auto` mode is drawn over the last column of the chat, so that column is left empty
|
|
8
|
+
const SCROLLBAR_WIDTH = 1;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Function that returns the columns to leave empty on the right of a component, besides the box margin.
|
|
12
|
+
*/
|
|
13
|
+
export type ReadRightPadding = () => number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns the columns to leave for Pi's fullscreen scrollbar: its width in the `auto` mode, else 0.
|
|
17
|
+
*
|
|
18
|
+
* In `auto` mode Pi draws the scrollbar over the last column of the chat when it scrolls. In `always` mode
|
|
19
|
+
* Pi already narrows the chat for it; `hidden` mode and regular TUI mode have no scrollbar. Pi gives
|
|
20
|
+
* extensions no reference to its scroll view, so it is found by shape among the children of the fullscreen
|
|
21
|
+
* layout root of `tui`: the child that holds the document, the first TUI child, and has a `scrollbar` mode.
|
|
22
|
+
* Pi replaces the TUI on a mode switch, and `tui` follows it, so call this on every render.
|
|
23
|
+
*/
|
|
24
|
+
export function readScrollbarPadding(tui: Component): number {
|
|
25
|
+
const document = readChildren(tui)?.[0];
|
|
26
|
+
if (!document)
|
|
27
|
+
return 0;
|
|
28
|
+
|
|
29
|
+
// Pi keeps the fullscreen layout root in a field that its types mark private
|
|
30
|
+
const layoutRoot: unknown = Reflect.get(tui, "layoutRoot");
|
|
31
|
+
const scrollView = readChildren(layoutRoot)?.find((child) => readChildren(child)?.[0] === document);
|
|
32
|
+
if (!isObject(scrollView) || !("scrollbar" in scrollView) || scrollView.scrollbar !== "auto")
|
|
33
|
+
return 0;
|
|
34
|
+
|
|
35
|
+
return SCROLLBAR_WIDTH;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns the `children` array of `value`, as Pi's `Container` has, or `undefined` when it has none.
|
|
40
|
+
*/
|
|
41
|
+
function readChildren(value: unknown): unknown[] | undefined {
|
|
42
|
+
if (!isObject(value) || !("children" in value) || !Array.isArray(value.children))
|
|
43
|
+
return undefined;
|
|
44
|
+
|
|
45
|
+
return value.children;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks if `value` is a non-null object.
|
|
50
|
+
*/
|
|
51
|
+
function isObject(value: unknown): value is object {
|
|
52
|
+
return typeof value === "object" && value !== null;
|
|
53
|
+
}
|
package/src/ui/status-line.ts
CHANGED
|
@@ -2,28 +2,51 @@
|
|
|
2
2
|
* Status line shown inside the prompt box.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Bash mode of the input:
|
|
9
|
+
* off a normal prompt
|
|
10
|
+
* context `!` runs a shell command; its output goes to the model context
|
|
11
|
+
* no-context `!!` runs a shell command; its output stays out of the context
|
|
12
|
+
*/
|
|
13
|
+
export type BashMode = "off" | "context" | "no-context";
|
|
8
14
|
|
|
9
15
|
/**
|
|
10
|
-
*
|
|
16
|
+
* Current state shown in the status line, with the colors to draw it.
|
|
11
17
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return `${modelText}${separator}${mode}${busy}`;
|
|
18
|
+
export interface StatusLineOptions {
|
|
19
|
+
theme: Theme;
|
|
20
|
+
/** Colors the thinking level or `Bash`; the editor `borderColor`, so it matches the accent bar. */
|
|
21
|
+
levelColor: (text: string) => string;
|
|
22
|
+
bashMode: BashMode;
|
|
23
|
+
model: ExtensionContext["model"];
|
|
24
|
+
/** Returns the display name of a provider id, like Pi's model registry: `opencode` → `OpenCode Zen`. */
|
|
25
|
+
getProviderName: (provider: string) => string;
|
|
26
|
+
thinkingLevel: string;
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
/**
|
|
25
|
-
*
|
|
30
|
+
* Builds the status line text: model, provider and thinking level.
|
|
31
|
+
* Like Pi's footer, the level shows only for models with reasoning support, and level `off` shows as `thinking off`.
|
|
32
|
+
* In bash mode, `Bash` replaces them. Pi's busy indicator is added by the editor.
|
|
26
33
|
*/
|
|
27
|
-
function
|
|
28
|
-
|
|
34
|
+
export function formatStatus(options: StatusLineOptions): string {
|
|
35
|
+
const { theme, levelColor, bashMode, model, getProviderName, thinkingLevel } = options;
|
|
36
|
+
if (bashMode === "context")
|
|
37
|
+
return levelColor("Bash");
|
|
38
|
+
|
|
39
|
+
if (bashMode === "no-context")
|
|
40
|
+
return `${levelColor("Bash")}${theme.fg("dim", " (no context)")}`;
|
|
41
|
+
|
|
42
|
+
const modelText = model
|
|
43
|
+
? `${theme.fg("text", model.name)} ${theme.fg("dim", getProviderName(model.provider))}`
|
|
44
|
+
: theme.fg("dim", "no model");
|
|
45
|
+
if (!model?.reasoning)
|
|
46
|
+
return modelText;
|
|
47
|
+
|
|
48
|
+
const separator = theme.fg("dim", " · ");
|
|
49
|
+
const level = levelColor(thinkingLevel === "off" ? "thinking off" : thinkingLevel);
|
|
50
|
+
|
|
51
|
+
return `${modelText}${separator}${level}`;
|
|
29
52
|
}
|