@each1024/pi-mini-mode 1.3.5 → 1.3.7

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.
@@ -0,0 +1,80 @@
1
+ import { existsSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { dirname, isAbsolute, resolve } from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { getCapabilities, sliceByColumn, stripTerminalSequences, visibleWidth } from "@earendil-works/pi-tui";
6
+
7
+ /** Pi 0.85's allowlist predates Otty; preserve explicit opt-outs and multiplexer detection. */
8
+ export function inputCapabilities(env = process.env) {
9
+ const caps = getCapabilities();
10
+ if (env.TERM_PROGRAM?.toLowerCase() !== "otty" || env.TMUX || /^(screen|tmux)/.test(env.TERM ?? "")) return caps;
11
+ return { ...caps, hyperlinks: env.PI_HYPERLINKS === "0" ? false : true,
12
+ images: env.PI_IMAGE_PROTOCOL === "0" || env.PI_IMAGE_PROTOCOL === "none" ? null : caps.images ?? "kitty" as const };
13
+ }
14
+
15
+ export function localPath(path: string, cwd: string): string {
16
+ return path.startsWith("~/") ? resolve(homedir(), path.slice(2)) : isAbsolute(path) ? path : resolve(cwd, path);
17
+ }
18
+
19
+ /** Opening/closing fences must stay intact; quoted paths inside them are not links. */
20
+ function fenceRanges(text: string): Array<[number, number]> {
21
+ const ranges: Array<[number, number]> = [];
22
+ const open = /(^|\n)( {0,3})(`{3,}|~{3,})[^\n]*\n/g;
23
+ for (let match = open.exec(text); match; match = open.exec(text)) {
24
+ const start = match.index + match[1].length;
25
+ const escaped = match[3].replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
26
+ // Search only after the opener, then resume after the closer, so a closing
27
+ // fence can never be read as the next opener and swallow the text behind it.
28
+ const close = new RegExp(`(?:^|\\n) {0,3}${escaped}[ \\t]*(?:\\n|$)`).exec(text.slice(open.lastIndex));
29
+ if (!close) {
30
+ ranges.push([start, text.length]);
31
+ break;
32
+ }
33
+ open.lastIndex += close.index + close[0].length;
34
+ ranges.push([start, open.lastIndex]);
35
+ }
36
+ return ranges;
37
+ }
38
+
39
+ /** Quoted paths may contain spaces; unquoted paths end at prose delimiters. */
40
+ export function filePaths(text: string) {
41
+ const matches: Array<{ path: string; start: number; end: number }> = [];
42
+ const fences = fenceRanges(text);
43
+ const pattern = /(["'`])([^\r\n]*?)\1|[^\s"'`<>()[\]{},。;!?]+/g;
44
+ for (const match of text.matchAll(pattern)) {
45
+ const path = (match[2] ?? match[0]).replace(/[.,;:!?]+$/, "");
46
+ if (!path || /[\x00-\x1f\x7f]/.test(path) || /^[a-z][\w+.-]*:\/\//i.test(path)) continue;
47
+ if (!/^(?:\/|~\/|\.{1,2}\/|[a-z]:[\\/])|[\\/]|\.[a-z\d]{1,12}$/i.test(path)) continue;
48
+ const start = match.index + (match[1] ? 1 : 0);
49
+ const end = start + path.length;
50
+ if (fences.some(([from, to]) => start < to && end > from)) continue;
51
+ matches.push({ path, start, end });
52
+ }
53
+ return matches;
54
+ }
55
+
56
+ // BEL survives Markdown's backslash-escape parsing, unlike the ST terminator.
57
+ export const fileLink = (text: string, path: string) => `\x1b]8;;${pathToFileURL(path).href}\x07\x1b[4m${text}\x1b[24m\x1b]8;;\x07`;
58
+
59
+ /** Display-only: never change persisted messages, existing links, or remote URLs. */
60
+ export function linkMessageFiles(text: string, cwd: string): string {
61
+ if (!inputCapabilities().hyperlinks) return text;
62
+ let imageNumber = 0;
63
+ return text.split(/(\x1b\]8;[^\x07]*(?:\x07)[\s\S]*?\x1b\]8;;\x07|\[[^\]\n]*\]\([^\n]*?\))/g).map((part, index) => {
64
+ if (index % 2) return part;
65
+ const matches = filePaths(part).map(match => ({ ...match, label: /\.(png|jpe?g|gif|webp)$/i.test(match.path) && existsSync(localPath(match.path, cwd)) ? `[image${++imageNumber}]` : match.path }));
66
+ for (const match of matches.reverse()) {
67
+ const path = localPath(match.path, cwd);
68
+ if (!existsSync(path)) continue;
69
+ part = part.slice(0, match.start) + fileLink(match.label, path) + part.slice(match.end);
70
+ }
71
+ return part;
72
+ }).join("");
73
+ }
74
+
75
+ /** Decorate rendered columns without removing the editor's ANSI cursor marker. */
76
+ export function linkRenderedPath(line: string, start: number, end: number, path: string, directory = false): string {
77
+ if (!inputCapabilities().hyperlinks || end <= start) return line;
78
+ return sliceByColumn(line, 0, start) + fileLink(sliceByColumn(line, start, end - start), directory ? dirname(path) : path)
79
+ + sliceByColumn(line, end, Math.max(0, visibleWidth(stripTerminalSequences(line)) - end));
80
+ }