Created
March 26, 2017 07:40
-
-
Save keenwon/1a9325dc5536132b1f29436b4aa208e5 to your computer and use it in GitHub Desktop.
terminal进度条
This file contains 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
const readline = require('readline'); | |
// 百分比 | |
let percentage = 0; | |
// 时长 | |
let time = 5 * 1000; | |
// 频率 | |
let frequency = 50; | |
// 步长 | |
let step = 1 / (time / frequency); | |
// 总长度(50字节) | |
let length = 50; | |
// 字符 | |
let char = '▪'; | |
const resetScreen = () => { | |
readline.cursorTo(process.stdout, 0, 0); | |
readline.clearScreenDown(process.stdout); | |
readline.clearLine(process.stdout); | |
}; | |
const print = size => { | |
process.stdout.write(`[${char.repeat(size) + ' '.repeat(length - size)}]\n`); | |
}; | |
const t = () => { | |
if (percentage >= 1) { | |
process.stdout.write('doen!'); | |
return; | |
} | |
percentage += step; | |
let size = Math.floor(length * percentage); | |
resetScreen(); | |
print(size); | |
setTimeout(t, frequency); | |
}; | |
t(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment