-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-progress-bar.mts
50 lines (45 loc) · 1.32 KB
/
custom-progress-bar.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import example from '../lib/definition.cjs';
import {tqdm, TqdmOptions, Tqdm, TqdmInput} from 'node-console-progress-bar-tqdm';
import * as timers from 'node:timers/promises';
type Item = {
value: number;
valueStr: string;
};
const total = 100;
function* gen(): Generator<Item, undefined, undefined> {
for (let i = 0; i < total; ++i) {
yield {
value: i,
valueStr: i.toString(),
};
}
}
async function iterate(tq: Tqdm<Generator<Item>>): Promise<Item[]> {
const res: Item[] = [];
for (const x of tq) {
res.push(x);
await timers.setTimeout(16);
}
return res;
}
export default example({
title: 'Custom progress style',
description: 'Fully customized progress bar written on TypeScript',
tags: ['TS', 'Generator', 'units', 'color', 'styling', 'max width'],
file: import.meta.url,
async run() {
const opts: TqdmOptions = {
total,
description: 'Custom styling',
maxColWidth: 100,
progressSymbol: '=',
progressBraces: ['[', ']'],
progressColor: '#f1f0c2',
unit: 'things',
};
const inp: TqdmInput = gen();
const tq = tqdm(inp, opts);
const res = await iterate(tq);
console.log('Result length:', res.length);
},
});