pi-better-show 0.6.0 → 0.6.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/package.json +1 -1
- package/src/extension/controller.ts +14 -5
- package/src/extension/edit.ts +45 -8
- package/src/extension/system.ts +78 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-better-show",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Interactive blocks for agent-written Markdown: one vocabulary, text fallback, and answer encoding shared by every host, plus the show_doc extension for pi",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
} from "./edit.ts";
|
|
39
39
|
import { createAllowList, HTML_FILE, IMAGE_FILE, MARKDOWN_FILE, REACT_FILE } from "./files.ts";
|
|
40
40
|
import type { PageServer, PageServerOptions } from "./server.ts";
|
|
41
|
+
import { onPath, urlOpener } from "./system.ts";
|
|
41
42
|
|
|
42
43
|
export const MESSAGE_TYPE = "better-show";
|
|
43
44
|
export const MINI_SDD_FILE = /(^|\/)docs\/specs\/[^/]+\/(decision|spec|design|verification)\.md$/;
|
|
@@ -49,8 +50,8 @@ export const MAX_DOCUMENTS = 32;
|
|
|
49
50
|
export const MAX_REMEMBERED_ANSWERS = 256;
|
|
50
51
|
/** How long a page just sent to the browser counts as open before its tab connects. */
|
|
51
52
|
export const PAGE_OPEN_WAIT_MS = 15_000;
|
|
52
|
-
/** The command that opens a URL in the default browser. */
|
|
53
|
-
export const OPENER = process.platform
|
|
53
|
+
/** The command that opens a URL in the default browser (system.ts: `rundll32` on Windows, by absolute path). */
|
|
54
|
+
export const OPENER = urlOpener(process.platform)[0];
|
|
54
55
|
|
|
55
56
|
export type View = "page" | "edit";
|
|
56
57
|
|
|
@@ -79,7 +80,14 @@ export const createPageServer = async (options: PageServerOptions): Promise<Page
|
|
|
79
80
|
|
|
80
81
|
export const defaultDeps = (): Deps => ({
|
|
81
82
|
editor: process.env[EDITOR_ENV],
|
|
82
|
-
onPath: (command) =>
|
|
83
|
+
onPath: (command) =>
|
|
84
|
+
onPath(
|
|
85
|
+
command,
|
|
86
|
+
process.platform,
|
|
87
|
+
process.env,
|
|
88
|
+
undefined,
|
|
89
|
+
(name) => spawnSync("sh", ["-c", 'command -v "$1"', "sh", name], { stdio: "ignore" }).status === 0,
|
|
90
|
+
),
|
|
83
91
|
launch: defaultLaunch,
|
|
84
92
|
run: defaultRun,
|
|
85
93
|
platform: process.platform,
|
|
@@ -100,8 +108,9 @@ export const defaultDeps = (): Deps => ({
|
|
|
100
108
|
},
|
|
101
109
|
servePages: createPageServer,
|
|
102
110
|
openUrl: (url) => {
|
|
103
|
-
const
|
|
104
|
-
|
|
111
|
+
const [opener, args] = urlOpener(process.platform);
|
|
112
|
+
const opened = defaultRun(opener, args(url));
|
|
113
|
+
if (opened.status !== 0) throw new Error(`${opener} failed: ${(opened.stderr || opened.stdout).trim()}`);
|
|
105
114
|
},
|
|
106
115
|
realpath: (path) => (existsSync(path) ? realpathSync(path) : path),
|
|
107
116
|
newId: randomUUID,
|
package/src/extension/edit.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawn, spawnSync } from "node:child_process";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { gitDiffHunks, windowsTextEditor } from "./system.ts";
|
|
3
4
|
|
|
4
5
|
/** Edit mode (D7): the user's own editor, no terminal pane. Saves are noticed by watching the file. */
|
|
5
6
|
|
|
@@ -48,8 +49,11 @@ export const defaultLaunch = (command: string, args: string[]): Promise<void> =>
|
|
|
48
49
|
});
|
|
49
50
|
});
|
|
50
51
|
|
|
51
|
-
/**
|
|
52
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Splits a command line into words, honouring single and double quotes and backslash escapes; no expansion happens. With `windows`, a
|
|
54
|
+
* backslash is an ordinary character, so `"C:\\Program Files\\Editor\\editor.exe" -w` keeps its path.
|
|
55
|
+
*/
|
|
56
|
+
export function shellWords(line: string, windows = false): string[] {
|
|
53
57
|
const words: string[] = [];
|
|
54
58
|
let word = "";
|
|
55
59
|
let quote: string | undefined;
|
|
@@ -58,12 +62,12 @@ export function shellWords(line: string): string[] {
|
|
|
58
62
|
const char = line[index] as string;
|
|
59
63
|
if (quote) {
|
|
60
64
|
if (char === quote) quote = undefined;
|
|
61
|
-
else if (char === "\\" && quote === '"' && index + 1 < line.length) word += line[++index];
|
|
65
|
+
else if (!windows && char === "\\" && quote === '"' && index + 1 < line.length) word += line[++index];
|
|
62
66
|
else word += char;
|
|
63
67
|
} else if (char === "'" || char === '"') {
|
|
64
68
|
quote = char;
|
|
65
69
|
started = true;
|
|
66
|
-
} else if (char === "\\" && index + 1 < line.length) {
|
|
70
|
+
} else if (!windows && char === "\\" && index + 1 < line.length) {
|
|
67
71
|
word += line[++index];
|
|
68
72
|
started = true;
|
|
69
73
|
} else if (/\s/.test(char)) {
|
|
@@ -135,7 +139,7 @@ export function createEditSession(deps: EditDeps, events: EditEvents) {
|
|
|
135
139
|
/** Opens the paths in the user's editor: the chosen command, else VS Code, else the system's text editor. */
|
|
136
140
|
async function open(paths: string[]): Promise<string> {
|
|
137
141
|
if (deps.editor) {
|
|
138
|
-
const [command, ...args] = shellWords(deps.editor);
|
|
142
|
+
const [command, ...args] = shellWords(deps.editor, deps.platform === "win32");
|
|
139
143
|
if (!command) throw new Error(`${EDITOR_ENV} is empty`);
|
|
140
144
|
if (!deps.onPath(command)) throw new Error(`${EDITOR_ENV}=${deps.editor}, but ${command} is not on PATH`);
|
|
141
145
|
await deps.launch(command, [...args, ...paths]);
|
|
@@ -145,6 +149,12 @@ export function createEditSession(deps: EditDeps, events: EditEvents) {
|
|
|
145
149
|
await deps.launch("code", paths);
|
|
146
150
|
return "VS Code";
|
|
147
151
|
}
|
|
152
|
+
if (deps.platform === "win32") {
|
|
153
|
+
// Notepad, by absolute path, is on every Windows machine; it opens one file per window.
|
|
154
|
+
const notepad = windowsTextEditor();
|
|
155
|
+
for (const path of paths) await deps.launch(notepad, [path]);
|
|
156
|
+
return "Notepad";
|
|
157
|
+
}
|
|
148
158
|
const [command, args] =
|
|
149
159
|
deps.platform === "darwin" ? (["open", ["-t", ...paths]] as const) : (["xdg-open", paths] as const);
|
|
150
160
|
if (!deps.onPath(command)) throw new Error(`edit mode needs ${EDITOR_ENV}, VS Code (code), or ${command} on PATH`);
|
|
@@ -160,6 +170,35 @@ export function createEditSession(deps: EditDeps, events: EditEvents) {
|
|
|
160
170
|
return "the system text editor";
|
|
161
171
|
}
|
|
162
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Zero-context hunks from `before` to `after`. `diff` when it is on PATH; on Windows without it (Git for Windows keeps `diff` out of the
|
|
175
|
+
* PATH it adds), `git diff --no-index`, whose header lines are taken out. Both exit 1 when the files differ.
|
|
176
|
+
*/
|
|
177
|
+
function diffOf(before: string, after: string, display: string): string {
|
|
178
|
+
if (deps.platform === "win32" && !deps.onPath("diff")) {
|
|
179
|
+
const git = deps.run("git", [
|
|
180
|
+
"-c",
|
|
181
|
+
"core.autocrlf=false",
|
|
182
|
+
"-c",
|
|
183
|
+
"core.safecrlf=false",
|
|
184
|
+
"diff",
|
|
185
|
+
"--no-index",
|
|
186
|
+
"--no-color",
|
|
187
|
+
"--no-ext-diff",
|
|
188
|
+
"-U0",
|
|
189
|
+
"--",
|
|
190
|
+
before,
|
|
191
|
+
after,
|
|
192
|
+
]);
|
|
193
|
+
if (git.status !== 0 && git.status !== 1)
|
|
194
|
+
throw new Error(`git diff failed: ${(git.stderr || git.stdout).trim()}`);
|
|
195
|
+
return gitDiffHunks(git.stdout);
|
|
196
|
+
}
|
|
197
|
+
const diff = deps.run("diff", ["-U0", "--label", `a/${display}`, "--label", `b/${display}`, before, after]);
|
|
198
|
+
if (diff.status !== 0 && diff.status !== 1) throw new Error(`diff failed: ${diff.stderr.trim()}`);
|
|
199
|
+
return diff.stdout;
|
|
200
|
+
}
|
|
201
|
+
|
|
163
202
|
const paused = (path: string): boolean =>
|
|
164
203
|
[...active.values()].some((call) => call.path === undefined || call.path === path);
|
|
165
204
|
|
|
@@ -171,9 +210,7 @@ export function createEditSession(deps: EditDeps, events: EditEvents) {
|
|
|
171
210
|
deps.writeFile(before, doc.snapshot);
|
|
172
211
|
// The content read, not the file: a save landing between the read and the diff waits for the next report.
|
|
173
212
|
deps.writeFile(after, content);
|
|
174
|
-
const
|
|
175
|
-
if (diff.status !== 0 && diff.status !== 1) throw new Error(`diff failed: ${diff.stderr.trim()}`);
|
|
176
|
-
const section = feedbackMessage(doc.display, diff.stdout, doc.duringShell);
|
|
213
|
+
const section = feedbackMessage(doc.display, diffOf(before, after, doc.display), doc.duringShell);
|
|
177
214
|
doc.snapshot = content;
|
|
178
215
|
doc.duringShell = false;
|
|
179
216
|
return section;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { win32 } from "node:path";
|
|
3
|
+
|
|
4
|
+
/** Platform details the extension needs from the operating system: the browser opener, the PATH lookup, the fallback text editor. */
|
|
5
|
+
|
|
6
|
+
type Env = Record<string, string | undefined>;
|
|
7
|
+
|
|
8
|
+
/** A Windows program under `%SystemRoot%`, by absolute path, so that a `rundll32` or `notepad` earlier on PATH (Git for Windows puts its own tools there) is never the one run. */
|
|
9
|
+
export const systemProgram = (env: Env, ...parts: string[]): string =>
|
|
10
|
+
win32.join(env.SystemRoot ?? env.SYSTEMROOT ?? "C:\\Windows", ...parts);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The command that opens a URL in the default browser. On Windows it is `rundll32 url.dll,FileProtocolHandler`, which takes the URL as one
|
|
14
|
+
* argument with no shell in between, so `&` and `%` in a query reach the browser as written (`cmd /c start` would parse them), and exits 0.
|
|
15
|
+
*/
|
|
16
|
+
export function urlOpener(
|
|
17
|
+
platform: string,
|
|
18
|
+
env: Env = process.env,
|
|
19
|
+
): [command: string, args: (url: string) => string[]] {
|
|
20
|
+
if (platform === "darwin") return ["open", (url) => [url]];
|
|
21
|
+
if (platform === "win32")
|
|
22
|
+
return [systemProgram(env, "System32", "rundll32.exe"), (url) => ["url.dll,FileProtocolHandler", url]];
|
|
23
|
+
return ["xdg-open", (url) => [url]];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const isFile = (path: string): boolean => {
|
|
27
|
+
try {
|
|
28
|
+
return statSync(path).isFile();
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether `command` names a program on PATH that can be started without a shell. On Windows the PATH is scanned, with no shell (a desk
|
|
36
|
+
* machine need not have `sh`), for `.exe` and `.com` only: Node spawns nothing else without `shell: true`, so a `code.cmd` shim counts as
|
|
37
|
+
* absent and the caller falls back instead of failing at launch. Elsewhere `sh -c 'command -v'` answers, as it did before.
|
|
38
|
+
*/
|
|
39
|
+
export function onPath(
|
|
40
|
+
command: string,
|
|
41
|
+
platform: string,
|
|
42
|
+
env: Env = process.env,
|
|
43
|
+
exists: (path: string) => boolean = isFile,
|
|
44
|
+
shell: (command: string) => boolean = () => false,
|
|
45
|
+
): boolean {
|
|
46
|
+
if (platform !== "win32") return shell(command);
|
|
47
|
+
const ext = /\.([^\\/.]+)$/.exec(command)?.[1]?.toLowerCase();
|
|
48
|
+
if (ext === "exe" || ext === "com") return checkNames([command], command, env, exists);
|
|
49
|
+
return checkNames([`${command}.exe`, `${command}.com`], command, env, exists);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function checkNames(names: string[], command: string, env: Env, exists: (path: string) => boolean): boolean {
|
|
53
|
+
const path = env.PATH ?? env.Path ?? "";
|
|
54
|
+
if (/[\\/]/.test(command)) return names.some((name) => exists(name));
|
|
55
|
+
const dirs = path
|
|
56
|
+
.split(";")
|
|
57
|
+
.map((dir) => dir.replace(/^"(.*)"$/, "$1"))
|
|
58
|
+
.filter((dir) => dir !== "");
|
|
59
|
+
return dirs.some((dir) => names.some((name) => exists(win32.join(dir, name))));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The text editor a Windows machine always has, for edit mode when neither PI_BETTER_SHOW_EDITOR nor VS Code is there; it opens one file per window. */
|
|
63
|
+
export const windowsTextEditor = (env: Env = process.env): string => systemProgram(env, "notepad.exe");
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* `git diff --no-index` output with git's own header lines taken out, so that what is left reads like `diff -U0` without `--label`: the
|
|
67
|
+
* `@@` hunks and their lines. The `---` and `+++` lines go too; the caller names the file itself. Git writes the line before a hunk after its
|
|
68
|
+
* header (`@@ -2 +2 @@ one`); that context is dropped, as `diff` has none. A binary change keeps git's `Binary files … differ` line.
|
|
69
|
+
*/
|
|
70
|
+
export function gitDiffHunks(stdout: string): string {
|
|
71
|
+
const lines = stdout.split("\n");
|
|
72
|
+
const first = lines.findIndex((line) => line.startsWith("@@"));
|
|
73
|
+
if (first === -1) return lines.find((line) => line.startsWith("Binary files ")) ?? "";
|
|
74
|
+
return lines
|
|
75
|
+
.slice(first)
|
|
76
|
+
.map((line) => (line.startsWith("@@") ? line.replace(/^(@@ [^@]* @@).*$/, "$1") : line))
|
|
77
|
+
.join("\n");
|
|
78
|
+
}
|