3 # Class that shows a progress bar, currently only supports a twirling
6 # You can specify a size for the progress bar if you want if you dont
7 # it will use the helper functions to figure out terminal dimensions
8 # and draw an appropriately sized bar
11 # 100.times {|i| print p.twirl(i+1, 100) + "\r"};puts
13 # * [ ==================================================> ] 100 / 100
15 def initialize(size=nil)
16 @twirl = ['|', '/', '-', "\\", '|', '/', '-', "\\"]
22 cols = Util.terminal_dimensions[0] - 22
24 # Defaults back to old behavior if it
25 # couldn't figure out the size or if
26 # its more than 60 wide
37 def twirl(current, total)
38 # if the size is negative there is just not enough
39 # space on the terminal, return a simpler version
40 return "\r#{current} / #{total}" if @size == 0
43 txt = "\r %s [ " % Util.colorize(:green, "*")
45 txt = "\r %s [ " % Util.colorize(:red, @twirl[@twirldex])
48 dashes = ((current.to_f / total) * @size).round
50 dashes.times { txt << "=" }
53 (@size - dashes).times { txt << " " }
55 txt << " ] #{current} / #{total}"
57 @twirldex == 7 ? @twirldex = 0 : @twirldex += 1