-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
134 lines (117 loc) · 3.48 KB
/
Copy pathindex.ts
File metadata and controls
134 lines (117 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type {
ExtensionAPI,
ExtensionCommandContext,
} from "@earendil-works/pi-coding-agent";
export type DebugKeysPi = Pick<ExtensionAPI, "registerCommand" | "sendMessage">;
type TerminalUnsubscribe = ReturnType<
ExtensionCommandContext["ui"]["onTerminalInput"]
>;
const CUSTOM_TYPE = "debug-keys";
const STATUS_KEY = "debug-keys";
const COMMANDS = [
{
value: "on",
label: "on",
description: "Start printing key codes",
},
{
value: "off",
label: "off",
description: "Stop printing key codes",
},
];
function print(pi: DebugKeysPi, content: string): void {
pi.sendMessage(
{
customType: CUSTOM_TYPE,
content,
display: true,
},
{ triggerTurn: false },
);
}
function usage(lead?: string): string {
return [
lead,
"Usage: /debug-keys on [count]|off",
"",
"Commands:",
" /debug-keys on [count] Start printing raw terminal input, then stop after the optional positive key count.",
" /debug-keys off Stop printing key codes.",
"",
"Output includes the JSON-escaped raw data.",
]
.filter((line): line is string => line !== undefined)
.join("\n");
}
export function formatKeyData(data: string): string {
return `/debug-keys: ${JSON.stringify(data)}`;
}
export function createExtension(pi: DebugKeysPi): void {
let unsubscribe: TerminalUnsubscribe | undefined;
let activeCtx: Pick<ExtensionCommandContext, "ui"> | undefined;
const stop = () => {
unsubscribe?.();
unsubscribe = undefined;
activeCtx?.ui.setStatus(STATUS_KEY, undefined);
activeCtx = undefined;
};
const start = (ctx: ExtensionCommandContext, count?: number) => {
if (unsubscribe) stop();
activeCtx = ctx;
let remaining = count;
unsubscribe = ctx.ui.onTerminalInput((data) => {
print(pi, formatKeyData(data));
if (remaining !== undefined) {
remaining -= 1;
if (remaining === 0) stop();
}
return undefined;
});
ctx.ui.setStatus(
STATUS_KEY,
count === undefined ? "/debug-keys on" : `/debug-keys on ${count}`,
);
};
pi.registerCommand("debug-keys", {
description: "Print key codes for extension development",
getArgumentCompletions: (prefix: string) => {
const normalized = prefix.trim().toLowerCase();
return COMMANDS.filter((command) => command.value.startsWith(normalized));
},
handler: async (args: string, ctx: ExtensionCommandContext) => {
const command = args.trim().toLowerCase();
if (command === "") {
print(pi, usage());
return;
}
const onMatch = /^on(?:\s+(\d+))?$/.exec(command);
if (onMatch) {
const count = onMatch[1] === undefined ? undefined : Number(onMatch[1]);
if (
count !== undefined &&
(!Number.isSafeInteger(count) || count < 1)
) {
print(pi, usage(`Invalid debug-keys count: ${onMatch[1]}`));
return;
}
start(ctx, count);
const limit =
count === undefined
? ""
: ` for ${count} ${count === 1 ? "keystroke" : "keystrokes"}`;
print(pi, `/debug-keys: debug key logging enabled${limit}`);
return;
}
if (command === "off") {
stop();
print(pi, "/debug-keys: debug key logging disabled");
return;
}
print(pi, usage(`Unknown debug-keys command: ${args.trim()}`));
},
});
}
export default function debugKeysExtension(pi: ExtensionAPI): void {
createExtension(pi);
}