Skip to content

Commit

Permalink
Page: refactor GetTitle() to take std::span and return std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 1, 2024
1 parent c754835 commit 4550a96
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/ChatPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>) const noexcept override {
return _("Chat");
}
};
Expand Down
10 changes: 5 additions & 5 deletions src/EditPlaylistPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Options.hxx"
#include "mpdclient.hxx"
#include "screen.hxx"
#include "util/SPrintf.hxx"

#include <mpd/client.h>

Expand Down Expand Up @@ -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<char> buffer) const noexcept override;
};

static bool
Expand Down Expand Up @@ -112,14 +113,13 @@ edit_playlist_page_init(ScreenManager &_screen, const Window window, Size size)
return std::make_unique<EditPlaylistPage>(_screen, window, size);
}

const char *
EditPlaylistPage::GetTitle(char *str, size_t size) const noexcept
std::string_view
EditPlaylistPage::GetTitle(std::span<char> 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
Expand Down
14 changes: 7 additions & 7 deletions src/FileBrowserPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "screen_client.hxx"
#include "Command.hxx"
#include "Options.hxx"
#include "util/SPrintf.hxx"
#include "util/UriUtil.hxx"

#include <mpd/client.h>
Expand Down Expand Up @@ -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<char> buffer) const noexcept override;
};

static void
Expand Down Expand Up @@ -270,8 +271,8 @@ screen_file_init(ScreenManager &_screen, const Window window, Size size) noexcep
return std::make_unique<FileBrowserPage>(_screen, window, size);
}

const char *
FileBrowserPage::GetTitle(char *str, size_t size) const noexcept
std::string_view
FileBrowserPage::GetTitle(std::span<char> buffer) const noexcept
{
const char *path = nullptr, *prev = nullptr, *slash = current_path.c_str();

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/HelpPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>) const noexcept override {
return _("Help");
}
};
Expand Down
17 changes: 8 additions & 9 deletions src/KeyDefPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> buffer) const noexcept override;

private:
/* virtual methods from class ListText */
Expand Down Expand Up @@ -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<char> 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
Expand Down Expand Up @@ -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<char> buffer) const noexcept override;

private:
/* virtual methods from class ListText */
Expand Down Expand Up @@ -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<char>) const noexcept
{
return _("Edit key bindings");
}
Expand Down
20 changes: 10 additions & 10 deletions src/LibraryPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mpdclient.hxx"
#include "filelist.hxx"
#include "Options.hxx"
#include "util/SPrintf.hxx"

#include <list>
#include <string>
Expand All @@ -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<char> 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 {
Expand Down Expand Up @@ -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<char> buffer) const noexcept override;
};

void
Expand Down Expand Up @@ -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()));

Expand All @@ -201,10 +201,10 @@ InitLibraryPage(ScreenManager &_screen, const Window window, Size size)
return std::make_unique<ArtistBrowserPage>(_screen, window, size);
}

const char *
SongListPage::GetTitle(char *str, size_t size) const noexcept
std::string_view
SongListPage::GetTitle(std::span<char> buffer) const noexcept
{
return MakePageTitle(str, size, _("Songs"), filter);
return MakePageTitle(buffer, _("Songs"), filter);
}

bool
Expand Down
29 changes: 14 additions & 15 deletions src/LyricsPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "TextPage.hxx"
#include "screen_utils.hxx"
#include "ncu.hxx"
#include "util/SPrintf.hxx"
#include "util/StringAPI.hxx"

#include <string>
Expand Down Expand Up @@ -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<char> buffer) const noexcept override;

private:
/* virtual methods from class PluginResponseHandler */
Expand Down Expand Up @@ -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<char> 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");
}
Expand Down
6 changes: 3 additions & 3 deletions src/OutputsPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> buffer) const noexcept override;

/* virtual methods from class ListRenderer */
void PaintListItem(Window window, unsigned i, unsigned y, unsigned width,
Expand Down Expand Up @@ -351,8 +351,8 @@ outputs_init(ScreenManager &screen, const Window window, Size size)
return std::make_unique<OutputsPage>(screen, window, size);
}

const char *
OutputsPage::GetTitle(char *, size_t) const noexcept
std::string_view
OutputsPage::GetTitle(std::span<char>) const noexcept
{
return _("Outputs");
}
Expand Down
11 changes: 4 additions & 7 deletions src/Page.hxx
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -11,8 +10,8 @@
#include <curses.h>

#include <utility>

#include <stddef.h>
#include <span>
#include <string_view>

enum class Command : unsigned;
struct mpdclient;
Expand Down Expand Up @@ -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<char> buffer) const noexcept = 0;
};

#endif
8 changes: 4 additions & 4 deletions src/ProxyPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> buffer) const noexcept
{
return current_page != nullptr
? current_page->GetTitle(s, size)
: "";
? current_page->GetTitle(buffer)
: std::string_view{};
}
2 changes: 1 addition & 1 deletion src/ProxyPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> buffer) const noexcept override;
};
10 changes: 5 additions & 5 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<char> buffer) const noexcept override;
};

const struct mpd_song *
Expand Down Expand Up @@ -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<char> 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
Expand Down
Loading

0 comments on commit 4550a96

Please sign in to comment.