Class: Prog::Pipe
- Inherits:
-
Object
- Object
- Prog::Pipe
- Defined in:
- lib/prog.rb
Instance Attribute Summary collapse
-
#highest_count ⇒ Object
Returns the value of attribute highest_count.
-
#tasks ⇒ Object
Returns the value of attribute tasks.
Instance Method Summary collapse
- #<<(task) ⇒ Object
-
#initialize ⇒ Pipe
constructor
A new instance of Pipe.
- #to_s(length: ) ⇒ Object
Constructor Details
#initialize ⇒ Pipe
Returns a new instance of Pipe.
11 12 13 14 |
# File 'lib/prog.rb', line 11 def initialize self.tasks = [] self.highest_count = 0 end |
Instance Attribute Details
#highest_count ⇒ Object
Returns the value of attribute highest_count.
9 10 11 |
# File 'lib/prog.rb', line 9 def highest_count @highest_count end |
#tasks ⇒ Object
Returns the value of attribute tasks.
8 9 10 |
# File 'lib/prog.rb', line 8 def tasks @tasks end |
Instance Method Details
#<<(task) ⇒ Object
47 48 49 50 51 |
# 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: ) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |