Class: Ektoplayer::Bindings

Inherits:
Object
  • Object
show all
Defined in:
lib/ektoplayer/bindings.rb

Overview

Keybinding storage.

Provides access for changing keybinds (with collision detection):

bind(), unbind()

Provides binding these keys to widgets

bind_section_to_widget(), bind_to_widget()

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBindings

Returns a new instance of Bindings.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ektoplayer/bindings.rb', line 21

def initialize
   @commands = {}

   reg 'quit',                      'Quit the program'
   reg 'refresh',                   'Refresh the screen'
   reg 'reload',                    'Apply database changes to browser'
   reg 'update',                    'Start a database update'

   reg 'player.stop',               'Stop playing'
   reg 'player.toggle',             'Toggle play/pause'
   reg 'player.forward',            'Seek forward'
   reg 'player.backward',           'Seek backward'

   reg 'tabs.next',                 'Select next tab'
   reg 'tabs.prev',                 'Select previous tab'

   reg 'browser.enter',             'Enter selected directory'
   reg 'browser.add_to_playlist',   'Add tracks under cursor to playlist'
   reg 'browser.back',              'Change to parent directory'

   reg 'playinginfo.toggle',        'Toggle playinginfo visibility'
   reg 'progressbar.toggle',        'Toggle progressbar visibility'
   reg 'tabbar.toggle',             'Toggle tabbar visibility'

   reg 'playlist.goto_current',     'Go to current playing track'
   reg 'playlist.clear',            'Delete all items in playlist'
   reg 'playlist.delete',           'Delete selected track from playlist'
   reg 'playlist.download_album',   'Download album archive for selected track'
   reg 'playlist.play',             'Play selected track'
   reg 'playlist.reload',           'Force redownload of selected track'
   reg 'playlist.play_next',        'Play next track in playlist'
   reg 'playlist.play_prev',        'Play previous track in playlist'

   %w(info help browser playlist splash).each do |w|
      reg("#{w}.show", "Show #{w}")
   end

   {      up: 'Move cursor up',      down: 'Move cursor down',
     page_up: 'Scroll page up', page_down: 'Scroll page down',
         top: 'Move to top',       bottom: 'Move to bottom'
   }.each do |cmd, desc|
      %w(browser info help playlist).each { |w| reg("#{w}.#{cmd}", desc) }
   end

   { search_up: 'Start search upwards',     search_next: 'Goto next search result',
     search_down: 'Start search downwards', search_prev: 'Goto previous search result',
     toggle_selection:  'Toggle multi line selection',
   }.each do |cmd, desc|
      %w(browser playlist).each { |w| reg("#{w}.#{cmd}", desc) }
   end

   @bindings = { }
   @bindings[:global] = {
      :'splash.show'             => [?`, ?^                          ],
      :'playlist.show'           => [?1                              ],
      :'browser.show'            => [?2                              ],
      :'info.show'               => [?3                              ],
      :'help.show'               => [?4,      ICurses::KEY_F1        ],

      :'playinginfo.toggle'      => [?!, ?\\, ICurses::KEY_F2        ],
      :'progressbar.toggle'      => [?%, ?~,  ICurses::KEY_F3        ],
      :'tabbar.toggle'           => [?=,      ICurses::KEY_F4        ],

      :'player.forward'          => [?f,      ICurses::KEY_RIGHT     ],
      :'player.backward'         => [?b,      ICurses::KEY_LEFT      ],
      :'player.stop'             => [?s                              ],
      :'player.toggle'           => [?p                              ],

      :'playlist.play_next'      => [?>                              ],
      :'playlist.play_prev'      => [?<                              ],

      :'tabs.next'               => [?l, ?}, '^i'                    ],
      :'tabs.prev'               => [?h, ?{, 353                     ],

      :quit                      => [?q                              ],
      :refresh                   => ['^l'                            ],
      :reload                    => ['^r'                            ],
      :update                    => [?U                              ]
   }
   @bindings[:playlist] = {
      # movement
      :'playlist.top'            => [?g,      ICurses::KEY_HOME      ],
      :'playlist.bottom'         => [?G,      ICurses::KEY_END       ],
      :'playlist.up'             => [?k,      ICurses::KEY_UP        ],
      :'playlist.down'           => [?j,      ICurses::KEY_DOWN      ],
      :'playlist.page_down'      => ['^d',    ICurses::KEY_NPAGE     ],
      :'playlist.page_up'        => ['^u',    ICurses::KEY_PPAGE     ],
      # selection
      :'playlist.toggle_selection' => ['^v'                          ],
      # search
      :'playlist.search_next'    => [?n                              ],
      :'playlist.search_prev'    => [?N                              ],
      :'playlist.search_up'      => [??                              ],
      :'playlist.search_down'    => [?/                              ],
      # playlist
      :'playlist.play'           => [         ICurses::KEY_ENTER     ],
      :'playlist.download_album' => [?$                              ],
      :'playlist.reload'         => [?r                              ],
      :'playlist.goto_current'   => [?o                              ],
      :'playlist.clear'          => [?c                              ],
      :'playlist.delete'         => [?d                              ],
      # other
      :'player.toggle'           => [' '                             ]
   }
   @bindings[:browser] = {
      # movement
      :'browser.top'             => [?g,      ICurses::KEY_HOME      ],
      :'browser.bottom'          => [?G,      ICurses::KEY_END       ],
      :'browser.up'              => [?k,      ICurses::KEY_UP        ],
      :'browser.down'            => [?j,      ICurses::KEY_DOWN      ],
      :'browser.page_up'         => ['^u',    ICurses::KEY_PPAGE     ],
      :'browser.page_down'       => ['^d',    ICurses::KEY_NPAGE     ],
      # selection
      :'browser.toggle_selection' => ['^v'                           ],
      # search
      :'browser.search_next'     => [?n                              ],
      :'browser.search_prev'     => [?N                              ],
      :'browser.search_up'       => [??                              ],
      :'browser.search_down'     => [?/                              ],
      # browser
      :'browser.add_to_playlist' => [' ', ?a                         ],
      :'browser.enter'           => [         ICurses::KEY_ENTER     ],
      :'browser.back'            => [?B,      ICurses::KEY_BACKSPACE ]
   }
   @bindings[:help] = {
      :'help.top'                => [?g,      ICurses::KEY_HOME      ],
      :'help.bottom'             => [?G,      ICurses::KEY_END       ],
      :'help.up'                 => [?k,      ICurses::KEY_UP        ],
      :'help.down'               => [?j,      ICurses::KEY_DOWN      ],
      :'help.page_up'            => ['^u',    ICurses::KEY_PPAGE     ],
      :'help.page_down'          => ['^d',    ICurses::KEY_NPAGE     ]
   }
   @bindings[:info] = {
      :'info.top'                => [?g,      ICurses::KEY_HOME      ],
      :'info.bottom'             => [?G,      ICurses::KEY_END       ],
      :'info.up'                 => [?k,      ICurses::KEY_UP        ],
      :'info.down'               => [?j,      ICurses::KEY_DOWN      ],
      :'info.page_up'            => ['^u',    ICurses::KEY_PPAGE     ],
      :'info.page_down'          => ['^d',    ICurses::KEY_NPAGE     ]
   }
   @bindings[:splash] = {}

   @bindings.default_proc = proc { |h,k| fail "Unknown widget #{k}" }
   @bindings.each do |widget, hash|
      hash.default_proc = proc { |h,k| h[k] = [] }
      hash.values.each do |keys|
         keys.map!(&method(:parse_key))
      end
   end
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



14
15
16
# File 'lib/ektoplayer/bindings.rb', line 14

def bindings
  @bindings
end

#commandsObject (readonly)

Returns the value of attribute commands.



14
15
16
# File 'lib/ektoplayer/bindings.rb', line 14

def commands
  @commands
end

Instance Method Details

#bind(widget, key, command) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/ektoplayer/bindings.rb', line 205

def bind(widget, key, command)
   widget, command = widget.to_sym, command.to_sym
   fail "Unknown command #{command}" unless @commands.include? command

   @bindings[widget][command].delete parse_key(key) rescue nil
   @bindings[widget][command] << parse_key(key)
   check_collisions
end

#bind_view(section, view, view_operations, operations) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/ektoplayer/bindings.rb', line 226

def bind_view(section, view, view_operations, operations)
   @bindings[section.to_sym].each do |command, keys|
      keys.each do |key|
         meth = view_operations.method(command) rescue operations.method(command)
         view.keys.on(key, &meth)
      end
   end
end

#keyname(key) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/ektoplayer/bindings.rb', line 172

def keyname(key)
   return 'SPACE' if (key == ' ' or key == 32)

   name = ICurses.keyname(key)
   if name.start_with? 'KEY_'
      name.sub('KEY_', '').delete('()')
   else
      name
   end
end

#parse_key(key) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ektoplayer/bindings.rb', line 183

def parse_key(key)
   return key if key.is_a? Integer
   
   if key.size == 1
      return key.ord
   elsif key.size == 2 and key[0] == ?^
      return key[1].upcase.ord - 64
   elsif key =~ /^(key_)?space$/i
      return ' '.ord
   end

   begin
      return Integer(key) 
   rescue
      key = key.upcase.tr(?-, ?_)
      key = "KEY_#{key}" unless key.start_with?('KEY_')
      return ICurses.const_get(key)
   end
rescue NameError
   fail "Unknown key: #{key}"
end

#register(command, description) ⇒ Object Also known as: reg



16
17
18
# File 'lib/ektoplayer/bindings.rb', line 16

def register(command, description)
   @commands[command.to_sym] = description.freeze
end

#unbind(widget, key) ⇒ Object



214
215
216
217
218
# File 'lib/ektoplayer/bindings.rb', line 214

def unbind(widget, key)
   @bindings[widget.to_sym].each do |command, keys|
      keys.delete( (parsed_key ||= parse_key(k)) )
   end
end

#unbind_allObject



220
221
222
223
224
# File 'lib/ektoplayer/bindings.rb', line 220

def unbind_all
   @bindings.each do |widget, commands|
      commands.clear
   end
end