Make the get_item function more generic and catch the IndexError in de keypress handler

This commit is contained in:
Valentijn 2018-05-02 23:18:03 +02:00
parent 21eb467c8d
commit 38caf12792

View file

@ -5,6 +5,8 @@ Components for song listing.
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-public-methods # pylint: disable=too-many-public-methods
from string import digits from string import digits
from operator import lt, gt
try: try:
# Python 3.x # Python 3.x
from string import ascii_letters from string import ascii_letters
@ -610,8 +612,11 @@ class SongListBox(urwid.Frame):
elif key == 'backspace': elif key == 'backspace':
self.perform_filtering(key) self.perform_filtering(key)
elif self._is_filtering: elif self._is_filtering:
return hotkey_manager.keypress("library_view", self, super(SongListBox, self), try:
size, key) return hotkey_manager.keypress("library_view", self, super(SongListBox, self),
size, key)
except IndexError:
pass
else: else:
return super(SongListBox, self).keypress(size, key) return super(SongListBox, self).keypress(size, key)
@ -620,12 +625,7 @@ class SongListBox(urwid.Frame):
def _get_filtered(self): def _get_filtered(self):
"""Get filtered list of items""" """Get filtered list of items"""
matches = self.get_filtered_items() matches = self.get_filtered_items()
if not matches:
return False
_, index = self.walker.get_focus() _, index = self.walker.get_focus()
return (matches, index) return (matches, index)
def move_to_beginning(self): def move_to_beginning(self):
@ -643,34 +643,29 @@ class SongListBox(urwid.Frame):
def move_up(self): def move_up(self):
"""Move the focus an item up in the playlist""" """Move the focus an item up in the playlist"""
matches, index = self._get_filtered() matches, index = self._get_filtered()
self.list_box.set_focus(*self.get_prev_item(matches, index)) self.list_box.set_focus(*self.get_item(matches, index, lt))
return False return False
def move_down(self): def move_down(self):
"""Move the focus an item down in the playlist """ """Move the focus an item down in the playlist """
matches, index = self._get_filtered() matches, index = self._get_filtered()
self.list_box.set_focus(*self.get_next_item(matches, index)) self.list_box.set_focus(*self.get_item(matches, index, gt))
return False return False
@staticmethod @staticmethod
def get_prev_item(matches, current_index): def get_item(matches, current_index, callback):
""" """
Get previous item index from matches list. Get an item index from the matches list
""" """
prev_items = [item for item in matches if item.index < current_index] # Some not so very nice code to get to be nice and generic
if prev_items: order = ['above', 'below']
return prev_items[-1].index, 'below' if callback is lt:
return matches[-1].index, 'above' order.reverse()
@staticmethod items = [item for item in matches if callback(item.index, current_index)]
def get_next_item(matches, current_index): if items:
""" return items[-1].index, order[0]
Get next item index from matches list. return matches[-1].index, order[1]
"""
next_items = [item for item in matches if item.index > current_index]
if next_items:
return next_items[0].index, 'above'
return matches[0].index, 'below'
def mouse_event(self, size, event, button, col, row, focus): def mouse_event(self, size, event, button, col, row, focus):
""" """