Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created January 28, 2012 09:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/1693656 to your computer and use it in GitHub Desktop.
Save apeiros/1693656 to your computer and use it in GitHub Desktop.
A progressbar in the command line
# Usage
# bar = ProgressBar.new
# bar.init
# 0.step(100, 0.5) { |i|
# bar.update(i)
# sleep(rand*0.1)
# }
# bar.finish
#
# you can update the progress and render independently if you wish. Use
# ProgessBar#progress= for that and don't pass a value to #update.
class ProgressBar
TopLeft = "\342\224\214"
Top = "\342\224\200"
TopRight = "\342\224\220"
Left = "\342\224\202"
Right = "\342\224\202"
BottomLeft = "\342\224\224"
Bottom = "\342\224\200"
BottomRight = "\342\224\230"
LightCheck = "\e[32m\342\234\223\e[0m"
Check = "\e[32m\342\234\224\e[0m"
attr_accessor :progress
def initialize(pad=["","",""], io=$stdout)
@progress = 0
@pad = pad
@io = io
end
def init
puts "","",""
end
def update(progress=nil)
@progress = progress if progress
prog = @progress
filled = (0.5*prog).round
free = 50-filled
@io.print "\e[3A",
@pad[0], TopLeft, Top*50, TopRight, "\n",
@pad[1], Left, "\e[44m", " "*filled, "\e[46m", " "*free, "\e[0m", Right, " ", prog.round, "%\n",
@pad[2], BottomLeft, Bottom*50, BottomRight, "\n"
@io.flush
end
def finish
@io.print "\e[3A",
@pad[0], TopLeft, Top*50, TopRight, "\n",
@pad[1], Left, "\e[44m", " "*50, "\e[0m", Right, " ", Check, " \n",
@pad[2], BottomLeft, Bottom*50, BottomRight, "\n"
@io.flush
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment