module Progress::ClassMethods

Class methods of Progress

Attributes

eta[R]
io[W]

Public Class Methods

extended(klass) click to toggle source
# File lib/progress/class_methods.rb, line 9
def self.extended(klass)
  klass.instance_variable_set(:@lock, Mutex.new)
end

Public Instance Methods

highlight=(value) click to toggle source

explicitly set highlighting [true/false/nil]

# File lib/progress/class_methods.rb, line 87
def highlight=(value)
  @highlight = true && value
end
highlight?() click to toggle source

highlight output using control characters

# File lib/progress/class_methods.rb, line 82
def highlight?
  @highlight.nil? ? io_tty? : @highlight
end
io() click to toggle source
# File lib/progress/class_methods.rb, line 103
def io
  @io ||= $stderr
end
io_tty?() click to toggle source
# File lib/progress/class_methods.rb, line 107
def io_tty?
  io.tty? || ENV['PROGRESS_TTY']
end
note=(note) click to toggle source

set note

# File lib/progress/class_methods.rb, line 65
def note=(note)
  return unless running?

  @levels.last.note = note
end
running?() click to toggle source

check if progress was started

# File lib/progress/class_methods.rb, line 60
def running?
  @levels && !@levels.empty?
end
set(new_current, note = nil) { || ... } click to toggle source

set value of current progress

# File lib/progress/class_methods.rb, line 38
def set(new_current, note = nil, &block)
  if running?
    ret = @levels.last.set(new_current, note, &block)
    print_message
    ret
  elsif block
    yield
  end
end
start(total = nil, title = nil) { || ... } click to toggle source

start progress indication

# File lib/progress/class_methods.rb, line 14
def start(total = nil, title = nil)
  init(total, title)
  print_message force: true
  return unless block_given?

  begin
    yield
  ensure
    stop
  end
end
stay_on_line=(value) click to toggle source

explicitly set staying on one line [true/false/nil]

# File lib/progress/class_methods.rb, line 77
def stay_on_line=(value)
  @stay_on_line = true && value
end
stay_on_line?() click to toggle source

stay on one line

# File lib/progress/class_methods.rb, line 72
def stay_on_line?
  @stay_on_line.nil? ? io_tty? : @stay_on_line
end
step(step = nil, note = nil) { || ... } click to toggle source

step current progress

# File lib/progress/class_methods.rb, line 27
def step(step = nil, note = nil, &block)
  if running?
    ret = @levels.last.step(step, note, &block)
    print_message
    ret
  elsif block
    yield
  end
end
stop() click to toggle source

stop progress

# File lib/progress/class_methods.rb, line 49
def stop
  return unless running?

  if @levels.length == 1
    print_message force: true, finish: true
    stop_beeper
  end
  @levels.pop
end
terminal_title=(value) click to toggle source

explicitly set showing progress in terminal title [true/false/nil]

# File lib/progress/class_methods.rb, line 97
def terminal_title=(value)
  @terminal_title = true && value
end
terminal_title?() click to toggle source

show progress in terminal title

# File lib/progress/class_methods.rb, line 92
def terminal_title?
  @terminal_title.nil? ? io_tty? : @terminal_title
end
without_beeper() { || ... } click to toggle source

don't refresh progress (eta) periodically for the duration of the block

# File lib/progress/class_methods.rb, line 112
def without_beeper
  old_state = @without_beeper
  @without_beeper = true
  yield
ensure
  @without_beeper = old_state
end

Private Instance Methods

build_message(options) click to toggle source
# File lib/progress/class_methods.rb, line 204
def build_message(options)
  current = 0
  reverse_parts = @levels.reverse.map do |level|
    current = level.to_f(current)

    part = current.zero? ? '......' : format('%5.1f%%', current * 100.0)

    if highlight? && part != '100.0%'
      part = "\e[1m#{part}\e[0m"
    end

    level.title ? "#{level.title}: #{part}" : part
  end
  message = reverse_parts.reverse * ' > '

  if options[:finish]
    message << " (elapsed: #{eta.elapsed})"
  elsif (left = eta.left(current))
    message << " (ETA: #{left})"
  end

  if running? && (note = @levels.last.note)
    message << " - #{note}"
  end

  message
end
init(total = nil, title = nil) { |: nil| ... } click to toggle source
# File lib/progress/class_methods.rb, line 124
def init(total = nil, title = nil)
  lock do
    if running?
      unless @started_in == Thread.current
        warn 'Can\'t start inner progress in different thread'
        return block_given? ? yield : nil
      end
    else
      @started_in = Thread.current
      @eta = Eta.new
      start_beeper
    end
    @levels ||= []
    @levels.push new(total, title)
  end
end
lock(force = true) { || ... } click to toggle source
# File lib/progress/class_methods.rb, line 141
def lock(force = true)
  if force
    @lock.lock
  else
    return unless @lock.try_lock
  end

  begin
    yield
  ensure
    @lock.unlock
  end
end
message_for_output(options) click to toggle source
# File lib/progress/class_methods.rb, line 184
def message_for_output(options)
  message = build_message(options)

  out = ''.dup
  out << "\r" if stay_on_line?
  out << message
  out << "\e[K" if stay_on_line?
  out << "\n" if !stay_on_line? || options[:finish]

  if terminal_title?
    out << "\e]0;"
    unless options[:finish]
      out << message.gsub(/\e\[\dm/, '').tr("\a", '␇')
    end
    out << "\a"
  end

  out
end
print_message(options = {}) click to toggle source
restart_beeper() click to toggle source
# File lib/progress/class_methods.rb, line 165
def restart_beeper
  @beeper.restart if @beeper
end
start_beeper() click to toggle source
# File lib/progress/class_methods.rb, line 155
def start_beeper
  @beeper = Beeper.new(10) do
    print_message unless @without_beeper
  end
end
stop_beeper() click to toggle source
# File lib/progress/class_methods.rb, line 161
def stop_beeper
  @beeper.stop if @beeper
end
time_to_print?() click to toggle source
# File lib/progress/class_methods.rb, line 169
def time_to_print?
  !@next_time_to_print || @next_time_to_print <= ElapsedTime.now
end