pi-autosuggestions 0.2.4 → 0.3.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.
@@ -1 +1 @@
1
- {".":"0.2.4"}
1
+ {".":"0.3.0"}
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.0](https://github.com/0xPlayerOne/pi-autosuggestions/compare/pi-autosuggestions-v0.2.4...pi-autosuggestions-v0.3.0) (2026-09-04)
4
+
5
+
6
+ ### Features
7
+
8
+ * command-name completion from PATH at the bash command position ([9b4eef6](https://github.com/0xPlayerOne/pi-autosuggestions/commit/9b4eef62c780979fc972cb2d304eeefe8fd98db7))
9
+
3
10
  ## [0.2.4](https://github.com/0xPlayerOne/pi-autosuggestions/compare/pi-autosuggestions-v0.2.3...pi-autosuggestions-v0.2.4) (2026-09-04)
4
11
 
5
12
 
package/README.md CHANGED
@@ -23,6 +23,21 @@ pi install git:github.com/0xPlayerOne/pi-autosuggestions@pi-autosuggestions-v0.2
23
23
  pi install npm:pi-autosuggestions
24
24
  ```
25
25
 
26
+ ## Scope
27
+
28
+ | Layer | Where it works |
29
+ |-------|----------------|
30
+ | History ghost suggestions | **Everywhere** — normal prompts and `!`/`!!` bash mode |
31
+ | Path completion + dropdown | **Bash mode only** (`!`/`!!` commands) |
32
+ | Command-name completion | **Bash mode only**, first word after `!` |
33
+ | Beam cursor | **Everywhere** |
34
+
35
+ History is **session-scoped**: the ghost can only suggest prompts you already
36
+ submitted *in the current session* (pi has no persistent shell-history file
37
+ across sessions yet). If you mostly run `!` commands, the ghost will mostly
38
+ appear in bash mode — it isn't restricted there, it just has nothing to
39
+ suggest elsewhere.
40
+
26
41
  ## Features
27
42
 
28
43
  ### zsh-autosuggestions behavior
@@ -37,6 +52,20 @@ pi install npm:pi-autosuggestions
37
52
  - **`Esc`** — dismiss the suggestion until your next edit
38
53
  - Suggestion hides when the cursor leaves end-of-line, returns when you come back
39
54
 
55
+ ### Command completion (bash mode)
56
+
57
+ The first word after `!` completes against **executables on your `$PATH`** —
58
+ like zsh's command position, no plugins needed:
59
+
60
+ | Input | Result |
61
+ |-------|--------|
62
+ | `!gi` | dropdown — `git`, `gh`, `gimp`, … (marked `command`) |
63
+ | `!whoa` | inline ghost `mi` → `whoami` |
64
+
65
+ File paths that also match are listed after the commands. Per-command
66
+ *subcommand* completion (`git st` → `status`) is zsh's `compdef` system and is
67
+ out of scope — use history or type it.
68
+
40
69
  ### Path completion (fish/zsh-completion flavored)
41
70
 
42
71
  In bash mode (`!command`), a second strategy completes **filesystem paths**:
@@ -20,6 +20,9 @@ import {
20
20
  } from "@earendil-works/pi-tui";
21
21
 
22
22
  /** Runtime access to Editor internals that are private in the type declarations. */
23
+ import { readdirSync, statSync } from "node:fs";
24
+ import type { AutocompleteItem } from "@earendil-works/pi-tui";
25
+
23
26
  interface EditorInternals {
24
27
  state: { lines: string[]; cursorLine: number; cursorCol: number };
25
28
  autocompleteProvider?: AutocompleteProvider | null;
@@ -248,6 +251,52 @@ class BashInlineEditor extends CustomEditor {
248
251
  * The ghost-vs-dropdown split (one match → ghost, many → dropdown) is
249
252
  * decided in updateGhost(), which sees the final suggestion counts.
250
253
  */
254
+ /** Executable names on $PATH, cached for the session (zsh command completion). */
255
+ private pathCommandCache: string[] | null = null;
256
+
257
+ private getPathCommands(): string[] {
258
+ if (this.pathCommandCache) {
259
+ return this.pathCommandCache;
260
+ }
261
+ const names = new Set<string>();
262
+ for (const dir of (process.env.PATH ?? "").split(":")) {
263
+ if (!dir) {
264
+ continue;
265
+ }
266
+ let entries;
267
+ try {
268
+ entries = readdirSync(dir);
269
+ } catch {
270
+ continue;
271
+ }
272
+ for (const name of entries) {
273
+ if (names.has(name)) {
274
+ continue;
275
+ }
276
+ try {
277
+ // eslint-disable-next-line no-bitwise
278
+ if (statSync(`${dir}/${name}`).mode & 0o111) {
279
+ names.add(name);
280
+ }
281
+ } catch {
282
+ // dangling symlink or unreadable entry
283
+ }
284
+ }
285
+ }
286
+ this.pathCommandCache = [...names].sort();
287
+ return this.pathCommandCache;
288
+ }
289
+
290
+ /** PATH commands starting with the typed command word. */
291
+ private matchingCommands(word: string): string[] {
292
+ if (!word) {
293
+ return [];
294
+ }
295
+ return this.getPathCommands()
296
+ .filter((name) => name.startsWith(word))
297
+ .slice(0, 100);
298
+ }
299
+
251
300
  private createBashProvider(current: AutocompleteProvider): AutocompleteProvider {
252
301
  const self = this;
253
302
  return {
@@ -257,26 +306,54 @@ class BashInlineEditor extends CustomEditor {
257
306
  }
258
307
  const before = (lines[cursorLine] ?? "").slice(0, cursorCol);
259
308
  let queryLines = lines;
309
+ let commandItems: AutocompleteItem[] = [];
310
+ let word = "";
260
311
  if (self.commandPosition(before)) {
261
- // Neutralize the command word so the provider completes the
262
- // (empty) argument position: every entry matches.
312
+ word = before.replace(/^[ \t]*!+/, "");
313
+ const commands = self.matchingCommands(word);
263
314
  queryLines = [...lines];
264
- queryLines[cursorLine] = before.replace(/![^!\s]*$/, "! ");
315
+ if (commands.length > 0) {
316
+ // The typed word is a prefix of real commands: offer them
317
+ // first, plus any matching file paths as fallback.
318
+ commandItems = commands.map((name) => ({
319
+ value: name,
320
+ label: name,
321
+ description: "command",
322
+ }));
323
+ queryLines[cursorLine] = `! ${word}`;
324
+ } else {
325
+ // Neutralize the command word so the provider completes the
326
+ // (empty) argument position: every entry matches.
327
+ queryLines[cursorLine] = before.replace(/![^!\s]*$/, "! ");
328
+ }
265
329
  }
266
- return current.getSuggestions(queryLines, cursorLine, cursorCol, {
330
+ const suggestions = await current.getSuggestions(queryLines, cursorLine, cursorCol, {
267
331
  ...options,
268
332
  force: true,
269
333
  });
334
+ if (commandItems.length === 0) {
335
+ return suggestions;
336
+ }
337
+ return {
338
+ items: [...commandItems, ...(suggestions?.items ?? [])],
339
+ prefix: word,
340
+ };
270
341
  },
271
342
 
272
343
  applyCompletion(lines, cursorLine, cursorCol, item, prefix) {
273
344
  const before = (lines[cursorLine] ?? "").slice(0, cursorCol);
274
345
  if (self.inBashMode() && self.commandPosition(before)) {
275
- // Completing from the command position: insert the chosen path
276
- // as the first argument ("!cd" + AppleStuff/ → "!cd AppleStuff/").
277
346
  const value = item.value ?? "";
278
347
  const line = lines[cursorLine] ?? "";
279
348
  const newLines = [...lines];
349
+ if (item.description === "command") {
350
+ // Completing the command word itself ("!gi" + git → "!git").
351
+ const start = cursorCol - prefix.length;
352
+ newLines[cursorLine] = line.slice(0, start) + value + line.slice(cursorCol);
353
+ return { lines: newLines, cursorLine, cursorCol: start + value.length };
354
+ }
355
+ // Completing from the command position: insert the chosen path
356
+ // as the first argument ("!cd" + AppleStuff/ → "!cd AppleStuff/").
280
357
  newLines[cursorLine] = `${line.slice(0, cursorCol)} ${value}${line.slice(cursorCol)}`;
281
358
  return { lines: newLines, cursorLine, cursorCol: cursorCol + 1 + value.length };
282
359
  }
@@ -336,7 +413,8 @@ class BashInlineEditor extends CustomEditor {
336
413
  if (!value || !value.toLowerCase().startsWith(token.toLowerCase())) {
337
414
  return null;
338
415
  }
339
- const separator = this.commandPosition(before) ? " " : "";
416
+ const isCommand = suggestions?.items[0]?.description === "command";
417
+ const separator = isCommand ? "" : (this.commandPosition(before) ? " " : "");
340
418
  const suggestion = before.slice(0, before.length - token.length) + separator + value;
341
419
  if (suggestion.length <= before.length) {
342
420
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-autosuggestions",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "description": "zsh-autosuggestions-style ghost completions for the pi coding agent — history-based inline suggestions, path completion in bash mode, blinking beam cursor",
5
5
  "keywords": [
6
6
  "pi-package",