From f743132ff4ef653d0904ae70865c5bbeeb84a1a5 Mon Sep 17 00:00:00 2001 From: kraxarn Date: Sat, 27 Jan 2024 19:47:05 +0100 Subject: [PATCH] Initial support for custom title format --- lib/include/lib/format.hpp | 10 ++++++ lib/include/lib/spotify/track.hpp | 1 - lib/src/format.cpp | 57 +++++++++++++++++++++++++++++++ lib/test/src/formattests.cpp | 25 ++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/lib/include/lib/format.hpp b/lib/include/lib/format.hpp index 4f95e956..f7c9e4fe 100644 --- a/lib/include/lib/format.hpp +++ b/lib/include/lib/format.hpp @@ -2,6 +2,8 @@ #include +#include "lib/spotify/track.hpp" + namespace lib { /** @@ -39,5 +41,13 @@ namespace lib * @param count Amount */ static auto count(unsigned int count) -> std::string; + + /** + * \brief Format title template + * \param track Track to format for + * \param format Template format + * \return Formatted title + */ + static auto title(const spt::track &track, const std::string &format) -> std::string; }; } diff --git a/lib/include/lib/spotify/track.hpp b/lib/include/lib/spotify/track.hpp index 9a53ec2c..2bd886f5 100644 --- a/lib/include/lib/spotify/track.hpp +++ b/lib/include/lib/spotify/track.hpp @@ -1,6 +1,5 @@ #pragma once -#include "lib/format.hpp" #include "lib/spotify/entity.hpp" #include "lib/spotify/image.hpp" diff --git a/lib/src/format.cpp b/lib/src/format.cpp index 617893c7..05af57da 100644 --- a/lib/src/format.cpp +++ b/lib/src/format.cpp @@ -64,3 +64,60 @@ auto lib::format::count(unsigned int count) -> std::string return lib::fmt::format("{}", count); } + +auto lib::format::title(const spt::track &track, const std::string &format) -> std::string +{ + std::string result; + size_t start_index = 0; + + while (true) + { + const auto prev_start_index = start_index; + start_index = format.find('{', start_index); + result.append(format.substr(prev_start_index, start_index - prev_start_index)); + + if (start_index == std::string::npos) + { + break; + } + + const auto end_index = format.find('}', start_index); + if (end_index == std::string::npos) + { + return format; + } + + const auto part = format.substr(start_index, end_index - start_index + 1); + + if (part == "{track}") + { + result.append(track.name); + } + else if (part == "{artist}") + { + if (!track.artists.empty()) + { + result.append(track.artists.at(0).name); + } + } + else if (part == "{artists}") + { + for (size_t i = 0; i < track.artists.size(); i++) + { + result.append(track.artists.at(i).name); + if (i < track.artists.size() - 1) + { + result.append(", "); + } + } + } + else + { + result.append(part); + } + + start_index = end_index + 1; + } + + return result; +} diff --git a/lib/test/src/formattests.cpp b/lib/test/src/formattests.cpp index 0dee9560..ed4ce79b 100644 --- a/lib/test/src/formattests.cpp +++ b/lib/test/src/formattests.cpp @@ -1,5 +1,7 @@ #include "thirdparty/doctest.h" #include "lib/format.hpp" +#include "lib/spotify/artist.hpp" +#include "lib/spotify/track.hpp" TEST_CASE("fmt::format") { @@ -27,4 +29,27 @@ TEST_CASE("fmt::format") CHECK_EQ(lib::format::size(1000000), "1 MB"); CHECK_EQ(lib::format::size(1000000000), "1 GB"); } + + SUBCASE("title") + { + lib::spt::track track; + track.name = "track"; + + lib::spt::artist artist1; + artist1.name = "artist1"; + track.artists.push_back(artist1); + + lib::spt::artist artist2; + artist2.name = "artist2"; + track.artists.push_back(artist2); + + CHECK_EQ(lib::format::title(track, "{track}"), "track"); + CHECK_EQ(lib::format::title(track, "{track"), "{track"); + CHECK_EQ(lib::format::title(track, "prefix - {track}"), "prefix - track"); + CHECK_EQ(lib::format::title(track, "{track} - suffix"), "track - suffix"); + CHECK_EQ(lib::format::title(track, "{track} - {invalid}"), "track - {invalid}"); + + CHECK_EQ(lib::format::title(track, "{artist} - {track}"), "artist1 - track"); + CHECK_EQ(lib::format::title(track, "{artists} - {track}"), "artist1, artist2 - track"); + } }