Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalCloseConfirm.ts

42 lines1.3 KB

1import { readLocalApi } from "~/localApi";
2​
3let pendingConfirmations = 0;
4​
5/** Whether a terminal-close confirmation is currently waiting on the user. */
6export function isTerminalCloseConfirmPending(): boolean {
7 return pendingConfirmations > 0;
8}
9​
10/**
11 * Confirmation for individual terminal close actions: drawer buttons, panel
12 * buttons, the `terminal.close` keybinding, and closing a terminal surface from
13 * the tab strip. Auto-exit cleanup and bulk tab closes skip this path and close
14 * directly.
15 */
16export async function confirmTerminalClose(
17 labels: readonly [string, ...string[]],
18): Promise<boolean> {
19 const localApi = readLocalApi();
20 if (!localApi) return true;
21 pendingConfirmations += 1;
22 try {
23 return await localApi.dialogs.confirm(
24 labels.length === 1
25 ? [
26 `Close terminal "${labels[0]}"?`,
27 "This stops the running process and clears its history.",
28 ].join("\n")
29 : [
30 `Close ${labels.length} terminals?`,
31 `This stops their running processes and clears their histories: ${labels
32 .map((label) => `"${label}"`)
33 .join(", ")}.`,
34 ].join("\n"),
35 { variant: "destructive" },
36 );
37 } catch {
38 return false;
39 } finally {
40 pendingConfirmations -= 1;
41 }