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
21
# 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)
  @rate             = Components::Rate.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



114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby-progressbar/base.rb', line 114

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



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

def decrement
  update_progress(:decrement)
end

#finishObject

Stopping The Bar



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

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

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @estimated_time.finished? && @bar.finished?
end

#incrementObject



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

def increment
  update_progress(:increment)
end

#inspectObject



142
143
144
# File 'lib/ruby-progressbar/base.rb', line 142

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

#log(string) ⇒ Object



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

def log(string)
  clear
  output.puts string

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

#pauseObject



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

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

#progress=(new_progress) ⇒ Object



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

def progress=(new_progress)
  update_progress(:progress=, new_progress)
end

#progress_mark=(mark) ⇒ Object

UI Updates



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

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

#refreshObject



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

def refresh
  update
end

#remainder_mark=(mark) ⇒ Object



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

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

#resetObject



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

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

#resumeObject



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

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

#start(options = {}) ⇒ Object

Starting The Bar



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

def start(options = {})
  clear

  with_update do
    with_progressables(:start, options)
    with_timers(:start)
  end
end

#started?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ruby-progressbar/base.rb', line 90

def started?
  @estimated_time.started? && @elapsed_time.started? && @bar.started?
end

#stopObject



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

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

#stopped?Boolean Also known as: paused?

Returns:

  • (Boolean)


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

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

#title=(title) ⇒ Object



105
106
107
108
109
# File 'lib/ruby-progressbar/base.rb', line 105

def title=(title)
  if output.tty?
    with_update { super }
  end
end

#to_s(format_string = nil) ⇒ Object



136
137
138
139
140
# File 'lib/ruby-progressbar/base.rb', line 136

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

  format(format_string)
end

#total=(new_total) ⇒ Object



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

def total=(new_total)
  update_progress(:total=, new_total)
end