simplify code responsible for parsing song formats

functions MPD::Song::toString() and Display::Songs were
almost identical. for now the latter uses MPD::Song::toString()
implementation, which also has been simplified.
This commit is contained in:
Andrzej Rybczak
2009-08-14 04:15:34 +02:00
parent 0f53677eb6
commit cbbabee0e7
7 changed files with 35 additions and 213 deletions

View File

@@ -52,7 +52,7 @@
## %p - performer ## %p - performer
## %d - disc ## %d - disc
## %C - comment ## %C - comment
## %r - begin right align ## $R - begin right alignment
## ##
## you can also put them in { } and then it will be displayed ## you can also put them in { } and then it will be displayed
## only if all requested values are available and/or define alternate ## only if all requested values are available and/or define alternate
@@ -77,7 +77,7 @@
## Note: colors can be nested. ## Note: colors can be nested.
## ##
# #
#song_list_format = "{%a - }{%t}|{$8%f$9}%r{$3(%l)$9}" #song_list_format = "{%a - }{%t}|{$8%f$9}$R{$3(%l)$9}"
# #
#song_status_format = "{(%l) }{%a - }{%t}|{%f}" #song_status_format = "{(%l) }{%a - }{%t}|{%f}"
# #

View File

@@ -280,7 +280,7 @@ For song format you can use:
%p - performer %p - performer
%d - disc %d - disc
%C - comment %C - comment
%r - begin right alignment $R - begin right alignment
You can also put them in { } and then they will be displayed only if all requested values are available and/or define alternate value with { }|{ } e.g. {%a - %t}|{%f} will check if artist and title tags are available and if they are, display them. Otherwise it'll display filename. You can also put them in { } and then they will be displayed only if all requested values are available and/or define alternate value with { }|{ } e.g. {%a - %t}|{%f} will check if artist and title tags are available and if they are, display them. Otherwise it'll display filename.

View File

@@ -23,20 +23,6 @@
#include "helpers.h" #include "helpers.h"
#include "playlist.h" #include "playlist.h"
namespace
{
template <typename C, typename T> void ParseColors(const std::basic_string<C> &s, T &buf)
{
for (typename std::basic_string<C>::const_iterator it = s.begin(); it != s.end(); ++it)
{
if (*it == '$')
buf << Color(*++it-'0');
else
buf << *it;
}
}
}
std::string Display::Columns() std::string Display::Columns()
{ {
if (Config.columns.empty()) if (Config.columns.empty())
@@ -218,133 +204,28 @@ void Display::Songs(const MPD::Song &s, void *data, Menu<MPD::Song> *menu)
if (!s.Localized()) if (!s.Localized())
const_cast<MPD::Song *>(&s)->Localize(); const_cast<MPD::Song *>(&s)->Localize();
const std::string &song_template = data ? *static_cast<std::string *>(data) : ""; std::string line = s.toString(*static_cast<std::string *>(data));
basic_buffer<my_char_t> buf; for (std::string::const_iterator it = line.begin(); it != line.end(); ++it)
bool right = 0;
for (std::string::const_iterator it = song_template.begin(); it != song_template.end(); ++it)
{ {
while (*it == '{') if (*it == '$')
{ {
std::string tags = s.Format_ParseBraces(it, song_template.end()); if (isdigit(*++it))
if (!right)
ParseColors(tags, *menu);
else
ParseColors(TO_WSTRING(tags), buf);
}
if (it == song_template.end())
break;
if (*it != '%' && *it != '$')
{ {
if (!right)
*menu << *it;
else
buf << *it;
}
else if (*it == '%')
{
switch (*++it)
{
case 'l':
if (!right)
*menu << s.GetLength();
else
buf << TO_WSTRING(s.GetLength());
break;
case 'D':
if (!right)
*menu << s.GetDirectory();
else
buf << TO_WSTRING(s.GetDirectory());
break;
case 'f':
if (!right)
*menu << s.GetName();
else
buf << TO_WSTRING(s.GetName());
break;
case 'a':
if (!right)
*menu << s.GetArtist();
else
buf << TO_WSTRING(s.GetArtist());
break;
case 'b':
if (!right)
*menu << s.GetAlbum();
else
buf << TO_WSTRING(s.GetAlbum());
break;
case 'y':
if (!right)
*menu << s.GetDate();
else
buf << TO_WSTRING(s.GetDate());
break;
case 'n':
if (!right)
*menu << s.GetTrack();
else
buf << TO_WSTRING(s.GetTrack());
break;
case 'g':
if (!right)
*menu << s.GetGenre();
else
buf << TO_WSTRING(s.GetGenre());
break;
case 'c':
if (!right)
*menu << s.GetComposer();
else
buf << TO_WSTRING(s.GetComposer());
break;
case 'p':
if (!right)
*menu << s.GetPerformer();
else
buf << TO_WSTRING(s.GetPerformer());
break;
case 'd':
if (!right)
*menu << s.GetDisc();
else
buf << TO_WSTRING(s.GetDisc());
break;
case 'C':
if (!right)
*menu << s.GetComment();
else
buf << TO_WSTRING(s.GetComment());
break;
case 't':
if (!right)
*menu << s.GetTitle();
else
buf << TO_WSTRING(s.GetTitle());
break;
case 'r':
right = 1;
break;
default:
break;
}
}
else
{
++it;
if (!right)
*menu << Color(*it-'0'); *menu << Color(*it-'0');
else
buf << Color(*it-'0');
} }
else if (*it == 'R') // right align
}
if (right)
{ {
menu->GotoXY(menu->GetWidth()-buf.Str().length(), menu->Y()); basic_buffer<my_char_t> buf;
*menu << buf; buf << U(" ");
String2Buffer(TO_WSTRING(line.substr(it-line.begin()+1)), buf);
*menu << XY(COLS-buf.Str().length(), menu->Y()) << buf;
break;
}
else
*menu << *it;
}
else
*menu << *it;
} }
} }

