aboutsummaryrefslogtreecommitdiff
path: root/user/pi/extensions/stash-on-escape.ts
blob: aa1cd06b9712962d437c772b7b66e195a46b56c2 (plain)
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
// On Escape clear the editor and store the text in the history instead of doing nothing.
import {
  CustomEditor,
  type ExtensionAPI,
  type ExtensionContext,
  type KeybindingsManager,
} from "@earendil-works/pi-coding-agent";
import type { EditorTheme, TUI } from "@earendil-works/pi-tui";

class StashOnEscapeEditor extends CustomEditor {
  constructor(
    tui: TUI,
    theme: EditorTheme,
    private readonly keys: KeybindingsManager,
    private readonly ctx: ExtensionContext,
  ) {
    super(tui, theme, keys);
  }

  handleInput(data: string): void {
    const text = this.getExpandedText?.() ?? this.getText();

    if (
      this.ctx.isIdle() &&
      text.trim() &&
      this.keys.matches(data, "app.interrupt")
    ) {
      this.addToHistory(text);
      this.setText("");
      return;
    }

    super.handleInput(data);
  }
}

export default function (pi: ExtensionAPI) {
  pi.on("session_start", (_event, ctx) => {
    if (ctx.mode !== "tui") return;

    ctx.ui.setEditorComponent(
      (tui, theme, keybindings) =>
        new StashOnEscapeEditor(tui, theme, keybindings, ctx),
    );
  });
}