Class: WTFChord::KeyboardPresentation

Inherits:
Object
  • Object
show all
Includes:
RomanNumbersHelper
Defined in:
lib/wtf_chord/keyboard_presentation.rb

Defined Under Namespace

Classes: KeyStruct

Constant Summary collapse

FRAME =
"\u250C\u2500\u252C\u2500\u252C\u252C\u2500\u252C\u2500\u252C\u2500\u252C\u2500\u252C\u252C\u2500\u252C\u252C\u2500\u252C\u2500\u2510\u252C\n\u2502 \u2502 \u2502\u2502 \u2502 \u2502 \u2502 \u2502\u2502 \u2502\u2502 \u2502 \u2502\u2502\n\u2502 \u2514\u252C\u2518\u2514\u252C\u2518 \u2502 \u2514\u252C\u2518\u2514\u252C\u2518\u2514\u252C\u2518 \u2502\u2502\n\u2502  \u2502  \u2502  \u2502  \u2502  \u2502  \u2502  \u2502\u2502\n\u2514\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2518\u2534\n \u2191                    |\n %s                    |\n                      |\n"
Key =
-> (mark) { Class.new(KeyStruct).tap { |c| c.mark = mark } }
KEYS =
[W[73], B[27], W[76], B[30], W[79], W[82], B[36], W[85], B[39], W[88], B[42], W[91]].
map.with_index(1).to_h.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RomanNumbersHelper

#romanize

Constructor Details

#initialize(octave = 1) ⇒ KeyboardPresentation

Returns a new instance of KeyboardPresentation.



53
54
55
56
# File 'lib/wtf_chord/keyboard_presentation.rb', line 53

def initialize(octave = 1)
  @octave = octave
  @frame = FRAME.dup % romanize(octave)
end

Instance Attribute Details

#frameObject (readonly)

Returns the value of attribute frame.



51
52
53
# File 'lib/wtf_chord/keyboard_presentation.rb', line 51

def frame
  @frame
end

Class Method Details

.press(*positions) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wtf_chord/keyboard_presentation.rb', line 39

def self.press(*positions)
  if positions.all? { |v| Integer === v }
    new.press(*positions)
  else
    positions.uniq(&:key).group_by(&:octave).inject(nil) do |a, (octave, strings)|
      pos = strings.map { |s| s.note.position }
      kb = new(octave).press(*pos)
      a.nil? ? kb : a + kb
    end
  end
end

Instance Method Details

#+(other) ⇒ Object



63
64
65
66
67
# File 'lib/wtf_chord/keyboard_presentation.rb', line 63

def +(other)
  pop!
  @frame.replace(@frame.lines(chomp: true).zip((+other).frame.lines).map(&:join).join)
  self
end

#+@Object



58
59
60
61
# File 'lib/wtf_chord/keyboard_presentation.rb', line 58

def +@
  shift!
  self
end

#finalize!Object



78
79
80
# File 'lib/wtf_chord/keyboard_presentation.rb', line 78

def finalize!
  @frame.gsub("|", " ").gsub(/[^\s](?=\n)/, "")
end

#pop!Object



82
83
84
# File 'lib/wtf_chord/keyboard_presentation.rb', line 82

def pop!
  @frame.replace(@frame.lines.each { |line| line[-3, 1] = "" }.join)
end

#press(*positions) ⇒ Object



69
70
71
72
# File 'lib/wtf_chord/keyboard_presentation.rb', line 69

def press(*positions)
  positions.each { |pos| KEYS.fetch(pos).(@frame) }
  self
end

#shift!Object



86
87
88
# File 'lib/wtf_chord/keyboard_presentation.rb', line 86

def shift!
  @frame.replace(@frame.lines.each { |line| line[0, 1] = "" }.join)
end

#to_sObject



74
75
76
# File 'lib/wtf_chord/keyboard_presentation.rb', line 74

def to_s
  finalize!
end