Skip to content

Instantly share code, notes, and snippets.

@tksugimoto
Last active August 12, 2017 13:15
Show Gist options
  • Save tksugimoto/5dd57b107403a265276419d2fb5f89a4 to your computer and use it in GitHub Desktop.
Save tksugimoto/5dd57b107403a265276419d2fb5f89a4 to your computer and use it in GitHub Desktop.
プログレス表示
const readline = require('readline');
console.log('start');
Array.from(Array(100), (_, i) => i + 1).forEach(i => {
setTimeout(() => {
/*
// ※ メッセージの長さが減少する場合は使えない
process.stdout.write(`working... ${i}%\r`);
/*/
// child_process でも正常に動作させるには readlineを使う
readline.clearLine(process.stdout);
readline.cursorTo(process.stdout, 0);
process.stdout.write(`working... ${i}%`);
//*/
}, i * 30);
});
const { spawn } = require('child_process');
const useCmd = false;
const progress =
useCmd
? spawn("cmd.exe", [
"/c",
"progress-bat.bat",
])
: spawn('node', [
'progress.js',
]);
//*
progress.stdout.pipe(process.stdout);
progress.stderr.pipe(process.stderr);
/*/
progress.stdout.setEncoding('utf8');
progress.stdout.on('data', (data) => {
process.stdout.write(data);
});
progress.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
//*/
progress.on('close', (code) => {
console.log(`\nchild process exited with code ${code}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment