Class: SimpleProgressbar

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

Instance Method Summary collapse

Constructor Details

#initializeSimpleProgressbar

Returns a new instance of SimpleProgressbar.



2
3
4
5
6
# File 'lib/simple_progressbar.rb', line 2

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



35
36
37
# File 'lib/simple_progressbar.rb', line 35

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

Instance Method Details

#interruptObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simple_progressbar.rb', line 18

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



30
31
32
33
# File 'lib/simple_progressbar.rb', line 30

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

#show(title, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/simple_progressbar.rb', line 8

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