Skip to content

Commit

Permalink
Multiple progress bars (#237)
Browse files Browse the repository at this point in the history
* Mutliple progress bars

* Make function more generic
  • Loading branch information
krizzu authored and satya164 committed Oct 10, 2017
1 parent 22a8224 commit 856555f
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 147 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"loader-utils": "^1.1.0",
"minimist": "^1.2.0",
"morgan": "^1.8.1",
"multi-progress": "^2.0.0",
"open-in-editor": "^2.2.0",
"opn": "^4.0.2",
"ora": "^1.2.0",
"progress-bar-webpack-plugin": "^1.9.3",
"react-deep-force-update": "^2.0.1",
"react-hot-loader": "3.0.0-beta.7",
"resolve": "^1.3.3",
Expand Down
76 changes: 76 additions & 0 deletions src/utils/haulProgressBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright 2017-present, Callstack.
* All rights reserved.
*
* haulProgressBar.js
*
* @flow
*/

const multiProgress = require('multi-progress');
const chalk = require('chalk');

const progressBarFactory = new multiProgress(process.stderr);

const progressBar = {};
const lastPercent = {};
const barOptions = {
complete: chalk.bold('='),
incomplete: ' ',
width: 20,
total: 100,
};
const labelOptions = {
android: {
color: 'green',
label: 'Android',
},
ios: {
color: 'cyan',
label: 'iOS',
},
};

/*
* Create label based on passed platform
* We use `labelOptions` to customize know platforms
* If platform is not know, returns default styles
*/
function createLabel(platform: string) {
if (labelOptions[platform]) {
const { color, label } = labelOptions[platform];
return `${chalk.bold[color](label)}`.padEnd(30);
}
return `${chalk.bold.magenta(platform)}`.padEnd(30);
}

/*
* Create progress bar itself
*/
function createBarFormat(platform: string) {
const label = createLabel(platform);

const leftBar = chalk.bold('[');
const rightBar = chalk.bold(']');
const percent = chalk.bold.blue(':percent');

return `${label}${leftBar}:bar${rightBar} ${percent}`;
}

module.exports = function createProgressBar(newPlatform: string) {
if (!progressBar[newPlatform]) {
progressBar[newPlatform] = progressBarFactory.newBar(
createBarFormat(newPlatform),
barOptions,
);
lastPercent[newPlatform] = 0;
}

return function haulProgressBar(platform: string, percent: number) {
const newPercent = Math.ceil(percent * barOptions.width);
if (newPercent !== lastPercent[platform]) {
progressBar[platform].update(percent);
lastPercent[platform] = newPercent;
}
};
};

0 comments on commit 856555f

Please sign in to comment.