From 60e66b47b6748c48f9ee36e9ef80bc4b5d1a09e1 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Fri, 11 Nov 2016 20:26:42 +0100 Subject: searchable: retrieve currently applied search constraint --- src/actions.cpp | 12 ++++++------ src/browser.cpp | 17 +++++++++++------ src/browser.h | 5 +++-- src/helpers.h | 2 +- src/interfaces.h | 6 ++++-- src/media_library.cpp | 39 +++++++++++++++++++++++++-------------- src/media_library.h | 5 +++-- src/playlist.cpp | 16 +++++++++++----- src/playlist.h | 5 +++-- src/playlist_editor.cpp | 29 +++++++++++++++++++---------- src/playlist_editor.h | 5 +++-- src/regex_filter.h | 32 ++++++++++++++++++++++++++++---- src/search_engine.cpp | 17 +++++++++++------ src/search_engine.h | 5 +++-- src/sel_items_adder.cpp | 16 +++++++++++----- src/sel_items_adder.h | 5 +++-- src/statusbar.cpp | 2 +- src/tag_editor.cpp | 29 +++++++++++++++++++---------- src/tag_editor.h | 5 +++-- 19 files changed, 168 insertions(+), 84 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index aa0a96d6..f8f994f7 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -1820,12 +1820,12 @@ void SelectFoundItems::run() { auto current_pos = m_list->choice(); myScreen->activeWindow()->scroll(NC::Scroll::Home); - bool found = m_searchable->find(SearchDirection::Forward, false, false); + bool found = m_searchable->search(SearchDirection::Forward, false, false); if (found) { Statusbar::print("Searching for items..."); m_list->currentP()->setSelected(true); - while (m_searchable->find(SearchDirection::Forward, false, true)) + while (m_searchable->search(SearchDirection::Forward, false, true)) m_list->currentP()->setSelected(true); Statusbar::print("Found items selected"); } @@ -2006,7 +2006,7 @@ void NextFoundItem::run() { Searchable *w = dynamic_cast(myScreen); assert(w != nullptr); - w->find(SearchDirection::Forward, Config.wrapped_search, true); + w->search(SearchDirection::Forward, Config.wrapped_search, true); listsChangeFinisher(); } @@ -2019,7 +2019,7 @@ void PreviousFoundItem::run() { Searchable *w = dynamic_cast(myScreen); assert(w != nullptr); - w->find(SearchDirection::Backward, Config.wrapped_search, true); + w->search(SearchDirection::Backward, Config.wrapped_search, true); listsChangeFinisher(); } @@ -2959,7 +2959,7 @@ void findItem(const SearchDirection direction) Statusbar::Helpers::FindImmediately(w, direction) ); Statusbar::put() << (boost::format("Find %1%: ") % direction).str(); - constraint = wFooter->prompt(); + constraint = wFooter->prompt(w->searchConstraint()); } try @@ -2967,7 +2967,7 @@ void findItem(const SearchDirection direction) if (constraint.empty()) { Statusbar::printf("Constraint unset"); - w->clearConstraint(); + w->clearSearchConstraint(); } else { diff --git a/src/browser.cpp b/src/browser.cpp index 00d1e4d5..416ed8a0 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -244,22 +244,27 @@ bool Browser::allowsSearching() return true; } +const std::string &Browser::searchConstraint() +{ + return m_search_predicate.constraint(); +} + void Browser::setSearchConstraint(const std::string &constraint) { m_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), - std::bind(browserEntryMatcher, ph::_1, ph::_2, false) - ); + constraint, + Config.regex_type, + std::bind(browserEntryMatcher, ph::_1, ph::_2, false)); } -void Browser::clearConstraint() +void Browser::clearSearchConstraint() { m_search_predicate.clear(); } -bool Browser::find(SearchDirection direction, bool wrap, bool skip_current) +bool Browser::search(SearchDirection direction, bool wrap, bool skip_current) { - return search(w, m_search_predicate, direction, wrap, skip_current); + return ::search(w, m_search_predicate, direction, wrap, skip_current); } /***********************************************************************/ diff --git a/src/browser.h b/src/browser.h index 4eb8a614..f5b912d3 100644 --- a/src/browser.h +++ b/src/browser.h @@ -63,9 +63,10 @@ struct Browser: Screen, HasSongs, Searchable, Tabbable // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; // HasSongs implementation virtual bool itemAvailable() OVERRIDE; diff --git a/src/helpers.h b/src/helpers.h index c4e5123e..ec95cd09 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -54,7 +54,7 @@ Iterator wrappedSearch(Iterator begin, Iterator current, Iterator end, template bool search(NC::Menu &m, const PredicateT &pred, - SearchDirection direction, bool wrap, bool skip_current) + SearchDirection direction, bool wrap, bool skip_current) { bool result = false; if (pred.defined()) diff --git a/src/interfaces.h b/src/interfaces.h index 97329fb5..09a8a648 100644 --- a/src/interfaces.h +++ b/src/interfaces.h @@ -32,9 +32,11 @@ struct Searchable { virtual bool allowsSearching() = 0; + + virtual const std::string &searchConstraint() = 0; virtual void setSearchConstraint(const std::string &constraint) = 0; - virtual void clearConstraint() = 0; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) = 0; + virtual void clearSearchConstraint() = 0; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) = 0; }; struct HasActions diff --git a/src/media_library.cpp b/src/media_library.cpp index eaeb0c67..778e1631 100644 --- a/src/media_library.cpp +++ b/src/media_library.cpp @@ -538,32 +538,43 @@ bool MediaLibrary::allowsSearching() return true; } +const std::string &MediaLibrary::searchConstraint() +{ + if (isActiveWindow(Tags)) + return m_tags_search_predicate.constraint(); + else if (isActiveWindow(Albums)) + return m_albums_search_predicate.constraint(); + else if (isActiveWindow(Songs)) + return m_songs_search_predicate.constraint(); + throw std::runtime_error("no active window"); +} + void MediaLibrary::setSearchConstraint(const std::string &constraint) { if (isActiveWindow(Tags)) { m_tags_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), - TagEntryMatcher - ); + constraint, + Config.regex_type, + TagEntryMatcher); } else if (isActiveWindow(Albums)) { m_albums_search_predicate = Regex::ItemFilter( - Regex::make(constraint, Config.regex_type), - std::bind(AlbumEntryMatcher, ph::_1, ph::_2, false) - ); + constraint, + Config.regex_type, + std::bind(AlbumEntryMatcher, ph::_1, ph::_2, false)); } else if (isActiveWindow(Songs)) { m_songs_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), - SongEntryMatcher - ); + constraint, + Config.regex_type, + SongEntryMatcher); } } -void MediaLibrary::clearConstraint() +void MediaLibrary::clearSearchConstraint() { if (isActiveWindow(Tags)) m_tags_search_predicate.clear(); @@ -573,15 +584,15 @@ void MediaLibrary::clearConstraint() m_songs_search_predicate.clear(); } -bool MediaLibrary::find(SearchDirection direction, bool wrap, bool skip_current) +bool MediaLibrary::search(SearchDirection direction, bool wrap, bool skip_current) { bool result = false; if (isActiveWindow(Tags)) - result = search(Tags, m_tags_search_predicate, direction, wrap, skip_current); + result = ::search(Tags, m_tags_search_predicate, direction, wrap, skip_current); else if (isActiveWindow(Albums)) - result = search(Albums, m_albums_search_predicate, direction, wrap, skip_current); + result = ::search(Albums, m_albums_search_predicate, direction, wrap, skip_current); else if (isActiveWindow(Songs)) - result = search(Songs, m_songs_search_predicate, direction, wrap, skip_current); + result = ::search(Songs, m_songs_search_predicate, direction, wrap, skip_current); return result; } diff --git a/src/media_library.h b/src/media_library.h index dbe17b37..b7a59390 100644 --- a/src/media_library.h +++ b/src/media_library.h @@ -50,9 +50,10 @@ struct MediaLibrary: Screen, HasColumns, HasSongs, Searchable, Tab // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; // HasSongs implementation virtual bool itemAvailable() OVERRIDE; diff --git a/src/playlist.cpp b/src/playlist.cpp index 9dd9bcc3..b8994182 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -153,21 +153,27 @@ bool Playlist::allowsSearching() return true; } +const std::string &Playlist::searchConstraint() +{ + return m_search_predicate.constraint(); +} + void Playlist::setSearchConstraint(const std::string &constraint) { m_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), playlistEntryMatcher - ); + constraint, + Config.regex_type, + playlistEntryMatcher); } -void Playlist::clearConstraint() +void Playlist::clearSearchConstraint() { m_search_predicate.clear(); } -bool Playlist::find(SearchDirection direction, bool wrap, bool skip_current) +bool Playlist::search(SearchDirection direction, bool wrap, bool skip_current) { - return search(w, m_search_predicate, direction, wrap, skip_current); + return ::search(w, m_search_predicate, direction, wrap, skip_current); } /***********************************************************************/ diff --git a/src/playlist.h b/src/playlist.h index d922795b..8661d8eb 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -50,9 +50,10 @@ struct Playlist: Screen, HasSongs, Searchable, Tabbable // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; // HasSongs implementation virtual bool itemAvailable() OVERRIDE; diff --git a/src/playlist_editor.cpp b/src/playlist_editor.cpp index 0c25b520..63027024 100644 --- a/src/playlist_editor.cpp +++ b/src/playlist_editor.cpp @@ -283,25 +283,34 @@ bool PlaylistEditor::allowsSearching() return true; } +const std::string &PlaylistEditor::searchConstraint() +{ + if (isActiveWindow(Playlists)) + return m_playlists_search_predicate.constraint(); + else if (isActiveWindow(Content)) + return m_content_search_predicate.constraint(); + throw std::runtime_error("no active window"); +} + void PlaylistEditor::setSearchConstraint(const std::string &constraint) { if (isActiveWindow(Playlists)) { m_playlists_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), - PlaylistEntryMatcher - ); + constraint, + Config.regex_type, + PlaylistEntryMatcher); } else if (isActiveWindow(Content)) { m_content_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), - SongEntryMatcher - ); + constraint, + Config.regex_type, + SongEntryMatcher); } } -void PlaylistEditor::clearConstraint() +void PlaylistEditor::clearSearchConstraint() { if (isActiveWindow(Playlists)) m_playlists_search_predicate.clear(); @@ -309,13 +318,13 @@ void PlaylistEditor::clearConstraint() m_content_search_predicate.clear(); } -bool PlaylistEditor::find(SearchDirection direction, bool wrap, bool skip_current) +bool PlaylistEditor::search(SearchDirection direction, bool wrap, bool skip_current) { bool result = false; if (isActiveWindow(Playlists)) - result = search(Playlists, m_playlists_search_predicate, direction, wrap, skip_current); + result = ::search(Playlists, m_playlists_search_predicate, direction, wrap, skip_current); else if (isActiveWindow(Content)) - result = search(Content, m_content_search_predicate, direction, wrap, skip_current); + result = ::search(Content, m_content_search_predicate, direction, wrap, skip_current); return result; } diff --git a/src/playlist_editor.h b/src/playlist_editor.h index b5ff33ca..769ab65c 100644 --- a/src/playlist_editor.h +++ b/src/playlist_editor.h @@ -50,9 +50,10 @@ struct PlaylistEditor: Screen, HasColumns, HasSongs, Searchable, T // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; // HasSongs implementation virtual bool itemAvailable() OVERRIDE; diff --git a/src/regex_filter.h b/src/regex_filter.h index 8b3f3c0a..a5bb82c4 100644 --- a/src/regex_filter.h +++ b/src/regex_filter.h @@ -80,14 +80,25 @@ struct Filter typedef std::function FilterFunction; Filter() { } - Filter(Regex rx, FilterFunction filter) - : m_rx(std::move(rx)), m_filter(std::move(filter)) { } + + template + Filter(const std::string &constraint, + boost::regex_constants::syntax_option_type flags, + FilterT &&filter) + : m_rx(make(constraint, flags)) + , m_constraint(constraint) + , m_filter(std::forward(filter)) + { } void clear() { m_filter = nullptr; } + const std::string &constraint() const { + return m_constraint; + } + bool operator()(const Item &item) const { assert(defined()); return m_filter(m_rx, item.value()); @@ -100,6 +111,7 @@ struct Filter private: Regex m_rx; + std::string m_constraint; FilterFunction m_filter; }; @@ -110,14 +122,25 @@ template struct ItemFilter typedef std::function FilterFunction; ItemFilter() { } - ItemFilter(Regex rx, FilterFunction filter) - : m_rx(std::move(rx)), m_filter(std::move(filter)) { } + + template + ItemFilter(const std::string &constraint, + boost::regex_constants::syntax_option_type flags, + FilterT &&filter) + : m_rx(make(constraint, flags)) + , m_constraint(constraint) + , m_filter(std::forward(filter)) + { } void clear() { m_filter = nullptr; } + const std::string &constraint() const { + return m_constraint; + } + bool operator()(const Item &item) { return m_filter(m_rx, item); } @@ -129,6 +152,7 @@ template struct ItemFilter private: Regex m_rx; + std::string m_constraint; FilterFunction m_filter; }; diff --git a/src/search_engine.cpp b/src/search_engine.cpp index e18f4b25..ee8ad186 100644 --- a/src/search_engine.cpp +++ b/src/search_engine.cpp @@ -246,22 +246,27 @@ bool SearchEngine::allowsSearching() return w.rbegin()->value().isSong(); } +const std::string &SearchEngine::searchConstraint() +{ + return m_search_predicate.constraint(); +} + void SearchEngine::setSearchConstraint(const std::string &constraint) { m_search_predicate = Regex::ItemFilter( - Regex::make(constraint, Config.regex_type), - std::bind(SEItemEntryMatcher, ph::_1, ph::_2, false) - ); + constraint, + Config.regex_type, + std::bind(SEItemEntryMatcher, ph::_1, ph::_2, false)); } -void SearchEngine::clearConstraint() +void SearchEngine::clearSearchConstraint() { m_search_predicate.clear(); } -bool SearchEngine::find(SearchDirection direction, bool wrap, bool skip_current) +bool SearchEngine::search(SearchDirection direction, bool wrap, bool skip_current) { - return search(w, m_search_predicate, direction, wrap, skip_current); + return ::search(w, m_search_predicate, direction, wrap, skip_current); } /***********************************************************************/ diff --git a/src/search_engine.h b/src/search_engine.h index 863e3db5..c90484cd 100644 --- a/src/search_engine.h +++ b/src/search_engine.h @@ -111,9 +111,10 @@ struct SearchEngine: Screen, HasActions, HasSongs, Searchabl // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; // HasActions implementation virtual bool actionRunnable() OVERRIDE; diff --git a/src/sel_items_adder.cpp b/src/sel_items_adder.cpp index 007b3b5f..9d1a8b5c 100644 --- a/src/sel_items_adder.cpp +++ b/src/sel_items_adder.cpp @@ -202,21 +202,27 @@ bool SelectedItemsAdder::allowsSearching() return true; } +const std::string &SelectedItemsAdder::searchConstraint() +{ + return m_search_predicate.constraint(); +} + void SelectedItemsAdder::setSearchConstraint(const std::string &constraint) { m_search_predicate = Regex::ItemFilter( - Regex::make(constraint, Config.regex_type), EntryMatcher - ); + constraint, + Config.regex_type, + EntryMatcher); } -void SelectedItemsAdder::clearConstraint() +void SelectedItemsAdder::clearSearchConstraint() { m_search_predicate.clear(); } -bool SelectedItemsAdder::find(SearchDirection direction, bool wrap, bool skip_current) +bool SelectedItemsAdder::search(SearchDirection direction, bool wrap, bool skip_current) { - return search(*w, m_search_predicate, direction, wrap, skip_current); + return ::search(*w, m_search_predicate, direction, wrap, skip_current); } /***********************************************************************/ diff --git a/src/sel_items_adder.h b/src/sel_items_adder.h index 4b27ba65..dc67b097 100644 --- a/src/sel_items_adder.h +++ b/src/sel_items_adder.h @@ -55,9 +55,10 @@ struct SelectedItemsAdder: Screen> *> // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; private: void populatePlaylistSelector(BaseScreen *screen); diff --git a/src/statusbar.cpp b/src/statusbar.cpp index 9353606d..718bbccf 100644 --- a/src/statusbar.cpp +++ b/src/statusbar.cpp @@ -228,7 +228,7 @@ bool Statusbar::Helpers::FindImmediately::operator()(const char *s) if (m_w->allowsSearching() && m_s != s) { m_w->setSearchConstraint(s); - m_found = m_w->find(m_direction, Config.wrapped_search, false); + m_found = m_w->search(m_direction, Config.wrapped_search, false); if (myScreen == myPlaylist) myPlaylist->enableHighlighting(); myScreen->refreshWindow(); diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index cd4dd770..5206ca1f 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -480,25 +480,34 @@ bool TagEditor::allowsSearching() return w == Dirs || w == Tags; } +const std::string &TagEditor::searchConstraint() +{ + if (w == Dirs) + return m_directories_search_predicate.constraint(); + else if (w == Tags) + return m_songs_search_predicate.constraint(); + throw std::runtime_error("shouldn't happen due to condition in allowsSearching"); +} + void TagEditor::setSearchConstraint(const std::string &constraint) { if (w == Dirs) { m_directories_search_predicate = Regex::Filter>( - Regex::make(constraint, Config.regex_type), - std::bind(DirEntryMatcher, ph::_1, ph::_2, false) - ); + constraint, + Config.regex_type, + std::bind(DirEntryMatcher, ph::_1, ph::_2, false)); } else if (w == Tags) { m_songs_search_predicate = Regex::Filter( - Regex::make(constraint, Config.regex_type), - SongEntryMatcher - ); + constraint, + Config.regex_type, + SongEntryMatcher); } } -void TagEditor::clearConstraint() +void TagEditor::clearSearchConstraint() { if (w == Dirs) m_directories_search_predicate.clear(); @@ -506,13 +515,13 @@ void TagEditor::clearConstraint() m_songs_search_predicate.clear(); } -bool TagEditor::find(SearchDirection direction, bool wrap, bool skip_current) +bool TagEditor::search(SearchDirection direction, bool wrap, bool skip_current) { bool result = false; if (w == Dirs) - result = search(*Dirs, m_directories_search_predicate, direction, wrap, skip_current); + result = ::search(*Dirs, m_directories_search_predicate, direction, wrap, skip_current); else if (w == Tags) - result = search(*Tags, m_songs_search_predicate, direction, wrap, skip_current); + result = ::search(*Tags, m_songs_search_predicate, direction, wrap, skip_current); return result; } diff --git a/src/tag_editor.h b/src/tag_editor.h index bd72ae59..ba368c5f 100644 --- a/src/tag_editor.h +++ b/src/tag_editor.h @@ -69,9 +69,10 @@ struct TagEditor: Screen, HasActions, HasColumns, HasSongs, Search // Searchable implementation virtual bool allowsSearching() OVERRIDE; + virtual const std::string &searchConstraint() OVERRIDE; virtual void setSearchConstraint(const std::string &constraint) OVERRIDE; - virtual void clearConstraint() OVERRIDE; - virtual bool find(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; + virtual void clearSearchConstraint() OVERRIDE; + virtual bool search(SearchDirection direction, bool wrap, bool skip_current) OVERRIDE; // HasActions implementation virtual bool actionRunnable() OVERRIDE; -- cgit v1.2.3