{"id":423,"date":"2010-01-13T22:48:34","date_gmt":"2010-01-13T12:48:34","guid":{"rendered":"http:\/\/software-testing.com.au\/blog\/?p=423"},"modified":"2010-09-28T11:50:29","modified_gmt":"2010-09-28T01:50:29","slug":"text-based-progress-bar-in-ruby-for-command-line-programs","status":"publish","type":"post","link":"http:\/\/www.software-testing.com.au\/blog\/2010\/01\/13\/text-based-progress-bar-in-ruby-for-command-line-programs\/","title":{"rendered":"Text-based progress bar in Ruby for command line programs"},"content":{"rendered":"<p>When I\u2019m running tests and tools, I frequently want to know how far through I am.  I\u2019d built a class to help with this a while back, but Corey Goldberg\u2019s recent post on a python version prompted me to post this Ruby version.<\/p>\n<p>First, you\u2019ll need to save this to progress_bar.rb:<\/p>\n<pre>class Progress_bar\r\n\r\n  attr_accessor :items_to_do, :items_done\r\n\r\n  def initialize(items_to_do, items_done=0)\r\n    reset(items_to_do, items_done)\r\n  end\r\n\r\n  def percent_complete\r\n    return (@items_complete*1.0\/@items_to_do*1.0)*100\r\n  end\r\n\r\n  def advance(steps_to_advance=1)\r\n    @items_complete+=steps_to_advance\r\n  end\r\n\r\n  def reset(items_to_do, items_done=0)\r\n    @items_to_do=items_to_do\r\n    @items_complete=items_done\r\n  end\r\n\r\n  def report\r\n    $stderr.print \"\\r#{progress_bar} #{@items_complete} of #{@items_to_do} done\"\r\n  end\r\n\r\n  def progress_bar\r\n    complete_bar=(percent_complete\/2.0).floor\r\n    incomplete_bar=((100-percent_complete)\/2.0).ceil\r\n    return \"[#{\"*\"*complete_bar}#{\"-\"*incomplete_bar}]\"\r\n  end\r\nend<\/pre>\n<p>You can then mix this in to so that when you iterate through collections, you can report on your progress. Handy for data-driven tests:<\/p>\n<pre>require 'progress_bar.rb'\r\n\r\nmodule Enumerable\r\n  def each_with_progress\r\n    progress=Progress_bar.new(self.size)\r\n\r\n    self.each do |item |\r\n      progress.advance\r\n      yield item\r\n      progress.report\r\n    end\r\n  end\r\nend\r\n\r\n# Examples:\r\n\r\nb={1=&gt;\"a\",2=&gt;\"b\"}\r\n\r\nputs \"Iterating through hash\"\r\nb.each_with_progress do | val |\r\n  # do stuff\r\n  sleep 2\r\nend\r\n\r\nputs\r\nputs \"Iterating through array\"\r\nc=[1,2,3,4]\r\nc.each_with_progress do | val |\r\n  # do stuff\r\n  sleep 2\r\nend\r\n\r\nputs \"Manual control...\"\r\npb=Progress_bar.new(40,20)\r\n5.times do\r\n  pb.advance\r\n  pb.report\r\n  sleep 1\r\nend\r\n\r\n5.times do\r\n  pb.advance(2)\r\n  sleep 1\r\n  pb.report\r\nend<\/pre>\n<p>If you run from the command line, your output will look something like this:<\/p>\n<p>[*******************************************&#8212;&#8212;-] 35 of 40 done<\/p>\n<p>This should all appear on a single line, although anything sent to standard output will force this to go to a new line. I never managed to get ANSI terminal display libraries working.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I\u2019m running tests and tools, I frequently want to know how far through I am. I\u2019d built a class to help with this a while back, but Corey Goldberg\u2019s recent post on a python version prompted me to post this Ruby version. First, you\u2019ll need to save this to progress_bar.rb: class Progress_bar attr_accessor :items_to_do, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-423","post","type-post","status-publish","format-standard","hentry","category-ruby"],"_links":{"self":[{"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/posts\/423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/comments?post=423"}],"version-history":[{"count":6,"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":552,"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/posts\/423\/revisions\/552"}],"wp:attachment":[{"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.software-testing.com.au\/blog\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}