Class: Hanoi::Jane::Formatters::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/hanoi/jane/formatters/console.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Console

Returns a new instance of Console.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
# File 'lib/hanoi/jane/formatters/console.rb', line 7

def initialize
  @fancy = false

  yield self if block_given?
end

Instance Attribute Details

#fancyObject

Returns the value of attribute fancy.



5
6
7
# File 'lib/hanoi/jane/formatters/console.rb', line 5

def fancy
  @fancy
end

#stacksObject

Returns the value of attribute stacks.



5
6
7
# File 'lib/hanoi/jane/formatters/console.rb', line 5

def stacks
  @stacks
end

Class Method Details

.assemble(stacks) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hanoi/jane/formatters/console.rb', line 17

def Console.assemble stacks
  a = []
  (Console.rotate stacks).each_with_index do |r, index|
    divided = (index + 1) == stacks.first.length ? true : false
    a.push Console.row r, widest: Console.biggest(stacks), divided: divided
  end

  a.push [:horiz_divider] * a[0].length

  a
end

.biggest(stacks) ⇒ Object



38
39
40
# File 'lib/hanoi/jane/formatters/console.rb', line 38

def Console.biggest stacks
  stacks.map { |s| s.compact.max }.compact.max
end

.disc(size, width) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hanoi/jane/formatters/console.rb', line 42

def Console.disc size, width
  return [:space] * Console.scale(width) unless size
  content = Console.scale size
  gap = (Console.scale(width) - content) / 2

  output = [:disc] * content

  gap.times do
    [:unshift, :push].each do |method|
      output.send(method, :space)
    end
  end

  output
end

.populate(stacks, fancy = false) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/hanoi/jane/formatters/console.rb', line 29

def Console.populate stacks, fancy = false
  charset = fancy ? 'fancy' : 'regular'
  (Console.assemble stacks).map do
    |r| r.map do |c|
      Config.instance.config.chars[charset][c.to_s]
    end
  end
end

.rotate(stacks) ⇒ Object



73
74
75
# File 'lib/hanoi/jane/formatters/console.rb', line 73

def Console.rotate stacks
  stacks.map { |s| s.clone }.transpose.reverse
end

.row(starter, widest:, spacing: 1, divided: false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/hanoi/jane/formatters/console.rb', line 58

def Console.row starter, widest:, spacing: 1, divided: false
  filler = [:space] * spacing
  filler = [:vert_divider] if divided

  starter.map { |d|
    Console.disc d, widest
  }.flat_map { |d|
    [d, filler]
  }.unshift(filler).flatten
end

.scale(size) ⇒ Object



69
70
71
# File 'lib/hanoi/jane/formatters/console.rb', line 69

def Console.scale size
  (size * 2) + 1
end

Instance Method Details

#to_sObject



13
14
15
# File 'lib/hanoi/jane/formatters/console.rb', line 13

def to_s
  (Console.populate stacks, @fancy).map { |r| r.join '' }.join "\n"
end