diff --git a/README.md b/README.md index 0c5fc994a3..7d75ee4529 100644 --- a/README.md +++ b/README.md @@ -383,6 +383,18 @@ echo`Current branch is ${branch}.` echo('Current branch is', branch) ``` +### `startSpinner()` + +Starts a simple CLI spinner, and returns `stop()` function. + +```js +import {startSpinner} from 'zx/experimental' + +let stop = startSpinner() +await $`long-running command` +stop() +``` + ### FAQ #### Passing env variables diff --git a/experimental.d.ts b/experimental.d.ts index e5a3d6bb12..1f9ef4cbd1 100644 --- a/experimental.d.ts +++ b/experimental.d.ts @@ -18,10 +18,12 @@ interface Echo { (pieces: TemplateStringsArray, ...args: any[]): void (...args: any[]): void } +export const echo: Echo interface Retry { (pieces: TemplateStringsArray, ...args: any[]): Promise } - -export const echo: Echo export const retry: (count: number) => Retry + +type StopSpinner = () => void +export function startSpinner(title: string): StopSpinner diff --git a/experimental.mjs b/experimental.mjs index 5f790020b1..03f3b29604 100644 --- a/experimental.mjs +++ b/experimental.mjs @@ -51,3 +51,9 @@ function stringify(arg) { } return `${arg}` } + +// Starts a simple CLI spinner, and returns stop() func. +export function startSpinner(title = '') { + let i = 0, spin = () => process.stdout.write(` ${'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[i++ % 10]} ${title}\r`) + return (id => () => clearInterval(id))(setInterval(spin, 100)) +}