Class: ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/rbsync.rb

Overview

プログレスバーを表示する.

prg= ProgressBar.new
prg.show_percent = true
prg.size = 60
prg.start("downloading\n")
100.times{|i|
  prg.progress(i, "#{i}/100")
  sleep 0.015
}
prg.end("done")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProgressBar

Returns a new instance of ProgressBar.



514
515
516
517
518
519
520
521
# File 'lib/rbsync.rb', line 514

def initialize()
  @out = $stdout
  @size = 10
  @bar_char_undone = "_"
  @bar_char_done   = "#"
  @show_percent    = true
  @printend_max_size = 0
end

Instance Attribute Details

#bar_char_doneObject

Returns the value of attribute bar_char_done.



513
514
515
# File 'lib/rbsync.rb', line 513

def bar_char_done
  @bar_char_done
end

#bar_char_undoneObject

Returns the value of attribute bar_char_undone.



513
514
515
# File 'lib/rbsync.rb', line 513

def bar_char_undone
  @bar_char_undone
end

#outObject

Returns the value of attribute out.



513
514
515
# File 'lib/rbsync.rb', line 513

def out
  @out
end

#show_percentObject

Returns the value of attribute show_percent.



513
514
515
# File 'lib/rbsync.rb', line 513

def show_percent
  @show_percent
end

#sizeObject

Returns the value of attribute size.



513
514
515
# File 'lib/rbsync.rb', line 513

def size
  @size
end

Instance Method Details

#clearObject



533
534
535
536
537
538
# File 'lib/rbsync.rb', line 533

def clear()
  out.print "\r"
  line_size = [@printend_max_size, @size].max
  out.print " " * line_size
  out.flush
end

#end(message = "") ⇒ Object



527
528
529
530
531
532
# File 'lib/rbsync.rb', line 527

def end(message="")
  progress(100)
  out.print " " + message
  out.puts ""
  out.flush
end

#progress(percent, message = nil) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/rbsync.rb', line 539

def progress(percent,message=nil)
  clear()
  str =""
  str <<  "\r"
  str <<  bar_char_done   * ((percent.to_f/100.to_f)*size).to_i
  str <<  bar_char_undone * (size - ((percent.to_f/100.to_f)*size).to_i)
  str <<  " " + percent.to_s + "%" if show_percent
  str <<  " " + message  if message
  @printend_max_size = str.size if str.size > @printend_max_size
  out.print str
  out.flush
end

#start(message = "") ⇒ Object



522
523
524
525
526
# File 'lib/rbsync.rb', line 522

def start(message="")
  out.puts message
  out.print bar_char_undone * size 
  out.flush
end