|
| 1 | +import * as Terminal from "@effect/platform/Terminal" |
| 2 | +import * as Ansi from "@effect/printer-ansi/Ansi" |
| 3 | +import * as Doc from "@effect/printer-ansi/AnsiDoc" |
| 4 | +import * as Optimize from "@effect/printer/Optimize" |
| 5 | +import * as Cause from "effect/Cause" |
| 6 | +import type * as Duration from "effect/Duration" |
| 7 | +import * as Effect from "effect/Effect" |
| 8 | +import * as Exit from "effect/Exit" |
| 9 | +import * as Fiber from "effect/Fiber" |
| 10 | +import * as Option from "effect/Option" |
| 11 | +import * as Ref from "effect/Ref" |
| 12 | +import * as InternalAnsiUtils from "./ansi-utils.js" |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + */ |
| 17 | +export interface SpinnerOptions<E = never, A = never> { |
| 18 | + readonly frames?: ReadonlyArray<string> |
| 19 | + readonly interval?: Duration.DurationInput |
| 20 | + readonly onSuccess?: (value: A) => string |
| 21 | + readonly onFailure?: (error: E) => string |
| 22 | +} |
| 23 | + |
| 24 | +// Full classic dots spinner sequence |
| 25 | +const DEFAULT_FRAMES: ReadonlyArray<string> = [ |
| 26 | + "⠋", |
| 27 | + "⠙", |
| 28 | + "⠹", |
| 29 | + "⠸", |
| 30 | + "⠼", |
| 31 | + "⠴", |
| 32 | + "⠦", |
| 33 | + "⠧", |
| 34 | + "⠇", |
| 35 | + "⠏" |
| 36 | +] |
| 37 | + |
| 38 | +const DEFAULT_INTERVAL: Duration.DurationInput = "80 millis" as Duration.DurationInput |
| 39 | + |
| 40 | +// Small render helpers to reduce per-frame work. |
| 41 | +const CLEAR_LINE = Doc.cat(Doc.eraseLine, Doc.cursorLeft) |
| 42 | +const CURSOR_HIDE = Doc.render(Doc.cursorHide, { style: "pretty" }) |
| 43 | +const CURSOR_SHOW = Doc.render(Doc.cursorShow, { style: "pretty" }) |
| 44 | +const renderWithWidth = (columns: number) => Doc.render({ style: "pretty", options: { lineWidth: columns } }) |
| 45 | + |
| 46 | +const optimizeAndRender = (columns: number, doc: Doc.Doc<any>, addNewline = false) => { |
| 47 | + const prepared = addNewline ? Doc.cat(doc, Doc.hardLine) : doc |
| 48 | + return prepared.pipe(Optimize.optimize(Optimize.Deep), renderWithWidth(columns)) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * A spinner that renders while `effect` runs and prints ✔/✖ on completion. |
| 53 | + * |
| 54 | + * @internal |
| 55 | + */ |
| 56 | +export const spinner = <A, E, R>( |
| 57 | + message: string, |
| 58 | + effect: Effect.Effect<A, E, R>, |
| 59 | + options?: SpinnerOptions<E, A> |
| 60 | +): Effect.Effect<A, E, R | Terminal.Terminal> => |
| 61 | + Effect.acquireUseRelease( |
| 62 | + // acquire |
| 63 | + Effect.gen(function*() { |
| 64 | + const terminal = yield* Terminal.Terminal |
| 65 | + |
| 66 | + // Hide cursor while active |
| 67 | + yield* Effect.orDie(terminal.display(CURSOR_HIDE)) |
| 68 | + |
| 69 | + const indexRef = yield* Ref.make(0) |
| 70 | + const exitRef = yield* Ref.make<Option.Option<Exit.Exit<A, E>>>(Option.none()) |
| 71 | + |
| 72 | + const frames = options?.frames ?? DEFAULT_FRAMES |
| 73 | + const frameCount = frames.length |
| 74 | + const interval = options?.interval ?? DEFAULT_INTERVAL |
| 75 | + |
| 76 | + const messageDoc = Doc.annotate(Doc.text(message), Ansi.bold) |
| 77 | + |
| 78 | + const displayDoc = (doc: Doc.Doc<any>, addNewline = false) => |
| 79 | + Effect.gen(function*() { |
| 80 | + const columns = yield* terminal.columns |
| 81 | + const out = optimizeAndRender(columns, doc, addNewline) |
| 82 | + yield* Effect.orDie(terminal.display(out)) |
| 83 | + }) |
| 84 | + |
| 85 | + const renderFrame = Effect.gen(function*() { |
| 86 | + const i = yield* Ref.modify(indexRef, (n) => [n, n + 1] as const) |
| 87 | + const spinnerDoc = Doc.annotate(Doc.text(frames[i % frameCount]!), Ansi.blue) |
| 88 | + |
| 89 | + const line = Doc.hsep([spinnerDoc, messageDoc]) |
| 90 | + yield* displayDoc(Doc.cat(CLEAR_LINE, line)) |
| 91 | + }) |
| 92 | + |
| 93 | + const computeFinalMessage = (exit: Exit.Exit<A, E>): string => |
| 94 | + Exit.match(exit, { |
| 95 | + onFailure: (cause) => { |
| 96 | + let baseMessage = message |
| 97 | + if (options?.onFailure) { |
| 98 | + const failureOption = Cause.failureOption(cause) |
| 99 | + if (Option.isSome(failureOption)) { |
| 100 | + baseMessage = options.onFailure(failureOption.value) |
| 101 | + } |
| 102 | + } |
| 103 | + if (Cause.isInterrupted(cause)) { |
| 104 | + return `${baseMessage} (interrupted)` |
| 105 | + } else if (Cause.isDie(cause)) { |
| 106 | + return `${baseMessage} (died)` |
| 107 | + } else { |
| 108 | + return baseMessage |
| 109 | + } |
| 110 | + }, |
| 111 | + onSuccess: (value) => options?.onSuccess ? options.onSuccess(value) : message |
| 112 | + }) |
| 113 | + |
| 114 | + const renderFinal = (exit: Exit.Exit<A, E>) => |
| 115 | + Effect.gen(function*() { |
| 116 | + const figures = yield* InternalAnsiUtils.figures |
| 117 | + const icon = Exit.isSuccess(exit) |
| 118 | + ? Doc.annotate(figures.tick, Ansi.green) |
| 119 | + : Doc.annotate(figures.cross, Ansi.red) |
| 120 | + |
| 121 | + const finalMessage = computeFinalMessage(exit) |
| 122 | + |
| 123 | + const msgDoc = Doc.annotate(Doc.text(finalMessage), Ansi.bold) |
| 124 | + const line = Doc.hsep([icon, msgDoc]) |
| 125 | + |
| 126 | + yield* displayDoc(Doc.cat(CLEAR_LINE, line), true) |
| 127 | + }) |
| 128 | + |
| 129 | + // Spinner fiber: loop until we see an Exit in exitRef, then render final line and stop. |
| 130 | + const loop = Effect.gen(function*() { |
| 131 | + while (true) { |
| 132 | + const maybeExit = yield* Ref.get(exitRef) |
| 133 | + if (Option.isSome(maybeExit)) { |
| 134 | + yield* renderFinal(maybeExit.value) |
| 135 | + break |
| 136 | + } |
| 137 | + yield* renderFrame |
| 138 | + yield* Effect.sleep(interval) |
| 139 | + } |
| 140 | + }).pipe( |
| 141 | + // Always restore cursor from inside the spinner fiber too |
| 142 | + Effect.ensuring(Effect.orDie(terminal.display(CURSOR_SHOW))) |
| 143 | + ) |
| 144 | + |
| 145 | + const fiber = yield* Effect.fork(loop) |
| 146 | + return { fiber, exitRef, terminal } |
| 147 | + }), |
| 148 | + // use |
| 149 | + (_) => effect, |
| 150 | + // release |
| 151 | + ({ exitRef, fiber, terminal }, exit) => |
| 152 | + Effect.gen(function*() { |
| 153 | + // Signal the spinner fiber to finish by setting the exitRef. |
| 154 | + // (No external interrupt of the spinner fiber.) |
| 155 | + yield* Ref.set(exitRef, Option.some(exit)) |
| 156 | + |
| 157 | + // Wait a short, bounded time for the spinner to flush final output. |
| 158 | + // If this ever times out in a pathological TTY, we fail-safe and continue. |
| 159 | + yield* Fiber.await(fiber).pipe(Effect.timeout("2 seconds"), Effect.ignore) |
| 160 | + }).pipe( |
| 161 | + // Ensure cursor is shown even if something above failed. |
| 162 | + Effect.ensuring(Effect.orDie(terminal.display(CURSOR_SHOW))) |
| 163 | + ) |
| 164 | + ) |
0 commit comments