Skip to content

Instantly share code, notes, and snippets.

@bmarini
Created September 14, 2011 21:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmarini/1217911 to your computer and use it in GitHub Desktop.
Save bmarini/1217911 to your computer and use it in GitHub Desktop.
CLI progress bar in ruby
class ProgressBar
def initialize(units=60)
@units = units.to_f
end
def print(completed, total)
norm = 1.0 / (total / @units)
progress = (completed * norm).ceil
pending = @units - progress
Kernel.print "[#{'=' * progress }#{' ' * ( pending )}] #{percentage(completed, total)}%\r"
end
def percentage(completed, total)
( ( completed / total.to_f ) * 100 ).round
end
end
# Usage:
#
# bar = ProgressBar.new
# bar.print(50, 100)
#
# => [============================== ] 50%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment