Class: Coltrane::Renderers::TextRenderer::RepresentationGuitarChordDrawer

Inherits:
BaseDrawer
  • Object
show all
Defined in:
lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb

Instance Attribute Summary

Attributes inherited from BaseDrawer

#flavor, #layout, #model, #options, #per_row

Instance Method Summary collapse

Methods inherited from BaseDrawer

#initialize

Constructor Details

This class inherits a constructor from Coltrane::Renderers::TextRenderer::BaseDrawer

Instance Method Details

#alt(str) ⇒ Object



71
72
73
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 71

def alt(str)
  Paint[str, '#2DD380']
end

#base_color(str) ⇒ Object



63
64
65
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 63

def base_color(str)
  Paint[str, '#6A4AC2']
end

#border(x, y) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 39

def border(x, y)
  edge = [guitar.tuning.size * 2 - 2, chord_height + 1]
  x_on_start  = x == 0
  y_on_start  = y == 0
  x_on_edge   = x == edge[0]
  y_on_edge   = y == edge[1]
  x_on_middle = (0...edge[0]).cover?(x)
  y_on_middle = (0...edge[1]).cover?(y)

  if    x_on_start  && y_on_start  then ''
  elsif x_on_middle && y_on_start  then x.odd? ? '' : ''
  elsif x_on_edge   && y_on_start  then ''
  elsif x_on_start  && y_on_middle then ''
  elsif x_on_middle && y_on_middle then x.odd? ? '' : ''
  elsif x_on_edge   && y_on_middle then ''
  elsif x_on_start  && y_on_edge   then ''
  elsif x_on_middle && y_on_edge   then x.odd? ? '' : ''
  elsif x_on_edge   && y_on_edge   then ''
  else ' '
  end
  # puts edge.inspect
  # "#{x},#{y} "
end

#chord_heightObject



9
10
11
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 9

def chord_height
  chord.max_fret_span
end

#guitarObject



13
14
15
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 13

def guitar
  chord.guitar
end

#highlight(str) ⇒ Object



67
68
69
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 67

def highlight(str)
  Paint[str, '#E67831']
end

#renderObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 17

def render
  render_top(chord) +
    (chord.lowest_fret..chord.lowest_fret + chord_height)
    .each_with_index.reduce('') do |memo1, (f, y)|
        memo1 +
          '   ' +
          (chord.guitar.strings.size * 2).times.map { |x| base_color border(x, y) }.join('') +
          "\n" +
          chord.guitar.strings.reduce(base_color(f.to_s.ljust(2, ' '))) do |memo2, s|
            memo2 +
              ' ' +
            render_note(chord.guitar_notes.select do |n|
              n.string == s && n.fret == f
            end.any?)
          end +
          " \n"
    end + '   ' +
    (chord.guitar.strings.size * 2).times.map do |x|
      base_color border(x, chord_height + 1)
    end.join('')
end

#render_note(found) ⇒ Object



89
90
91
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 89

def render_note(found)
  found ? highlight('') : base_color('')
end

#render_top(chord) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/coltrane/renderers/text_renderer/representation_guitar_chord_drawer.rb', line 75

def render_top(chord)
  chord.guitar.strings.reduce('   ') do |memo, s|
    memo +
      if chord.guitar_notes.select do |n|
        n.string == s && n.fret == 0 end.any?
        highlight ''
      elsif chord.guitar_notes.select { |n| n.string == s && n.fret.nil? }.any?
        alt 'x '
      else
        '  '
      end
  end + "\n"
end