@d3ara1n/pi-command-palette 0.1.2 → 0.2.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/README.md CHANGED
@@ -24,7 +24,7 @@ Or add to `~/.pi/agent/settings.json`:
24
24
 
25
25
  | Shortcut | Action |
26
26
  |----------|--------|
27
- | `Ctrl+Shift+P` | Open command palette |
27
+ | `Ctrl+Shift+P` _(default, configurable)_ | Open command palette |
28
28
 
29
29
  The palette lists:
30
30
 
@@ -42,4 +42,28 @@ The "Model: Switch Model" action opens a secondary overlay listing all models wi
42
42
 
43
43
  ## Configuration
44
44
 
45
- No configuration needed. Works out of the box.
45
+ The default shortcut is `Ctrl+Shift+P`. If it conflicts with your terminal, override it via either of the following (evaluated in order, first match wins).
46
+
47
+ ### 1. Environment variable
48
+
49
+ Useful for terminals that intercept `Ctrl+Shift+<key>` before it reaches the session (e.g. Termius on Windows/WSL2):
50
+
51
+ ```bash
52
+ export PI_COMMAND_PALETTE_KEY=ctrl+alt+k
53
+ ```
54
+
55
+ Add it to your shell profile to persist (`~/.zshrc` on macOS, `~/.bashrc` on bash).
56
+
57
+ ### 2. settings.json
58
+
59
+ Set `commandPalette.shortcut` in `~/.pi/agent/settings.json` (global) or `.pi/settings.json` in your project (project overrides global):
60
+
61
+ ```jsonc
62
+ {
63
+ "commandPalette": {
64
+ "shortcut": "ctrl+alt+k"
65
+ }
66
+ }
67
+ ```
68
+
69
+ Any valid pi keybinding string works (e.g. `ctrl+shift+k`, `ctrl+alt+p`, `ctrl+k`). Restart pi (or run `/reload`) after changing the shortcut.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d3ara1n/pi-command-palette",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "description": "Global command palette for pi — press Ctrl+Shift+P to search and run commands from anywhere",
6
6
  "main": "src/index.ts",
package/src/config.ts ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Resolve the command-palette shortcut key from configuration sources.
3
+ *
4
+ * Priority (highest wins):
5
+ * 1. `PI_COMMAND_PALETTE_KEY` env var — works even when the terminal intercepts
6
+ * `Ctrl+Shift+P` before it reaches the session (e.g. Termius on Windows/WSL2).
7
+ * 2. `settings.json` `commandPalette.shortcut` (project `.pi/settings.json`
8
+ * overrides global `~/.pi/agent/settings.json`)
9
+ * 3. default `"ctrl+shift+p"`
10
+ *
11
+ * Why not a CLI flag? Flags are applied to the extension runtime AFTER extensions
12
+ * load, so `pi.getFlag()` only returns the registered default at `registerShortcut()`
13
+ * time. Env vars and settings files are both available immediately at process
14
+ * start, so they are the correct mechanisms here.
15
+ */
16
+
17
+ import * as fs from "node:fs";
18
+ import * as os from "node:os";
19
+ import * as path from "node:path";
20
+ import type { KeyId } from "@earendil-works/pi-tui";
21
+
22
+ export const DEFAULT_SHORTCUT = "ctrl+shift+p";
23
+
24
+ function getAgentDir(): string {
25
+ const envDir = process.env.PI_AGENT_DIR;
26
+ if (envDir) return envDir;
27
+ return path.join(os.homedir(), ".pi", "agent");
28
+ }
29
+
30
+ function readSettings(filePath: string): Record<string, unknown> {
31
+ try {
32
+ if (!fs.existsSync(filePath)) return {};
33
+ return JSON.parse(fs.readFileSync(filePath, "utf-8"));
34
+ } catch {
35
+ return {};
36
+ }
37
+ }
38
+
39
+ /** Lowercase + trim; returns undefined for empty/non-string input. */
40
+ function normalizeKey(raw: unknown): string | undefined {
41
+ if (typeof raw !== "string") return undefined;
42
+ const trimmed = raw.trim().toLowerCase();
43
+ return trimmed || undefined;
44
+ }
45
+
46
+ /**
47
+ * Resolve the command-palette shortcut key from env var → settings → default.
48
+ *
49
+ * @param cwd - Project working directory, for project-level settings override.
50
+ * Defaults to `process.cwd()` (accurate at pi startup when shortcuts register).
51
+ */
52
+ export function resolveShortcutKey(cwd: string = process.cwd()): KeyId {
53
+ // 1. Env var — highest priority, for terminals that intercept the default combo.
54
+ const envKey = normalizeKey(process.env.PI_COMMAND_PALETTE_KEY);
55
+ if (envKey) return envKey as KeyId;
56
+
57
+ // 2. settings.json — global ~/.pi/agent/settings.json; project overrides global.
58
+ const globalSettings = readSettings(path.join(getAgentDir(), "settings.json"));
59
+ const projectSettings = readSettings(path.join(cwd, ".pi", "settings.json"));
60
+ const globalCfg = (globalSettings.commandPalette ?? {}) as { shortcut?: unknown };
61
+ const projectCfg = (projectSettings.commandPalette ?? {}) as { shortcut?: unknown };
62
+ const settingsKey = normalizeKey(projectCfg.shortcut ?? globalCfg.shortcut);
63
+ if (settingsKey) return settingsKey as KeyId;
64
+
65
+ // 3. default
66
+ return DEFAULT_SHORTCUT as KeyId;
67
+ }
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  SelectList,
24
24
  Text,
25
25
  } from "@earendil-works/pi-tui";
26
+ import { resolveShortcutKey } from "./config.ts";
26
27
 
27
28
  // ── Types ──────────────────────────────────────────────────────────
28
29
 
@@ -389,7 +390,7 @@ async function showCommandPalette(pi: ExtensionAPI, ctx: ExtensionContext): Prom
389
390
  // ── Extension entry point ──────────────────────────────────────────
390
391
 
391
392
  export default function commandPaletteExtension(pi: ExtensionAPI) {
392
- pi.registerShortcut("ctrl+shift+p", {
393
+ pi.registerShortcut(resolveShortcutKey(), {
393
394
  description: "Open command palette",
394
395
  handler: async (ctx) => {
395
396
  await showCommandPalette(pi, ctx);