Last active
September 21, 2020 06:41
-
-
Save kenanchristian/59cf4713e78cbbd695610e556d8805a3 to your computer and use it in GitHub Desktop.
[[Typescript CLI] Add UX Improvements] Use Chalk & CLI Progress to improve experience #writing #cli #oclif
This file contains 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
import {SingleBar, Presets} from 'cli-progress' | |
import {yellow, green, cyan} from 'chalk' | |
import {sleep} from '../lib/util' | |
interface PizzaData { | |
crust: string; | |
toppings: string[]; | |
extraSauce: boolean; | |
count: number; | |
} | |
... | |
async makePizza(pizzaData: PizzaData) { | |
const {crust, toppings, extraSauce, count} = pizzaData | |
this.log(yellow('Order taken! Making your 🍕!')) | |
const progressBar = new SingleBar({ | |
format: `Pizza Progress | ${cyan('{bar}')} | {percentage}% | ETA: {eta}s`, | |
}, Presets.shades_classic) | |
progressBar.start((count * toppings.length), 0) | |
// Create pizza to count | |
const pizzaToMake = new Array(count).fill({crust, toppings, extraSauce}) | |
for (const pizza of pizzaToMake) { | |
await sleep(1000) | |
for (const _ of pizza.toppings) { | |
progressBar.increment() | |
await sleep(1000) | |
} | |
if (pizza.extraSauce) { | |
await sleep(1000) | |
} | |
} | |
progressBar.stop() | |
} | |
async run() { | |
... | |
await this.makePizza(pizzaData) | |
this.log(green('Your 🍕 is ready!')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment