screen_artist: more generic page title generator
authorMax Kellermann <max@musicpd.org>
Wed, 17 Oct 2018 10:15:15 +0000 (12:15 +0200)
committerMax Kellermann <max@musicpd.org>
Wed, 17 Oct 2018 10:33:29 +0000 (12:33 +0200)
src/TagFilter.cxx
src/TagFilter.hxx
src/TagListPage.hxx
src/screen_artist.cxx

index e89fc6b..23e3419 100644 (file)
@@ -40,3 +40,17 @@ AddConstraints(struct mpd_connection *connection,
                                              MPD_OPERATOR_DEFAULT,
                                              i.first, i.second.c_str());
 }
+
+std::string
+ToString(const TagFilter &filter) noexcept
+{
+       std::string result;
+
+       for (const auto &i : filter) {
+               if (!result.empty())
+                       result.insert(0, " - ");
+               result.insert(0, i.second);
+       }
+
+       return result;
+}
index a5aee08..7f610f3 100644 (file)
@@ -39,4 +39,8 @@ void
 AddConstraints(struct mpd_connection *connection,
               const TagFilter &filter) noexcept;
 
+gcc_pure
+std::string
+ToString(const TagFilter &filter) noexcept;
+
 #endif
index cb7b712..74360d0 100644 (file)
@@ -58,13 +58,17 @@ public:
                return filter;
        }
 
-       template<typename F, typename T>
-       void SetFilter(F &&_filter, T &&_title) noexcept {
+       template<typename F>
+       void SetFilter(F &&_filter) noexcept {
                filter = std::forward<F>(_filter);
-               title = std::forward<T>(_title);
                AddPendingEvents(~0u);
        }
 
+       template<typename T>
+       void SetTitle(T &&_title) noexcept {
+               title = std::forward<T>(_title);
+       }
+
        /**
         * Create a filter for the item below the cursor.
         */
index ea0ae5a..bf7601e 100644 (file)
 #include <assert.h>
 #include <string.h>
 
+static const char *
+MakePageTitle(char *buffer, size_t size, const char *prefix,
+             const TagFilter &filter)
+{
+       if (filter.empty())
+               return prefix;
+
+       snprintf(buffer, size, "%s: %s", prefix,
+                Utf8ToLocale(ToString(filter).c_str()).c_str());
+       return buffer;
+}
+
 class SongListPage final : public FileListPage {
        Page *const parent;
 
@@ -145,7 +157,8 @@ SongListPage::LoadSongList(struct mpdclient &c)
 void
 ArtistBrowserPage::OpenArtistList(struct mpdclient &c)
 {
-       artist_list_page.SetFilter(TagFilter{}, _("Artists"));
+       artist_list_page.SetFilter(TagFilter{});
+       artist_list_page.SetTitle(_("Artists"));
 
        SetCurrentPage(c, &artist_list_page);
 }
@@ -153,17 +166,12 @@ ArtistBrowserPage::OpenArtistList(struct mpdclient &c)
 void
 ArtistBrowserPage::OpenAlbumList(struct mpdclient &c, std::string _artist)
 {
-       const char *title = _("Albums");
+       album_list_page.SetFilter(TagFilter{{artist_list_page.GetTag(), std::move(_artist)}});
 
        char buffer[64];
-       if (!_artist.empty()) {
-               snprintf(buffer, sizeof(buffer), "%s: %s", title,
-                        Utf8ToLocale(_artist.c_str()).c_str());
-               title = buffer;
-       }
-
-       album_list_page.SetFilter(TagFilter{{artist_list_page.GetTag(), std::move(_artist)}},
-                                 title);
+       album_list_page.SetTitle(MakePageTitle(buffer, sizeof(buffer),
+                                              _("Albums"),
+                                              album_list_page.GetFilter()));
 
        SetCurrentPage(c, &album_list_page);
 }
@@ -184,27 +192,7 @@ screen_artist_init(ScreenManager &_screen, WINDOW *w, Size size)
 const char *
 SongListPage::GetTitle(char *str, size_t size) const noexcept
 {
-       const char *artist = FindTag(filter, artist_tag);
-       if (artist == nullptr)
-               artist = "?";
-
-       const char *const album = FindTag(filter, album_tag);
-
-       if (album == nullptr)
-               snprintf(str, size, "%s: %s",
-                        _("Artist"),
-                        Utf8ToLocale(artist).c_str());
-       else if (*album != '\0')
-               snprintf(str, size, "%s: %s - %s",
-                        _("Album"),
-                        Utf8ToLocale(artist).c_str(),
-                        Utf8ToLocale(album).c_str());
-       else
-               snprintf(str, size, "%s: %s",
-                        _("Artist"),
-                        Utf8ToLocale(artist).c_str());
-
-       return str;
+       return MakePageTitle(str, size, _("Songs"), filter);
 }
 
 bool