Class: Progress

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

Instance Method Summary collapse

Constructor Details

#initialize(message, time = nil) ⇒ Progress

ANSI escape code to clear line from cursor to end of line “e” is an alternative to “033” cf. en.wikipedia.org/wiki/ANSI_escape_code



6
7
8
9
10
# File 'lib/progress.rb', line 6

def initialize(message, time = nil)
  @message = message
  @time = time ? time : Time.now
  @reset = "\r\e[0K"
end

Instance Method Details

#finish!(message_addition = "") ⇒ Object



17
18
19
20
21
22
23
# File 'lib/progress.rb', line 17

def finish!(message_addition = "")
  time = Time.now - @time
  message = @message + message_addition
  str = "#{@reset} #{message}" + "100%".rjust(60-message.length)
  print  str + "Took: #{"%.2f" % time} sec.\n".rjust(100-str.length)
  $stdout.flush
end

#progress(num, message_addition = "") ⇒ Object Also known as: update



11
12
13
14
15
16
# File 'lib/progress.rb', line 11

def progress(num, message_addition = "")
  message = @message+message_addition
  # reset lines
  print "#{@reset} #{message}" + "#{num}%".rjust(60-message.length)
  $stdout.flush
end