View File

@@ -64,9 +64,6 @@ template <typename C> void String2Buffer(const std::basic_string<C> &s, basic_bu
{ {
switch (*it) switch (*it)
{ {
case '$':
buf << *it;
break;
case 'b': case 'b':
buf << fmtBold; buf << fmtBold;
break; break;
@@ -90,6 +87,9 @@ template <typename C> void String2Buffer(const std::basic_string<C> &s, basic_bu
break; break;
} }
break; break;
default:
buf << *it;
break;
} }
} }
} }

View File

@@ -242,13 +242,13 @@ void DefaultConfiguration(ncmpcpp_config &conf)
conf.mpd_host = "localhost"; conf.mpd_host = "localhost";
conf.empty_tag = "<empty>"; conf.empty_tag = "<empty>";
conf.song_list_columns_format = "(7f)[green]{l} (25)[cyan]{a} (40)[]{t} (30)[red]{b}"; conf.song_list_columns_format = "(7f)[green]{l} (25)[cyan]{a} (40)[]{t} (30)[red]{b}";
conf.song_list_format = "{%a - }{%t}|{$8%f$9}%r{$3(%l)$9}"; conf.song_list_format = "{{%a - }{%t}|{$8%f$9}$R{$3(%l)$9}}";
conf.song_status_format = "{(%l) }{%a - }{%t}|{%f}"; conf.song_status_format = "{{(%l) }{%a - }{%t}|{%f}}";
conf.song_window_title_format = "{%a - }{%t}|{%f}"; conf.song_window_title_format = "{{%a - }{%t}|{%f}}";
conf.song_library_format = "{%n - }{%t}|{%f}"; conf.song_library_format = "{{%n - }{%t}|{%f}}";
conf.tag_editor_album_format = "{(%y) }%b"; conf.tag_editor_album_format = "{{(%y) }%b}";
conf.new_header_first_line = "$b$1$aqqu$/a$9 {%t}|{%f} $1$atqq$/a$9$/b"; conf.new_header_first_line = "{$b$1$aqqu$/a$9 {%t}|{%f} $1$atqq$/a$9$/b}";
conf.new_header_second_line = "{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}"; conf.new_header_second_line = "{{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}}";
conf.browser_playlist_prefix << clRed << "(playlist)" << clEnd << ' '; conf.browser_playlist_prefix << clRed << "(playlist)" << clEnd << ' ';
conf.pattern = "%n - %t"; conf.pattern = "%n - %t";
conf.selected_item_prefix << clMagenta; conf.selected_item_prefix << clMagenta;

View File

@@ -289,7 +289,7 @@ void MPD::Song::SetPosition(int pos)
itsSong->pos = pos; itsSong->pos = pos;
} }
std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std::string::const_iterator end_it) const std::string MPD::Song::ParseFormat(std::string::const_iterator &it) const
{ {
std::string result; std::string result;
bool has_some_tags = 0; bool has_some_tags = 0;
@@ -298,7 +298,7 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std::
{ {
while (*it == '{') while (*it == '{')
{ {
std::string tags = Format_ParseBraces(it, end_it); std::string tags = ParseFormat(it);
if (!tags.empty()) if (!tags.empty())
{ {
has_some_tags = 1; has_some_tags = 1;
@@ -307,8 +307,6 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std::
} }
if (*it == '}') if (*it == '}')
break; break;
else if (it == end_it)
return "";
if (*it == '%') if (*it == '%')
{ {
@@ -382,7 +380,7 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std::
--brace_counter; --brace_counter;
} }
if (*++it == '|') if (*++it == '|')
return Format_ParseBraces(++it, end_it); return ParseFormat(++it);
else else
return ""; return "";
} }
@@ -405,65 +403,8 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std::
std::string MPD::Song::toString(const std::string &format) const std::string MPD::Song::toString(const std::string &format) const
{ {
std::string result; std::string::const_iterator it = format.begin();
for (std::string::const_iterator it = format.begin(); it != format.end(); ++it) return ParseFormat(it);
{
while (*it == '{')
result += Format_ParseBraces(it, format.end());
if (it == format.end())
break;
if (*it == '%')
{
switch (*++it)
{
case 'l':
result += GetLength();
break;
case 'D':
result += GetDirectory();
break;
case 'f':
result += GetName();
break;
case 'a':
result += GetArtist();
break;
case 'b':
result += GetAlbum();
break;
case 'y':
result += GetDate();
break;
case 'n':
result += GetTrack();
break;
case 'g':
result += GetGenre();
break;
case 'c':
result += GetComposer();
break;
case 'p':
result += GetPerformer();
break;
case 'd':
result += GetDisc();
break;
case 'C':
result += GetComment();
break;
case 't':
result += GetTitle();
break;
default:
break;
}
}
else
result += *it;
}
return result;
} }
MPD::Song &MPD::Song::operator=(const MPD::Song &s) MPD::Song &MPD::Song::operator=(const MPD::Song &s)

View File

@@ -92,9 +92,9 @@ namespace MPD
Song &operator=(const Song &); Song &operator=(const Song &);
static std::string ShowTime(int); static std::string ShowTime(int);
std::string Format_ParseBraces(std::string::const_iterator &it, std::string::const_iterator end_it) const;
private: private:
void SetHashAndSlash(); void SetHashAndSlash();
std::string ParseFormat(std::string::const_iterator &it) const;
mpd_Song *itsSong; mpd_Song *itsSong;
std::string itsNewName; std::string itsNewName;