class Prog::Pipe

Attributes

highest_count[RW]
tasks[RW]

Public Class Methods

new() click to toggle source
# File lib/prog.rb, line 11
def initialize
  self.tasks = []
  self.highest_count = 0
end

Public Instance Methods

<<(task) click to toggle source
# File lib/prog.rb, line 47
def <<(task)
  tasks << task
  task.pipe = self
  task.force_finish if task.max_count.zero?
end
to_s(length: $stdout.winsize[1]) click to toggle source
# File lib/prog.rb, line 16
def to_s(length: $stdout.winsize[1])
  return ' ' * length if tasks.count.zero?

  unused_length = length
  self.highest_count = [highest_count, tasks.count].max

  ## Set up the first barrier
  display_string = +'|'
  unused_length -= 1

  ## Get a first pass at equal task length
  # first_pass_task_length = unused_length/working_tasks.count
  first_pass_task_length = unused_length / highest_count
  if first_pass_task_length < 2
    raise "Prog::Pipe length (#{length}) too small to " \
          "fit all tasks (#{tasks.count})"
  end
  tasks.each do |task|
    task.working_length = first_pass_task_length
  end

  ## Distribute the remaining space evenly among the first n tasks
  remaining_space = unused_length - (first_pass_task_length * highest_count)
  tasks[0...remaining_space].each { |task| task.working_length += 1 }

  tasks.each do |task|
    display_string << task.to_s
  end
  display_string.ljust(length, ' ')
end