Class: Coltrane::Cli::Guitar

Inherits:
Representation show all
Includes:
Color
Defined in:
lib/cli/guitar.rb

Overview

Renders notes in a common most popular ukulele scheme

Direct Known Subclasses

BassGuitar, Ukulele

Constant Summary collapse

SPECIAL_FRETS =
[3, 5, 7, 9, 12, 15, 17, 19].freeze

Constants inherited from Representation

Representation::ACCEPTED_FLAVORS

Instance Method Summary collapse

Methods inherited from Representation

build, #hint, inherited

Constructor Details

#initialize(notes, flavor, tuning: %w[E A D G B E],, frets: 22) ⇒ Guitar

Returns a new instance of Guitar.



10
11
12
13
14
15
16
# File 'lib/cli/guitar.rb', line 10

def initialize(notes, flavor, tuning: %w[E A D G B E], frets: 22)
  @notes    = notes
  @tuning   = tuning.reverse
  @frets    = frets
  @flavor   = flavor
  @ref_note = @notes.first
end

Instance Method Details

#place_empty(str_i) ⇒ Object



46
47
48
# File 'lib/cli/guitar.rb', line 46

def place_empty(str_i)
  Paint['--', HSL.new(180 + str_i * 3,50,30).html]
end

#place_mark(note) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cli/guitar.rb', line 50

def place_mark(note)
  mark =
    case @flavor
    when :notes     then note.pretty_name.ljust(2, ' ')
    when :intervals then (@ref_note - note).name.ljust(2, '-')
    when :degrees   then @notes.degree(note).to_s.rjust(2, '0')
    when :marks     then '  ' # '◼◼'
    else raise WrongFlavorError
    end

  base_hue = (180 + note.number * 10) % 360 # + 260
  Paint[
    mark,
    HSL.new(0, 0, 100).html,
    HSL.new(base_hue, 100, 30).html
  ]
end

#renderObject



18
19
20
# File 'lib/cli/guitar.rb', line 18

def render
  [render_notes, render_special_frets, hint].join("\n" * 2)
end

#render_notesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cli/guitar.rb', line 22

def render_notes
  @tuning.each_with_index.map do |string, str_i|
    string_note = Note[string]
    Array.new(@frets + 2) do |i|
      if i.zero?
        Paint[string, HSL.new(140 + str_i * 20,50,50).html]
      else
        fret = i - 1
        note = string_note + fret
        m = (@notes.include?(note) ? place_mark(note) : place_empty(str_i))
        fret.zero? ? (m + ' |') : m
      end
    end.join(' ')
  end.join("\n")
end

#render_special_fretsObject



38
39
40
41
42
43
44
# File 'lib/cli/guitar.rb', line 38

def render_special_frets
  '  ' +
  Array.new(@frets + 2) do |fret|
    m = SPECIAL_FRETS.include?(fret) ? fret.to_s.rjust(2, 0.to_s) : '  '
    "#{m}#{'  ' if fret.zero?}"
  end.join(' ')
end