« get me outta code hell

show status while computing writes - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/cli.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-06-28 23:09:24 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-06-28 23:09:50 -0300
commitdcc3a7d96dbe93efb5da4c921e22c29019b456dd (patch)
tree9f194afe8145cc88753af697845362d18dfc7596 /src/util/cli.js
parent79ca74f92e5ed361f86b392a6ec0dc56f0aec0f3 (diff)
show status while computing writes
Diffstat (limited to 'src/util/cli.js')
-rw-r--r--src/util/cli.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util/cli.js b/src/util/cli.js
index d28ef40..cbcbf7c 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -277,3 +277,35 @@ export function progressPromiseAll(msgOrMsgFn, array) {
     )
   );
 }
+
+export function progressCallAll(msgOrMsgFn, array) {
+  if (!array.length) {
+    return [];
+  }
+
+  const msgFn =
+    typeof msgOrMsgFn === 'function' ? msgOrMsgFn : () => msgOrMsgFn;
+
+  let done = 0,
+    total = array.length;
+  process.stdout.write(`\r${msgFn()} [0/${total}]`);
+  const start = Date.now();
+  const vals = [];
+
+  for (const fn of array) {
+    const val = fn();
+    done++;
+    const pc = (Math.round((done / total) * 1000) / 10 + '%').padEnd('99.9%'.length, ' ');
+    if (done === total) {
+      const time = Date.now() - start;
+      process.stdout.write(
+        `\r\x1b[2m${msgFn()} [${pc}] \x1b[0;32mDone! \x1b[0;2m(${time} ms) \x1b[0m\n`
+      );
+    } else {
+      process.stdout.write(`\r${msgFn()} [${pc}] `);
+    }
+    vals.push(val);
+  }
+
+  return vals;
+}