From 4550a96b9451e8d0e98f31c2b187b421484f2f1c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 1 Sep 2024 12:59:10 +0200 Subject: [PATCH] Page: refactor GetTitle() to take std::span and return std::string_view --- src/ChatPage.cxx | 2 +- src/EditPlaylistPage.cxx | 10 +++++----- src/FileBrowserPage.cxx | 14 +++++++------- src/HelpPage.cxx | 2 +- src/KeyDefPage.cxx | 17 ++++++++--------- src/LibraryPage.cxx | 20 ++++++++++---------- src/LyricsPage.cxx | 29 ++++++++++++++--------------- src/OutputsPage.cxx | 6 +++--- src/Page.hxx | 11 ++++------- src/ProxyPage.cxx | 8 ++++---- src/ProxyPage.hxx | 2 +- src/QueuePage.cxx | 10 +++++----- src/SearchPage.cxx | 19 ++++++++----------- src/SongPage.cxx | 7 +++---- src/TabBar.cxx | 8 ++++---- src/TabBar.hxx | 4 +++- src/TagListPage.cxx | 6 +++--- src/TagListPage.hxx | 2 +- src/TitleBar.cxx | 2 +- src/TitleBar.hxx | 9 ++++----- src/screen_paint.cxx | 6 +----- 21 files changed, 91 insertions(+), 103 deletions(-) diff --git a/src/ChatPage.cxx b/src/ChatPage.cxx index 1e6ec112..a6822580 100644 --- a/src/ChatPage.cxx +++ b/src/ChatPage.cxx @@ -45,7 +45,7 @@ class ChatPage final : public TextPage { void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *, size_t) const noexcept override { + std::string_view GetTitle(std::span) const noexcept override { return _("Chat"); } }; diff --git a/src/EditPlaylistPage.cxx b/src/EditPlaylistPage.cxx index 3b3a722b..ce3bc1f1 100644 --- a/src/EditPlaylistPage.cxx +++ b/src/EditPlaylistPage.cxx @@ -12,6 +12,7 @@ #include "Options.hxx" #include "mpdclient.hxx" #include "screen.hxx" +#include "util/SPrintf.hxx" #include @@ -50,7 +51,7 @@ class EditPlaylistPage final : public FileListPage { bool OnMouse(struct mpdclient &c, Point p, mmask_t bstate) override; #endif - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; }; static bool @@ -112,14 +113,13 @@ edit_playlist_page_init(ScreenManager &_screen, const Window window, Size size) return std::make_unique(_screen, window, size); } -const char * -EditPlaylistPage::GetTitle(char *str, size_t size) const noexcept +std::string_view +EditPlaylistPage::GetTitle(std::span buffer) const noexcept { if (name.empty()) return _("Playlist"); - snprintf(str, size, "%s: %s", _("Playlist"), name.c_str()); - return str; + return SPrintf(buffer, "%s: %s", _("Playlist"), name.c_str()); } void diff --git a/src/FileBrowserPage.cxx b/src/FileBrowserPage.cxx index ac2530bb..39f358a4 100644 --- a/src/FileBrowserPage.cxx +++ b/src/FileBrowserPage.cxx @@ -16,6 +16,7 @@ #include "screen_client.hxx" #include "Command.hxx" #include "Options.hxx" +#include "util/SPrintf.hxx" #include "util/UriUtil.hxx" #include @@ -68,7 +69,7 @@ class FileBrowserPage final : public FileListPage { /* virtual methods from class Page */ void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; }; static void @@ -270,8 +271,8 @@ screen_file_init(ScreenManager &_screen, const Window window, Size size) noexcep return std::make_unique(_screen, window, size); } -const char * -FileBrowserPage::GetTitle(char *str, size_t size) const noexcept +std::string_view +FileBrowserPage::GetTitle(std::span buffer) const noexcept { const char *path = nullptr, *prev = nullptr, *slash = current_path.c_str(); @@ -285,10 +286,9 @@ FileBrowserPage::GetTitle(char *str, size_t size) const noexcept /* fall back to full path */ path = current_path.c_str(); - snprintf(str, size, "%s: %s", - /* translators: caption of the browser screen */ - _("Browse"), Utf8ToLocale(path).c_str()); - return str; + return SPrintf(buffer, "%s: %s", + /* translators: caption of the browser screen */ + _("Browse"), Utf8ToLocale(path).c_str()); } void diff --git a/src/HelpPage.cxx b/src/HelpPage.cxx index 6ffb7dbf..d9efb24e 100644 --- a/src/HelpPage.cxx +++ b/src/HelpPage.cxx @@ -229,7 +229,7 @@ class HelpPage final : public ListPage, ListRenderer, ListText { void Paint() const noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *, size_t) const noexcept override { + std::string_view GetTitle(std::span) const noexcept override { return _("Help"); } }; diff --git a/src/KeyDefPage.cxx b/src/KeyDefPage.cxx index 302a4398..834da6e3 100644 --- a/src/KeyDefPage.cxx +++ b/src/KeyDefPage.cxx @@ -111,7 +111,7 @@ class CommandKeysPage final : public ListPage, ListText { void OnOpen(struct mpdclient &c) noexcept override; void Paint() const noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; private: /* virtual methods from class ListText */ @@ -228,12 +228,11 @@ CommandKeysPage::OnOpen([[maybe_unused]] struct mpdclient &c) noexcept // TODO } -const char * -CommandKeysPage::GetTitle(char *str, size_t size) const noexcept +std::string_view +CommandKeysPage::GetTitle(std::span buffer) const noexcept { - snprintf(str, size, _("Edit keys for %s"), - get_key_command_name(Command(subcmd))); - return str; + return SPrintf(buffer, _("Edit keys for %s"), + get_key_command_name(Command(subcmd))); } void @@ -347,7 +346,7 @@ class CommandListPage final : public ListPage, ListText { void OnOpen(struct mpdclient &c) noexcept override; void Paint() const noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; private: /* virtual methods from class ListText */ @@ -446,8 +445,8 @@ CommandListPage::OnOpen([[maybe_unused]] struct mpdclient &c) noexcept lw.SetLength(command_length()); } -const char * -CommandListPage::GetTitle(char *, size_t) const noexcept +std::string_view +CommandListPage::GetTitle(std::span) const noexcept { return _("Edit key bindings"); } diff --git a/src/LibraryPage.cxx b/src/LibraryPage.cxx index 33374153..062e7b11 100644 --- a/src/LibraryPage.cxx +++ b/src/LibraryPage.cxx @@ -12,6 +12,7 @@ #include "mpdclient.hxx" #include "filelist.hxx" #include "Options.hxx" +#include "util/SPrintf.hxx" #include #include @@ -36,16 +37,15 @@ GetTagPlural(enum mpd_tag_type tag) noexcept } } -static const char * -MakePageTitle(char *buffer, size_t size, const char *prefix, +static std::string_view +MakePageTitle(std::span buffer, 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; + return SPrintf(buffer, "%s: %s", prefix, + Utf8ToLocale(ToString(filter).c_str()).c_str()); } class SongListPage final : public FileListPage { @@ -80,7 +80,7 @@ class SongListPage final : public FileListPage { /* virtual methods from class Page */ void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; }; void @@ -181,7 +181,7 @@ ArtistBrowserPage::OpenTagPage(struct mpdclient &c, page->SetFilter(std::move(filter)); char buffer[64]; - page->SetTitle(MakePageTitle(buffer, sizeof(buffer), + page->SetTitle(MakePageTitle(buffer, _("Albums"), page->GetFilter())); @@ -201,10 +201,10 @@ InitLibraryPage(ScreenManager &_screen, const Window window, Size size) return std::make_unique(_screen, window, size); } -const char * -SongListPage::GetTitle(char *str, size_t size) const noexcept +std::string_view +SongListPage::GetTitle(std::span buffer) const noexcept { - return MakePageTitle(str, size, _("Songs"), filter); + return MakePageTitle(buffer, _("Songs"), filter); } bool diff --git a/src/LyricsPage.cxx b/src/LyricsPage.cxx index 0a8cd067..7618f01b 100644 --- a/src/LyricsPage.cxx +++ b/src/LyricsPage.cxx @@ -17,6 +17,7 @@ #include "TextPage.hxx" #include "screen_utils.hxx" #include "ncu.hxx" +#include "util/SPrintf.hxx" #include "util/StringAPI.hxx" #include @@ -113,7 +114,7 @@ class LyricsPage final : public TextPage, PluginResponseHandler { void OnOpen(struct mpdclient &c) noexcept override; void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *, size_t) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; private: /* virtual methods from class PluginResponseHandler */ @@ -308,28 +309,26 @@ LyricsPage::Update(struct mpdclient &c, unsigned) noexcept MaybeLoad(c.GetPlayingSong()); } -const char * -LyricsPage::GetTitle(char *str, size_t size) const noexcept +std::string_view +LyricsPage::GetTitle(std::span buffer) const noexcept { if (plugin_cycle != nullptr) { - snprintf(str, size, "%s (%s)", - _("Lyrics"), - /* translators: this message is displayed - while data is retrieved */ - _("loading...")); - return str; + return SPrintf(buffer, "%s (%s)", + _("Lyrics"), + /* translators: this message is displayed + while data is retrieved */ + _("loading...")); } else if (artist != nullptr && title != nullptr && !IsEmpty()) { - int n; - n = snprintf(str, size, "%s: %s - %s", + std::size_t n; + n = snprintf(buffer.data(), buffer.size(), "%s: %s - %s", _("Lyrics"), artist, title); if (options.lyrics_show_plugin && !plugin_name.empty() && - (unsigned int) n < size - 1) - snprintf(str + n, size - n, " (%s)", - plugin_name.c_str()); + n < buffer.size() - 1) + n += snprintf(buffer.data() + n, buffer.size() - n, " (%s)", plugin_name.c_str()); - return str; + return {buffer.data(), n}; } else return _("Lyrics"); } diff --git a/src/OutputsPage.cxx b/src/OutputsPage.cxx index 2f2fff9c..2d03b2ef 100644 --- a/src/OutputsPage.cxx +++ b/src/OutputsPage.cxx @@ -129,7 +129,7 @@ class OutputsPage final : public ListPage, ListRenderer { void Paint() const noexcept override; void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; /* virtual methods from class ListRenderer */ void PaintListItem(Window window, unsigned i, unsigned y, unsigned width, @@ -351,8 +351,8 @@ outputs_init(ScreenManager &screen, const Window window, Size size) return std::make_unique(screen, window, size); } -const char * -OutputsPage::GetTitle(char *, size_t) const noexcept +std::string_view +OutputsPage::GetTitle(std::span) const noexcept { return _("Outputs"); } diff --git a/src/Page.hxx b/src/Page.hxx index ed250b5b..2a8853f5 100644 --- a/src/Page.hxx +++ b/src/Page.hxx @@ -1,8 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef NCMPC_PAGE_HXX -#define NCMPC_PAGE_HXX +#pragma once #include "config.h" #include "Point.hxx" @@ -11,8 +10,8 @@ #include #include - -#include +#include +#include enum class Command : unsigned; struct mpdclient; @@ -111,7 +110,5 @@ public: #endif [[gnu::pure]] - virtual const char *GetTitle(char *s, size_t size) const noexcept = 0; + virtual std::string_view GetTitle(std::span buffer) const noexcept = 0; }; - -#endif diff --git a/src/ProxyPage.cxx b/src/ProxyPage.cxx index 5733f15e..9ac0c978 100644 --- a/src/ProxyPage.cxx +++ b/src/ProxyPage.cxx @@ -97,10 +97,10 @@ ProxyPage::OnMouse(struct mpdclient &c, Point p, mmask_t bstate) } #endif -const char * -ProxyPage::GetTitle(char *s, size_t size) const noexcept +std::string_view +ProxyPage::GetTitle(std::span buffer) const noexcept { return current_page != nullptr - ? current_page->GetTitle(s, size) - : ""; + ? current_page->GetTitle(buffer) + : std::string_view{}; } diff --git a/src/ProxyPage.hxx b/src/ProxyPage.hxx index ee3c4660..232d24d8 100644 --- a/src/ProxyPage.hxx +++ b/src/ProxyPage.hxx @@ -49,5 +49,5 @@ public: bool OnMouse(struct mpdclient &c, Point p, mmask_t bstate) override; #endif - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; }; diff --git a/src/QueuePage.cxx b/src/QueuePage.cxx index a7f4ed37..8f93ad17 100644 --- a/src/QueuePage.cxx +++ b/src/QueuePage.cxx @@ -27,6 +27,7 @@ #include "LyricsPage.hxx" #include "db_completion.hxx" #include "event/CoarseTimerEvent.hxx" +#include "util/SPrintf.hxx" #ifndef NCMPC_MINI #include "hscroll.hxx" @@ -138,7 +139,7 @@ class QueuePage final : public ListPage, ListRenderer, ListText { bool OnMouse(struct mpdclient &c, Point p, mmask_t bstate) override; #endif - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; }; const struct mpd_song * @@ -368,14 +369,13 @@ QueuePage::OnClose() noexcept #endif } -const char * -QueuePage::GetTitle(char *str, size_t size) const noexcept +std::string_view +QueuePage::GetTitle(std::span buffer) const noexcept { if (connection_name.empty()) return _("Queue"); - snprintf(str, size, _("Queue on %s"), connection_name.c_str()); - return str; + return SPrintf(buffer, _("Queue on %s"), connection_name.c_str()); } void diff --git a/src/SearchPage.cxx b/src/SearchPage.cxx index bd2ae383..8a0dad25 100644 --- a/src/SearchPage.cxx +++ b/src/SearchPage.cxx @@ -119,7 +119,7 @@ class SearchPage final : public FileListPage { void Paint() const noexcept override; void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; }; /* search info */ @@ -438,21 +438,18 @@ SearchPage::Paint() const noexcept } } -const char * -SearchPage::GetTitle(char *str, size_t size) const noexcept +std::string_view +SearchPage::GetTitle(std::span buffer) const noexcept { if (advanced_search_mode && !pattern.empty()) - snprintf(str, size, "%s '%s'", _("Search"), pattern.c_str()); + return SPrintf(buffer, "%s '%s'", _("Search"), pattern.c_str()); else if (!pattern.empty()) - snprintf(str, size, - "%s '%s' [%s]", - _("Search"), - pattern.c_str(), - my_gettext(mode[options.search_mode].label)); + return SPrintf(buffer, "%s '%s' [%s]", + _("Search"), + pattern.c_str(), + my_gettext(mode[options.search_mode].label)); else return _("Search"); - - return str; } void diff --git a/src/SongPage.cxx b/src/SongPage.cxx index 6f570a50..841d05b5 100644 --- a/src/SongPage.cxx +++ b/src/SongPage.cxx @@ -141,7 +141,7 @@ class SongPage final : public ListPage, ListText { void Paint() const noexcept override; void Update(struct mpdclient &c, unsigned events) noexcept override; bool OnCommand(struct mpdclient &c, Command cmd) override; - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; private: /* virtual methods from class ListText */ @@ -191,9 +191,8 @@ screen_song_init(ScreenManager &_screen, const Window window, Size size) noexcep return std::make_unique(_screen, window, size); } -const char * -SongPage::GetTitle([[maybe_unused]] char *str, - [[maybe_unused]] size_t size) const noexcept +std::string_view +SongPage::GetTitle(std::span) const noexcept { return _("Song viewer"); } diff --git a/src/TabBar.cxx b/src/TabBar.cxx index 1434636c..8e4579e5 100644 --- a/src/TabBar.cxx +++ b/src/TabBar.cxx @@ -11,7 +11,7 @@ #include "Window.hxx" static void -PaintPageTab(const Window window, Command cmd, const char *label, bool selected) noexcept +PaintPageTab(const Window window, Command cmd, std::string_view label, bool selected) noexcept { SelectStyle(window, selected ? Style::TITLE : Style::TITLE_BOLD); if (selected) @@ -37,18 +37,18 @@ PaintPageTab(const Window window, Command cmd, const char *label, bool selected) void PaintTabBar(const Window window, const PageMeta ¤t_page_meta, - const char *current_page_title) noexcept + std::string_view current_page_title) noexcept { for (unsigned i = 0;; ++i) { const auto *page = GetPageMeta(i); if (page == nullptr) break; - const char *title = nullptr; + std::string_view title{}; if (page == ¤t_page_meta) title = current_page_title; - if (title == nullptr) + if (title.data() == nullptr) title = my_gettext(page->title); PaintPageTab(window, page->command, title, diff --git a/src/TabBar.hxx b/src/TabBar.hxx index 24e4e953..eab379ed 100644 --- a/src/TabBar.hxx +++ b/src/TabBar.hxx @@ -3,9 +3,11 @@ #pragma once +#include + struct Window; struct PageMeta; void PaintTabBar(Window window, const PageMeta ¤t_page_meta, - const char *current_page_title) noexcept; + std::string_view current_page_title) noexcept; diff --git a/src/TagListPage.cxx b/src/TagListPage.cxx index 420a97d0..85725407 100644 --- a/src/TagListPage.cxx +++ b/src/TagListPage.cxx @@ -136,10 +136,10 @@ TagListPage::Paint() const noexcept lw.Paint(*this); } -const char * -TagListPage::GetTitle(char *, size_t) const noexcept +std::string_view +TagListPage::GetTitle(std::span) const noexcept { - return title.c_str(); + return title; } void diff --git a/src/TagListPage.hxx b/src/TagListPage.hxx index 1f269261..92f30741 100644 --- a/src/TagListPage.hxx +++ b/src/TagListPage.hxx @@ -98,7 +98,7 @@ public: mmask_t bstate) override; #endif - const char *GetTitle(char *s, size_t size) const noexcept override; + std::string_view GetTitle(std::span buffer) const noexcept override; /* virtual methods from class ListRenderer */ void PaintListItem(Window window, unsigned i, unsigned y, unsigned width, diff --git a/src/TitleBar.cxx b/src/TitleBar.cxx index 07cc41b5..3f280ac4 100644 --- a/src/TitleBar.cxx +++ b/src/TitleBar.cxx @@ -59,7 +59,7 @@ TitleBar::Update(const struct mpd_status *status) noexcept void TitleBar::Paint(const PageMeta ¤t_page_meta, - const char *title) const noexcept + std::string_view title) const noexcept { window.MoveCursor({0, 0}); window.ClearToEol(); diff --git a/src/TitleBar.hxx b/src/TitleBar.hxx index 001f0d4a..83f4e920 100644 --- a/src/TitleBar.hxx +++ b/src/TitleBar.hxx @@ -1,11 +1,12 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef NCMPC_TITLE_BAR_H -#define NCMPC_TITLE_BAR_H +#pragma once #include "Window.hxx" +#include + struct mpd_status; struct PageMeta; @@ -25,7 +26,5 @@ public: void OnResize(unsigned width) noexcept; void Update(const struct mpd_status *status) noexcept; void Paint(const PageMeta ¤t_page_meta, - const char *title) const noexcept; + std::string_view title) const noexcept; }; - -#endif diff --git a/src/screen_paint.cxx b/src/screen_paint.cxx index 42c3536a..6c5624aa 100644 --- a/src/screen_paint.cxx +++ b/src/screen_paint.cxx @@ -5,14 +5,10 @@ #include "Page.hxx" #include "Options.hxx" -#include - void ScreenManager::PaintTopWindow() noexcept { - const char *title = current_page->second->GetTitle(buf, buf_size); - assert(title != nullptr); - + const auto title = current_page->second->GetTitle({buf, buf_size}); title_bar.Paint(GetCurrentPageMeta(), title); }