@jackice/pi-approval-gate 0.1.4 → 0.1.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.6
4
+
5
+ - Use a full-width rounded approval dialog with a warning heading and a highlighted selection.
6
+ - Select Allow once by default. Enter still requires the confirmation delay, and Esc denies immediately.
7
+
8
+ ## 0.1.5
9
+
10
+ - Add OMP-style borders, spacing, and keyboard hints to the default approval dialog. Adapt the command preview to fit the choices and bottom border in short terminals.
11
+
3
12
  ## 0.1.4
4
13
 
5
14
  - English code comments, changelog, and review notes. No behavior change.
package/README.md CHANGED
@@ -32,7 +32,7 @@ PI_APPROVAL_ARM_MS
32
32
 
33
33
  Pi parses extension flags after loading the extension. Settings are read when each request is created.
34
34
 
35
- The native-list view is the default. It starts on **Deny** and supports Page Up and Page Down for long commands. It uses the same approval gate, confirmation delay, and cancellation rules as the styled panel.
35
+ The native-list view is the default. Both views start on **Allow once** and require Enter after the confirmation delay. Press Down to select **Deny**, or Esc to deny immediately. Use Page Up and Page Down to inspect long commands.
36
36
 
37
37
  ### Optional uv guard
38
38
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jackice/pi-approval-gate",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Dangerous bash approval gate for Pi with final execution-parameter binding.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/dialog.ts CHANGED
@@ -36,6 +36,7 @@ export function createApprovalPrompt(
36
36
  const view = options.ui === "native"
37
37
  ? (nativeView = createNativeApprovalView(request, tui, theme, options.maxCommandLines))
38
38
  : new ApprovalView(fromPiTheme(theme), {
39
+ selected: "allow_once",
39
40
  request: {
40
41
  toolName: request.toolName,
41
42
  command: request.command,
@@ -70,7 +71,7 @@ export function createApprovalPrompt(
70
71
  blocked: { kind: "permission", message: "bash approval" },
71
72
  ...(options.ui === "native" ? {
72
73
  overlay: true,
73
- overlayOptions: { width: "90%", maxHeight: "90%", margin: 1 },
74
+ overlayOptions: { width: "100%", maxHeight: "90%", anchor: "bottom-center", margin: { top: 1, bottom: 1 } },
74
75
  onHandle: (handle) => nativeView?.setOverlayHandle(handle),
75
76
  } : {}),
76
77
  },
@@ -1,5 +1,5 @@
1
1
  import type { ExtensionUIContext, Theme } from "@earendil-works/pi-coding-agent";
2
- import { Key, SelectList, matchesKey, truncateToWidth, visibleWidth, type OverlayHandle, type TUI } from "@earendil-works/pi-tui";
2
+ import { Key, matchesKey, truncateToWidth, visibleWidth, type OverlayHandle, type TUI } from "@earendil-works/pi-tui";
3
3
 
4
4
  import type { ApprovalRequest, PromptChoice } from "./gate.ts";
5
5
 
@@ -13,24 +13,19 @@ export function createNativeApprovalView(
13
13
  theme: Theme,
14
14
  maxCommandLines: number | undefined,
15
15
  ) {
16
- const selection = new SelectList([
17
- { label: "Deny", value: "deny" },
16
+ let selected: PromptChoice = "allow_once";
17
+ const choices = [
18
18
  { label: "Allow once", value: "allow_once" },
19
- ], 2, {
20
- selectedPrefix: (text) => theme.fg("accent", text),
21
- selectedText: (text) => theme.fg("accent", text),
22
- description: (text) => theme.fg("muted", text),
23
- scrollInfo: (text) => theme.fg("muted", text),
24
- noMatch: (text) => theme.fg("muted", text),
25
- });
19
+ { label: "Deny", value: "deny" },
20
+ ] as const;
26
21
  const limit = maxCommandLines !== undefined && Number.isFinite(maxCommandLines) && maxCommandLines >= 1
27
22
  ? Math.floor(maxCommandLines) : 8;
28
- const title = `Bash approval · ${escapeControls(request.toolName)}`;
23
+ const title = "⚠ Dangerous command";
29
24
  const risk = `Risk: ${request.risk.toUpperCase()}`;
30
25
  const fields = [
31
- `cwd: ${escapeControls(request.cwd)}`,
32
- `reason: ${escapeControls(request.reason)}`,
33
- `command: ${escapeControls(request.command)}`,
26
+ `Reason: ${escapeControls(request.reason)}`,
27
+ `Cwd: ${escapeControls(request.cwd)}`,
28
+ `Command: ${escapeControls(request.command)}`,
34
29
  ];
35
30
  let overlayHandle: OverlayHandle | undefined;
36
31
  let lines: string[] = [];
@@ -57,48 +52,74 @@ export function createNativeApprovalView(
57
52
  overlayHandle = handle;
58
53
  },
59
54
  render(width: number): readonly string[] {
60
- if (width <= 0) {
55
+ if (width < 3) {
61
56
  renderedContextFits = false;
62
57
  return [];
63
58
  }
59
+ const inset = width >= 12 ? " " : "";
60
+ const innerWidth = width - 2 - 2 * inset.length;
64
61
  if (lastWidth !== width) {
65
62
  lastWidth = width;
66
- lines = fields.flatMap((field) => wrapCodeText(field, width));
63
+ lines = fields.flatMap((field) => wrapCodeText(field, innerWidth));
67
64
  }
68
- pageRows = Math.max(1, Math.min(limit, Math.floor(tui.terminal.rows * 0.75) - 6));
65
+ const terminalRows = tui.terminal.rows;
66
+ // Match the overlay's height cap, including its top and bottom margins.
67
+ const availableRows = Math.min(Math.floor(terminalRows * 0.9), terminalRows - 2);
68
+ const spacer = availableRows >= 15 ? [""] : [];
69
+ const previewRows = availableRows - 7 - 4 * spacer.length;
70
+ const paginated = lines.length > Math.min(limit, previewRows);
71
+ pageRows = Math.max(1, Math.min(limit, previewRows - Number(paginated)));
69
72
  offset = Math.min(offset, Math.max(0, lines.length - pageRows));
70
73
  const end = Math.min(lines.length, offset + pageRows);
71
74
  const fullPage = `Page ${offset + 1}-${end}/${lines.length}`;
72
75
  const compactRange = `${offset + 1}-${end}/${lines.length}`;
73
76
  const currentPage = `${offset + 1}/${lines.length}`;
74
- const page = visibleWidth(fullPage) <= width
77
+ const page = visibleWidth(fullPage) <= innerWidth
75
78
  ? fullPage
76
- : visibleWidth(compactRange) <= width
79
+ : visibleWidth(compactRange) <= innerWidth
77
80
  ? compactRange
78
- : visibleWidth(currentPage) <= width
81
+ : visibleWidth(currentPage) <= innerWidth
79
82
  ? currentPage
80
83
  : String(offset + 1);
81
- const riskLabel = visibleWidth(risk) <= width
84
+ const riskLabel = visibleWidth(risk) <= innerWidth
82
85
  ? risk
83
- : visibleWidth(`Risk: ${request.risk[0]!.toUpperCase()}`) <= width
86
+ : visibleWidth(`Risk: ${request.risk[0]!.toUpperCase()}`) <= innerWidth
84
87
  ? `Risk: ${request.risk[0]!.toUpperCase()}`
85
88
  : request.risk[0]!.toUpperCase();
86
- const hint = "PgUp/PgDn: scroll · Esc: deny";
87
- const choices = selection.render(width);
88
- const output = [
89
- truncateToWidth(theme.fg("warning", title), width),
90
- truncateToWidth(theme.fg("warning", riskLabel), width),
91
- truncateToWidth(theme.fg("muted", page), width),
92
- ...lines.slice(offset, end),
93
- truncateToWidth(theme.fg("muted", hint), width),
94
- ...choices,
89
+ const controls = "↑/↓ navigate ↵ select Esc cancel";
90
+ const fullHint = paginated ? controls + " PgUp/PgDn scroll" : controls;
91
+ const hint = visibleWidth(fullHint) <= innerWidth ? fullHint : paginated ? "↑/↓ ↵ Esc PgUp/PgDn" : "↑/↓ ↵ Esc";
92
+ const optionRows = choices.map(({ label, value }) => {
93
+ const active = value === selected;
94
+ const row = `${active ? "❯" : " "} ${label}`;
95
+ if (!active || visibleWidth(row) > innerWidth) return row;
96
+ return theme.bg("selectedBg", theme.fg("warning", row) + " ".repeat(innerWidth - visibleWidth(row)));
97
+ });
98
+ const body = [
99
+ ...spacer,
100
+ theme.fg("warning", riskLabel),
101
+ ...(paginated ? [theme.fg("muted", page)] : []),
102
+ ...lines.slice(offset, end).map((line) => theme.fg("warning", line)),
103
+ ...spacer,
104
+ theme.fg("warning", "Allow?"),
105
+ ...optionRows,
106
+ ...spacer,
107
+ theme.fg("dim", hint),
108
+ ...spacer,
95
109
  ];
110
+ const border = (text: string) => theme.fg("accent", text);
111
+ const heading = truncateToWidth(` ${title} `, width - 3, "");
112
+ const top = border("╭─") + theme.fg("warning", theme.bold(heading))
113
+ + border("─".repeat(width - 3 - visibleWidth(heading)) + "╮");
114
+ const side = border("│");
115
+ const output = [top, ...body.map((line) => {
116
+ const content = truncateToWidth(line, innerWidth);
117
+ return side + inset + content + " ".repeat(innerWidth - visibleWidth(content)) + inset + side;
118
+ }), border("╰" + "─".repeat(width - 2) + "╯")];
96
119
  renderedWidth = width;
97
120
  renderedRows = output.length;
98
- renderedContextFits = [title, riskLabel, page, hint].every((line) => visibleWidth(line) <= width)
99
- && choices.length > 0
100
- && choices.every((line) => visibleWidth(line) <= width)
101
- && choices.some((line) => line.includes("Allow once"));
121
+ renderedContextFits = body.every((line) => visibleWidth(line) <= innerWidth)
122
+ && visibleWidth(title) <= width - 3;
102
123
  return output;
103
124
  },
104
125
  invalidate(): void {
@@ -108,10 +129,10 @@ export function createNativeApprovalView(
108
129
  handleInput(data: string, keys: ApprovalKeys): PromptChoice | undefined {
109
130
  if (keys.matches(data, "tui.select.cancel")) return "deny";
110
131
  if (keys.matches(data, "tui.select.confirm")) {
111
- return canAllow() && selection.getSelectedItem()?.value === "allow_once" ? "allow_once" : "deny";
132
+ return canAllow() && selected === "allow_once" ? "allow_once" : "deny";
112
133
  }
113
- if (keys.matches(data, "tui.select.up")) selection.setSelectedIndex(0);
114
- else if (keys.matches(data, "tui.select.down")) selection.setSelectedIndex(1);
134
+ if (keys.matches(data, "tui.select.up")) selected = "allow_once";
135
+ else if (keys.matches(data, "tui.select.down")) selected = "deny";
115
136
  else if (matchesKey(data, Key.pageUp) || keys.matches(data, "tui.select.pageUp")) offset = Math.max(0, offset - pageRows);
116
137
  else if (matchesKey(data, Key.pageDown) || keys.matches(data, "tui.select.pageDown")) offset = Math.min(Math.max(0, lines.length - pageRows), offset + pageRows);
117
138
  return undefined;