Skip to content

Commit 40928f9

Browse files
Overhaul songlist searches
1 parent 3b91f04 commit 40928f9

File tree

1 file changed

+45
-37
lines changed

1 file changed

+45
-37
lines changed

clay/ui/urwid/songlist.py

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ def __init__(self, app):
421421

422422
self.current_item = None
423423
self.tracks = []
424+
self.tracks_walker = urwid.SimpleFocusListWalker([])
424425
self.walker = urwid.SimpleFocusListWalker([])
425426

426427
player.track_changed += self.track_changed
@@ -467,24 +468,31 @@ def perform_filtering(self, char):
467468
self.app.append_cancel_action(self.end_filtering)
468469
self.filter_query = ''
469470
self._is_filtering = True
471+
self.tracks_walker[:] = self.walker
470472

471473
if char == 'backspace':
472474
self.filter_query = self.filter_query[:-1]
475+
if self.filter_query == "":
476+
self.end_filtering()
477+
return
473478
else:
474479
self.filter_query += char
475480
self.filter_box.set_text(self.filter_prefix + self.filter_query)
476481

477482
matches = self.get_filtered_items()
478483
self.filter_info.set_text('{} matches'.format(len(matches)))
479-
if matches:
480-
self.walker.set_focus(matches[0].index)
484+
self.walker[:] = matches
485+
self.walker.set_focus(0)
486+
487+
if self.app.current_page.name == 'Library':
488+
self.update_indexes()
481489

482490
def get_filtered_items(self):
483491
"""
484492
Get song items that match the search query.
485493
"""
486494
matches = []
487-
for songitem in self.walker:
495+
for songitem in self.tracks_walker:
488496
if not isinstance(songitem, SongListItem):
489497
continue
490498
if self.filter_query.lower() in songitem.full_title.lower():
@@ -499,6 +507,9 @@ def end_filtering(self):
499507
(self.list_box, ('weight', 1))
500508
]
501509
self._is_filtering = False
510+
self.filter_box.set_text('')
511+
self.filter_info.set_text('')
512+
self.walker[:] = self.tracks_walker
502513

503514
def set_placeholder(self, text):
504515
"""
@@ -557,13 +568,21 @@ def item_activated(self, songitem):
557568
Toggles track playback state or loads entire playlist
558569
that contains current track into player queue.
559570
"""
571+
page = self.app.current_page
572+
560573
if songitem.is_currently_played:
561574
player.play_pause()
562-
elif self.app.current_page.append:
575+
# There are some pages like search library where overwriting the queue
576+
# doesn't make much sense. We can also assume that someone searching
577+
# for a specific song also wants to append.
578+
elif (page.append or self._is_filtering) and page.name != 'Queue':
563579
self.item_append_requested(songitem)
564580
else:
565581
player.load_queue(self.tracks, songitem.index)
566582

583+
if self._is_filtering:
584+
self.walker[:] = self.get_filtered_items()
585+
567586
@staticmethod
568587
def item_append_requested(songitem):
569588
"""
@@ -678,13 +697,14 @@ def append_track(self, track):
678697
self.walker.append(tracks[0])
679698
self.update_indexes()
680699

681-
def remove_track(self, track):
700+
def remove_track(self, track, ):
682701
"""
683702
Remove a song item that matches *track* from this song list (if found).
684703
"""
685704
for songlistitem in self.walker:
686705
if songlistitem.track == track:
687706
self.walker.remove(songlistitem)
707+
688708
self.update_indexes()
689709

690710
def update_indexes(self):
@@ -710,55 +730,43 @@ def keypress(self, size, key):
710730

711731
return None
712732

713-
def _get_filtered(self):
714-
"""Get filtered list of items"""
715-
matches = self.get_filtered_items()
716-
_, index = self.walker.get_focus()
717-
return (matches, index)
718-
719733
def move_to_beginning(self):
720734
"""Move to the focus to beginning of the songlist"""
721-
matches, _ = self._get_filtered()
722-
self.list_box.set_focus(matches[0].index, 'below')
735+
self.list_box.set_focus(0, 'below')
723736
return False
724737

725738
def move_to_end(self):
726739
"""Move to the focus to end of the songlist"""
727-
matches, _ = self._get_filtered()
728-
self.list_box.set_focus(matches[-1].index, 'above')
740+
self.list_box.set_focus(-1, 'above')
729741
return False
730742

731743
def move_up(self):
732744
"""Move the focus an item up in the playlist"""
733-
matches, index = self._get_filtered()
734-
self.list_box.set_focus(*self.get_item(matches, index, lt))
745+
_, index = self.walker.get_focus()
746+
747+
if index is None:
748+
return False
749+
750+
if index <= 0:
751+
self.list_box.set_focus(len(self.walker) - 1, 'below')
752+
else:
753+
self.list_box.set_focus(index - 1, 'above')
754+
735755
return False
736756

737757
def move_down(self):
738758
"""Move the focus an item down in the playlist """
739-
matches, index = self._get_filtered()
740-
self.list_box.set_focus(*self.get_item(matches, index, gt))
741-
return False
742-
743-
@staticmethod
744-
def get_item(matches, current_index, callback):
745-
"""
746-
Get an item index from the matches list
747-
"""
748-
# Some not so very nice code to get to be nice and generic
749-
order = ['above', 'below']
750-
if callback is lt:
751-
order.reverse()
752-
753-
items = [item for item in matches if callback(item.index, current_index)]
759+
_, index = self.walker.get_focus()
754760

755-
# Please witness some terrible code below.
761+
if index is None:
762+
return False
756763

757-
index = -1 if callback is lt else 0
764+
if index >= len(self.walker) - 1:
765+
self.list_box.set_focus(0, 'above')
766+
else:
767+
self.list_box.set_focus(index + 1, 'below')
758768

759-
if items:
760-
return items[index].index, order[0]
761-
return matches[index].index, order[1]
769+
return False
762770

763771
def mouse_event(self, size, event, button, col, row, focus):
764772
"""

0 commit comments

Comments
 (0)