Skip to content

Commit

Permalink
Add experimental startSpinner() func
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 27, 2022
1 parent 53a215e commit 72c8cf0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions experimental.d.ts
Expand Up @@ -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<ProcessOutput>
}

export const echo: Echo
export const retry: (count: number) => Retry

type StopSpinner = () => void
export function startSpinner(title: string): StopSpinner
6 changes: 6 additions & 0 deletions experimental.mjs
Expand Up @@ -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))
}

0 comments on commit 72c8cf0

Please sign in to comment.