Class: MongodbLogger::Utils::Progressbar

Inherits:
Object
  • Object
show all
Defined in:
lib/mongodb_logger/utils/progressbar.rb

Instance Method Summary collapse

Constructor Details

#initializeProgressbar

Returns a new instance of Progressbar.



5
6
7
8
9
# File 'lib/mongodb_logger/utils/progressbar.rb', line 5

def initialize
  @last_length = 0
  @title = ""
  @progress = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



38
39
40
# File 'lib/mongodb_logger/utils/progressbar.rb', line 38

def method_missing(method, *args, &block)
  @self_before_instance_eval.send method, *args, &block
end

Instance Method Details

#interruptObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongodb_logger/utils/progressbar.rb', line 21

def interrupt
  # TODO: Make some of the strings constants so we don't have to use a magic number here.
  progressbar_length = 16 + @last_length + @title.length
  move_cursor = "\e[#{progressbar_length}D"
  print move_cursor + (" " * progressbar_length) + move_cursor
  STDOUT.flush
  yield
  puts
  print @title + " "
  render_progress(@progress)
end

#progress(percent) ⇒ Object



33
34
35
36
# File 'lib/mongodb_logger/utils/progressbar.rb', line 33

def progress(percent)
  print "\e[#{15 + @last_length}D"
  render_progress(percent)
end

#show(title, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/mongodb_logger/utils/progressbar.rb', line 11

def show(title, &block)
  @title = title
  print @title + " "
  start_progress
  # thanks to http://www.dcmanges.com/blog/ruby-dsls-instance-eval-with-delegation
  @self_before_instance_eval = eval "self", block.binding
  instance_eval(&block)
  finish_progress
end