public
Description: my random ruby scripts
Homepage:
Clone URL: git://github.com/kastner/ruby-junk.git
Click here to lend your support to: ruby-junk and make a donation at www.pledgie.com !
ruby-junk / progress.rb
100755 49 lines (36 sloc) 0.71 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
 
module Progress
  extend self
 
  attr_accessor :file, :total, :current
  
  WIDTH = `tput cols`.to_i - 2
  
  def height
    `tput lines`.to_i
  end
  
  def percent_done
   ("%0.4f" % (current.call.to_f / total.call)).to_f
  end
 
  def done_chars
   (percent_done * WIDTH).to_i
  end
 
  def progress
   out = ""
   out << "["
   out << "=" * (done_chars)
   out << ">"
   out << "-" * ((WIDTH - done_chars) - 1)
   out << "]"
  end
 
  def status
   "#{current.call}/#{total.call} (#{percent_done * 100}%)"
  end
 
  def output
    s = status
 
    unless @cleared
      puts "\n\n"
      @cleared = true
    end
    
    print "\033[2A" # up 2 lines
    puts s
    puts progress
  end
end