Skip to content

Commit

Permalink
Initial support for custom title format
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Jan 27, 2024
1 parent d75d749 commit f743132
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/include/lib/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <string>

#include "lib/spotify/track.hpp"

namespace lib
{
/**
Expand Down Expand Up @@ -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;
};
}
1 change: 0 additions & 1 deletion lib/include/lib/spotify/track.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "lib/format.hpp"
#include "lib/spotify/entity.hpp"
#include "lib/spotify/image.hpp"

Expand Down
57 changes: 57 additions & 0 deletions lib/src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 25 additions & 0 deletions lib/test/src/formattests.cpp
Original file line number Diff line number Diff line change
@@ -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")
{
Expand Down Expand Up @@ -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");
}
}

0 comments on commit f743132

Please sign in to comment.