84
80

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CLIでプログレスバーみたいのを出力する

Last updated at Posted at 2014-05-07

コマンドラインのツールつくるときとかに、ダウンロード中の進捗をパーセントで表示したかったんだけど、表示をクリアして再描画する方法がわからなかったので調べた。

\rで現在の行をクリアできるらしい。追記:行のクリアじゃなかった。コメント参照

rubyなら

100.times do |num|
  print "working... #{num}%\r"
  sleep 0.05
  STDOUT.flush
end

Node.jsだと

var i = 0;
setInterval(function() {
  process.stdout.write('working... ' + i + '%\r');
  i++;
  if (i === 100) process.exit();
}, 50);

こんな感じになる。

fc9afe8ffc3baef7f2898a7f3d03f38b.gif

84
80
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
84
80

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?