Skip to content

Commit 262d3f3

Browse files
committed
feat: add progress bar with example large input
1 parent 0cfaa56 commit 262d3f3

File tree

5 files changed

+8454
-22
lines changed

5 files changed

+8454
-22
lines changed

package-lock.json

Lines changed: 47 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
"author": "",
2222
"license": "ISC",
2323
"devDependencies": {
24-
"cac": "^6.7.14",
25-
"chalk": "^4.1.2",
24+
"@types/cli-progress": "^3.11.0",
2625
"@types/node": "^20.2.1",
2726
"@typescript-eslint/eslint-plugin": "^5.57.0",
2827
"@typescript-eslint/parser": "^5.57.0",
2928
"@vitest/coverage-c8": "^0.32.0",
30-
"eslint": "^8.37.0",
29+
"cac": "^6.7.14",
30+
"chalk": "^4.1.2",
3131
"esbuild": "^0.18.1",
32+
"eslint": "^8.37.0",
3233
"eslint-config-prettier": "^8.8.0",
3334
"eslint-import-resolver-alias": "^1.1.2",
3435
"eslint-plugin-import": "^2.27.5",
@@ -45,5 +46,8 @@
4546
},
4647
"exports": {
4748
".": "./dist/src/index.js"
49+
},
50+
"dependencies": {
51+
"cli-progress": "^3.12.0"
4852
}
4953
}

src/helpers/sort.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { SingleBar } from "cli-progress";
2+
13
export const Compare = {
24
LESS_THAN: -1,
35
BIGGER_THAN: 1,
@@ -129,12 +131,20 @@ export const compareStrings = (a: string, b: string) => {
129131
};
130132

131133
// Using Bubble Sort
132-
export const sortProducts = (products: ProductTuple[], method: SortMethod = "ascending") => {
134+
export const sortProducts = (products: ProductTuple[], method: SortMethod = "ascending", progress?: SingleBar) => {
133135
const result = filterProductsById(products);
136+
// const result = [...products]; // uncomment to test unfiltered for longer duration sort
134137

135-
for (let p = 0; p < products.length; p++) {
138+
if (progress) {
139+
progress.start(result.length, 0);
140+
}
141+
142+
for (let p = 0; p < result.length; p++) {
136143
// Outer pass
137144
// iterate over products
145+
if (progress) {
146+
progress.increment();
147+
}
138148

139149
for (let i = 0; i < result.length; i++) {
140150
// Inner pass
@@ -178,5 +188,9 @@ export const sortProducts = (products: ProductTuple[], method: SortMethod = "asc
178188
result.reverse();
179189
}
180190

191+
if (progress) {
192+
progress.stop();
193+
}
194+
181195
return result;
182196
};

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cac from "cac";
44
import { version } from "../package.json";
55
import { checkColumns, readCsvSync, createCsvFile, logger, parseCsv } from "./helpers";
66
import { sortProducts } from "./helpers/sort";
7+
import cliprogress from "cli-progress";
78

89
const cli = cac();
910

@@ -39,8 +40,10 @@ async function run(args: string[], options: CliOptions = {}) {
3940
checkColumns(columns);
4041
logger.info(`processing ${rows.length} rows`);
4142

43+
const progress = new cliprogress.SingleBar({}, cliprogress.Presets.shades_classic);
44+
4245
const startedSort = Date.now();
43-
const sortedRows = sortProducts(rows);
46+
const sortedRows = sortProducts(rows, "ascending", progress);
4447
const finishedSort = Date.now();
4548

4649
const sortedProducts = [columns, ...sortedRows];

0 commit comments

Comments
 (0)