Node.js 如何在Nodejs CLI中将ProcessBar固定到顶行?
终端输出为: 但实际上这正是我真正想要的: progressbar始终是第一行,并得到响应,然后显示在下面。Node.js 如何在Nodejs CLI中将ProcessBar固定到顶行?,node.js,command-line-interface,Node.js,Command Line Interface,终端输出为: 但实际上这正是我真正想要的: progressbar始终是第一行,并得到响应,然后显示在下面。 不管怎样,要解决这个问题吗 Nodejs: var request = require('request'); var ProgressBar = require('progress'); var year=[14,15,16]; var month=[1,2,3,4,5,6,7]; var bar = new ProgressBar('Processing [:bar] :perce
不管怎样,要解决这个问题吗 Nodejs:
var request = require('request');
var ProgressBar = require('progress');
var year=[14,15,16];
var month=[1,2,3,4,5,6,7];
var bar = new ProgressBar('Processing [:bar] :percent', {
complete: '=',
incomplete: '-',
width: 30,
total: year.length*month.length,
});
/*-------------------------------------*/
function init(year,month){
check(year,month);
}
function check(year,month){
var options = { method: 'POST',
url: 'http://dev.site/date.php',
formData:{year:year,month:month}
};
request(options, function (error, response, body) {
if (error) {
console.log(error);;
}
if (body=='A task @') {
bar.tick();
console.log('\n'+body+year+':'+month);
}else{
bar.tick();
}
})
}
/*-------------------------------------*/
for (var i = 0; i < year.length; i++) {
for (var n = 0; n < month.length; n++) {
init(year[i],month[n]);
}
}
var request=require('request');
var ProgressBar=需要(“进度”);
风险值年份=[14,15,16];
风险值月份=[1,2,3,4,5,6,7];
var bar=new ProgressBar('处理[:bar]:百分比'{
完成:“=”,
不完整:'-',
宽度:30,
总计:年长*月长,
});
/*-------------------------------------*/
函数初始化(年、月){
支票(年、月);
}
功能检查(年、月){
var options={method:'POST',
网址:'http://dev.site/date.php',
formData:{年:年,月:月}
};
请求(选项、功能(错误、响应、正文){
如果(错误){
console.log(错误);;
}
if(body='A task@'){
bar.tick();
console.log(“\n”+正文+年+”:“+月);
}否则{
bar.tick();
}
})
}
/*-------------------------------------*/
对于(变量i=0;i
使用您可以做到这一点
以下是一个独立版本:
const ProgressBar = require('progress');
const ansiEscapes = require('ansi-escapes');
const write = process.stdout.write.bind(process.stdout);
let bar = new ProgressBar('Processing [:bar] :percent', {
complete : '=',
incomplete : '-',
width : 30,
total : 100
});
// Start by clearing the screen and positioning the cursor on the second line
// (because the progress bar will be positioned on the first line)
write(ansiEscapes.clearScreen + ansiEscapes.cursorTo(0, 1));
let i = 0;
setInterval(() => {
// Save cursor position and move it to the top left corner.
write(ansiEscapes.cursorSavePosition + ansiEscapes.cursorTo(0, 0));
// Update the progress bar.
bar.tick();
// Restore the cursor position.
write(ansiEscapes.cursorRestorePosition);
// Write a message every 10 ticks.
if (++i % 10 === 0) {
console.log('Now at', i);
}
// We're done.
if (i === 100) {
process.exit(0);
}
}, 100);
太多了!确实有效!也许我们需要添加
“使用严格”代码>在顶部