From cbbabee0e78101bbd026f6b8be92e0aad77eaad9 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Fri, 14 Aug 2009 04:15:34 +0200 Subject: [PATCH] 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. --- doc/config | 4 +- doc/ncmpcpp.1 | 2 +- src/display.cpp | 151 +++++------------------------------------------ src/helpers.h | 6 +- src/settings.cpp | 14 ++--- src/song.cpp | 69 ++-------------------- src/song.h | 2 +- 7 files changed, 35 insertions(+), 213 deletions(-) diff --git a/doc/config b/doc/config index 77475c8c..bbc712dd 100644 --- a/doc/config +++ b/doc/config @@ -52,7 +52,7 @@ ## %p - performer ## %d - disc ## %C - comment -## %r - begin right align +## $R - begin right alignment ## ## you can also put them in { } and then it will be displayed ## only if all requested values are available and/or define alternate @@ -77,7 +77,7 @@ ## 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}" # diff --git a/doc/ncmpcpp.1 b/doc/ncmpcpp.1 index 6479f6c2..9db900cb 100644 --- a/doc/ncmpcpp.1 +++ b/doc/ncmpcpp.1 @@ -280,7 +280,7 @@ For song format you can use: %p - performer %d - disc %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. diff --git a/src/display.cpp b/src/display.cpp index a700a68b..0689045c 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -23,20 +23,6 @@ #include "helpers.h" #include "playlist.h" -namespace -{ - template void ParseColors(const std::basic_string &s, T &buf) - { - for (typename std::basic_string::const_iterator it = s.begin(); it != s.end(); ++it) - { - if (*it == '$') - buf << Color(*++it-'0'); - else - buf << *it; - } - } -} - std::string Display::Columns() { if (Config.columns.empty()) @@ -218,133 +204,28 @@ void Display::Songs(const MPD::Song &s, void *data, Menu *menu) if (!s.Localized()) const_cast(&s)->Localize(); - const std::string &song_template = data ? *static_cast(data) : ""; - basic_buffer buf; - bool right = 0; - - for (std::string::const_iterator it = song_template.begin(); it != song_template.end(); ++it) + std::string line = s.toString(*static_cast(data)); + for (std::string::const_iterator it = line.begin(); it != line.end(); ++it) { - while (*it == '{') + if (*it == '$') { - std::string tags = s.Format_ParseBraces(it, song_template.end()); - 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) + if (isdigit(*++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; + *menu << Color(*it-'0'); } + else if (*it == 'R') // right align + { + basic_buffer 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 - { - ++it; - if (!right) - *menu << Color(*it-'0'); - else - buf << Color(*it-'0'); - } - - } - if (right) - { - menu->GotoXY(menu->GetWidth()-buf.Str().length(), menu->Y()); - *menu << buf; + *menu << *it; } } diff --git a/src/helpers.h b/src/helpers.h index 55980d5d..1d5d7b67 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -64,9 +64,6 @@ template void String2Buffer(const std::basic_string &s, basic_bu { switch (*it) { - case '$': - buf << *it; - break; case 'b': buf << fmtBold; break; @@ -90,6 +87,9 @@ template void String2Buffer(const std::basic_string &s, basic_bu break; } break; + default: + buf << *it; + break; } } } diff --git a/src/settings.cpp b/src/settings.cpp index 6875a7fc..da88e69d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -242,13 +242,13 @@ void DefaultConfiguration(ncmpcpp_config &conf) conf.mpd_host = "localhost"; conf.empty_tag = ""; 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_status_format = "{(%l) }{%a - }{%t}|{%f}"; - conf.song_window_title_format = "{%a - }{%t}|{%f}"; - conf.song_library_format = "{%n - }{%t}|{%f}"; - 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_second_line = "{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}"; + conf.song_list_format = "{{%a - }{%t}|{$8%f$9}$R{$3(%l)$9}}"; + conf.song_status_format = "{{(%l) }{%a - }{%t}|{%f}}"; + conf.song_window_title_format = "{{%a - }{%t}|{%f}}"; + conf.song_library_format = "{{%n - }{%t}|{%f}}"; + 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_second_line = "{{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}}"; conf.browser_playlist_prefix << clRed << "(playlist)" << clEnd << ' '; conf.pattern = "%n - %t"; conf.selected_item_prefix << clMagenta; diff --git a/src/song.cpp b/src/song.cpp index 74861d3f..c82fef50 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -289,7 +289,7 @@ void MPD::Song::SetPosition(int 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; bool has_some_tags = 0; @@ -298,7 +298,7 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std:: { while (*it == '{') { - std::string tags = Format_ParseBraces(it, end_it); + std::string tags = ParseFormat(it); if (!tags.empty()) { has_some_tags = 1; @@ -307,8 +307,6 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std:: } if (*it == '}') break; - else if (it == end_it) - return ""; if (*it == '%') { @@ -382,7 +380,7 @@ std::string MPD::Song::Format_ParseBraces(std::string::const_iterator &it, std:: --brace_counter; } if (*++it == '|') - return Format_ParseBraces(++it, end_it); + return ParseFormat(++it); else 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 result; - for (std::string::const_iterator it = format.begin(); it != format.end(); ++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; + std::string::const_iterator it = format.begin(); + return ParseFormat(it); } MPD::Song &MPD::Song::operator=(const MPD::Song &s) diff --git a/src/song.h b/src/song.h index bb0ca0b3..6b66915c 100644 --- a/src/song.h +++ b/src/song.h @@ -92,9 +92,9 @@ namespace MPD Song &operator=(const Song &); static std::string ShowTime(int); - std::string Format_ParseBraces(std::string::const_iterator &it, std::string::const_iterator end_it) const; private: void SetHashAndSlash(); + std::string ParseFormat(std::string::const_iterator &it) const; mpd_Song *itsSong; std::string itsNewName;