From 18d68283cebe219cd29714449971d6c3ef5d1159 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Thu, 29 Oct 2020 09:43:43 +0100 Subject: [PATCH] Add exclude_pattern option Fixes https://github.com/ncmpcpp/ncmpcpp/issues/422 --- CHANGELOG.md | 1 + doc/config | 9 +++++++-- src/actions.cpp | 4 ++-- src/mpdpp.cpp | 14 +++++++++----- src/mpdpp.h | 4 ++-- src/screens/media_library.cpp | 4 +++- src/screens/search_engine.cpp | 8 ++++++-- src/settings.cpp | 1 + src/settings.h | 1 + 9 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d48d84dbb..fc5495955 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ for potentially improved bottom part of the spectrum visualizer in terminals with transparent background. * Add support for fetching lyrics from tags. +* Add `exclude_pattern` option to a configuration file. # ncmpcpp-0.9.2 (2021-01-24) * Revert suppression of output of all external commands as that makes e.g album diff --git a/doc/config b/doc/config index 791f2084a..739842a17 100644 --- a/doc/config +++ b/doc/config @@ -37,10 +37,15 @@ # #mpd_crossfade_time = 5 # -# Exclude pattern for random song action -# http://www.boost.org/doc/libs/1_46_1/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html +# Exclude pattern for random song action. Applied to song's file path. +# http://www.boost.org/doc/libs/1_83_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html #random_exclude_pattern = "^(temp|midi_songs).*" # +# Exclude pattern for random song/tag action, search engine, media library (but not browser!) +# Applied to song's file path. +# http://www.boost.org/doc/libs/1_83_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html +#exclude_pattern = "^(temp|incoming)/.*" +# ##### music visualizer ##### ## ## In order to make music visualizer work with MPD you need to use the fifo diff --git a/src/actions.cpp b/src/actions.cpp index eaf75febd..f2492b120 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -2250,9 +2250,9 @@ void AddRandomItems::run() { bool success; if (rnd_type == 's') - success = Mpd.AddRandomSongs(number, Config.random_exclude_pattern, Global::RNG); + success = Mpd.AddRandomSongs(number, Config.random_exclude_pattern, Config.exclude_pattern, Global::RNG); else - success = Mpd.AddRandomTag(tag_type, number, Global::RNG); + success = Mpd.AddRandomTag(tag_type, number, Config.exclude_pattern, Global::RNG); if (success) Statusbar::printf("%1% random %2%%3% added to playlist", number, tag_type_str, number == 1 ? "" : "s"); } diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index 9a92e5df4..d16ce720e 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -581,7 +581,7 @@ bool Connection::Add(const std::string &path) return result; } -bool Connection::AddRandomTag(mpd_tag_type tag, size_t number, std::mt19937 &rng) +bool Connection::AddRandomTag(mpd_tag_type tag, size_t number, const std::string &exclude_pattern, std::mt19937 &rng) { std::vector tags( std::make_move_iterator(GetList(tag)), @@ -592,6 +592,7 @@ bool Connection::AddRandomTag(mpd_tag_type tag, size_t number, std::mt19937 &rng std::shuffle(tags.begin(), tags.end(), rng); auto it = tags.begin(); + boost::regex re(exclude_pattern); for (size_t i = 0; i < number && it != tags.end(); ++i) { StartSearch(true); @@ -602,13 +603,14 @@ bool Connection::AddRandomTag(mpd_tag_type tag, size_t number, std::mt19937 &rng paths.push_back(s->getURI()); StartCommandsList(); for (const auto &path : paths) - AddSong(path); + if (exclude_pattern.empty() || !boost::regex_match(path, re)) + AddSong(path); CommitCommandsList(); } return true; } -bool Connection::AddRandomSongs(size_t number, const std::string &random_exclude_pattern, std::mt19937 &rng) +bool Connection::AddRandomSongs(size_t number, const std::string &random_exclude_pattern, const std::string &exclude_pattern, std::mt19937 &rng) { prechecksNoCommandsList(); std::vector files; @@ -620,7 +622,7 @@ bool Connection::AddRandomSongs(size_t number, const std::string &random_exclude } mpd_response_finish(m_connection.get()); checkErrors(); - + if (number > files.size()) { //if (itsErrorHandler) @@ -633,8 +635,10 @@ bool Connection::AddRandomSongs(size_t number, const std::string &random_exclude StartCommandsList(); auto it = files.begin(); boost::regex re(random_exclude_pattern); + boost::regex re2(exclude_pattern); for (size_t i = 0; i < number && it != files.end(); ++it) { - if (random_exclude_pattern.empty() || !boost::regex_match((*it), re)) { + if ((random_exclude_pattern.empty() || !boost::regex_match((*it), re)) + && (exclude_pattern.empty() || !boost::regex_match((*it), re2))) { AddSong(*it); i++; } diff --git a/src/mpdpp.h b/src/mpdpp.h index 902606d7a..ec1b56aee 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -558,8 +558,8 @@ struct Connection int AddSong(const std::string &, int = -1); // returns id of added song int AddSong(const Song &, int = -1); // returns id of added song - bool AddRandomTag(mpd_tag_type, size_t, std::mt19937 &rng); - bool AddRandomSongs(size_t number, const std::string &random_exclude_pattern, std::mt19937 &rng); + bool AddRandomTag(mpd_tag_type, size_t, const std::string &exclude_pattern, std::mt19937 &rng); + bool AddRandomSongs(size_t number, const std::string &random_exclude_pattern, const std::string &exclude_pattern, std::mt19937 &rng); bool Add(const std::string &path); void Delete(unsigned int pos); void DeleteRange(unsigned begin, unsigned end); diff --git a/src/screens/media_library.cpp b/src/screens/media_library.cpp index 2cdfceef1..6b6c24519 100644 --- a/src/screens/media_library.cpp +++ b/src/screens/media_library.cpp @@ -468,13 +468,15 @@ void MediaLibrary::update() sunfilter_songs.set(ReapplyFilter::Yes, true); auto &album = Albums.current()->value(); size_t idx = 0; + boost::regex re(Config.exclude_pattern); for (MPD::SongIterator s = getSongsFromAlbum(album), end; s != end; ++s, ++idx) { if (idx < Songs.size()) Songs[idx].value() = std::move(*s); else - Songs.addItem(std::move(*s)); + if (Config.exclude_pattern.empty() || !boost::regex_match(s->getURI(), re)) + Songs.addItem(std::move(*s)); }; if (idx < Songs.size()) Songs.resizeList(idx); diff --git a/src/screens/search_engine.cpp b/src/screens/search_engine.cpp index f0e20e0b0..e33eb4eba 100644 --- a/src/screens/search_engine.cpp +++ b/src/screens/search_engine.cpp @@ -470,8 +470,10 @@ void SearchEngine::Search() Mpd.AddSearch(MPD_TAG_DATE, itsConstraints[9]); if (!itsConstraints[10].empty()) Mpd.AddSearch(MPD_TAG_COMMENT, itsConstraints[10]); + boost::regex re(Config.exclude_pattern); for (MPD::SongIterator s = Mpd.CommitSearchSongs(), end; s != end; ++s) - w.addItem(std::move(*s)); + if (Config.exclude_pattern.empty() || !boost::regex_match(s->getURI(), re)) + w.addItem(std::move(*s)); return; } @@ -510,6 +512,7 @@ void SearchEngine::Search() } LocaleStringComparison cmp(std::locale(), Config.ignore_leading_the); + boost::regex re(Config.exclude_pattern); for (; s != end; ++s) { bool any_found = true, found = true; @@ -587,7 +590,8 @@ void SearchEngine::Search() } if (any_found && found) - w.addItem(*s); + if (Config.exclude_pattern.empty() || !boost::regex_match(s->getURI(), re)) + w.addItem(*s); } } diff --git a/src/settings.cpp b/src/settings.cpp index ddde73e07..4aca1c0f8 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -230,6 +230,7 @@ bool Configuration::read(const std::vector &config_paths, bool igno p.add("mpd_connection_timeout", &mpd_connection_timeout, "5"); p.add("mpd_crossfade_time", &crossfade_time, "5"); p.add("random_exclude_pattern", &random_exclude_pattern, ""); + p.add("exclude_pattern", &exclude_pattern, ""); p.add("visualizer_data_source", &visualizer_data_source, "/tmp/mpd.fifo", adjust_path); p.add("visualizer_output_name", &visualizer_output_name, "Visualizer feed"); p.add("visualizer_in_stereo", &visualizer_in_stereo, "yes", yes_no); diff --git a/src/settings.h b/src/settings.h index e9a8af087..0758822d3 100644 --- a/src/settings.h +++ b/src/settings.h @@ -222,6 +222,7 @@ struct Configuration std::vector screen_sequence; std::string random_exclude_pattern; + std::string exclude_pattern; SortMode browser_sort_mode; LyricsFetchers lyrics_fetchers;