如何在 Nodejs CLI 中将 ProcessBar 修复到顶行?

Chw*_*ega 4 command-line-interface node.js

终端输出是: 标准输出 但实际上这就是我真正想要的:进度条将始终是第一行,并得到响应,然后在下面显示。
无论如何要解决这个问题?

在此处输入图片说明

节点:

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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

rob*_*lep 5

使用ansi-escapes您也许可以做到这一点。

这是一个独立版本:

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);
Run Code Online (Sandbox Code Playgroud)