Class: ProgressBar::Base

Inherits:
Object
  • Object
show all
Includes:
Formatter, LengthCalculator
Defined in:
lib/ruby-progressbar/base.rb

Constant Summary collapse

DEFAULT_OUTPUT_STREAM =
$stdout

Constants included from Formatter

Formatter::DEFAULT_FORMAT_STRING, Formatter::DEFAULT_NON_TTY_FORMAT_STRING, Formatter::DEFAULT_TITLE

Instance Method Summary collapse

Methods included from Formatter

#format, #progress, #total

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby-progressbar/base.rb', line 8

def initialize(options = {})
  self.output       = options[:output] || DEFAULT_OUTPUT_STREAM
  autostart         = options.fetch(:autostart, true)

  super(options)

  @bar              = Components::Bar.new(options)
  @estimated_time   = Components::EstimatedTimer.new(options)
  @elapsed_time     = Components::ElapsedTimer.new
  @throttle         = Components::Throttle.new(options)

  start :at => options[:starting_at] if autostart
end

Instance Method Details

#clearObject

Output



107
108
109
110
111
112
113
114
115
116
# File 'lib/ruby-progressbar/base.rb', line 107

def clear
  self.last_update_length = 0

  if output.tty?
    output.print clear_string
    output.print "\r"
  else
    output.print "\n"
  end
end

#decrementObject

Updating The Bar’s Progress



37
38
39
# File 'lib/ruby-progressbar/base.rb', line 37

def decrement
  with_update { with_progressables(:decrement) }
end

#finishObject

Stopping The Bar



56
57
58
# File 'lib/ruby-progressbar/base.rb', line 56

def finish
  with_update { with_progressables(:finish) } unless finished?
end

#finished?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ruby-progressbar/base.rb', line 85

def finished?
  @bar.progress == @bar.total
end

#incrementObject



41
42
43
# File 'lib/ruby-progressbar/base.rb', line 41

def increment
  with_update { with_progressables(:increment) }
end

#inspectObject



135
136
137
# File 'lib/ruby-progressbar/base.rb', line 135

def inspect
  "#<ProgressBar:#{progress}/#{total || 'unknown'}>"
end

#log(string) ⇒ Object



122
123
124
125
126
127
# File 'lib/ruby-progressbar/base.rb', line 122

def log(string)
  clear
  output.puts string

  update(:force => true) unless stopped?
end

#pauseObject



60
61
62
# File 'lib/ruby-progressbar/base.rb', line 60

def pause
  with_update { with_timers(:pause) } unless paused?
end

#progress=(new_progress) ⇒ Object



45
46
47
# File 'lib/ruby-progressbar/base.rb', line 45

def progress=(new_progress)
  with_update { with_progressables(:progress=, new_progress) }
end

#progress_mark=(mark) ⇒ Object

UI Updates



92
93
94
# File 'lib/ruby-progressbar/base.rb', line 92

def progress_mark=(mark)
  with_update { @bar.progress_mark = mark }
end

#refreshObject



118
119
120
# File 'lib/ruby-progressbar/base.rb', line 118

def refresh
  update
end

#remainder_mark=(mark) ⇒ Object



96
97
98
# File 'lib/ruby-progressbar/base.rb', line 96

def remainder_mark=(mark)
  with_update { @bar.remainder_mark = mark }
end

#resetObject



72
73
74
75
76
77
# File 'lib/ruby-progressbar/base.rb', line 72

def reset
  with_update do
    @bar.reset
    with_timers(:reset)
  end
end

#resumeObject



68
69
70
# File 'lib/ruby-progressbar/base.rb', line 68

def resume
  with_update { with_timers(:resume) } if stopped?
end

#start(options = {}) ⇒ Object

Starting The Bar



25
26
27
28
29
30
31
32
# File 'lib/ruby-progressbar/base.rb', line 25

def start(options = {})
  clear

  with_update do
    with_progressables(:start, options)
    @elapsed_time.start
  end
end

#stopObject



64
65
66
# File 'lib/ruby-progressbar/base.rb', line 64

def stop
  with_update { with_timers(:stop) } unless stopped?
end

#stopped?Boolean Also known as: paused?

Returns:

  • (Boolean)


79
80
81
# File 'lib/ruby-progressbar/base.rb', line 79

def stopped?
  (@estimated_time.stopped? && @elapsed_time.stopped?) || finished?
end

#title=(title) ⇒ Object



100
101
102
# File 'lib/ruby-progressbar/base.rb', line 100

def title=(title)
  with_update { super }
end

#to_s(format_string = nil) ⇒ Object



129
130
131
132
133
# File 'lib/ruby-progressbar/base.rb', line 129

def to_s(format_string = nil)
  format_string ||= @format_string

  format(format_string)
end

#total=(new_total) ⇒ Object



49
50
51
# File 'lib/ruby-progressbar/base.rb', line 49

def total=(new_total)
  with_update { with_progressables(:total=, new_total) }
end