From 8e7bcd219b10a80e11086c60e7fa4cda5f1e8384 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Wed, 12 Sep 2012 19:36:16 +0200 Subject: [PATCH] display: improve showSongs a bit --- src/display.cpp | 24 ++++++++++++++---------- src/helpers.h | 21 +++++++++++++-------- src/settings.cpp | 16 ++++++++-------- src/settings.h | 2 +- src/status.cpp | 6 +++--- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 95654539..c69cd21e 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -108,12 +108,14 @@ void showSongs(NC::Menu &menu, const MPD::Song &s, HasSongs &screen, const st bool separate_albums, is_now_playing, is_selected, discard_colors; setProperties(menu, s, screen, separate_albums, is_now_playing, is_selected, discard_colors); + size_t y = menu.getY(); std::string line = s.toString(format, "$"); for (auto it = line.begin(); it != line.end(); ++it) { if (*it == '$') { - if (++it == line.end()) // end of format + ++it; + if (it == line.end()) // end of format { menu << '$'; break; @@ -125,25 +127,27 @@ void showSongs(NC::Menu &menu, const MPD::Song &s, HasSongs &screen, const st } else if (*it == 'R') // right align { - NC::WBuffer buf; - buf << L" "; - String2Buffer(ToWString(line.substr(it-line.begin()+1)), buf); + NC::Buffer buf; + buf << " "; + stringToBuffer(++it, line.end(), buf); if (discard_colors) buf.removeFormatting(); + size_t x_off = menu.getWidth() - wideLength(ToWString(buf.str())); if (is_now_playing) - buf << Config.now_playing_suffix; - menu << NC::XY(menu.getWidth()-buf.str().length()-(is_selected ? Config.selected_item_suffix_length : 0), menu.getY()) << buf; - if (separate_albums) - menu << NC::fmtUnderlineEnd; - return; + x_off -= Config.now_playing_suffix_length; + if (is_selected) + x_off -= Config.selected_item_suffix_length; + menu << NC::XY(x_off, y) << buf; + break; } else // not a color nor right align, just a random character menu << *--it; } else if (*it == MPD::Song::FormatEscapeCharacter) { + ++it; // treat '$' as a normal character if song format escape char is prepended to it - if (++it == line.end() || *it != '$') + if (it == line.end() || *it != '$') --it; menu << *it; } diff --git a/src/helpers.h b/src/helpers.h index bd04e416..d7b4462e 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -327,15 +327,14 @@ template <> struct StringConverter { std::wstring operator()(const char *s) { return ToWString(s); } }; -template -void String2Buffer(const std::basic_string &s, NC::basic_buffer &buf) +template +void stringToBuffer(Iterator first, Iterator last, NC::basic_buffer &buf) { - StringConverter< NC::basic_buffer > cnv; - for (auto it = s.begin(); it != s.end(); ++it) + for (auto it = first; it != last; ++it) { if (*it == '$') { - if (++it == s.end()) + if (++it == last) { buf << '$'; break; @@ -361,9 +360,9 @@ void String2Buffer(const std::basic_string &s, NC::basic_buffer &b buf << NC::fmtReverse; break; case '/': - if (++it == s.end()) + if (++it == last) { - buf << cnv("$/"); + buf << '$' << '/'; break; } switch (*it) @@ -394,7 +393,7 @@ void String2Buffer(const std::basic_string &s, NC::basic_buffer &b else if (*it == MPD::Song::FormatEscapeCharacter) { // treat '$' as a normal character if song format escape char is prepended to it - if (++it == s.end() || *it != '$') + if (++it == last || *it != '$') --it; buf << *it; } @@ -403,6 +402,12 @@ void String2Buffer(const std::basic_string &s, NC::basic_buffer &b } } +template +void stringToBuffer(const std::basic_string &s, NC::basic_buffer &buf) +{ + stringToBuffer(s.begin(), s.end(), buf); +} + template void ShowTime(T &buf, size_t length, bool short_names) { StringConverter cnv; diff --git a/src/settings.cpp b/src/settings.cpp index ec365950..f955aeeb 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -432,7 +432,7 @@ void Configuration::Read() if (song_status_format.find("$") != std::string::npos) { NC::Buffer status_no_colors; - String2Buffer(song_status_format, status_no_colors); + stringToBuffer(song_status_format, status_no_colors); song_status_format_no_colors = status_no_colors.str(); } else @@ -509,7 +509,7 @@ void Configuration::Read() if (!v.empty()) { browser_playlist_prefix.clear(); - String2Buffer(v, browser_playlist_prefix); + stringToBuffer(v, browser_playlist_prefix); } } else if (name == "progressbar_look") @@ -545,7 +545,7 @@ void Configuration::Read() if (!v.empty()) { selected_item_prefix.clear(); - String2Buffer(v, selected_item_prefix); + stringToBuffer(v, selected_item_prefix); selected_item_prefix_length = wideLength(ToWString(selected_item_prefix.str())); } } @@ -554,7 +554,7 @@ void Configuration::Read() if (!v.empty()) { selected_item_suffix.clear(); - String2Buffer(v, selected_item_suffix); + stringToBuffer(v, selected_item_suffix); selected_item_suffix_length = wideLength(ToWString(selected_item_suffix.str())); } } @@ -563,7 +563,7 @@ void Configuration::Read() if (!v.empty()) { now_playing_prefix.clear(); - String2Buffer(v, now_playing_prefix); + stringToBuffer(v, now_playing_prefix); now_playing_prefix_length = wideLength(ToWString(now_playing_prefix.str())); } } @@ -572,8 +572,8 @@ void Configuration::Read() if (!v.empty()) { now_playing_suffix.clear(); - String2Buffer(ToWString(v), now_playing_suffix); - now_playing_suffix_length = wideLength(now_playing_suffix.str()); + stringToBuffer(v, now_playing_suffix); + now_playing_suffix_length = wideLength(ToWString(now_playing_suffix.str())); } } else if (name == "modified_item_prefix") @@ -581,7 +581,7 @@ void Configuration::Read() if (!v.empty()) { modified_item_prefix.clear(); - String2Buffer(v, modified_item_prefix); + stringToBuffer(v, modified_item_prefix); } } else if (name == "color1") diff --git a/src/settings.h b/src/settings.h index 968f84b4..2c4c2e07 100644 --- a/src/settings.h +++ b/src/settings.h @@ -91,7 +91,7 @@ struct Configuration NC::Buffer selected_item_prefix; NC::Buffer selected_item_suffix; NC::Buffer now_playing_prefix; - NC::WBuffer now_playing_suffix; + NC::Buffer now_playing_suffix; NC::Buffer modified_item_prefix; NC::Color color1; diff --git a/src/status.cpp b/src/status.cpp index 5b68a268..5dddeebb 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -345,8 +345,8 @@ void Status::Changes::elapsedTime() } NC::WBuffer first, second; - String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, "$"))), first); - String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, "$"))), second); + stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, "$"))), first); + stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, "$"))), second); size_t first_len = wideLength(first.str()); size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2; @@ -397,7 +397,7 @@ void Status::Changes::elapsedTime() tracklength += "]"; } NC::WBuffer np_song; - String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, "$"))), np_song); + stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, "$"))), np_song); *wFooter << NC::XY(0, 1) << wclrtoeol << NC::fmtBold << player_state << NC::fmtBoldEnd; np_song.write(*wFooter, playing_song_scroll_begin, wFooter->getWidth()-player_state.length()-tracklength.length(), L" ** "); *wFooter << NC::fmtBold << NC::XY(wFooter->getWidth()-tracklength.length(), 1) << tracklength << NC::fmtBoldEnd;