« get me outta code hell

only update progress status every N ms - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-06-28 23:25:11 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-06-28 23:25:11 -0300
commit9db3ec35804bbe566d1d20401fecedc657434256 (patch)
treea84347a25dd0d2172aee1e06c379606e42733272 /src/util
parent1484f926086582fe28f4cbc6b05f8b8279404e84 (diff)
only update progress status every N ms
Diffstat (limited to 'src/util')
-rw-r--r--src/util/cli.js10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/util/cli.js b/src/util/cli.js
index cbcbf7cb..99c0638c 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -286,23 +286,29 @@ export function progressCallAll(msgOrMsgFn, array) {
   const msgFn =
     typeof msgOrMsgFn === 'function' ? msgOrMsgFn : () => msgOrMsgFn;
 
+  const updateInterval = 1000 / 60;
+
   let done = 0,
     total = array.length;
   process.stdout.write(`\r${msgFn()} [0/${total}]`);
   const start = Date.now();
   const vals = [];
+  let lastTime = 0;
 
   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 pc = '100%'.padEnd('99.9%'.length, ' ');
       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 {
+    } else if (Date.now() - lastTime >= updateInterval) {
+      const pc = (Math.round((done / total) * 1000) / 10 + '%').padEnd('99.9%'.length, ' ');
       process.stdout.write(`\r${msgFn()} [${pc}] `);
+      lastTime = Date.now();
     }
     vals.push(val);
   }