Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8945

console出力の同じ行を更新する

$
0
0

tl;dr

readlineライブラリのcusorTo()を使うと、consoleの同じ行を更新することができます。

説明

パッケージインストール時などにconsoleの同じ行が更新されて経過が表示されることがありますが、Node.jsで実現できます。

普通のconsole.logの場合

lettime=0;setInterval(()=>{console.log(`time: ${++time}`);},1000);

実行結果

time: 1
time: 2
time: 3
time: 4
...( 以下、略 )...

readlineを使った場合

lettime=0setInterval(()=>{readline.cursorTo(process.stdout,0);process.stdout.write(`time: ${++time}`);},1000)

実行結果(5秒後)

time: 5

以上。


Viewing all articles
Browse latest Browse all 8945