Class: Commander::UI::ProgressBar

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

Overview

Progress Bar

Terminal progress bar utility. In its most basic form requires that the developer specifies when the bar should be incremented. Note that a hash of tokens may be passed to #increment, (or returned when using Object#progress).

uris = %w(
http://vision-media.ca
http://yahoo.com
http://google.com
)

bar = Commander::UI::ProgressBar.new uris.length, options
threads = []
uris.each do |uri|
threads << Thread.new do
  begin
    res = open uri
    bar.increment :uri => uri
  rescue Exception => e
    bar.increment :uri => "#{uri} failed"
  end
end
end
threads.each { |t| t.join }

The Object method #progress is also available:

progress uris, :width => 10 do |uri|
res = open uri
{ :uri => uri } # Can now use :uri within :format option
end

Instance Method Summary collapse

Constructor Details

#initialize(total, options = {}) ⇒ ProgressBar

Creates a new progress bar.

Options

:title              Title, defaults to "Progress"
:width              Width of :progress_bar
:progress_str       Progress string, defaults to "="
:incomplete_str     Incomplete bar string, defaults to '.'
:format             Defaults to ":title |:progress_bar| :percent_complete% complete "
:tokens             Additional tokens replaced within the format string
:complete_message   Defaults to "Process complete"

Tokens

:title
:percent_complete
:progress_bar
:step
:steps_remaining
:total_steps
:time_elapsed
:time_remaining


424
425
426
427
428
429
430
431
432
433
# File 'lib/commander/user_interaction.rb', line 424

def initialize(total, options = {})
  @total_steps, @step, @start_time = total, 0, Time.now
  @title = options.fetch :title, 'Progress'
  @width = options.fetch :width, 25
  @progress_str = options.fetch :progress_str, '='
  @incomplete_str = options.fetch :incomplete_str, '.'
  @complete_message = options.fetch :complete_message, 'Process complete'
  @format = options.fetch :format, ':title |:progress_bar| :percent_complete% complete '
  @tokens = options.fetch :tokens, {}
end

Instance Method Details

#completed?Boolean

Whether or not the operation has completed.

Returns:

  • (Boolean)


505
506
507
# File 'lib/commander/user_interaction.rb', line 505

def completed?
  @step == @total_steps
end

#erase_lineObject

Erase previous terminal line.



520
521
522
523
# File 'lib/commander/user_interaction.rb', line 520

def erase_line
  # highline does not expose the output stream
  HighLine.default_instance.instance_variable_get('@output').print "\r\e[K"
end

#finished?Boolean

Whether or not the operation is complete, and we have finished.

Returns:

  • (Boolean)


499
500
501
# File 'lib/commander/user_interaction.rb', line 499

def finished?
  @step == @total_steps + 1
end

#generate_tokensObject

Generates tokens for this step.



471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/commander/user_interaction.rb', line 471

def generate_tokens
  {
    title: @title,
    percent_complete: percent_complete,
    progress_bar: progress_bar,
    step: @step,
    steps_remaining: steps_remaining,
    total_steps: @total_steps,
    time_elapsed: format('%0.2fs', time_elapsed),
    time_remaining: @step.positive? ? format('%0.2fs', time_remaining) : '',
  }.merge! @tokens
end

#increment(tokens = {}) ⇒ Object

Increment progress. Optionally pass tokens which can be displayed in the output format.



512
513
514
515
516
# File 'lib/commander/user_interaction.rb', line 512

def increment(tokens = {})
  @step += 1
  @tokens.merge! tokens if tokens.is_a? Hash
  show
end

#percent_completeObject

Completion percentage.



437
438
439
440
441
442
443
# File 'lib/commander/user_interaction.rb', line 437

def percent_complete
  if @total_steps.zero?
    100
  else
    @step * 100 / @total_steps
  end
end

#progress_barObject

Formatted progress bar.



465
466
467
# File 'lib/commander/user_interaction.rb', line 465

def progress_bar
  (@progress_str * (@width * percent_complete / 100)).ljust @width, @incomplete_str
end

#showObject

Output the progress bar.



486
487
488
489
490
491
492
493
494
495
# File 'lib/commander/user_interaction.rb', line 486

def show
  return if finished?

  erase_line
  if completed?
    HighLine.default_instance.say UI.replace_tokens(@complete_message, generate_tokens) if @complete_message.is_a? String
  else
    HighLine.default_instance.say UI.replace_tokens(@format, generate_tokens) << ' '
  end
end

#steps_remainingObject

Number of steps left.



459
460
461
# File 'lib/commander/user_interaction.rb', line 459

def steps_remaining
  @total_steps - @step
end

#time_elapsedObject

Time that has elapsed since the operation started.



447
448
449
# File 'lib/commander/user_interaction.rb', line 447

def time_elapsed
  Time.now - @start_time
end

#time_remainingObject

Estimated time remaining.



453
454
455
# File 'lib/commander/user_interaction.rb', line 453

def time_remaining
  (time_elapsed / @step) * steps_remaining
end