From 0450db795e3c4c3da390b84e4ae8f2571856f1e2 Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Sat, 23 Jul 2022 09:53:25 -0600 Subject: [PATCH] Revert "move lint to perennial-alias instead of chipper and fix perennial `grunt lint`, https://github.com/phetsims/chipper/issues/1286" This reverts commit e684c0dae4ba6348a68a784ee3e0fa156bb593a8. --- js/common/showCommandLineProgress.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 js/common/showCommandLineProgress.js diff --git a/js/common/showCommandLineProgress.js b/js/common/showCommandLineProgress.js new file mode 100644 index 00000000..c62aee8d --- /dev/null +++ b/js/common/showCommandLineProgress.js @@ -0,0 +1,27 @@ +// Copyright 2022, University of Colorado Boulder + +/** + * Helper function to show a progress bar on the command line. + * @author Sam Reid (PhET Interactive Simulations) + * @author Michael Kauzmann (PhET Interactive Simulations) + */ + +/* eslint-env node */ +const _ = require( 'lodash' ); // eslint-disable-line + +/** + * See https://jagascript.com/how-to-build-a-textual-progress-bar-for-cli-and-terminal-apps/ + * @param {number} progress - decimal between 0 and 1 + * @param {boolean} newline - if each new progress should give a new line, should be false during progress, and true when finally completed + * @param {Object} [options] + */ +module.exports = function showCommandLineProgress( progress, newline, options ) { + options = _.extend( { + progressBarLength: 40 // in characters + }, options ); + + const dots = '.'.repeat( Math.round( progress * options.progressBarLength ) ); + const empty = ' '.repeat( Math.round( ( 1 - progress ) * options.progressBarLength ) ); + const newlineString = newline ? '\n' : ''; + process.stdout.write( `\r[${dots}${empty}] ${( progress * 100 ).toFixed( 2 )}%${newlineString}` ); +}; \ No newline at end of file