Last active
August 29, 2015 14:26
-
-
Save lahmatiy/cd8376b836f9bc1d265a to your computer and use it in GitHub Desktop.
Done on the knee progressbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chalk = require('chalk'); | |
var readline = require('readline'); | |
var BAR_LENGTH = 40; | |
var lines = 0; | |
function repeatStr(str, len){ | |
return new Array(parseInt(len) + 1).join(str); | |
} | |
function drawBarLine(fill, str){ | |
return ( | |
chalk.bgBlue(repeatStr(' ', fill)) + | |
chalk.bgWhite(repeatStr(' ', BAR_LENGTH - fill)) + | |
' ' + (str || '') | |
); | |
} | |
function createProgress(title){ | |
var line = lines; | |
lines += 2; | |
process.stdout.write(title + '\n' + drawBarLine(0) + '\n'); | |
return { | |
update: function(percent, comment){ | |
var offset = lines - line - 1; | |
readline.moveCursor(process.stdout, 0, -offset); | |
readline.clearLine(process.stdout, 0); | |
process.stdout.write(drawBarLine(Math.round((percent / 100) * BAR_LENGTH), comment) + '\r'); | |
readline.moveCursor(process.stdout, 0, offset); | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['task1', 'task2', 'task3'].forEach(function(name){ | |
var bar = createProgress(name); | |
var percent = 0; | |
function tick(){ | |
percent++; | |
bar.update(percent, 'some comment: ' + percent + '%'); | |
if (percent < 100) | |
setTimeout(tick, 100 * Math.random()); | |
} | |
setTimeout(tick, 100); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment