@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 44 lines 1.6 KB
Raw
1 import * as Data from "effect/Data";
2 import * as Effect from "effect/Effect";
3 import * as Runtime from "effect/Runtime";
4 import * as Interaction from "../../Interaction.ts";
5 import { exitDeclined } from "./errors.ts";
6
7 /**
8 * A declined confirmation prompt. By the time this propagates the decline
9 * message has already been printed and the exit code set, so the
10 * `[Runtime.errorReported] = false` marker suppresses the runtime's default
11 * failure log — the process just exits 1, matching the CLI's documented
12 * "1 = failure or decline" contract.
13 */
14 export class ConfirmationDeclined extends Data.TaggedError("ConfirmationDeclined") {
15 readonly [Runtime.errorReported] = false;
16 }
17
18 /**
19 * Gate a destructive operation behind a confirmation prompt.
20 *
21 * Returns void when `yes` was passed or the user approves. Otherwise prints
22 * `abortMessage` (default "Aborted."), sets the exit code to 1 via
23 * {@link exitDeclined}, and fails with {@link ConfirmationDeclined} so the
24 * command short-circuits before the destructive work.
25 */
26 export const confirmOrDecline = Effect.fn(function* (options: {
27 readonly yes: boolean;
28 readonly message: string;
29 readonly confirmLabel?: string;
30 readonly cancelLabel?: string;
31 readonly abortMessage?: string;
32 }) {
33 if (options.yes) return;
34 const approved = yield* Interaction.confirm({
35 message: options.message,
36 initialValue: false,
37 confirmLabel: options.confirmLabel,
38 cancelLabel: options.cancelLabel,
39 });
40 if (approved) return;
41 yield* Interaction.info(options.abortMessage ?? "Aborted.");
42 yield* exitDeclined;
43 return yield* new ConfirmationDeclined();
44 });