Class: KeyboardLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/keyboard_distance/keyboard_layout.rb

Constant Summary collapse

LAYOUTS =
{
  :qwerty => {
    :normal => ['`1234567890-=', ' qwertyuiop[]\\', " asdfghjkl;'", ' zxcvbnm,./'],
    :shift  => ['~!@#$%^&*()_+', ' QWERTYUIOP{}|',  ' ASDFGHJKL:"', ' ZXCVBNM<>?']
  },
  :qwertz => {
    :normal => ['`1234567890-=', ' qwertzuiop[]\\', " asdfghjkl;'", ' yxcvbnm,./'],
    :shift  => ['~!@#$%^&*()_+', ' QWERTZUIOP{}|',  ' ASDFGHJKL:"', ' YXCVBNM<>?']
  }
}
NATIONAL_KEYS =
{
  :polish => ["ąćęłńóśżźĄĆĘŁŃÓŚŻŹ", "acelnoszzACELNOSZZ"]
}
EMPTY_KEY =
' '

Class Method Summary collapse

Class Method Details

.build_alted_keys_map(national) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/keyboard_distance/keyboard_layout.rb', line 46

def build_alted_keys_map(national)
  national_keys = NATIONAL_KEYS[national]
  raise "Undefined national keys #{}" if national_keys.nil?

  raise "Different number of national and ASCII keys for " \
    " :#{national}" unless national_keys[0].size == national_keys[1].size
  
  alt_map = {}
  national_keys[0].split('').each_with_index do |char, idx|
    alt_map[char] = national_keys[1][idx]
  end

  alt_map
end

.build_shifted_keys_map(layout) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/keyboard_distance/keyboard_layout.rb', line 31

def build_shifted_keys_map(layout)
  keys = layout_as_array(layout, :normal).flatten
  shifted_keys = layout_as_array(layout, :shift).flatten

  raise "Different number of shifted and unshifted keys for layout" \
    " :#{layout}" unless keys.size == shifted_keys.size

  shift_map = {}
  shifted_keys.each_with_index do |shifted_key, idx|
    shift_map[shifted_key] = keys[idx]
  end

  shift_map
end

.foreach_unique_key_pair(layout, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/keyboard_distance/keyboard_layout.rb', line 61

def foreach_unique_key_pair(layout, &block)
  keyboard = layout_as_array(layout, :normal)

  keyboard.each_with_index do |row_1, idx_11|
    row_1.each_with_index do |key_1, idx_12|
      keyboard.each_with_index do |row_2, idx_21|
        row_2.each_with_index do |key_2, idx_22|
          next if key_1 == key_2 || key_1 == EMPTY_KEY || key_2 == EMPTY_KEY
          yield key_1, key_2, [idx_11, idx_12, idx_21, idx_22]
        end
      end
    end
  end
end

.layout_defined?(layout) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/keyboard_distance/keyboard_layout.rb', line 23

def layout_defined?(layout)
  LAYOUTS.keys.include?(layout)
end

.national_defined?(national) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/keyboard_distance/keyboard_layout.rb', line 27

def national_defined?(national)
  NATIONAL_KEYS.keys.include?(national)
end


76
77
78
79
80
81
82
83
# File 'lib/keyboard_distance/keyboard_layout.rb', line 76

def print_layout(layout, type=:normal)
  keyboard = layout_as_array(layout, type)
  max_row_size = keyboard.map(&:size).max
    
  puts format_header(max_row_size), format_separator(max_row_size)
  keyboard.each_with_index { |keys, idx| puts format_row(idx, keys) }
  puts format_separator(max_row_size)
end