-
-
Notifications
You must be signed in to change notification settings - Fork 406
CLI: add Prompt.spinner #5404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kitlangton
wants to merge
1
commit into
main
Choose a base branch
from
feat/spinner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−0
Open
CLI: add Prompt.spinner #5404
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as Prompt from "@effect/cli/Prompt" | ||
import { NodeRuntime, NodeTerminal } from "@effect/platform-node" | ||
import { Console, Effect } from "effect" | ||
|
||
// Demonstration of success, failure, and custom final messages | ||
const program = Effect.gen(function*() { | ||
// Success case with custom success message | ||
const user = yield* Prompt.spinner( | ||
Effect.sleep("1200 millis").pipe(Effect.as({ id: 42, name: "Ada" })), | ||
{ | ||
message: "Fetching user…", | ||
onSuccess: (user: { id: number; name: string }) => `Loaded ${user.name} (ID: ${user.id})` | ||
} | ||
) | ||
yield* Console.log(`User: ${JSON.stringify(user)}`) | ||
|
||
// Failure case with custom error message and proper error handling | ||
yield* Prompt.spinner( | ||
Effect.sleep("800 millis").pipe(Effect.zipRight(Effect.fail(new Error("Network timeout")))), | ||
{ | ||
message: "Processing data…", | ||
onFailure: (error: Error) => `Processing failed: ${error.message}` | ||
} | ||
).pipe( | ||
Effect.catchAll((error) => Console.log(`Caught error: ${error.message}`)) | ||
) | ||
|
||
// Success case with both success and error mappers | ||
yield* Prompt.spinner( | ||
Effect.sleep("600 millis").pipe(Effect.as({ uploaded: 5, skipped: 2 })), | ||
{ | ||
message: "Uploading files…", | ||
onSuccess: (result: { uploaded: number; skipped: number }) => `Uploaded ${result.uploaded} files (${result.skipped} skipped)`, | ||
onFailure: (error: unknown) => `Upload failed: ${error}` | ||
} | ||
) | ||
|
||
// Simple case without custom messages (uses original message) | ||
yield* Prompt.spinner( | ||
Effect.sleep("300 millis").pipe(Effect.as("done")), | ||
{ | ||
message: "Cleaning up…" | ||
} | ||
) | ||
|
||
// Timeout case - demonstrates spinner handles timeout/interruption gracefully | ||
yield* Prompt.spinner( | ||
Effect.sleep("2 seconds").pipe(Effect.as("completed")), | ||
{ | ||
message: "Long running task…", | ||
onSuccess: () => "Task completed successfully", | ||
onFailure: () => "Task timed out" | ||
} | ||
).pipe( | ||
Effect.timeout("800 millis"), | ||
Effect.catchAll((error) => Console.log(`Caught timeout: ${error._tag}`)) | ||
) | ||
|
||
// Die case - demonstrates spinner handles defects gracefully | ||
yield* Prompt.spinner( | ||
Effect.sleep("400 millis").pipe(Effect.zipRight(Effect.die("Unexpected system error"))), | ||
{ | ||
message: "Risky operation…", | ||
onFailure: (error: unknown) => `Operation failed: ${error}` | ||
} | ||
).pipe( | ||
Effect.catchAllCause((cause) => Console.log(`Caught defect: ${cause}`)) | ||
) | ||
|
||
yield* Console.log("All done!") | ||
}) | ||
|
||
const MainLive = NodeTerminal.layer | ||
|
||
program.pipe(Effect.provide(MainLive), NodeRuntime.runMain) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import * as Terminal from "@effect/platform/Terminal" | ||
import * as Ansi from "@effect/printer-ansi/Ansi" | ||
import * as Doc from "@effect/printer-ansi/AnsiDoc" | ||
import * as Optimize from "@effect/printer/Optimize" | ||
import * as Cause from "effect/Cause" | ||
import type * as Duration from "effect/Duration" | ||
import * as Effect from "effect/Effect" | ||
import * as Exit from "effect/Exit" | ||
import * as Fiber from "effect/Fiber" | ||
import { dual } from "effect/Function" | ||
import * as Option from "effect/Option" | ||
import * as InternalAnsiUtils from "./ansi-utils.js" | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface SpinnerOptions<A, E> { | ||
readonly message: string | ||
readonly frames?: ReadonlyArray<string> | ||
readonly interval?: Duration.DurationInput | ||
readonly onSuccess?: (value: A) => string | ||
readonly onFailure?: (error: E) => string | ||
} | ||
|
||
// Full classic dots spinner sequence | ||
const DEFAULT_FRAMES: ReadonlyArray<string> = [ | ||
"⠋", | ||
"⠙", | ||
"⠹", | ||
"⠸", | ||
"⠼", | ||
"⠴", | ||
"⠦", | ||
"⠧", | ||
"⠇", | ||
"⠏" | ||
] | ||
|
||
const DEFAULT_INTERVAL: Duration.DurationInput = "80 millis" as Duration.DurationInput | ||
|
||
// Small render helpers to reduce per-frame work. | ||
const CLEAR_LINE = Doc.cat(Doc.eraseLine, Doc.cursorLeft) | ||
const CURSOR_HIDE = Doc.render(Doc.cursorHide, { style: "pretty" }) | ||
const CURSOR_SHOW = Doc.render(Doc.cursorShow, { style: "pretty" }) | ||
const renderWithWidth = (columns: number) => Doc.render({ style: "pretty", options: { lineWidth: columns } }) | ||
|
||
const optimizeAndRender = (columns: number, doc: Doc.Doc<any>, addNewline = false) => { | ||
const prepared = addNewline ? Doc.cat(doc, Doc.hardLine) : doc | ||
return prepared.pipe(Optimize.optimize(Optimize.Deep), renderWithWidth(columns)) | ||
} | ||
|
||
/** | ||
* A spinner that renders while `effect` runs and prints ✔/✖ on completion. | ||
* | ||
* @internal | ||
*/ | ||
export const spinner: { | ||
<A, E, R>( | ||
options: SpinnerOptions<A, E> | ||
): (effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | Terminal.Terminal> | ||
<A, E, R>( | ||
effect: Effect.Effect<A, E, R>, | ||
options: SpinnerOptions<A, E> | ||
): Effect.Effect<A, E, R | Terminal.Terminal> | ||
} = dual( | ||
2, | ||
<A, E, R>( | ||
effect: Effect.Effect<A, E, R>, | ||
options: SpinnerOptions<A, E> | ||
): Effect.Effect<A, E, R | Terminal.Terminal> => | ||
Effect.acquireUseRelease( | ||
// acquire | ||
Effect.gen(function*() { | ||
const terminal = yield* Terminal.Terminal | ||
|
||
// Hide cursor while active | ||
yield* Effect.orDie(terminal.display(CURSOR_HIDE)) | ||
|
||
let index = 0 | ||
let exit: Exit.Exit<A, E> | undefined = undefined | ||
|
||
const message = options.message | ||
const frames = options.frames ?? DEFAULT_FRAMES | ||
const frameCount = frames.length | ||
const interval = options.interval ?? DEFAULT_INTERVAL | ||
|
||
const messageDoc = Doc.annotate(Doc.text(message), Ansi.bold) | ||
|
||
const displayDoc = (doc: Doc.Doc<any>, addNewline = false) => | ||
Effect.gen(function*() { | ||
const columns = yield* terminal.columns | ||
const out = optimizeAndRender(columns, doc, addNewline) | ||
yield* Effect.orDie(terminal.display(out)) | ||
}) | ||
|
||
const renderFrame = Effect.gen(function*() { | ||
const i = index | ||
index = index + 1 | ||
const spinnerDoc = Doc.annotate(Doc.text(frames[i % frameCount]!), Ansi.blue) | ||
|
||
const line = Doc.hsep([spinnerDoc, messageDoc]) | ||
yield* displayDoc(Doc.cat(CLEAR_LINE, line)) | ||
}) | ||
|
||
const computeFinalMessage = (exit: Exit.Exit<A, E>): string => | ||
Exit.match(exit, { | ||
onFailure: (cause) => { | ||
let baseMessage = message | ||
if (options.onFailure) { | ||
const failureOption = Cause.failureOption(cause) | ||
if (Option.isSome(failureOption)) { | ||
baseMessage = options.onFailure(failureOption.value) | ||
} | ||
} | ||
if (Cause.isInterrupted(cause)) { | ||
return `${baseMessage} (interrupted)` | ||
} else if (Cause.isDie(cause)) { | ||
return `${baseMessage} (died)` | ||
} else { | ||
return baseMessage | ||
} | ||
}, | ||
onSuccess: (value) => options.onSuccess ? options.onSuccess(value) : message | ||
}) | ||
|
||
const renderFinal = (exit: Exit.Exit<A, E>) => | ||
Effect.gen(function*() { | ||
const figures = yield* InternalAnsiUtils.figures | ||
const icon = Exit.isSuccess(exit) | ||
? Doc.annotate(figures.tick, Ansi.green) | ||
: Doc.annotate(figures.cross, Ansi.red) | ||
|
||
const finalMessage = computeFinalMessage(exit) | ||
|
||
const msgDoc = Doc.annotate(Doc.text(finalMessage), Ansi.bold) | ||
const line = Doc.hsep([icon, msgDoc]) | ||
|
||
yield* displayDoc(Doc.cat(CLEAR_LINE, line), true) | ||
}) | ||
|
||
// Spinner fiber: loop until we see an Exit in exit, then render final line and stop. | ||
const loop = Effect.gen(function*() { | ||
while (true) { | ||
if (exit !== undefined) { | ||
yield* renderFinal(exit) | ||
break | ||
} | ||
yield* renderFrame | ||
yield* Effect.sleep(interval) | ||
} | ||
}).pipe( | ||
// Always restore cursor from inside the spinner fiber too | ||
Effect.ensuring(Effect.orDie(terminal.display(CURSOR_SHOW))) | ||
) | ||
|
||
const fiber = yield* Effect.fork(loop) | ||
return { | ||
fiber, | ||
terminal, | ||
setExit: (e: Exit.Exit<A, E>) => { | ||
exit = e | ||
} | ||
} | ||
}), | ||
// use | ||
(_) => effect, | ||
// release | ||
({ fiber, setExit, terminal }, exitValue) => | ||
Effect.gen(function*() { | ||
// Signal the spinner fiber to finish by setting the exit. | ||
// (No external interrupt of the spinner fiber.) | ||
setExit(exitValue) | ||
|
||
// Wait a short, bounded time for the spinner to flush final output. | ||
// If this ever times out in a pathological TTY, we fail-safe and continue. | ||
yield* Fiber.await(fiber).pipe(Effect.timeout("2 seconds"), Effect.ignore) | ||
}).pipe( | ||
// Ensure cursor is shown even if something above failed. | ||
Effect.ensuring(Effect.orDie(terminal.display(CURSOR_SHOW))) | ||
) | ||
) | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there are a bunch of compilation errors in the example here.